1.顶点绕Y轴旋转
Shader "Custom/test1" {
SubShader {
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct v2f{
float4 pos:POSITION;
fixed4 col:COLOR;
};
v2f vert(appdata_base v)
{
//声明一个随时间变化的角度
float angle = length(v.vertex)*_SinTime.w;
//声明一个绕着y轴的旋转矩阵
float4x4 m={
float4(cos(angle),0,sin(angle),0),
float4(0,1,0,0),
float4(-sin(angle),0,cos(angle),0),
float4(0,0,0,1)
};
//在mvp执行之前先通过旋转来影响顶点信息
//v.vertex = mul(m,v.vertex);
//也可以直接对x z做计算 不需要进行矩阵乘法 减少计算量
float x = cos(angle)*v.vertex.x+sin(angle)*v.vertex.z;
float z = -sin(angle)*v.vertex.x+cos(angle)*v.vertex.z;
v.vertex.x = x;
v.vertex.z = z;
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.col = float4(0,1,1,1);
return o;
}
fixed4 frag(v2f IN):COLOR
{
return IN.col;
}
ENDCG
}
}
}
2.x随时间分量缩放
Shader "Custom/test1" {
SubShader {
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct v2f{
float4 pos:POSITION;
fixed4 col:COLOR;
};
v2f vert(appdata_base v)
{
//声明一个随时间变化的角度
float angle = length(v.vertex)*_SinTime.w;
//声明一个绕着y轴的旋转矩阵
float4x4 m={
float4(cos(angle),0,sin(angle),0),
float4(0,1,0,0),
float4(-sin(angle),0,cos(angle),0),
float4(0,0,0,1)
};
//在mvp执行之前先通过旋转来影响顶点信息
//v.vertex = mul(m,v.vertex);
//也可以直接对x z做计算 不需要进行矩阵乘法 减少计算量
//float x = cos(angle)*v.vertex.x+sin(angle)*v.vertex.z;
//float z = -sin(angle)*v.vertex.x+cos(angle)*v.vertex.z;\
float sangle = v.vertex.z+_Time.y;
float x = (sin(sangle)/8+0.5)*v.vertex.x;
v.vertex.x = x;
//v.vertex.z = z;
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.col = float4(0,1,1,1);
return o;
}
fixed4 frag(v2f IN):COLOR
{
return IN.col;
}
ENDCG
}
}
}