Displacement along normal not working

Hi all,

I haven’t had the chance to use Cinder in a while, so figured the holidays were a good time to dive back in. In my first attempt at shaking off the rust, I’ve clearly forgotten a few things.

I’ve created and animated a texture using some simplex noise. I had no trouble doing a texture lookup in the vertex shader to adjust the y value of the vertices of a geom::Plane(), however trying to apply the logic to an Isosphere by displacing the vertices along their respective normals seems to be causing me some issues. There seems to be some distortion along the z-axis that is causing the sphere to flatten. I know it’s something trivial I’m overlooking - here’s the vertex shader:

#version 150

uniform mat4 ciModelViewProjection;
uniform mat3 ciNormalMatrix;

in vec4 ciPosition;
in vec3 ciNormal;
in vec2	ciTexCoord0;

out vec2 texCoordVarying;
out vec3 vNormal;

// texture containing incoming noise data
uniform sampler2D tex0;

void main()
{
    
    vNormal = normalize(ciNormalMatrix * ciNormal);

    // we need to scale up the values we get from the texture
    float scale = 0.05f;
    
    // here we get the red channel value from the texture
    // to use it as displacement along the normal
    float displacementY = texture(tex0, ciTexCoord0).r;
    
    vec3 newPosition = ciPosition.xyz + (vNormal * displacementY * scale);

    gl_Position = ciModelViewProjection * vec4(newPosition, 1.);
    
    // pass the texture coordinates to the fragment shader
    texCoordVarying = ciTexCoord0;
}

Thanks, and happy holidays!

Hi,

you’re transforming the normal to view space before displacing the vertex in object space, hence you’re working in two different coordinate spaces. For the displacement, simply use ciNormal instead of vNormal.

-Paul

Classic. Thanks Paul, that was it.