// 顶点位移
Shader "Study/ImageShader2"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
/**
坐标转换
local(object) -> world -> camera(view) -> screen(project)
vector(x,y,z,w)
局部到全局 world = o2w_mat * local
世界到相机 camera = w2c_mat * world
相机到屏幕 screen = c2s_mat * camera
UnityObjectToClipPos:已经完成了 local-> screen 的转换
mul(UNITY_MATRIX_MVP,v.vertex) = UnityObjectToClipPos(v.vertex) MVP: M-局部到全局 V:世界到相机 P:相机到屏幕
M = mul(unity_ObjectToWorld,v.vertex)
// 位置矩阵
// 百度左手坐标系坐标变换
float4x4 = {
1,0,0,x,
0,1,0,y,
0,0,1,z,
0,0,0,1
}
// 缩放 -> 旋转 -> 平移
float4x4 = {
_Scale.x,0,0,_Position.x,
0,_Scale.y,0,_Position.y,
0,0,_Scale.z,_Position.z,
0,0,0,1
}
**/
v2f vert (appdata v)
{
// 在这些坐标变换
v2f o;
// o.vertex = UnityObjectToClipPos(v.vertex);
// o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
float4 worldPos = mul(unity_ObjectToWorld,v.vertex);
o.vertex = mul(UNITY_MATRIX_VP,worldPos);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
// col.rgb = 1 - col.rgb;
return col;
}
ENDCG
}
}
Fallback "Sprites/Default"
}
【002-Shader-顶点位移】
于 2022-05-19 14:43:21 首次发布