unity还原three——顶点,三角面,uv
public class Geometry { public Geometry(string name, Data data, Hashtable hash) { Debug.Log("解析type为Geometry的模型---开始"); GameObject gameobject = new GameObject(); gameobject.name = name; gameobject.AddComponent<MeshFilter>(); gameobject.AddComponent<MeshRenderer>(); //从json中取到的数组 int[] _faces = data.faces; float[] _vertices = data.vertices; float[] uvss = data.uvs; List<Vector3> verticesOld = new List<Vector3>(); List<Vector3> vertices = new List<Vector3>(); List<int> faces = new List<int>(); List<Vector2> _uvs = new List<Vector2>(); List<Vector2> uvs = new List<Vector2>(); //通过array构造顶点 for (int i = 0; i < _vertices.Length; i += 3) { verticesOld.Add(new Vector3(_vertices[i], _vertices[i + 1], _vertices[i + 2])); } Debug.Log(name + " uvs.Length:" + uvss.Length); for (int i = 0; i < uvss.Length; i += 2) { _uvs.Add(new Vector2(uvss[i],uvss[i+1])); } //根据_faces中的顶点,uv数据对顶点和uv进行重构。 int count = 0; if (_faces.Length > 0) { int loop = 0; //faces中包含uv数据 if (_faces[0] != 50 ) { int i; for (i = 0; i < _faces.Length; ) { if (_faces[i] == 58) { loop = 12; }else if (_faces[i] == 186) { loop = 15; }else if (_faces[i] == 170) { loop = 14; } vertices.Add(verticesOld[_faces[i + 1]]); vertices.Add(verticesOld[_faces[i + 2]]); vertices.Add(verticesOld[_faces[i + 3]]); faces.Add(count++); faces.Add(count++); faces.Add(count++); uvs.Add(_uvs[_faces[i + 5]]); uvs.Add(_uvs[_faces[i + 6]]); uvs.Add(_uvs[_faces[i + 7]]); //打印未考虑到的type if (_faces[i] != 58 && _faces[i] != 186 && _faces[i] != 170) { Debug.Log("faces中未包含type:"+_faces[i]+","+_faces[i+1]+","+_faces[i+2]); } i += loop; } } //faces中不包含uv else if (_faces[0] == 50) { loop = 9; for (int i = 0; i < _faces.Length; i += loop) { vertices.Add(verticesOld[_faces[i + 1]]); vertices.Add(verticesOld[_faces[i + 2]]); vertices.Add(verticesOld[_faces[i + 3]]); faces.Add(count++); faces.Add(count++); faces.Add(count++); } } } Mesh mesh = new Mesh(); mesh.name = name; mesh.vertices = vertices.ToArray(); mesh.triangles = faces.ToArray(); mesh.uv = uvs.ToArray(); gameobject.GetComponent<MeshFilter>().mesh = mesh; //将网格资源存入给定的磁盘路径下,以便导出 AssetDatabase.CreateAsset(mesh, ExportClass.prePath + name + "mesh.asset"); //渲染材质 if (hash["materials"] != null) { new MaterialComponent().setMaterialInfo((Materials)hash["materials"], gameobject); } gameobject.GetComponent<MeshRenderer>().receiveShadows = (bool) hash["receiveShadow"]; if ((bool) hash["castShadow"]) { gameobject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On; } else { gameobject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } setTransform((MatrixParse) hash["matrixParse"], gameobject); //挂在父对象下 gameobject.transform.parent = GameObject.Find(hash["parentName"].ToString()).transform; Debug.Log("解析type为Geometry的模型---结束"); } //设置位移旋转缩放 private void setTransform(MatrixParse matrixParse, GameObject gameObject) { gameObject.transform.localScale = matrixParse.GetLocalScale; Vector3 pos = matrixParse.GetPosition; gameObject.transform.position = new Vector3(-pos.x,pos.y,pos.z); gameObject.transform.Rotate(new Vector3(matrixParse.GetAngles.x,0,0),Space.Self); gameObject.transform.Rotate(new Vector3(0,matrixParse.GetAngles.y,0),Space.Self); gameObject.transform.Rotate(new Vector3(0,0,matrixParse.GetAngles.z),Space.Self); //反转 Reverse(gameObject.transform,matrixParse); } private void Reverse(Transform form,MatrixParse matrixParse) { //反转 Vector3 pos = matrixParse.GetPosition; form.position = new Vector3(-pos.x,pos.y,pos.z); Vector3 scale = matrixParse.GetLocalScale; form.localScale = new Vector3(-scale.x,-scale.y,-scale.z); form.Rotate(new Vector3(180,0,0),Space.World); } }