openglshadernoisenormals

Per-Vertex Normals from perlin noise?


I'm generating terrain in Opengl geometry shader and am having trouble calculating normals for lighting. I'm generating the terrain dynamically each frame with a perlin noise function implemented in the geometry shader. Because of this, I need an efficient way to calculate normals per-vertex based on the noise function (no texture or anything). I am able to take cross product of 2 side to get face normals, but they are generated dynamically with the geometry so I cannot then go back and smooth the face normals for vertex normals. How can I get vertex normals on the fly just using the noise function that generates the height of my terrain in the y plane (therefore height being between 1 and -1). I believe I have to sample the noise function 4 times for each vertex, but I tried something like the following and it didn't work...

vec3 xP1 = vertex + vec3(1.0, 0.0, 0.0);
vec3 xN1 = vertex + vec3(-1.0, 0.0, 0.0);
vec3 zP1 = vertex + vec3(0.0, 0.0, 1.0);
vec3 zN1 = vertex + vec3(0.0, 0.0, -1.0);

float sx = snoise(xP1) - snoise(xN1);
float sz = snoise(zP1) - snoise(zN1);

vec3 n = vec3(-sx, 1.0, sz);
normalize(n);

return n;

The above actually generated lighting that moved around like perlin noise! So any advice for how I can get the per-vertex normals correctly?


Solution

  • You didn't say exactly how you were actually generating the positions. So I'm going to assume that you're using the Perlin noise to generate height values in a height map. So, for any position X, Y in the hieghtmap, you use a 2D noise function to generate the Z value.

    So, let's assume that your position is computed as follows:

    vec3 CalcPosition(in vec2 loc) {
        float height = MyNoiseFunc2D(loc);
        return vec3(loc, height);
    }
    

    This generates a 3D position. But in what space is this position in? That's the question.

    Most noise functions expect loc to be two values on some particular floating-point range. How good your noise function is will determine what range you can pass values in. Now, if your model space 2D positions are not guaranteed to be within the noise function's range, then you need to transform them to that range, do the computations, and then transform it back to model space.

    In so doing, you now have a 3D position. The transform for the X and Y values is simple (the reverse of the transform to the noise function's space), but what of the Z? Here, you have to apply some kind of scale to the height. The noise function will return a number on the range [0, 1), so you need to scale this range to the same model space that your X and Y values are going to. This is typically done by picking a maximum height and scaling the position appropriately. Therefore, our revised calc position looks something like this:

    vec3 CalcPosition(in vec2 modelLoc, const in mat3 modelToNoise, const in mat4 noiseToModel)
    {
        vec2 loc = modelToNoise * vec3(modelLoc, 1.0);
        float height = MyNoiseFunc2D(loc);
        vec4 modelPos = noiseToModel * vec4(loc, height, 1.0);
        return modelPos.xyz;
    }
    

    The two matrices transform to the noise function's space, and then transform back. Your actual code could use less complicated structures, depending on your use case, but a full affine transformation is simple to describe.

    OK, now that we have established that, what you need to keep in mind is this: nothing makes sense unless you know what space it is in. Your normal, your positions, nothing matters until you establish what space it is in.

    This function returns positions in model space. We need to calculate normals in model space. To do that, we need 3 positions: the current position of the vertex, and two positions that are slightly offset from the current position. The positions we get must be in model space, or our normal will not be.

    Therefore, we need to have the following function:

    void CalcDeltas(in vec2 modelLoc, const in mat3 modelToNoise, const in mat4 noiseToModel, out vec3 modelXOffset, out vec3 modelYOffset)
    {
        vec2 loc = modelToNoise * vec3(modelLoc, 1.0);
        vec2 xOffsetLoc = loc + vec2(delta, 0.0);
        vec2 yOffsetLoc = loc + vec2(0.0, delta);
        float xOffsetHeight = MyNoiseFunc2D(xOffsetLoc);
        float yOffsetHeight = MyNoiseFunc2D(yOffsetLoc);
        modelXOffset = (noiseToModel * vec4(xOffsetLoc, xOffsetHeight, 1.0)).xyz;
        modelYOffset = (noiseToModel * vec4(yOffsetLoc, yOffsetHeight, 1.0)).xyz;
    }
    

    Obviously, you can merge these two functions into one.

    The delta value is a small offset in the space of the noise texture's input. The size of this offset depends on your noise function; it needs to be big enough to return a height that is significantly different from the one returned by the actual current position. But it needs to be small enough that you aren't pulling from random parts of the noise distribution.

    You should get to know your noise function.

    Now that you have the three positions (the current position, the x-offset, and the y-offset) in model space, you can compute the vertex normal in model space:

    vec3 modelXGrad = modelXOffset - modelPosition;
    vec3 modelYGrad = modelYOffset - modelPosition;
    
    vec3 modelNormal = normalize(cross(modelXGrad, modelYGrad));
    

    From here, do the usual things. But never forget to keep track of the spaces of your various vectors.

    Oh, and one more thing: this should be done in the vertex shader. There's no reason to do this in a geometry shader, since none of the computations affect other vertices. Let the GPU's parallelism work for you.