public static GLTFMesh Deserialize(GLTFRoot root, JsonReader reader) { var mesh = new GLTFMesh(); while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var curProp = reader.Value.ToString(); switch (curProp) { case "primitives": mesh.Primitives = reader.ReadList(() => GLTFMeshPrimitive.Deserialize(root, reader)); break; case "weights": mesh.Weights = reader.ReadDoubleList(); break; default: mesh.DefaultPropertyDeserializer(root, reader); break; } } return(mesh); }
public static GLTFMeshPrimitive Deserialize(GLTFRoot root, JsonReader reader) { var primitive = new GLTFMeshPrimitive(); while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var curProp = reader.Value.ToString(); switch (curProp) { case "attributes": primitive.Attributes = reader.ReadAsDictionary(() => new GLTFAccessorId { Id = reader.ReadAsInt32().Value, Root = root }); break; case "indices": primitive.Indices = GLTFAccessorId.Deserialize(root, reader); break; case "material": primitive.Material = GLTFMaterialId.Deserialize(root, reader); break; case "mode": primitive.Mode = (GLTFDrawMode)reader.ReadAsInt32().Value; break; case "targets": primitive.Targets = reader.ReadList(() => { return(reader.ReadAsDictionary(() => new GLTFAccessorId { Id = reader.ReadAsInt32().Value, Root = root })); }); break; default: primitive.DefaultPropertyDeserializer(root, reader); break; } } return(primitive); }
private GameObject CreateMeshPrimitive(GLTFMeshPrimitive primitive) { var primitiveObj = new GameObject("Primitive"); var meshFilter = primitiveObj.AddComponent <MeshFilter>(); var attributes = _attributesCache[primitive]; var mesh = new Mesh { vertices = attributes.Vertices, normals = attributes.Normals, uv = attributes.Uv, uv2 = attributes.Uv2, uv3 = attributes.Uv3, uv4 = attributes.Uv4, colors = attributes.Colors, triangles = attributes.Triangles, tangents = attributes.Tangents }; meshFilter.mesh = mesh; var meshRenderer = primitiveObj.AddComponent <MeshRenderer>(); if (primitive.Material != null) { var materialCacheKey = new MaterialCacheKey { Material = primitive.Material.Value, UseVertexColors = attributes.Colors != null }; meshRenderer.material = FindOrCreateMaterial(materialCacheKey); } else { var materialCacheKey = new MaterialCacheKey { Material = new GLTFMaterial(), UseVertexColors = attributes.Colors != null }; meshRenderer.material = FindOrCreateMaterial(materialCacheKey); } return(primitiveObj); }