一個cube是24個頂點,12個三角面
Shader "my/jc"
{
SubShader
{
Tags
{
"RenderType"="Opaque"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal:NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex + v.normal * 0.1);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return fixed4(1, 1, 1, 1);
}
ENDCG
}
}
}
cube不是8個頂點而是24個頂點,即在cube每個頂點的位置,實際上有三個相同的且法線垂直且互不相連的頂點。由于傳遞過程中頂點是無法被得知是屬于哪個面片上的頂點,而且每個頂點連接配接 3 個面片,是以一個頂點同時擁有 3 個法線等屬性,這就需要 3 個頂點來表示,這就有了 3 * 8 = 24 個頂點。
Q:如何立方體6個面貼上不同的圖
A:spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class uv : MonoBehaviour {
MeshFilter msf;
Mesh mesh;
[SerializeField]
Vector2[] uvs;
// Use this for initialization
void Start () {
msf = GetComponent<MeshFilter>();
Mesh meshCopy = Mesh.Instantiate(msf.sharedMesh) as Mesh; // Make a deep copy
meshCopy.name = "Cube2";
mesh = msf.mesh = meshCopy; // Assign the copy to the meshes
if (mesh == null || mesh.uv.Length != 24)
{
Debug.Log("Script needs to be attached to built-in cube");
return;
}
uvs = mesh.uv;
// Front
uvs[0] =new Vector2(0.0f, 0.0f);
uvs[1] =new Vector2(0.333f, 0.0f);
uvs[2] =new Vector2(0.0f, 0.333f);
uvs[3] = new Vector2(0.333f, 0.333f);
// Top
uvs[8] = new Vector2(0.334f, 0.0f);
uvs[9] = new Vector2(0.666f, 0.0f);
uvs[4] = new Vector2(0.334f, 0.333f);
uvs[5] = new Vector2(0.666f, 0.333f);
// Back
uvs[10] = new Vector2(0.667f, 0.0f);
uvs[11] = new Vector2(1.0f, 0.0f);
uvs[6] = new Vector2(0.667f, 0.333f);
uvs[7] = new Vector2(1.0f, 0.333f);
// Bottom
uvs[15] = new Vector2(0.0f, 0.333f);
uvs[14] = new Vector2(0.333f, 0.334f);
uvs[12] = new Vector2(0.0f, 0.666f);
uvs[13] = new Vector2(0.333f, 0.666f);
// Left
uvs[19] = new Vector2(0.334f, 0.334f);
uvs[18] = new Vector2(0.666f, 0.334f);
uvs[16] = new Vector2(0.334f, 0.666f);
uvs[17] = new Vector2(0.666f, 0.666f);
// Right
uvs[23] = new Vector2(0.667f, 0.334f);
uvs[22] = new Vector2(1.00f, 0.334f);
uvs[20] = new Vector2(0.667f, 0.666f);
uvs[21] =new Vector2(1.0f, 0.666f);
mesh.uv = uvs;
}
}
類似于展開UV,把貼圖的每一塊指派給每個uv點
meshfilter元件有個mesh屬性,mesh屬性又有個uv屬性,這個uv屬性會直接控制顯示貼圖的哪一部分,以及如何顯示貼圖
uv中的每一項和vertices中的每一項都是一一對應的
- 參考的标準是,unity剛導入時的那張圖,即原圖
- 原圖的左下角uv坐标定為(0,0),原圖的右上角的uv坐标定位(1,1),原圖的其它任何一個位置按照比例都會有一個uv坐标,比如原圖的左上角的uv坐标定位(0,1),原圖的右下角的UV坐标定位(1,0),原圖的中心(對角線的交點)位置為(0.5,0.5),等等