• Unity 顶点vertices,uv,与图片贴图,与mesh


    mesh就是组成3d物体的三角形们。

    mesh由顶点组成的三角形组成,三角形的大小 并不 需要一样,由顶点之间的位置决定。

    mesh可以是一个或者多个面。

    贴图的原点在左下角,uv是贴图的坐标,数量和顶点数一样(不是100%确定,比如前后左右4个面,贴图最终如何封闭,我还不知道),是贴图和顶点的对应关系。

    新建空场景,把一下代码放到main camera

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. #endif
    8. public class codeMesh2 : MonoBehaviour
    9. {
    10. GUIStyle fontStyle = new GUIStyle();
    11. string msg = " - - ";
    12. // Start is called before the first frame update
    13. Vector3 center = new Vector3();
    14. GameObject o;
    15. bool ready = false;
    16. //int startCalled = 0;
    17. Camera c;
    18. string err = "";
    19. int moveRage = 25;
    20. int moveRage2 = 0;
    21. int moveRage2_1 = 0;
    22. Mesh mesh = null;
    23. public int width = 10;
    24. public int height = 10;
    25. public Texture aText = null;
    26. ///
    27. /// 上一次更新帧率的时间
    28. ///
    29. private float m_lastUpdateShowTime = 0f;
    30. ///
    31. /// 更新显示帧率的时间间隔
    32. ///
    33. private readonly float m_updateTime = 0.05f;
    34. ///
    35. /// 帧数
    36. ///
    37. private int m_frames = 0;
    38. ///
    39. /// 帧间间隔
    40. ///
    41. //private float m_frameDeltaTime = 0;
    42. private float m_FPS = 0;
    43. //private Rect m_fps, m_dtime;
    44. //顶点数组
    45. private Vector3[] vertices;
    46. //顶点法线
    47. private Vector3[] normals;
    48. private MeshFilter meshFilter;
    49. private int i = 0;
    50. private bool f = false;
    51. private string test2 = "";
    52. private int waitCount = 0;
    53. private void Awake()
    54. {
    55. Application.targetFrameRate = 30;
    56. moveRage2 = moveRage * 2;
    57. moveRage2_1 = moveRage * 2 - 1;
    58. fontStyle.normal.background = null; //设置背景填充
    59. fontStyle.normal.textColor = Color.red; //设置字体颜色
    60. fontStyle.fontSize = 40;
    61. fontStyle.wordWrap = true;
    62. c = GetComponent();
    63. msg += " Waiting ";
    64. Vector3 v1 = new Vector3(5, 5, 5);
    65. Vector3 v2 = new Vector3(5, 5, 3);
    66. test2 = (v2 - v1).normalized.ToString();
    67. }
    68. void Start()
    69. {
    70. try
    71. {
    72. o = GenerateMeshTest1();
    73. center = c.transform.position;
    74. o.name = "d mesh";
    75. o.transform.position = new Vector3(-5, -5, 20);
    76. o.transform.localScale = new Vector3(10.0f, 10.0f, 10.0f);
    77. o.transform.rotation = Quaternion.Euler(new Vector3(0, 45, 45));
    78. m_lastUpdateShowTime = Time.realtimeSinceStartup;
    79. ready = true;
    80. }
    81. catch (Exception e)
    82. {
    83. err = printException(e);
    84. }
    85. aText = Resources.Load("type/two_sizes") as Texture;
    86. o.GetComponent().material.mainTexture = aText;
    87. #if UNITY_EDITOR
    88. showNormals();
    89. #endif
    90. }
    91. private void OnGUI()
    92. {
    93. GUI.color = Color.red;
    94. msg = "fps=" + m_FPS + "/"+ Application.targetFrameRate +(err.Length>1?(", err=" + err):"") + " waitCount = " + (ready? waitCount : (waitCount++));
    95. GUI.Label(new Rect(50, 10, 900, 100), "" + test2, fontStyle);
    96. GUI.Label(new Rect(100, 100, 900, 100), "[V07]:" + msg, fontStyle);
    97. }
    98. // Update is called once per frame
    99. void Update()
    100. {
    101. if (!ready) return;
    102. int ri = i % (moveRage2);
    103. float x = ri - moveRage;
    104. if (!f)
    105. {
    106. x = moveRage - ri;
    107. }
    108. if (ri == moveRage2_1)
    109. {
    110. f = !f;
    111. }
    112. //o.transform.position = new Vector3(x, x, 20);
    113. //Debug.Log(x);
    114. //o.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
    115. /*
    116. Mesh mesh = o.GetComponent().mesh;
    117. //mesh.Clear();
    118. Vector3[] vertices = mesh.vertices;
    119. Vector3[] normals = mesh.normals;
    120. for (var i = 0; i < vertices.Length; i++)
    121. {
    122. vertices[i] += normals[i] * Mathf.Sin(Time.time);
    123. }
    124. mesh.vertices = vertices;
    125. */
    126. m_frames++;
    127. if (Time.realtimeSinceStartup - m_lastUpdateShowTime >= m_updateTime)
    128. {
    129. m_FPS = m_frames / (Time.realtimeSinceStartup - m_lastUpdateShowTime);
    130. //m_frameDeltaTime = (Time.realtimeSinceStartup - m_lastUpdateShowTime) / m_frames;
    131. m_frames = 0;
    132. m_lastUpdateShowTime = Time.realtimeSinceStartup;
    133. //Debug.Log("FPS: " + m_FPS + ",间隔: " + m_FrameDeltaTime);
    134. }
    135. i++;
    136. }
    137. private GameObject GenerateMeshTest1()
    138. {
    139. GameObject obj = new GameObject();
    140. meshFilter = obj.AddComponent();
    141. //创建mesh
    142. mesh = new Mesh();
    143. meshFilter.mesh = mesh;
    144. MeshRenderer renderer = obj.AddComponent();
    145. //标准材质
    146. Material mat = new Material(Shader.Find("Legacy Shaders/Transparent/Diffuse"));
    147. mat.color = Color.white;
    148. renderer.material = mat;
    149. //创建顶点和UV
    150. vertices = new Vector3[6];
    151. Vector2[] uv = new Vector2[6];
    152. vertices[0] = new Vector3(0, 1, 0);//0
    153. vertices[1] = new Vector3(1, 1, 0);//1
    154. vertices[2] = new Vector3(1, 0, 0);//2
    155. vertices[3] = new Vector3(0, 0, 0);//3
    156. vertices[4] = new Vector3(0, 0, 1);//4
    157. vertices[5] = new Vector3(1, 0, 1);//5-
    158. int k = 0;
    159. uv[k++%6] = new Vector2(0, 1);
    160. uv[k++ % 6] = new Vector2(1, 1);
    161. uv[k++ % 6] = new Vector2(1, 0.5f);
    162. uv[k++ % 6] = new Vector2(0,0.5f);
    163. uv[k++ % 6] = new Vector2(0, 0);
    164. uv[k++ % 6] = new Vector2(1, 0);
    165. mesh.vertices = vertices;
    166. mesh.uv = uv;
    167. //mat.mainTexture = aText;
    168. int[] triangles = new int[12];
    169. k = 0;
    170. triangles[k++] = 3;
    171. triangles[k++] = 2;
    172. triangles[k++] = 5;
    173. triangles[k++] = 3;
    174. triangles[k++] = 5;
    175. triangles[k++] = 4;
    176. triangles[k++] = 0;
    177. triangles[k++] = 1;
    178. triangles[k++] = 2;
    179. triangles[k++] = 0;
    180. triangles[k++] = 2;
    181. triangles[k++] = 3;
    182. mesh.triangles = triangles; //三角面
    183. mesh.RecalculateNormals(); //计算法线
    184. /*Vector3[] normals = new Vector3[4];
    185. normals[0] = Vector3.back;
    186. normals[1] = Vector3.back;
    187. normals[2] = Vector3.back;
    188. normals[3] = Vector3.back;
    189. mesh.normals = normals;*/
    190. //mesh.SetIndices
    191. //mat.SetTexture()
    192. return obj;
    193. }
    194. public static string printException(Exception e)
    195. {
    196. return "\n\trs=" + e.HResult + ",\n\tmsg=" + e.Message + ",\n\tstack=" + e.StackTrace + "\n------------------------------------\n";
    197. }
    198. #if UNITY_EDITOR
    199. private void showNormals()
    200. {
    201. if (mesh != null)
    202. {
    203. //当前对象的操做从局部空间转换到世界空间 这样在下面的操作位置即可同步 否则位置是不会同步的
    204. Handles.matrix = meshFilter.transform.localToWorldMatrix;
    205. Handles.color = Color.green;
    206. int vertextCount = mesh.vertices.Length;
    207. //采用从顶点的位置[法线的起点】到法线的终点位置,既可以显示出当前顶点的法线
    208. for (int index = 0; index < vertextCount; index++)
    209. {
    210. Handles.DrawLine(vertices[index], vertices[index] + normals[index]);
    211. }
    212. }
    213. }
    214. #endif
    215. }

    two_sizes.png是一张 宽高比为1:2的图片。

    三角形之间的定义顺序不会影响贴图。但单个三角形三个顶点是逆时针,则从外部可以看到贴图,从内部看不到。反之亦然:

    (顺时针之后,从内部可见)

    如果把图片直接放到场景,则两面都可见。

  • 相关阅读:
    Spring Boot常用的参数验证技巧和使用方法
    不到5分钟就被赶出来的面试,我直呼太变态了......
    Go的全新漏洞检测工具govulncheck来了
    Session会话技术详解
    linux安装git
    M401H_山东移动_SM0_SD46_国科6323芯片_安卓4_AI语音_免拆卡刷固件包
    二分算法(超详细)
    合并多个.a库为一个.a库
    vue中引入字体
    JS操作字符串方法学习系列(5)-每天学习10个方法
  • 原文地址:https://blog.csdn.net/leinchu/article/details/132617547