Vertex Modifier of Surface Shader

Vertex Modifier of Surface Shader

  Surface shader compilation directive vertex:functionName  可以用于指定顶点着色器。A function that takes inout appdata_full parameter.

  Surface shader中顶点函数传出的坐标必须是Local坐标。在Vert执行完毕后,Surface shader会自动将此Local坐标乘以MVP矩阵。

 1 Shader "Example/Normal Extrusion" {
 2     Properties {
 3       _MainTex ("Texture", 2D) = "white" {}
 4       _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
 5     }
 6     SubShader {
 7       Tags { "RenderType" = "Opaque" }
 8       CGPROGRAM
 9       #pragma surface surf Lambert vertex:vert
10       struct Input {
11           float2 uv_MainTex;
12       };
13       float _Amount;
14       void vert (inout appdata_full v) {
15           v.vertex.xyz += v.normal * _Amount;
16       }
17       sampler2D _MainTex;
18       void surf (Input IN, inout SurfaceOutput o) {
19           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
20       }
21       ENDCG
22     } 
23     Fallback "Diffuse"
24   }

  

  如果想传递给surface自定义数据(per-pixel),顶点函数需要添加out Input 参数如下:

 1   Shader "Example/Custom Vertex Data" {
 2     Properties {
 3       _MainTex ("Texture", 2D) = "white" {}
 4     }
 5     SubShader {
 6       Tags { "RenderType" = "Opaque" }
 7       CGPROGRAM
 8       #pragma surface surf Lambert vertex:vert
 9       struct Input {
10           float2 uv_MainTex;
11           float3 customColor;
12       };
13       void vert (inout appdata_full v, out Input o) {
14           UNITY_INITIALIZE_OUTPUT(Input,o);
15           o.customColor = abs(v.normal);
16       }
17       sampler2D _MainTex;
18       void surf (Input IN, inout SurfaceOutput o) {
19           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
20           o.Albedo *= IN.customColor;
21       }
22       ENDCG
23     } 
24     Fallback "Diffuse"
25   }

  

参考:file:///C:/Program%20Files%20(x86)/Unity/Editor/Data/Documentation/html/en/Manual/SL-SurfaceShaderExamples.html

原文地址:https://www.cnblogs.com/tekkaman/p/3886888.html