直接できるとは思えませんが、2 番目のUV
チャネルを使用して ID を保存できます。
これは、法線を色としていつ適用するかを決定するために「頂点 ID」を使用して 再利用されたUnity 法線シェーディングの例です。
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 を割り当てるために作成したダミー スクリプトです。ID は、y 軸上の頂点の位置に基づいて割り当てられます。境界は、手元にあるメッシュに基づいてランダムに選択されました。
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;
}
}