顶点着色器仅适用于某些顶点

时间:2013-05-07 09:32:25

标签: unity3d shader vertex

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

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

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

是否有任何技巧可以修改顶点,具体取决于它们附加的是什么,或者它们是“数字”?

1 个答案:

答案 0 :(得分:2)

我认为您不能直接这样做,但您可以使用第二个UV频道来存储您的ID。

以下是使用“顶点ID”重新定位的Unity normal shading example,以确定何时将法线应用为颜色。

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;
}
}
相关问题