예제 #1
0
        protected virtual void BuildMeshAttributes(MeshPrimitive primitive, int meshID, int primitiveIndex)
        {
            if (_assetCache.MeshCache[meshID][primitiveIndex].MeshAttributes.Count == 0)
            {
                Dictionary <string, AttributeAccessor> attributeAccessors = new Dictionary <string, AttributeAccessor>(primitive.Attributes.Count + 1);
                foreach (var attributePair in primitive.Attributes)
                {
                    BufferCacheData   bufferCacheData   = _assetCache.BufferCache[attributePair.Value.Value.BufferView.Value.Buffer.Id];
                    AttributeAccessor AttributeAccessor = new AttributeAccessor()
                    {
                        AccessorId = attributePair.Value,
                        Stream     = bufferCacheData.Stream,
                        Offset     = bufferCacheData.ChunkOffset
                    };

                    attributeAccessors[attributePair.Key] = AttributeAccessor;
                }

                if (primitive.Indices != null)
                {
                    BufferCacheData   bufferCacheData = _assetCache.BufferCache[primitive.Indices.Value.BufferView.Value.Buffer.Id];
                    AttributeAccessor indexBuilder    = new AttributeAccessor()
                    {
                        AccessorId = primitive.Indices,
                        Stream     = bufferCacheData.Stream,
                        Offset     = bufferCacheData.ChunkOffset
                    };

                    attributeAccessors[SemanticProperties.INDICES] = indexBuilder;
                }

                GLTFHelpers.BuildMeshAttributes(ref attributeAccessors);
                _assetCache.MeshCache[meshID][primitiveIndex].MeshAttributes = attributeAccessors;
            }
        }
예제 #2
0
        protected virtual void BuildMeshAttributes(MeshPrimitive primitive, int meshID, int primitiveIndex)
        {
            if (_assetCache.MeshCache[meshID][primitiveIndex].MeshAttributes.Count == 0)
            {
                Dictionary <string, AttributeAccessor> attributeAccessors = new Dictionary <string, AttributeAccessor>(primitive.Attributes.Count + 1);
                foreach (var attributePair in primitive.Attributes)
                {
                    BufferCacheData   bufferCacheData   = _assetCache.BufferCache[attributePair.Value.Value.BufferView.Value.Buffer.Id];
                    AttributeAccessor AttributeAccessor = new AttributeAccessor()
                    {
                        AccessorId = attributePair.Value,
                        Stream     = bufferCacheData.Stream,
                        Offset     = bufferCacheData.ChunkOffset
                    };

                    attributeAccessors[attributePair.Key] = AttributeAccessor;
                }

                if (primitive.Indices != null)
                {
                    BufferCacheData   bufferCacheData = _assetCache.BufferCache[primitive.Indices.Value.BufferView.Value.Buffer.Id];
                    AttributeAccessor indexBuilder    = new AttributeAccessor()
                    {
                        AccessorId = primitive.Indices,
                        Stream     = bufferCacheData.Stream,
                        Offset     = bufferCacheData.ChunkOffset
                    };

                    attributeAccessors[SemanticProperties.INDICES] = indexBuilder;
                }

                GLTFHelpers.BuildMeshAttributes(ref attributeAccessors);

                // Flip vectors and triangles to the Unity coordinate system.
                if (attributeAccessors.ContainsKey(SemanticProperties.POSITION))
                {
                    NumericArray resultArray = attributeAccessors[SemanticProperties.POSITION].AccessorContent;
                    resultArray.AsVertices = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsVertices);
                    attributeAccessors[SemanticProperties.POSITION].AccessorContent = resultArray;
                }
                if (attributeAccessors.ContainsKey(SemanticProperties.INDICES))
                {
                    NumericArray resultArray = attributeAccessors[SemanticProperties.INDICES].AccessorContent;
                    resultArray.AsTriangles = GLTFUnityHelpers.FlipFaces(resultArray.AsTriangles);
                    attributeAccessors[SemanticProperties.INDICES].AccessorContent = resultArray;
                }
                if (attributeAccessors.ContainsKey(SemanticProperties.NORMAL))
                {
                    NumericArray resultArray = attributeAccessors[SemanticProperties.NORMAL].AccessorContent;
                    resultArray.AsNormals = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsNormals);
                    attributeAccessors[SemanticProperties.NORMAL].AccessorContent = resultArray;
                }
                // TexCoord goes from 0 to 3 to match GLTFHelpers.BuildMeshAttributes
                for (int i = 0; i < 4; i++)
                {
                    if (attributeAccessors.ContainsKey(SemanticProperties.TexCoord(i)))
                    {
                        NumericArray resultArray = attributeAccessors[SemanticProperties.TexCoord(i)].AccessorContent;
                        resultArray.AsTexcoords = GLTFUnityHelpers.FlipTexCoordArrayV(resultArray.AsTexcoords);
                        attributeAccessors[SemanticProperties.TexCoord(i)].AccessorContent = resultArray;
                    }
                }
                if (attributeAccessors.ContainsKey(SemanticProperties.TANGENT))
                {
                    NumericArray resultArray = attributeAccessors[SemanticProperties.TANGENT].AccessorContent;
                    resultArray.AsTangents = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsTangents);
                    attributeAccessors[SemanticProperties.TANGENT].AccessorContent = resultArray;
                }

                _assetCache.MeshCache[meshID][primitiveIndex].MeshAttributes = attributeAccessors;
            }
        }