我正在尝试编写一个只适用于某些顶点的顶点着色器。
在Unity中,文本渲染为一系列不相交的4顶点多边形。我正在尝试变换/旋转/缩放这些多边形,但它们是分开的。这样我就可以独立地移动每个字母。
我不认为这是可能的,但如果每个顶点都有一个ID,我可以将它们分组为0-3,4-7等,并将它们移动到那个方向。
根据顶点所附加的内容或它们的“编号”来修改顶点有什么诀窍吗?
发布于 2013-05-07 22:13:11
我不相信你可以直接这么做,但是你可以使用第二个UV通道来存储你的I。
这是一个重新调整用途的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的虚拟脚本。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;
}
}https://stackoverflow.com/questions/16415702
复制相似问题