예제 #1
0
        protected virtual GameObject CreateMeshPrimitive(MeshPrimitive primitive)
        {
            var primitiveObj = new GameObject("Primitive");

            var meshFilter  = primitiveObj.AddComponent <MeshFilter>();
            var vertexCount = primitive.Attributes[SemanticProperties.POSITION].Value.Count;

            if (primitive.Contents == null)
            {
                primitive.Contents = new UnityEngine.Mesh
                {
                    vertices = primitive.Attributes[SemanticProperties.POSITION].Value.AsVertexArray(),

                    normals = primitive.Attributes.ContainsKey(SemanticProperties.NORMAL)
                                                ? primitive.Attributes[SemanticProperties.NORMAL].Value.AsNormalArray()
                                                : null,

                    uv = primitive.Attributes.ContainsKey(SemanticProperties.TexCoord(0))
                                                ? primitive.Attributes[SemanticProperties.TexCoord(0)].Value.AsTexcoordArray()
                                                : null,

                    uv2 = primitive.Attributes.ContainsKey(SemanticProperties.TexCoord(1))
                                                ? primitive.Attributes[SemanticProperties.TexCoord(1)].Value.AsTexcoordArray()
                                                : null,

                    uv3 = primitive.Attributes.ContainsKey(SemanticProperties.TexCoord(2))
                                                ? primitive.Attributes[SemanticProperties.TexCoord(2)].Value.AsTexcoordArray()
                                                : null,

                    uv4 = primitive.Attributes.ContainsKey(SemanticProperties.TexCoord(3))
                                                ? primitive.Attributes[SemanticProperties.TexCoord(3)].Value.AsTexcoordArray()
                                                : null,

                    colors = primitive.Attributes.ContainsKey(SemanticProperties.Color(0))
                                                ? primitive.Attributes[SemanticProperties.Color(0)].Value.AsColorArray()
                                                : null,

                    triangles = primitive.Indices != null
                                                ? primitive.Indices.Value.AsTriangles()
                                                : MeshPrimitive.GenerateTriangles(vertexCount),

                    tangents = primitive.Attributes.ContainsKey(SemanticProperties.TANGENT)
                                                ? primitive.Attributes[SemanticProperties.TANGENT].Value.AsTangentArray()
                                                : null
                };
            }

            meshFilter.sharedMesh = primitive.Contents;

            var material = CreateMaterial(
                primitive.Material != null ? primitive.Material.Value : DefaultMaterial,
                primitive.Attributes.ContainsKey(SemanticProperties.Color(0))
                );

            var meshRenderer = primitiveObj.AddComponent <MeshRenderer>();

            meshRenderer.material = material;

            return(primitiveObj);
        }
예제 #2
0
        public void BuildMeshAttributes()
        {
            if (Attributes.ContainsKey(SemanticProperties.POSITION))
            {
                var accessor = Attributes[SemanticProperties.POSITION].Value;
                accessor.AsVertexArray();
            }

            if (Indices != null)
            {
                var accessor = Indices.Value;
                accessor.AsTriangles();
            }

            if (Attributes.ContainsKey(SemanticProperties.NORMAL))
            {
                var accessor = Attributes[SemanticProperties.NORMAL].Value;
                accessor.AsNormalArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(0)))
            {
                var accessor = Attributes[SemanticProperties.TexCoord(0)].Value;
                accessor.AsTexcoordArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(1)))
            {
                var accessor = Attributes[SemanticProperties.TexCoord(1)].Value;
                accessor.AsTexcoordArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(2)))
            {
                var accessor = Attributes[SemanticProperties.TexCoord(2)].Value;
                accessor.AsTexcoordArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(3)))
            {
                var accessor = Attributes[SemanticProperties.TexCoord(3)].Value;
                accessor.AsTexcoordArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.Color(0)))
            {
                var accessor = Attributes[SemanticProperties.Color(0)].Value;
                accessor.AsColorArray();
            }
            if (Attributes.ContainsKey(SemanticProperties.TANGENT))
            {
                var accessor = Attributes[SemanticProperties.TANGENT].Value;
                accessor.AsTangentArray();
            }
        }
예제 #3
0
 /// <summary>
 /// Uses the accessor to parse the buffer into attributes needed to construct the mesh primitive
 /// </summary>
 /// <param name="attributes">A dictionary that contains a mapping of attribute name to data needed to parse</param>
 public static void BuildMeshAttributes(ref Dictionary <string, AttributeAccessor> attributes)
 {
     if (attributes.ContainsKey(SemanticProperties.POSITION))
     {
         var          attributeAccessor = attributes[SemanticProperties.POSITION];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsVertexArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.INDICES))
     {
         var          attributeAccessor = attributes[SemanticProperties.INDICES];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTriangles(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.NORMAL))
     {
         var          attributeAccessor = attributes[SemanticProperties.NORMAL];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsNormalArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.TexCoord(0)))
     {
         var          attributeAccessor = attributes[SemanticProperties.TexCoord(0)];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.TexCoord(1)))
     {
         var          attributeAccessor = attributes[SemanticProperties.TexCoord(1)];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.TexCoord(2)))
     {
         var          attributeAccessor = attributes[SemanticProperties.TexCoord(2)];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.TexCoord(3)))
     {
         var          attributeAccessor = attributes[SemanticProperties.TexCoord(3)];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.Color(0)))
     {
         var          attributeAccessor = attributes[SemanticProperties.Color(0)];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsColorArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
     if (attributes.ContainsKey(SemanticProperties.TANGENT))
     {
         var          attributeAccessor = attributes[SemanticProperties.TANGENT];
         NumericArray resultArray       = attributeAccessor.AccessorContent;
         int          offset            = (int)LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
         attributeAccessor.AccessorId.Value.AsTangentArray(ref resultArray, bufferViewCache, offset);
         attributeAccessor.AccessorContent = resultArray;
     }
 }
예제 #4
0
        public MeshPrimitiveAttributes BuildMeshAttributes(Dictionary <Buffer, byte[]> bufferCache)
        {
            var attributes = new MeshPrimitiveAttributes();

            if (Attributes.ContainsKey(SemanticProperties.POSITION))
            {
                var accessor   = Attributes[SemanticProperties.POSITION].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Vertices = accessor.AsVector3Array(bufferData);
            }

            if (Indices != null)
            {
                var accessor           = Indices.Value;
                var bufferData         = bufferCache[accessor.BufferView.Value.Buffer.Value];
                var unflippedTriangles = accessor.AsIntArray(bufferData);
                var triangles          = new int[unflippedTriangles.Length];
                for (int i = 0; i < unflippedTriangles.Length; i += 3)
                {
                    triangles[i + 2] = unflippedTriangles[i];
                    triangles[i + 1] = unflippedTriangles[i + 1];
                    triangles[i]     = unflippedTriangles[i + 2];
                }
                attributes.Triangles = triangles;
            }
            else
            {
                var triangles = new int[attributes.Vertices.Length];
                for (int i = 0; i < triangles.Length; i += 3)
                {
                    triangles[i + 2] = i;
                    triangles[i + 1] = i + 1;
                    triangles[i]     = i + 2;
                }
                attributes.Triangles = triangles;
            }

            if (Attributes.ContainsKey(SemanticProperties.NORMAL))
            {
                var accessor   = Attributes[SemanticProperties.NORMAL].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Normals = accessor.AsVector3Array(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(0)))
            {
                var accessor   = Attributes[SemanticProperties.TexCoord(0)].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Uv = accessor.AsVector2Array(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(1)))
            {
                var accessor   = Attributes[SemanticProperties.TexCoord(1)].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Uv2 = accessor.AsVector2Array(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(2)))
            {
                var accessor   = Attributes[SemanticProperties.TexCoord(2)].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Uv3 = accessor.AsVector2Array(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.TexCoord(3)))
            {
                var accessor   = Attributes[SemanticProperties.TexCoord(3)].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Uv4 = accessor.AsVector2Array(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.Color(0)))
            {
                var accessor   = Attributes[SemanticProperties.Color(0)].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Colors = accessor.AsColorArray(bufferData);
            }
            if (Attributes.ContainsKey(SemanticProperties.TANGENT))
            {
                var accessor   = Attributes[SemanticProperties.TANGENT].Value;
                var bufferData = bufferCache[accessor.BufferView.Value.Buffer.Value];
                attributes.Tangents = accessor.AsVector4Array(bufferData);
            }

            return(attributes);
        }
예제 #5
0
        // a mesh *might* decode to multiple prims if there are submeshes
        private MeshPrimitive[] ExportPrimitive(GameObject gameObject)
        {
            var filter  = gameObject.GetComponent <MeshFilter>();
            var meshObj = filter.sharedMesh;

            var renderer     = gameObject.GetComponent <MeshRenderer>();
            var materialsObj = renderer.sharedMaterials;

            var prims = new MeshPrimitive[meshObj.subMeshCount];

            // don't export any more accessors if this mesh is already exported
            MeshPrimitive[] primVariations;
            if (_meshToPrims.TryGetValue(meshObj, out primVariations) &&
                meshObj.subMeshCount == primVariations.Length)
            {
                for (var i = 0; i < primVariations.Length; i++)
                {
                    prims[i]          = primVariations[i].Clone();
                    prims[i].Material = ExportMaterial(materialsObj[i]);
                }

                return(prims);
            }

            AccessorId aPosition = null, aNormal = null, aTangent = null,
                       aTexcoord0 = null, aTexcoord1 = null, aColor0 = null;

            aPosition = ExportAccessor(InvertZ(meshObj.vertices));

            if (meshObj.normals.Length != 0)
            {
                aNormal = ExportAccessor(InvertZ(meshObj.normals));
            }

            if (meshObj.tangents.Length != 0)
            {
                aTangent = ExportAccessor(InvertW(meshObj.tangents));
            }

            if (meshObj.uv.Length != 0)
            {
                aTexcoord0 = ExportAccessor(InvertY(meshObj.uv));
            }

            if (meshObj.uv2.Length != 0)
            {
                aTexcoord1 = ExportAccessor(InvertY(meshObj.uv2));
            }

            if (meshObj.colors.Length != 0)
            {
                aColor0 = ExportAccessor(meshObj.colors);
            }

            MaterialId lastMaterialId = null;

            for (var submesh = 0; submesh < meshObj.subMeshCount; submesh++)
            {
                var primitive = new MeshPrimitive();
                primitive.Contents = meshObj;

                var triangles = meshObj.GetTriangles(submesh);
                primitive.Indices = ExportAccessor(FlipFaces(triangles));

                primitive.Attributes = new Dictionary <string, AccessorId>();
                primitive.Attributes.Add(SemanticProperties.POSITION, aPosition);

                if (aNormal != null)
                {
                    primitive.Attributes.Add(SemanticProperties.NORMAL, aNormal);
                }
                if (aTangent != null)
                {
                    primitive.Attributes.Add(SemanticProperties.TANGENT, aTangent);
                }
                if (aTexcoord0 != null)
                {
                    primitive.Attributes.Add(SemanticProperties.TexCoord(0), aTexcoord0);
                }
                if (aTexcoord1 != null)
                {
                    primitive.Attributes.Add(SemanticProperties.TexCoord(1), aTexcoord1);
                }
                if (aColor0 != null)
                {
                    primitive.Attributes.Add(SemanticProperties.Color(0), aColor0);
                }

                if (submesh < materialsObj.Length)
                {
                    primitive.Material = ExportMaterial(materialsObj[submesh]);
                    lastMaterialId     = primitive.Material;
                }
                else
                {
                    primitive.Material = lastMaterialId;
                }

                prims[submesh] = primitive;
            }

            _meshToPrims[meshObj] = prims;

            return(prims);
        }