【Unity Shader学习笔记】(一)在表面着色器中控制顶点变换

通常境况下,我们可以方便地使用表面着色器对材质进行简单的金属光泽、平滑度等设置。但是如果要想对顶点进行控制,就需要使用顶点片段着色器。然而,在顶点片段着色器中,连最基本的漫反射、高光等都需要手动去写,显然比较麻烦。因此,如果能在表面着色器中进行顶点的控制就好了。

当然,这是可以做到的!

首先,在Unity中生成一个基本的表面着色器,在Project选项卡中,Create——Shader——Standard Surface Shader,将生成的Shader命名为“MyNewShader”。



默认的代码是下面这个样子的,其中只有表面着色器的一些指令,不能手动控制顶点。

Shader "Custom/MyNewShader" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _Metallic;
		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

为了能在里面使用顶点函数,需要做下面的操作。

将下面语句

#pragma surface surf Standard fullforwardshadows

替换为

#pragma surface surf Standard fullforwardshadows vertex:vert

并且,将下面的代码加到CGPROGRAM——ENGCG代码块之间的任意位置。

		//增加的顶点控制代码
		void vert(inout appdata_full v)
		{
			//x,y,z为随时间变化的量,数值可以自己随意设置
			float x =  sin(_SinTime*20); 
			float y =  sin(_SinTime*20);
			float z =  sin(_SinTime*20);
			//定义一个4*4的矩阵类型,将旋转和平移包含进去
			float4x4 m = {1,0,0,x,
					      0,1,0,y,
					      0,0,1,z,
					      0,0,0,1};
			//对顶点进行变换
			v.vertex = mul(m,v.vertex);
		}

需要注意的是,这段顶点控制代码会在表面着色函数surf()之前被执行,即我们的控制是在模型空间内进行的。之后输出的顶点信息会被作为标准的表面着色器的输入数据。vert()的位置并不影响它的执行顺序。完整的Shader代码如下:

Shader "Custom/MyNewShader" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows  vertex:vert

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _Metallic;
		fixed4 _Color;
		//增加的顶点控制代码
		void vert(inout appdata_full v)
		{
			//x,y,z为随时间变化的量,数值可以自己随意设置
			float x =  sin(_SinTime*20); 
			float y =  sin(_SinTime*20);
			float z =  sin(_SinTime*20);
			//定义一个4*4的矩阵类型,将旋转和平移包含进去
			float4x4 m = {1,0,0,x,
					      0,1,0,y,
					      0,0,1,z,
					      0,0,0,1};
			//对顶点进行变换
			v.vertex = mul(m,v.vertex);
		}
		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

在场景中生成一个Cube,然后生成一个新的Material,将该Material的Shader替换为我们自己的MyNewShader即可。运行程序看到下图的效果,Cube的渲染位置会随着时间变化,而它本身的物理位置是不动的。瞧,是不是很容易就做到了在表面着色器中进行顶点控制了。



posted @ 2016-12-18 09:40  雁回晴空  阅读(506)  评论(0编辑  收藏  举报