vec4 triplanarTexture( sampler2D sampler, vec3 weights, vec3 triplanerPosition ) { vec4 sample = vec4( 0.0 ); sample += texture( sampler, triplanerPosition.xy ) * weights.z; sample += texture( sampler, triplanerPosition.xz ) * weights.y; sample += texture( sampler, triplanerPosition.zy * vec2( -1.0, 1.0 ) ) * weights.x; return sample; } vec2 heightMapUV( vec3 worldPosition, vec3 terrainSize, vec3 terrainOffset ) { vec3 inTerrain = worldPosition - terrainOffset; vec3 terrainUV = inTerrain / terrainSize; return vec2( terrainUV.x, terrainUV.z ); } vec4 fromHeightMap( sampler2D sampler, vec3 worldPosition, vec3 terrainSize, vec3 terrainOffset ) { vec3 inTerrain = worldPosition - terrainOffset; vec3 terrainUV = inTerrain / terrainSize; return texture( sampler, vec2( terrainUV.x, terrainUV.z ) ); } vec4 fromHeightMapSmoothed( sampler2D terrain, vec2 terrainUV, vec2 kernelSize, float weightSmoothing ) { vec4 output = vec4( 0, 0, 0, 0 ); float sumW = 0.0; for ( int i = -1; i < 2; i++ ) { for ( int j= -1; j < 2; j++ ) { vec2 offset = vec2( kernelSize.x * float(i), kernelSize.y * float(j) ); float w = 1.0 / ( weightSmoothing + length( offset ) ); output += texture( terrain, terrainUV + offset ) * w; sumW += w; } } return output / sumW; } vec3 heightMapDirection( sampler2D terrain, vec2 terrainUV, vec2 kernelSize, float normalScale ) { float hL = texture( terrain, terrainUV - vec2( kernelSize.x, 0 ) ).r * normalScale; float hR = texture( terrain, terrainUV + vec2( kernelSize.x, 0 ) ).r * normalScale; float hD = texture( terrain, terrainUV - vec2( 0, kernelSize.y ) ).r * normalScale; float hU = texture( terrain, terrainUV + vec2( 0, kernelSize.y ) ).r * normalScale; return vec3( hL - hR, 2.0, hD - hU ); } vec3 heightMapNormal( sampler2D terrain, vec2 terrainUV, vec2 kernelSize, float normalScale ) { return normalize( heightMapDirection( terrain, terrainUV, kernelSize, normalScale ) ); } vec3 heightMapNormalSmoothed( sampler2D terrain, vec2 terrainUV, vec2 kernelSize, float normalScale, float kernelSpread ) { vec3 n0 = heightMapDirection( terrain, terrainUV, kernelSize, normalScale ); vec3 n1 = heightMapDirection( terrain, terrainUV, kernelSize * kernelSpread, normalScale ) * 0.5; vec3 n2 = heightMapDirection( terrain, terrainUV, kernelSize * kernelSpread * kernelSpread, normalScale ) * 0.25; return normalize( n0 + n1 + n2 ); }