Beispiel #1
0
        private static void MergeMeshes(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes)
        {
            if (mergeFromRoot.Meshes == null)
            {
                return;
            }

            if (mergeToRoot.Meshes == null)
            {
                mergeToRoot.Meshes = new List <Mesh>(mergeFromRoot.Meshes.Count);
            }

            mergeToRoot.Meshes.AddRange(mergeFromRoot.Meshes);
            for (int i = previousGLTFSizes.PreviousMeshCount; i < mergeToRoot.Meshes.Count; ++i)
            {
                Mesh mesh = mergeToRoot.Meshes[i];
                if (mesh.Primitives != null)
                {
                    foreach (MeshPrimitive primitive in mesh.Primitives)
                    {
                        foreach (var attributeAccessorPair in primitive.Attributes)
                        {
                            AccessorId accessorId = attributeAccessorPair.Value;
                            accessorId.Id  += previousGLTFSizes.PreviousAccessorCount;
                            accessorId.Root = mergeToRoot;
                        }

                        if (primitive.Indices != null)
                        {
                            AccessorId accessorId = primitive.Indices;
                            accessorId.Id  += previousGLTFSizes.PreviousAccessorCount;
                            accessorId.Root = mergeToRoot;
                        }

                        if (primitive.Material != null)
                        {
                            MaterialId materialId = primitive.Material;
                            materialId.Id  += previousGLTFSizes.PreviousMaterialCount;
                            materialId.Root = mergeToRoot;
                        }

                        if (primitive.Targets != null)
                        {
                            foreach (Dictionary <string, AccessorId> targetsDictionary in primitive.Targets)
                            {
                                foreach (var targetsPair in targetsDictionary)
                                {
                                    AccessorId accessorId = targetsPair.Value;
                                    accessorId.Id  += previousGLTFSizes.PreviousAccessorCount;
                                    accessorId.Root = mergeToRoot;
                                }
                            }
                        }
                    }
                }
            }
        }
        public static MeshPrimitive Deserialize(GLTFRoot root, JsonReader reader)
        {
            var primitive = new MeshPrimitive();

            while (reader.Read() && reader.TokenType == JsonToken.PropertyName)
            {
                var curProp = reader.Value.ToString();

                switch (curProp)
                {
                case "attributes":
                    primitive.Attributes = reader.ReadAsDictionary(() => new AccessorId
                    {
                        Id   = reader.ReadAsInt32().Value,
                        Root = root
                    });
                    break;

                case "indices":
                    primitive.Indices = AccessorId.Deserialize(root, reader);
                    break;

                case "material":
                    primitive.Material = MaterialId.Deserialize(root, reader);
                    break;

                case "mode":
                    primitive.Mode = (DrawMode)reader.ReadAsInt32().Value;
                    break;

                case "targets":
                    primitive.Targets = reader.ReadList(() =>
                    {
                        return(reader.ReadAsDictionary(() => new AccessorId
                        {
                            Id = reader.ReadAsInt32().Value,
                            Root = root
                        }));
                    });
                    break;

                default:
                    primitive.DefaultPropertyDeserializer(root, reader);
                    break;
                }
            }

            return(primitive);
        }
Beispiel #3
0
        private MaterialId ExportMaterial(UnityEngine.Material materialObj)
        {
            var id = _root.GetMaterialId(materialObj);

            if (id != null)
            {
                return(id);
            }

            var material = new Material();

            if (ExportNames)
            {
                material.Name = materialObj.name;
            }

            if (materialObj.HasProperty("_Cutoff"))
            {
                material.AlphaCutoff = materialObj.GetFloat("_Cutoff");
            }

            switch (materialObj.GetTag("RenderType", false, ""))
            {
            case "TransparentCutout":
                material.AlphaMode = AlphaMode.MASK;
                break;

            case "Transparent":
                material.AlphaMode = AlphaMode.BLEND;
                break;

            default:
                material.AlphaMode = AlphaMode.OPAQUE;
                break;
            }

            material.DoubleSided = materialObj.HasProperty("_Cull") &&
                                   materialObj.GetInt("_Cull") == (float)UnityEngine.Rendering.CullMode.Off;

            if (materialObj.HasProperty("_EmissionColor"))
            {
                material.EmissiveFactor = materialObj.GetColor("_EmissionColor");
            }

            if (materialObj.HasProperty("_EmissionMap"))
            {
                var emissionTex = materialObj.GetTexture("_EmissionMap");

                if (emissionTex != null)
                {
                    material.EmissiveTexture = ExportTextureInfo(emissionTex);
                }
            }

            if (materialObj.HasProperty("_BumpMap"))
            {
                var normalTex = materialObj.GetTexture("_BumpMap");

                if (normalTex != null)
                {
                    material.NormalTexture = ExportNormalTextureInfo(normalTex, materialObj);
                }
            }

            if (materialObj.HasProperty("_OcclusionMap"))
            {
                var occTex = materialObj.GetTexture("_OcclusionMap");
                if (occTex != null)
                {
                    material.OcclusionTexture = ExportOcclusionTextureInfo(occTex, materialObj);
                }
            }

            switch (materialObj.shader.name)
            {
            case "Standard":
            case "GLTF/GLTFStandard":
                material.PbrMetallicRoughness = ExportPBRMetallicRoughness(materialObj);
                break;

            case "GLTF/GLTFConstant":
                material.CommonConstant = ExportCommonConstant(materialObj);
                break;
            }

            material.ContentsWithoutVC = materialObj;

            id = new MaterialId {
                Id   = _root.Materials.Count,
                Root = _root
            };
            _root.Materials.Add(material);

            return(id);
        }
Beispiel #4
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);
        }