Object shadows...
conceptninja opened this issue · 5 comments
Hi, firstly, MAGNIFICENT job with the terrain generator, by far the best one I found.
Minor Issue though... How would I be able to get Object shadows to display on the terrain?
I noticed they were missing on your terrain demo as well?
I do receive shadows when using MeshPhong material though. But not when using your great blended texture shader material :/
Again, great work!
Thanks
Martin
Thanks, glad you're finding the library useful 😃
It's possible that there's a bug or something that has changed in recent versions of the Three.js library - which version are you using?
Also, since shadows work with a phong material, I assume you have set your objects to cast shadows?
Yeah, my objects all cast their shadows when I change the material to Phong Material. I'm just not getting them to work when using your blend shader material... And that one looks the best by far :-)
So I'm thinking it's a problem with the shadowmap not going through to the shader anymore or incorrectly somehow?
Any ideas where I can start looking?
PS. sorry forgot to mention which version i'm using, i'm using r76 (the latest one at the time of this post)
I think I found the problem, it seems to be this part:
THREE.ShaderChunk.shadowmap_fragment,
that chunk doesn't exist in the new r76 anymore so it doesn't do any model shadow calculations in the fragment loop. Unfortunately my shader skills are a little lacking so i'm not sure what to put in there. I tried to copy the fragment piece from an earlier three js version, but it just throws more errors :/
Is there any chance you can see what has changed around that bit?
Ok, I finally got it working!! WOOHOO, here is my modified fragment shader to enable the shadow map:
`fragmentShader: [
'uniform vec3 diffuse;',
'uniform vec3 emissive;',
'uniform float opacity;',
'varying vec3 vLightFront;',
'#ifdef DOUBLE_SIDED',
' varying vec3 vLightBack;',
'#endif',
THREE.ShaderChunk.common,
THREE.ShaderChunk.packing,
THREE.ShaderChunk.lights_pars,
THREE.ShaderChunk.shadowmap_pars_fragment,
//THREE.ShaderChunk.color_pars_fragment,
//THREE.ShaderChunk.map_pars_fragment,
//THREE.ShaderChunk.alphamap_pars_fragment,
//THREE.ShaderChunk.lightmap_pars_fragment,
//THREE.ShaderChunk.envmap_pars_fragment,
//THREE.ShaderChunk.fog_pars_fragment,
//THREE.ShaderChunk.shadowmap_pars_fragment,
//THREE.ShaderChunk.specularmap_pars_fragment,
THREE.ShaderChunk.logdepthbuf_pars_fragment,
declare,
'varying vec2 MyvUv;',
'varying vec3 vPosition;',
'varying vec3 myNormal;',
'void main() {',
// TODO: The second vector here is the object's "up" vector. Ideally we'd just pass it in directly.
'float slope = acos(max(min(dot(myNormal, vec3(0.0, 0.0, 1.0)), 1.0), -1.0));',
' vec3 outgoingLight = vec3( 0.0 );', // outgoing light does not have an alpha; the surface does
' vec4 diffuseColor = vec4( diffuse, opacity );',
' vec4 color = texture2D( texture_0, MyvUv * vec2( ' + glslifyNumber(t0Repeat.x) + ', ' + glslifyNumber(t0Repeat.y) + ' ) + vec2( ' + glslifyNumber(t0Offset.x) + ', ' + glslifyNumber(t0Offset.y) + ' ) ); // base',
assign,
' diffuseColor = color;',
// ' gl_FragColor = color;',
THREE.ShaderChunk.logdepthbuf_fragment,
//THREE.ShaderChunk.map_fragment,
//THREE.ShaderChunk.color_fragment,
//THREE.ShaderChunk.alphamap_fragment,
//THREE.ShaderChunk.alphatest_fragment,
//THREE.ShaderChunk.specularmap_fragment,
' #ifdef DOUBLE_SIDED',
' if ( gl_FrontFacing )',
' outgoingLight += diffuseColor.rgb * vLightFront + emissive;',
' else',
' outgoingLight += diffuseColor.rgb * vLightBack + emissive;',
' #else',
' outgoingLight += diffuseColor.rgb * vLightFront + emissive;',
' #endif',
//THREE.ShaderChunk.lightmap_fragment,
//THREE.ShaderChunk.envmap_fragment,
//THREE.ShaderChunk.shadowmap_fragment,
//THREE.ShaderChunk.linear_to_gamma_fragment,
//THREE.ShaderChunk.fog_fragment,
'IncidentLight directLight;',
'DirectionalLight directionalLight;',
'float shadowValue = 1.0;',
'for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {',
'directionalLight = directionalLights[ i ];',
'shadowValue = getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] );',
'}',
//' gl_FragColor = vec4( outgoingLight, diffuseColor.a );', // This will probably change in future three.js releases
' gl_FragColor = vec4( outgoingLight*shadowValue, diffuseColor.a );', // This will probably change in future three.js releases
'}'
].join('\n')`
I hope it helps someone!
Nice work, thanks. I will have to find some time to review if anything else in the shader needs to be updated to keep up with the changes in Three.js and commit the shadow fix.