我正在尝试编写一个仅适用于某些顶点的顶点着色器。

在 Unity 中,文本呈现为一系列脱节的 4 顶点多边形。
我正在尝试转换/旋转/缩放这些多边形,但要分开进行。
所以我可以独立移动每个字母。

我认为这是不可能的,但如果每个顶点都有一个 ID,我可以将它们分组为 0-3、4-7 等,然后以这种方式移动它们。

是否有任何技巧可以根据顶点所连接的内容或它们是哪个“数字”来修改顶点?

最佳答案

我不相信您可以直接这样做,但您可以使用第二个 UV channel 来存储您的 ID。

这是一个重新设计的 Unity normal shading example 使用“顶点 ID”来确定何时将法线应用为颜色。

Shader "Custom/ColorIdentity" {
Properties {
    //_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
    Pass {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata {
                float4 vertex : SV_POSITION;
                float3 normal : NORMAL;
                float4 texCoord2 : TEXCOORD1;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                float3 color : COLOR0;
            };

            v2f vert (appdata v)
            {
                v2f o;

                //here is where I read the vertex id, in texCoord2[0]
                //in this case I'm binning all values > 1 to id 1, else id 0
                //that way they can by multiplied against to the vertex normal
                //to turn color on and off.
                int vID = (v.texCoord2[0]>1)?1:0;

                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.color = v.normal * vID; //Use normal as color for vertices w/ ids>1
                return o;
            }

            half4 frag (v2f i) : COLOR
            {
                return half4 (i.color, 1);
            }
        ENDCG
    }
}
Fallback "Diffuse"
}

此外,这是我为分配完全任意的 ID 而编写的虚拟脚本。根据顶点在 y 轴上的位置分配 ID。边界是根据我手边的网格随机选择的:
public class AssignIdentity : MonoBehaviour {
public GameObject LiveObject;

// Use this for initialization
void Start () {
    Mesh mesh = LiveObject.GetComponent<MeshFilter>().mesh;
    Vector3[] vertices = mesh.vertices;
    Vector2[] uv2s = new Vector2[vertices.Length];

    int i = 0;
    foreach(Vector3 v in vertices){
        if(v.y>9.0f || v.y < 4.0){
            uv2s[i++] = new Vector2(2,0);
        } else {
            uv2s[i++] = new Vector2(1,0);
        }
    }
    mesh.uv2 = uv2s;
}
}

关于unity3d - 仅某些顶点的顶点着色器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16415702/

10-14 16:14
查看更多