I get flat render ,how to fixed? Code (CSharp): Shader "Unlit/Curvature" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #pragma target 3.0 #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; float3 viewNormal:TEXCOORD1; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.viewNormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { // sample the texture // fixed4 col = tex2D(_MainTex, i.uv); fixed4 curvature= length(fwidth(float4(i.viewNormal,1)))*40; return curvature; // apply fog // UNITY_APPLY_FOG(i.fogCoord, col); // return col; } ENDCG } } }
Technically that's correct. The curvature is constant across the entire face of the mesh, so there's nothing to "fix". Obviously it's ugly though. Usually the solution is to bake out the curvature using an external application, either saving it as a texture or baking it into the vertex data.
http://madebyevan.com/shaders/curvature/ it seems smooth.curvature can not calculate vertex normal ,only face normal?and what agrithm use in curvature Node within substance ?
Nope, that's faceted too. It's just a really, really high poly mesh so it's hiding that fact. You could do that too if you wanted, but it has to be an actual high poly mesh. Real time tessellation doesn't affect the normal, so the derivatives retain their faceted appearance after tessellation. In a shader, yes. Derivatives, the fwidth or ddx & ddy functions, only work in the fragment shader. There are academic papers out there showing how to compute the curvature using geometry shaders, but those use adjacency information which Unity doesn't support. The closest you can get to real time per-vertex curvature would be to use a compute shader to iterate over the mesh and store the data in a compute buffer to be read from SV_VertexID, or something like Keijiro's Skinner which stores the "index" of the vertex in the UV data.