Exemple #1
0
        public static MaterialCommonConstant Deserialize(GLTFRoot root, JsonReader reader)
        {
            var commonConstant = new MaterialCommonConstant();

            if (reader.Read() && reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("Asset must be an object.");
            }

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

                switch (curProp)
                {
                case "ambientFactor":
                    commonConstant.AmbientFactor = reader.ReadAsRGBColor();
                    break;

                case "lightmapTexture":
                    commonConstant.LightmapTexture = TextureInfo.Deserialize(root, reader);
                    break;

                case "lightmapFactor":
                    commonConstant.LightmapFactor = reader.ReadAsRGBColor();
                    break;

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

            return(commonConstant);
        }
        public static Material Deserialize(GLTFRoot root, JsonReader reader)
        {
            var material = new Material();

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

                switch (curProp)
                {
                case "pbrMetallicRoughness":
                    material.PbrMetallicRoughness = PbrMetallicRoughness.Deserialize(root, reader);
                    break;

                case "commonConstant":
                    material.CommonConstant = MaterialCommonConstant.Deserialize(root, reader);
                    break;

                case "normalTexture":
                    material.NormalTexture = NormalTextureInfo.Deserialize(root, reader);
                    break;

                case "occlusionTexture":
                    material.OcclusionTexture = OcclusionTextureInfo.Deserialize(root, reader);
                    break;

                case "emissiveTexture":
                    material.EmissiveTexture = TextureInfo.Deserialize(root, reader);
                    break;

                case "emissiveFactor":
                    material.EmissiveFactor = reader.ReadAsRGBColor();
                    break;

                case "alphaMode":
                    material.AlphaMode = reader.ReadStringEnum <AlphaMode>();
                    break;

                case "alphaCutoff":
                    material.AlphaCutoff = reader.ReadAsDouble().Value;
                    break;

                case "doubleSided":
                    material.DoubleSided = reader.ReadAsBoolean().Value;
                    break;

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

            return(material);
        }
Exemple #3
0
        private MaterialCommonConstant ExportCommonConstant(UnityEngine.Material materialObj)
        {
            if (_root.ExtensionsUsed == null)
            {
                _root.ExtensionsUsed = new List <string>();
                _root.ExtensionsUsed.Add("KHR_materials_common");
            }
            else if (!_root.ExtensionsUsed.Contains("KHR_materials_common"))
            {
                _root.ExtensionsUsed.Add("KHR_materials_common");
            }

            var constant = new MaterialCommonConstant();

            if (materialObj.HasProperty("_AmbientFactor"))
            {
                constant.AmbientFactor = materialObj.GetColor("_AmbientFactor");
            }

            if (materialObj.HasProperty("_LightMap"))
            {
                var lmTex = materialObj.GetTexture("_LightMap");

                if (lmTex != null)
                {
                    constant.LightmapTexture = ExportTextureInfo(lmTex);
                }
            }

            if (materialObj.HasProperty("_LightFactor"))
            {
                constant.LightmapFactor = materialObj.GetColor("_LightFactor");
            }

            return(constant);
        }
Exemple #4
0
        private static void MergeMaterialsImagesTexturesAndSamplers(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes)
        {
            if (mergeFromRoot.Samplers != null)
            {
                if (mergeToRoot.Samplers == null)
                {
                    mergeToRoot.Samplers = new List <Sampler>(mergeFromRoot.Samplers.Count);
                }

                mergeToRoot.Samplers.AddRange(mergeFromRoot.Samplers);
            }

            if (mergeFromRoot.Images != null)
            {
                if (mergeToRoot.Images == null)
                {
                    mergeToRoot.Images = new List <Image>(mergeFromRoot.Images.Count);
                }

                mergeToRoot.Images.AddRange(mergeFromRoot.Images);
                for (int i = previousGLTFSizes.PreviousImageCount; i < mergeToRoot.Images.Count; ++i)
                {
                    Image image = mergeToRoot.Images[i];
                    if (image.BufferView != null)
                    {
                        BufferViewId bufferViewId = image.BufferView;
                        bufferViewId.Id  += previousGLTFSizes.PreviousBufferViewCount;
                        bufferViewId.Root = mergeToRoot;
                    }
                }
            }

            if (mergeFromRoot.Textures != null)
            {
                if (mergeToRoot.Textures == null)
                {
                    mergeToRoot.Textures = new List <Texture>(mergeFromRoot.Textures.Count);
                }

                mergeToRoot.Textures.AddRange(mergeFromRoot.Textures);
                for (int i = previousGLTFSizes.PreviousTextureCount; i < mergeToRoot.Textures.Count; ++i)
                {
                    Texture texture = mergeToRoot.Textures[i];

                    if (texture.Sampler != null)
                    {
                        SamplerId samplerId = texture.Sampler;
                        samplerId.Id  += previousGLTFSizes.PreviousSamplerCount;
                        samplerId.Root = mergeToRoot;
                    }

                    if (texture.Source != null)
                    {
                        ImageId samplerId = texture.Source;
                        samplerId.Id  += previousGLTFSizes.PreviousImageCount;
                        samplerId.Root = mergeToRoot;
                    }
                }
            }

            if (mergeFromRoot.Materials != null)
            {
                if (mergeToRoot.Materials == null)
                {
                    mergeToRoot.Materials = new List <Material>(mergeFromRoot.Materials.Count);
                }

                mergeToRoot.Materials.AddRange(mergeFromRoot.Materials);
                for (int i = previousGLTFSizes.PreviousMaterialCount; i < mergeToRoot.Materials.Count; ++i)
                {
                    Material material = mergeToRoot.Materials[i];

                    PbrMetallicRoughness pbrMetallicRoughness = material.PbrMetallicRoughness;
                    if (pbrMetallicRoughness != null)
                    {
                        if (pbrMetallicRoughness.BaseColorTexture != null)
                        {
                            TextureId textureId = pbrMetallicRoughness.BaseColorTexture.Index;
                            textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                            textureId.Root = mergeToRoot;
                        }
                        if (pbrMetallicRoughness.MetallicRoughnessTexture != null)
                        {
                            TextureId textureId = pbrMetallicRoughness.MetallicRoughnessTexture.Index;
                            textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                            textureId.Root = mergeToRoot;
                        }
                    }

                    MaterialCommonConstant commonConstant = material.CommonConstant;
                    if (commonConstant?.LightmapTexture != null)
                    {
                        TextureId textureId = material.CommonConstant.LightmapTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.EmissiveTexture != null)
                    {
                        TextureId textureId = material.EmissiveTexture.Index;
                        material.EmissiveTexture.Index.Id += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.NormalTexture != null)
                    {
                        TextureId textureId = material.NormalTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }

                    if (material.OcclusionTexture != null)
                    {
                        TextureId textureId = material.OcclusionTexture.Index;
                        textureId.Id  += previousGLTFSizes.PreviousTextureCount;
                        textureId.Root = mergeToRoot;
                    }
                }
            }
        }