private Mesh LoadMesh(glTFLoader.Schema.MeshPrimitive[] primitives) { var subMeshes = new GltfMesh[primitives.Length]; for (int i = 0; i < primitives.Length; i++) { var primitive = primitives[i]; subMeshes[i] = LoadSubMesh(primitive); } return(CreateUnityMesh(subMeshes)); }
private GltfMesh LoadSubMesh(glTFLoader.Schema.MeshPrimitive primitive) { var mesh = new GltfMesh(); // Consider moving this logic into another class (SubMeshLoader.cs?) foreach (var attribute in primitive.Attributes) { LoadMeshAttribute(mesh, attribute.Key, attribute.Value); } LoadMeshAttribute(mesh, "INDEX", primitive.Indices.GetValueOrDefault()); return(mesh); }
private void LoadMeshAttribute(GltfMesh mesh, string attributeName, int accessorIndex) { var arr = accessorLoader.LoadAccessor(accessorIndex); switch (attributeName) { case "POSITION": { var vertices = (Vector3[])arr; CoordinateSystemConverter.Convert(vertices); mesh.vertices = vertices; break; } case "NORMAL": { var normals = (Vector3[])arr; CoordinateSystemConverter.Convert(normals); mesh.normals = normals; break; } case "TANGENT": { var tangents = (Vector4[])arr; CoordinateSystemConverter.Convert(tangents); mesh.tangents = tangents; break; } case "TEXCOORD_0": { var texCoords = (Vector2[])arr; CoordinateSystemConverter.FlipTexCoords(texCoords); mesh.texCoords = texCoords; break; } case "COLOR_0": { var colors = (Vector4[])arr; mesh.colors = TypeConverter.ConvertColors(colors); break; } case "INDEX": { var indices = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { indices[i] = Convert.ToInt32(arr.GetValue(i)); } CoordinateSystemConverter.FlipIndices(indices); mesh.indices = indices; break; } case "JOINTS_0": mesh.joints = (Vector4[])arr; break; case "WEIGHTS_0": mesh.weights = (Vector4[])arr; break; default: Debug.LogWarning($"Invalid accessor name ({attributeName})"); break; } }