[置顶] Cg入门14:Vertex Shader - 几何变换 —顶点扭曲

mul ( UNITY_MATRIX_MVP ,upPos):
参数说明
由第一个参数 UNITY_MATRIX_MVP 矩阵去影响第二个参数upPos向量(或者矩阵)

Shader "Sbin/vf35" {
	SubShader {
		
		pass{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct v2f{
				float4 pos : POSITION;
				float4 col : COLOR;
				
			};

			v2f vert(appdata_base v){
				//---------------------旋转xz面顶点start-----------------------------
				/*
				float angle = length(v.vertex)*_SinTime.w;
				//沿y轴旋转矩阵:
				float4x4 m = 
				{
					float4(cos(angle),0,sign(angle),0),
					float4(0,1,0,0),
					float4(-sign(angle),0,cos(angle),0),
					float4(0,0,0,1)
				};
				v.vertex = mul(m,v.vertex);

				//优化计算
				float x = v.vertex.x*cos(angle)+v.vertex.z*sign(angle);
				float z = v.vertex.x*-sign(angle)+v.vertex.z*cos(angle);

				v.vertex.x = x;
				v.vertex.z = z;
				*/
				//---------------------旋转xz面顶点end-----------------------------

				//---------------------缩放xz面顶点start-----------------------------
				float angle = v.vertex.z + _Time.y;
				float4x4 sm = 
				{
					sin(angle)/8+0.5,0,0,0,
					0,1,0,0,
					0,0,1,0,
					0,0,0,1,
				};

				v.vertex = mul(sm,v.vertex);

				//优化计算,不知道为什么不正确?
				//float x = (sign(angle)/2+0.5)*v.vertex.x;
				//---------------------缩放xz面顶点start-----------------------------

				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.col = fixed4(0,1,1,1);

				return o;
			}
			
			fixed4 frag(v2f v):COLOR
			{
				return v.col;
			}

			ENDCG
		}
	} 
}

沿y轴旋转xz面顶点效果:
[置顶] Cg入门14:Vertex Shader - 几何变换 —顶点扭曲_第1张图片
水波纹效果:

你可能感兴趣的:([置顶] Cg入门14:Vertex Shader - 几何变换 —顶点扭曲)