Giter Site home page Giter Site logo

Object shadows... about three.terrain HOT 5 CLOSED

icecreamyou avatar icecreamyou commented on August 25, 2024
Object shadows...

from three.terrain.

Comments (5)

IceCreamYou avatar IceCreamYou commented on August 25, 2024

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?

from three.terrain.

conceptninja avatar conceptninja commented on August 25, 2024

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)

from three.terrain.

conceptninja avatar conceptninja commented on August 25, 2024

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?

from three.terrain.

conceptninja avatar conceptninja commented on August 25, 2024

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!

from three.terrain.

IceCreamYou avatar IceCreamYou commented on August 25, 2024

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.

from three.terrain.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.