在渲染期间,材质球始终优先计算每个顶点。 在此处使用顶点方法时,将调用顶点方法。 计算顶点后,以一定格式输出到下一个流。 其中有一步是我们放置的surfa方法,它接收的输入是可以控制的(我们用顶点方法输出)。
整个shader相关过程如下。
1、shader打算对一个顶点进行计算。 由于自定义了顶点方法,因此调整了vert
2、vert的第一参数决定该顶点需要带来哪些属性。 我们得到这个属性中的顶点颜色,把这个顶点颜色放入Input结构中,供后续的surf使用;
Shader代码:
shader ' custom/11.30/0 ' { properties } _ maintex (' base (RGB ),2D )=' white ' } sub shader { tags } ' render struct Input{ float2 uv_MainTex; 浮点4版本颜色; (; voidsurf(inputin,inout SurfaceOutput o ) half4c=tex2d(_maintex,In.uv_MainTex ); o.Albedo=c.rgb*In.vertColor; }voidvetx(inoutappdata_fullv,out Input o ) unity_initialize_output ) input,o ); o.vertColor=v.color; } ENDCG }}上面的代码使用了这一行
# pragmasurfacesurflydsbvertex 3360 vert在灯光模型方法之后添加了vertex:vert。 其中vertex是关键字,后跟冒号,后跟您定义的名称,并对应于以下方法: 这是因为将我们的shader编译为Unity时,需要与顶点相关的操作,所以在编译时,编译我们指定的顶点方法,Unity引擎在执行该shader程序时使用我们的顶点方法
让我们看看指定的这个方法的内容
voidvert(inoutappdata_fullv,out Input o ) unity _ initialize _ output (input,o ); o.vertColor=v.color; }第一个参数的类型是appdata_full,它是从APP应用程序发送到着色器的数据的结构,例如位置(vertex,法线),以及本文介绍的顶点颜色(color )。
sructappdata _ full { float4vertex : position; 浮点4切线:切线; float 3正规:正规; float4 texcoord : TEXCOORD0; float4 texcoord1 : TEXCOORD1; fixed4 color : COLOR; #ifdefined(shader_API_Xbox360 ) half4 texcoord2 : TEXCOORD2; half4 texcoord3 : TEXCOORD3; half4 texcoord4 : TEXCOORD4; half4 texcoord5 : TEXCOORD5; #endif};
虽然有材质球,但必须将顶点颜色传递给模型。 我们用c#代码实现。
程序代码:
using UnityEngine; using System.Collections; public class class 11 _ 30: mono behaviour {//usethisforinitializationvoidstart ({ mesh mesh=getcomponentmeshfilter ) } vetter color [ ] colors=new color [ vertices.length ]; colors [0]=Color.red; mesh.colors=colors; }效果图:
因为顶点太多,只指定了第一个顶点的颜色,所以只有从第一个顶点到其他顶点的插值效果看起来如图所示。
转载于:https://www.cn blogs.com/wong siuming/p/5007529.html