Esempio n. 1
0
        private PbrMetallicRoughness ExportPBRMetallicRoughness(UnityEngine.Material material)
        {
            var pbr = new PbrMetallicRoughness();

            if (material.HasProperty("_Color"))
            {
                pbr.BaseColorFactor = material.GetColor("_Color");
            }

            if (material.HasProperty("_MainTex"))
            {
                var mainTex = material.GetTexture("_MainTex");

                if (mainTex != null)
                {
                    pbr.BaseColorTexture = ExportTextureInfo(mainTex);
                }
            }

            if (material.HasProperty("_Metallic"))
            {
                pbr.MetallicFactor = material.GetFloat("_Metallic");
            }

            if (material.HasProperty("_Roughness"))
            {
                pbr.RoughnessFactor = material.GetFloat("_Roughness");
            }
            else if (material.HasProperty("_Glossiness"))
            {
                pbr.RoughnessFactor = 1 - material.GetFloat("_Glossiness");
            }

            if (material.HasProperty("_MetallicRoughnessMap"))
            {
                var mrTex = material.GetTexture("_MetallicRoughnessMap");

                if (mrTex != null)
                {
                    pbr.MetallicRoughnessTexture = ExportTextureInfo(mrTex);
                }
            }
            else if (material.HasProperty("_MetallicGlossMap"))
            {
                var mgTex = material.GetTexture("_MetallicGlossMap");

                if (mgTex != null)
                {
                    pbr.MetallicRoughnessTexture = ExportTextureInfo(mgTex);
                }
            }

            return(pbr);
        }
Esempio n. 2
0
        public static PbrMetallicRoughness Deserialize(GLTFRoot root, JsonReader reader)
        {
            var metallicRoughness = new PbrMetallicRoughness();

            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 "baseColorFactor":
                    metallicRoughness.BaseColorFactor = reader.ReadAsRGBAColor();
                    break;

                case "baseColorTexture":
                    metallicRoughness.BaseColorTexture = TextureInfo.Deserialize(root, reader);
                    break;

                case "metallicFactor":
                    metallicRoughness.MetallicFactor = reader.ReadAsDouble().Value;
                    break;

                case "roughnessFactor":
                    metallicRoughness.RoughnessFactor = reader.ReadAsDouble().Value;
                    break;

                case "metallicRoughnessTexture":
                    metallicRoughness.MetallicRoughnessTexture = TextureInfo.Deserialize(root, reader);
                    break;

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

            return(metallicRoughness);
        }
Esempio n. 3
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;
                    }
                }
            }
        }