Hi,
I did some vertex displacement on ground mesh and tried to do normal calculation in fragment shader. Customization to the shader were applied via CustomMaterial.
My first attempt was to force the fragment shader to use the default normal computation. It gave flat shaded shadow effect.
Then I tried to use the same approach, but sample the height again in fragment shader instead of using the height data from vertex shader. It gave me result below with very clear contour lines.
After this, I tried to sampling the neighboring heights from the heightmap and computed normal as the following. Unfortunately it gave the same result.
vec3 slope = vec3(hLeft - hRight, 0.01, hDown - hUp);
normalW = normalize(slope);
There are some parameters I changed that help to reduce the contour lines. But I don’t know if it is the correct way? Is there standard way of fixing such problem?
For example, I achieved this result by changing paramY
value. But it also makes the lighting effect weaker.
vec3 slope = vec3(hLeft - hRight, paramY, hDown - hUp);
normalW = normalize(slope);
Finally this version below seems to be the best so far. This is achieved by manipulating a scale
factor and the different offset
distance between current position and the neighboring positions for height sampling.
vec3 slope = vec3(hLeft - hRight, 1.0, hDown - hUp);
slope.x = slope.x * scale;
slope.z = slope.z * scale;
normalW = normalize(slope);
It also seem to be related to the heightmap used and the height range I choose for the heightmap. One of the heightmap seems don’t have this problem by default. But I was able to make the contour lines to show up for it as well by changing the parameters I mentioned above.