I’m using a greyscale image to displace a plane and want to recalculate the correct normals inside of my shader. There’s a technique, which seem to work quite good, like for example in this video here, shifting additional points around:
For some reason, of all the examples i tried to implement, not even one worked- Can maybe someone see, where my error lies? In most examples, they did some crazy wooble or stuff like that, but I literally just want to displace on the y-axis- What I’m missing to make it work?
Here’s my shader:
Shader "BellsArietta/Terrain" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_HeightMap ("Height Map", 2D) = "gray" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
[Header(Tessellation)]
_TessDetail ("Tessellation", Range(1,15)) = 4
_TessMin ("Min", float) = 5
_TessMax ("Max", float) = 15
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf Standard fullforwardshadows addshadow vertex:vert tessellate:tessDistance
#pragma target 4.6
#include "Tessellation.cginc"
sampler2D _MainTex, _HeightMap;
fixed4 _Color;
half _Glossiness, _Metallic;
fixed _TessDetail, _TessMin, _TessMax;
float4 tessDistance (appdata_full v0, appdata_full v1, appdata_full v2) {
return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, _TessMin, _TessMax, _TessDetail);
}
void vert (inout appdata_full v)
{
float d = tex2Dlod(_HeightMap, float4(v.texcoord.xy,0,0)).r;
float3 v0 = v.vertex.xyz;
float3 bitangent = cross(v.normal, v.tangent.xyz);
float3 v1 = v0 + (v.tangent.xyz * 0.01);
float3 v2 = v0 + (bitangent * 0.01);
float ns0 = v0.y + d;
float ns1 = v1.y + d;
float ns2 = v2.y + d;
v0.xyz += ns0 * v.normal;
v1.xyz += ns1 * v.normal;
v2.xyz += ns2 * v.normal;
float3 modifiedNormal = cross(v2-v0, v1-v0);
v.normal = normalize(-modifiedNormal);
v.vertex.xyz = v0;
}
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}