unity shader 之 幾何變換--扭曲
阿新 • • 發佈:2019-01-26
Shader "Custom/twist" { SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "unitycg.cginc" struct v2f { float4 pos : POSITION; float4 color : COLOR0; }; v2f vert(appdata_base v) { float angle = length(v.vertex) * _SinTime.w; //旋轉矩陣 //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), //}; //對旋轉矩陣進行優化計算,其實就是手動的進行旋轉矩陣的相乘的結果 float x = cos(angle) * v.vertex.x + sin(angle) * v.vertex.z; float z = cos(angle) * v.vertex.z - sin(angle) * v.vertex.x; v.vertex.x = x; v.vertex.z = z; //v.vertex = mul(m, v.vertex); v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.color = fixed4(0, 1, 1, 1); return o; } fixed4 frag(v2f IN) : COLOR { return IN.color; } ENDCG } } FallBack "Diffuse" }
效果如下(需要執行):
繼續改造程式碼,呈現一些不同的效果:
修改 22 行如下:
float angle = length(v.vertex) * _Time.y;
註釋掉32, 33, 34, 39, 41,然後在36行新增如下程式碼:
float x = sin(angle) * v.vertex.x;
執行後效果如下(類似水波的效果):