I have a terrain based on CDLOD paper. It uses a single static mesh that is scaled & dynamically morphed to progress between LOD levels (marked by different colors)
![](https://uploads.gamedev.net/forums/monthly_2020_03/7ad42c7448a04b45b0436c342885381c.mesh.png)
![](https://uploads.gamedev.net/forums/monthly_2020_03/79662035f969479194f055eabd193d1d.terrain1.png)
I compute the world position of each vertex to sample the heightmap.
float2 GetTextureCoordinates( float3 worldPos )
{
float2 texcoord = ( worldPos.xz - gWorldMin ) / ( gWorldMax - gWorldMin );
texcoord = clamp( texcoord, 0.0f, 1.0f );
return texcoord;
}
Currently I use a compute shader to generate a normal map like this
#ifndef TERRAIN_COMPUTE_NORMAL_HLSL
#define TERRAIN_COMPUTE_NORMAL_HLSL
#include "Constants.hlsl"
#include "Common.hlsl"
Texture2D gHeightmap : register( TEXTURE_REGISTER_EXTRA );
RWTexture2D<float3> gNormalTexture : register( UAV_REGISTER );
[numthreads(TERRAIN_NORMAL_THREADS_AXIS, TERRAIN_NORMAL_THREADS_AXIS, 1)]
void cs_main(uint3 groupID : SV_GroupID, uint3 dispatchTID : SV_DispatchThreadID, uint3 groupTID : SV_GroupThreadID, uint groupIndex : SV_GroupIndex)
{
float3 normal = SobelFilter( gHeightmap, int3( dispatchTID.xy, 0) );
normal += 1.0f;
normal *= 0.5f;
gNormalTexture[ dispatchTID.xy ] = normal;
}
#endif
The result looks promising (a snapshot of it):
![](https://uploads.gamedev.net/forums/monthly_2020_03/2d935561e7364092a028aaf68e0bfeb6.normal.png)
I must be doing something wrong though because the results looks blocky and shit when sampled:
![](https://uploads.gamedev.net/forums/monthly_2020_03/d8d03d6b4b864bcc8ca0f6a3e6aa3c79.normal2.png)
const int2 offset = 0;
const int mipmap = 0;
float3 normal = gNormalMap.SampleLevel( gLinearSampler, postMorphTexcoord, mipmap, offset ).rgb;
normal *= 2.0;
normal -= 1.0;
VertexOut ret;
ret.mNormal = mul( ( float3x3 )gFrameView, normal );
ret.mNormal = normalize( ret.mNormal );
postMorphTexcoord variable is the same I sample the heightmap with.
1. I am thinking I either need to add some “weight”/interpolation before sampling or try and average neighouring pixels (again, even though I did it when computing the normal map?)
2. I am primarily interested in quality of speed; is there anything else I could do in this regard?
Any input is welcome!