Unity Vertex Transformation and Effects

According to the Unity rendering pipeline model, vertices need to be converted to clip space in the vertex function.

With the help of Unity built-in functions, refer to the article . Implement the conversion.

Such as: UnityObjectToClipPos(v.vertex); Convert v.vertex to clipping space coordinates

Here are a few examples

Example 1 Change the color according to the model coordinates

Take the x>0 of the model as the switching condition

v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);  //v.vertex原始顶点函数,转换成裁剪空间后的坐标 o.vertex
if (v.vertex.x > 0) //模型x轴坐标
    o.col = float4(1, 0, 0, 1);  //红色
else
    o.col = float4(0, 1, 0, 1);  //绿色

return o;

Example 2 Change the color according to the world coordinates

v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);  //v.vertex原始顶点函数,转换成裁剪空间后的坐标
float4 wpsp = mul(unity_ObjectToWorld, v.vertex); //v.vertex原始顶点函数转换成世界坐标

if (wpsp.x > 0) //世界x轴坐标
    o.col = float4(1, 0, 0, 1);  //红色
else
    o.col = float4(0, 1, 0, 1);  //绿色

return o;

When the moving object is larger than the origin of the world coordinates, the vertex of the model is displayed in red, otherwise it is displayed in green, as shown in the figure below:

Sample project download

Guess you like

Origin blog.csdn.net/st75033562/article/details/129277807