I’m trying to use a HeightMap texture in a vertex shader to morph a flat terrain to have hills/valleys, etc.
I’m using a quadtree structure, and each quad is it’s own gameobject/mesh. I have a _HeightMap texture that that is passed to every quad using material.SetTexture. However, I see a repeated pattern, which I think means the texture is being scaled and then tiled on each quad, when I want the heightmap to ‘stretch’ over all quads. What’s the proper way to do this?
Here is my shader code:
Shader "Sagan/Proland" {
SubShader{
Pass {
CGPROGRAM
#include "Assets/Resources/Shaders/Sagan.cginc"
#pragma vertex vert // vert function is the vertex shader
#pragma fragment frag // frag function is the fragment shader
float4x4 _TerrainMatrixWTL, _QuadPosition;
sampler2D _HeightMap;
// vertex shader
saganVertexOutput vert(appdata_base v) {
saganVertexOutput v_out;
float4 vertexPos = v.vertex;
float textureHeightValue = tex2Dlod(_HeightMap, float4(vertexPos.xz, 0, 0)).r;
vertexPos.y += textureHeightValue;
v_out.position = mul(UNITY_MATRIX_MVP, vertexPos);
v_out.color = color(vertexPos);
return v_out;
}
// fragment shader
float4 frag(saganVertexOutput input) : COLOR {
return input.color;
// Here the fragment shader returns the "col" input
// parameter with semantic TEXCOORD0 as nameless
// output parameter with semantic COLOR.
}
ENDCG
}
}
}
Here is what I’m seeing: