- Home /
 
 
               Question by 
               Txguug · Dec 31, 2017 at 02:25 PM · 
                shader programmingvertex shaderdisplacement  
              
 
              Shading after vertex displacement
I've written this shader that uses the red channel of a texture ( _DispTex ) to change the vertexes. It works, but the returned deformed mesh isn't shaded How is this accomplished?
I've tried tinkering with the disp void: added a line` v.normal = v.vertex.xyz * d;` which gives a sort of shading but... wrong. I've tried many things I could find online without success. Getting frustrated! Is it even possible without transforming the mesh itself?
 Shader "Custom/PlaneShader"
 {
 
     Properties
     {
         _MainTex("Base (RGB)", 2D) = "blue" {}
         _DispTex("Displacement texture", 2D) = "bump" {}
         _Displacement("Displacement", Range(-10.0, 10.0)) = 1
     }
 
 
     SubShader
     {
         Tags{ "RenderType" = "Opaque" }
         LOD 200
         CGPROGRAM
 
         #pragma surface surf Lambert vertex:disp 
         #pragma target 3.0
 
         sampler2D _DispTex;
         float _Displacement;
         sampler2D _MainTex;
 
     struct Input
     {
         float2 uv_MainTex;
         float2 uv_DispTex;
     };
 
     struct appdata 
     {
         float4 vertex : POSITION;
         float4 tangent : TANGENT;
         float3 normal : NORMAL;
         float2 texcoord : TEXCOORD0;
         float2 texcoord1 : TEXCOORD1;
         float2 texcoord2 : TEXCOORD2;
     };
 
 
     void disp(inout appdata v)
     {
         float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
         v.vertex.xyz += v.normal * d;
     }
 
     void surf(Input IN, inout SurfaceOutput o)
     {
         o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
         o.Normal = UnpackNormal(tex2D(_MainTex, IN.uv_MainTex));
     }
 
     ENDCG
     }
 
     FallBack "Diffuse"
 }
 
 
              
               Comment
              
 
               
              Your answer