I do try to work out Ogre::ShadowTechnique::SHADOWTYPE_TEXTURE_ADDITIVE.
As you see it worked, but not well enough --- some of the shadows were "scrambled" especially when the shadow was between light source and camera...
I do use mixed shaders --- some entities use RTSS -- mostly the walking creatures and some entities use custom shaders -- here mostly the building blocks of the terrain -- dirt, lava, rock, gold etc.
Here's example material files :
Code: Select all
vertex_program myLavaVertexShader glsl
{
source LavaDistortion.vert
default_params
{
param_named_auto projectionMatrix projection_matrix
param_named_auto viewMatrix view_matrix
param_named_auto worldMatrix world_matrix
param_named_auto time1 time
}
}
fragment_program myLavaFragmentShader glsl
{
source LavaDistortion.frag
default_params
{
param_named_auto time2 time
}
}
material Lava //: RTSS/NormalMapping_MultiPass
{
// receive_shadows on
technique
{
pass
{
vertex_program_ref myLavaVertexShader
{
}
fragment_program_ref myLavaFragmentShader
{
param_named iChannel0 int 0
param_named iChannel1 int 1
}
texture_unit
{
texture Lava.png 2d
}
texture_unit
{
texture Channel0.png 2d
}
}
}
}
and the fragment shader :
Code: Select all
#version 330 core
#extension GL_ARB_explicit_uniform_location : enable
// The MIT License
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// One way to avoid texture tile repetition one using one small
// texture to cover a huge area. Basically, it creates 8 different
// offsets for the texture and picks two to interpolate between.
//
// Unlike previous methods that tile space (https://www.shadertoy.com/view/lt2GDd
// or https://www.shadertoy.com/view/4tsGzf), this one uses a random
// low frequency texture (cache friendly) to pick the actual
// texture's offset.
//
// Also, this one mipmaps to something (ugly, but that's better than
// not having mipmaps at all like in previous methods)
//
// More info here: http://www.iquilezles.org/www/articles/texturerepetition/texturerepetition.htm
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
in vec2 out_UV0;
in vec3 FragPos;
uniform float time2;
out vec4 color;
float sum( vec3 v ) { return v.x+v.y+v.z; }
vec3 textureNoTile( in vec2 x, float v )
{
float k = texture( iChannel1, 0.005*x ).x; // cheap (cache friendly) lookup
vec2 duvdx = dFdx( x );
vec2 duvdy = dFdy( x );
float l = k*8.0;
float f = fract(l);
#if 1
float ia = floor(l); // my method
float ib = ia + 1.0;
#else
float ia = floor(l+0.5); // suslik's method (see comments)
float ib = floor(l);
f = min(f, 1.0-f)*2.0;
#endif
vec2 offa = sin(vec2(3.0,7.0)*ia); // can replace with any other hash
vec2 offb = sin(vec2(3.0,7.0)*ib); // can replace with any other hash
vec3 cola = textureGrad( iChannel0, x + v*offa, duvdx, duvdy ).xyz;
vec3 colb = textureGrad( iChannel0, x + v*offb, duvdx, duvdy ).xyz;
return mix( cola, colb, smoothstep(0.2,0.8,f-0.1*sum(cola-colb)) );
}
void main( )
{
vec2 uv = FragPos.xy/2.0 ;
float f = smoothstep( -1.0, 1.0, sin(time2/50.0) );
vec3 col = textureNoTile( (uv), f );
color = vec4( col, 1.0 );
}
Also the DCW1110 material :
Code: Select all
vertex_program myDCW1110TileVertexShader glsl
{
source DCW.vert
default_params
{
param_named_auto projectionMatrix projection_matrix
param_named_auto viewMatrix view_matrix
param_named_auto worldMatrix world_matrix
param_named_auto lightMatrix texture_worldviewproj_matrix
}
}
fragment_program myDCW1110TileFragmentShader glsl
{
source DCW.frag
default_params
{
param_named_auto ambientLightColour ambient_light_colour
param_named_auto lightDiffuseColour light_diffuse_colour 0.0
param_named_auto lightSpecularColour light_specular_colour 0.0
param_named_auto lightPos light_position 0.0 0.0 0.0 0.0
param_named_auto cameraPosition camera_position
param_named_auto diffuseSurface surface_diffuse_colour
param_named seatColor float4 1.0 1.0 1.0 1.0
param_named shadowingEnabled bool false
}
}
material DCW1110 //: RTSS/NormalMapping_MultiPass
{
technique
{
pass
{
diffuse 1.0 1.0 1.0 1.0
vertex_program_ref myDCW1110TileVertexShader
{
}
fragment_program_ref myDCW1110TileFragmentShader
{
param_named decalmap int 0
param_named normalmap int 1
param_named crossmap int 2
param_named shadowmap int 3
}
// We use blending, so that we can see the underlying texture.
texture_unit decalmap
{
texture DungeonClaimedWall1110.png
}
texture_unit normalmap
{
texture DungeonClaimedWall1110Normal.png
}
texture_unit crossmap
{
texture DungeonClaimedWallMask.png
}
texture_unit shadowmap
{
content_type shadow
tex_address_mode clamp
filtering bilinear
}
}
}
}
and fragment :
Code: Select all
#version 330 core
uniform sampler2D decalmap;
uniform sampler2D normalmap;
uniform sampler2D crossmap;
uniform sampler2D shadowmap;
uniform vec4 ambientLightColour;
uniform vec4 lightDiffuseColour;
uniform vec4 lightSpecularColour;
uniform vec4 lightPos;
uniform vec4 cameraPosition;
uniform vec4 diffuseSurface;
uniform vec4 seatColor;
uniform bool shadowingEnabled;
in vec2 out_UV0;
in vec2 out_UV1;
in vec2 out_UV2;
in vec3 FragPos;
in vec4 VertexPos;
in mat3 TBN;
out vec4 color;
void main (void)
{
// compute Normal
vec3 Normal = texture(normalmap, out_UV1.st).rgb;
Normal.xyz = 2 * Normal.xyz - (1.0,1.0,1.0);
Normal = normalize(TBN * Normal);
vec4 shadow = vec4(1.0, 1.0, 1.0,1.0);
vec4 tmpVertexPos = VertexPos;
// compute shadowmap
if(shadowingEnabled){
if(tmpVertexPos.z > 0 ){
tmpVertexPos /= tmpVertexPos.w;
shadow = texture(shadowmap, tmpVertexPos.xy);
}
}
// compute lightDir
vec3 lightDir = normalize(lightPos.xyz - FragPos*lightPos.w);
// compute Specular
vec3 viewDirection = normalize( cameraPosition.xyz - FragPos);
vec3 reflectedLightDirection = normalize(reflect(-1.0*lightDir.xyz,Normal));
float spec = max(dot(reflectedLightDirection, viewDirection ), 0.0) ;
spec = pow(spec,16);
vec3 specular = spec * lightSpecularColour.rgb;
// compute Diffuse
float diff = max(dot(lightDir,Normal), 0.0);
vec3 diffuse = diff * lightDiffuseColour.rgb ;
vec3 result;
// precompute the lighting term
vec3 lightingTerm = (diffuse + specular + ambientLightColour.rgb/2.0 ) * shadow.rgb;
vec4 crossMap = texture(crossmap, out_UV2.st);
if(crossMap.r < 0.00005)
{
vec3 texelColor = texture(decalmap, out_UV0.st).rgb;
if(diffuseSurface==vec4(1.0,1.0,1.0,1.0))
result = lightingTerm * texelColor;
else
result = lightingTerm * mix(texelColor,diffuseSurface.rgb,0.5);
}
else{
if(diffuseSurface==vec4(1.0,1.0,1.0,1.0))
result = lightingTerm * crossMap.rgb * seatColor.rgb;
else
result = lightingTerm * mix( crossMap.rgb * seatColor.rgb,diffuseSurface.rgb,0.5);
}
color = vec4( result.xyz, 1.0);
}