Search code examples
glslwebglnormalsrender-to-texture

WebGL Normal calculations from position texture


Iam trying to create a procedural water puddle in webGL with "water ripples" by vertex displacement. The problem I'm having is that I get a noise I can't explain.

Below is the first pass vertex shader where I calculate the vertex positions that i later render to a texture that i then use in the second pass.

void main()         {           
            float damping = 0.5;
            vNormal = normal;

            // wave radius
            float timemod = 0.55;
            float ttime = mod(time , timemod);

            float frequency = 2.0*PI/waveWidth;
            float phase = frequency * 0.21;

            vec4 v = vec4(position,1.0);
            // Loop through array of start positions
            for(int i = 0; i < 200; i++){

                float cCenterX = ripplePos[i].x;
                float cCenterY = ripplePos[i].y;
                vec2 center = vec2(cCenterX, cCenterY) ;

                if(center.x == 0.0 && center.y == 0.0)
                    center = normalize(center);

                // wave width
                float tolerance = 0.005;

                radius = sqrt(pow( uv.x - center.x , 2.0) + pow( uv.y -center.y, 2.0));
                // Creating a ripple
                float w_height = (tolerance - (min(tolerance,pow(ripplePos[i].z-radius*10.0,2.0)) )) * (1.0-ripplePos[i].z/timemod) *5.82;

                // -2.07 in the end to keep plane at right height. Trial and error solution
                v.z += waveHeight*(1.0+w_height/tolerance) / 2.0 - 2.07;

                vNormal = normal+v.z;
            }
            vPosition = v.xyz;

            gl_Position = projectionMatrix * modelViewMatrix * v;
        }

And the first pass fragment shader that writes to the texture:

        void main()
        {
            vec3 p = normalize(vPosition);

            p.x = (p.x+1.0)*0.5;
            p.y = (p.y+1.0)*0.5;


            gl_FragColor = vec4(  normalize(p), 1.0);
        }

The second vertex shader is a standard passthrough.

Second pass fragmentshader is where I try to calculate the normals to be used for light calculations.

void main() {

            float w = 1.0 / 200.0;
            float h = 1.0 / 200.0;

            // Nearest Nieghbours 
            vec3 p0 = texture2D(rttTexture, vUV).xyz;
            vec3 p1 = texture2D(rttTexture, vUV + vec2(-w,  0)).xyz;
            vec3 p2 = texture2D(rttTexture, vUV + vec2( w,  0)).xyz;
            vec3 p3 = texture2D(rttTexture, vUV + vec2( 0,  h)).xyz;
            vec3 p4 = texture2D(rttTexture, vUV + vec2( 0, -h)).xyz;

            vec3 nVec1 = p2 - p0;
            vec3 nVec2 = p3 - p0;

            vec3 vNormal = cross(nVec1, nVec2);

            vec3 N = normalize(vNormal);

             float theZ =  texture2D(rttTexture, vUV).r;
            //gl_FragColor = vec4(1.,.0,1.,1.);
          //gl_FragColor = texture2D(tDiffuse, vUV);
            gl_FragColor = vec4(vec3(N), 1.0);
        }

The result is this:

enter image description here

The image displays the normalmap and the noise I'm refering to is the inconsistency of the blue.

Here is a live demonstration: http://oskarhavsvik.se/jonasgerling_water_ripple/waterRTT-clean.html

I appreciate any tips and pointers, not only fixes for this problem. But the code in genereal, I'm here to learn.


Solution

  • After a brief look it seems like your problem is in storing x/y positions.

    gl_FragColor = vec4(vec3(p0*0.5+0.5), 1.0);
    

    enter image description here

    You don't need to store them anyway, because the texel position implicitly gives the x/y value. Just change your normal points to something like this...

    vec3 p2 = vec3(1, 0, texture2D(rttTexture, vUV + vec2(w, 0)).z);
    

    Rather than 1, 0 you will want to use a scale appropriate to the size of your displayed quad relative to the wave height. Anyway, the result now looks like this.

    enter image description here

    The height/z seems to be scaled by distance from the centre, so I went looking for a normalize() and removed it...

                vec3 p = vPosition;
                gl_FragColor = vec4(p*0.5+0.5, 1.0);
    

    The normals now look like this...

    enter image description here