Esempio n. 1
0
        protected Shader FindShader(string shaderName)
        {
            var shader = Shader.Find(shaderName);

            if (shader == null)
            {
                logger?.Error(LogCode.ShaderMissing, shaderName);
            }
            return(shader);
        }
        static void ExportUnlit(Material material, UnityEngine.Material uMaterial, IGltfWritable gltf, ICodeLogger logger)
        {
            gltf.RegisterExtensionUsage(Extension.MaterialsUnlit);
            material.extensions = material.extensions ?? new MaterialExtension();
            material.extensions.KHR_materials_unlit = new MaterialUnlit();

            var pbr = material.pbrMetallicRoughness ?? new PbrMetallicRoughness();

            if (uMaterial.HasProperty(k_Color))
            {
                pbr.baseColor = uMaterial.GetColor(k_Color);
            }

            if (uMaterial.HasProperty(k_MainTex))
            {
                var mainTex = uMaterial.GetTexture(k_MainTex);
                if (mainTex != null)
                {
                    if (mainTex is Texture2D)
                    {
                        pbr.baseColorTexture = ExportTextureInfo(mainTex, TextureMapType.Main, gltf);
                        ExportTextureTransform(pbr.baseColorTexture, uMaterial, k_MainTex, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "main", material.name);
                    }
                }
            }

            material.pbrMetallicRoughness = pbr;
        }
 /// <inheritdoc />
 public void SetParent(uint nodeIndex, uint parentIndex)
 {
     if (nodes[nodeIndex] == null || nodes[parentIndex] == null)
     {
         logger?.Error(LogCode.HierarchyInvalid);
         return;
     }
     nodes[nodeIndex].transform.SetParent(nodes[parentIndex].transform, false);
 }
Esempio n. 4
0
        static unsafe JobHandle?GetJointsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            uint4 *output,
            int outputByteStride,
            ICodeLogger logger
            )
        {
            Profiler.BeginSample("GetJointsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.UnsignedByte:
                var jointsUInt8Job = new Jobs.ConvertBoneJointsUInt8ToUInt32Job();
                jointsUInt8Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 4;
                jointsUInt8Job.input            = (byte *)input;
                jointsUInt8Job.outputByteStride = outputByteStride;
                jointsUInt8Job.result           = output;
                jobHandle = jointsUInt8Job.Schedule(count, GltfImport.DefaultBatchCount);
                break;

            case GLTFComponentType.UnsignedShort:
                var jointsUInt16Job = new Jobs.ConvertBoneJointsUInt16ToUInt32Job();
                jointsUInt16Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 8;
                jointsUInt16Job.input            = (byte *)input;
                jointsUInt16Job.outputByteStride = outputByteStride;
                jointsUInt16Job.result           = output;
                jobHandle = jointsUInt16Job.Schedule(count, GltfImport.DefaultBatchCount);
                break;

            default:
                logger?.Error(LogCode.TypeUnsupported, "Joints", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Esempio n. 5
0
        /// <summary>
        /// Converts a Unity material to a glTF material.
        /// </summary>
        /// <param name="uMaterial">Source material</param>
        /// <param name="material">Resulting material</param>
        /// <param name="gltf">Associated IGltfWriter. Is used for adding images and textures.</param>
        /// <param name="logger">Logger used for reporting</param>
        /// <returns>True if no errors occured, false otherwise</returns>
        public override bool ConvertMaterial(UnityEngine.Material uMaterial, out Material material, IGltfWritable gltf, ICodeLogger logger)
        {
            var success = true;

            material = new Material {
                name = uMaterial.name,
                pbrMetallicRoughness = new PbrMetallicRoughness {
                    metallicFactor  = 0,
                    roughnessFactor = 1.0f
                }
            };

            SetAlphaModeAndCutoff(uMaterial, material);
            material.doubleSided = IsDoubleSided(uMaterial);

            //
            // Emission
            //
            if (uMaterial.HasProperty(k_EmissiveColor))
            {
                var emissionColor = uMaterial.GetColor(k_EmissiveColor);

                // Clamp emissiveColor to 0..1
                var maxFactor = math.max(emissionColor.r, math.max(emissionColor.g, emissionColor.b));
                if (maxFactor > 1f)
                {
                    emissionColor.r /= maxFactor;
                    emissionColor.g /= maxFactor;
                    emissionColor.b /= maxFactor;
                    // TODO: use maxFactor as emissiveStrength (KHR_materials_emissive_strength)
                }

                material.emissive = emissionColor;
            }

            if (uMaterial.HasProperty(k_EmissionColorMap))
            {
                var emissionTex = uMaterial.GetTexture(k_EmissionColorMap);

                if (emissionTex != null)
                {
                    if (emissionTex is Texture2D)
                    {
                        material.emissiveTexture = ExportTextureInfo(emissionTex, gltf);
                        ExportTextureTransform(material.emissiveTexture, uMaterial, k_EmissionColorMap, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "emission", material.name);
                        success = false;
                    }
                }
            }

            //
            // Normal Map
            //
            if (
                uMaterial.HasProperty(k_NormalMap) &&
                uMaterial.IsKeywordEnabled(k_KeywordNormalMapTangentSpace)
                )
            {
                var normalTex = uMaterial.GetTexture(k_NormalMap);

                if (normalTex != null)
                {
                    if (normalTex is Texture2D)
                    {
                        material.normalTexture = ExportNormalTextureInfo(normalTex, uMaterial, gltf);
                        ExportTextureTransform(material.normalTexture, uMaterial, k_NormalMap, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "normal", uMaterial.name);
                        success = false;
                    }
                }
            }


            var mainTexProperty = uMaterial.HasProperty(k_BaseColorMap) ? k_BaseColorMap : k_MainTex;

            if (IsUnlit(uMaterial))
            {
                ExportUnlit(material, uMaterial, mainTexProperty, gltf, logger);
            }
            else
            {
                success &= ExportPbrMetallicRoughness(
                    uMaterial,
                    material,
                    gltf,
                    logger
                    );
            }



            return(success);
        }
Esempio n. 6
0
        static bool ExportPbrMetallicRoughness(
            UnityEngine.Material uMaterial,
            Material material,
            IGltfWritable gltf,
            ICodeLogger logger
            )
        {
            var success = true;
            var pbr     = new PbrMetallicRoughness {
                metallicFactor = 0, roughnessFactor = 1.0f
            };

            MaskMapImageExport ormImageExport = null;

            if (uMaterial.IsKeywordEnabled(k_KeywordMaskMap) && uMaterial.HasProperty(k_MaskMap))
            {
                var maskMap = uMaterial.GetTexture(k_MaskMap) as Texture2D;
                if (maskMap != null)
                {
                    ormImageExport = new MaskMapImageExport(maskMap);
                    if (AddImageExport(gltf, ormImageExport, out var ormTextureId))
                    {
                        // TODO: smartly detect if metallic roughness channels are used and not create the
                        // texture info if not.
                        pbr.metallicRoughnessTexture = new TextureInfo {
                            index = ormTextureId
                        };
                        ExportTextureTransform(pbr.metallicRoughnessTexture, uMaterial, k_MaskMap, gltf);

                        // TODO: smartly detect if occlusion channel is used and not create the
                        // texture info if not.
                        material.occlusionTexture = new OcclusionTextureInfo {
                            index = ormTextureId
                        };
                        if (uMaterial.HasProperty(k_AORemapMin))
                        {
                            var occMin = uMaterial.GetFloat(k_AORemapMin);
                            material.occlusionTexture.strength = math.clamp(1 - occMin, 0, 1);
                            var occMax = uMaterial.GetFloat(k_AORemapMax);
                            if (occMax < 1f)
                            {
                                // TODO: remap texture values
                                logger?.Warning(LogCode.RemapUnsupported, "AO");
                            }
                        }
                    }
                }
            }

            if (uMaterial.HasProperty(k_BaseColor))
            {
                pbr.baseColor = uMaterial.GetColor(k_BaseColor);
            }
            else
            if (uMaterial.HasProperty(k_Color))
            {
                pbr.baseColor = uMaterial.GetColor(k_Color);
            }

            if (uMaterial.HasProperty(k_BaseColorMap))
            {
                // TODO if additive particle, render black into alpha
                // TODO use private Material.GetFirstPropertyNameIdByAttribute here, supported from 2020.1+
                var mainTex = uMaterial.GetTexture(k_BaseColorMap);

                if (mainTex)
                {
                    if (mainTex is Texture2D)
                    {
                        pbr.baseColorTexture = ExportTextureInfo(mainTex, gltf);
                        ExportTextureTransform(pbr.baseColorTexture, uMaterial, k_BaseColorMap, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "main", uMaterial.name);
                        success = false;
                    }
                }
            }

            if (uMaterial.HasProperty(k_Metallic))
            {
                pbr.metallicFactor = uMaterial.GetFloat(k_Metallic);
            }

            if (ormImageExport != null && uMaterial.HasProperty(k_SmoothnessRemapMax))
            {
                pbr.roughnessFactor = uMaterial.GetFloat(k_SmoothnessRemapMax);
                if (uMaterial.HasProperty(k_SmoothnessRemapMin) && uMaterial.GetFloat(k_SmoothnessRemapMin) > 0)
                {
                    logger?.Warning(LogCode.RemapUnsupported, "Smoothness");
                }
            }
            else
            if (uMaterial.HasProperty(k_Smoothness))
            {
                pbr.roughnessFactor = 1f - uMaterial.GetFloat(k_Smoothness);
            }

            material.pbrMetallicRoughness = pbr;
            return(success);
        }
        protected unsafe JobHandle?GetTangentsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            float4 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetTangentsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.Float: {
                var jobTangent = new ConvertTangentsFloatToFloatInterleavedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 16,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            case GLTFComponentType.Short: {
                Assert.IsTrue(normalized);
                var jobTangent = new ConvertTangentsInt16ToFloatInterleavedNormalizedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 8,
                    input            = (short *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            case GLTFComponentType.Byte: {
                Assert.IsTrue(normalized);
                var jobTangent = new ConvertTangentsInt8ToFloatInterleavedNormalizedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                    input            = (sbyte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            default:
                logger?.Error(LogCode.TypeUnsupported, "Tangent", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
        /// <summary>
        /// Converts a Unity material to a glTF material.
        /// </summary>
        /// <param name="uMaterial">Source material</param>
        /// <param name="material">Resulting material</param>
        /// <param name="gltf">Associated IGltfWriter. Is used for adding images and textures.</param>
        /// <param name="logger">Logger used for reporting</param>
        /// <returns>True if no errors occured, false otherwise</returns>
        internal static bool ConvertMaterial(UnityEngine.Material uMaterial, out Material material, IGltfWritable gltf, ICodeLogger logger)
        {
            var success = true;

            material = new Material {
                name = uMaterial.name,
                pbrMetallicRoughness = new PbrMetallicRoughness {
                    metallicFactor  = 0,
                    roughnessFactor = 1.0f
                }
            };

            switch (uMaterial.GetTag("RenderType", false, ""))
            {
            case "TransparentCutout":
                if (uMaterial.HasProperty(k_Cutoff))
                {
                    material.alphaCutoff = uMaterial.GetFloat(k_Cutoff);
                }
                material.alphaModeEnum = Material.AlphaMode.MASK;
                break;

            case "Transparent":
            case "Fade":
                material.alphaModeEnum = Material.AlphaMode.BLEND;
                break;

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

            material.doubleSided = uMaterial.HasProperty(k_Cull) &&
                                   uMaterial.GetInt(k_Cull) == (int)CullMode.Off;

            if (uMaterial.IsKeywordEnabled("_EMISSION"))
            {
                if (uMaterial.HasProperty(k_EmissionColor))
                {
                    material.emissive = uMaterial.GetColor(k_EmissionColor);
                }

                if (uMaterial.HasProperty(k_EmissionMap))
                {
                    // var emissionTex = uMaterial.GetTexture(k_EmissionMap);
                    //
                    // if (emissionTex != null) {
                    //  if(emissionTex is Texture2D) {
                    //      material.emissiveTexture = ExportTextureInfo(emissionTex, TextureMapType.Emission);
                    //                        ExportTextureTransform(material.EmissiveTexture, uMaterial, "_EmissionMap");
                    //  } else {
                    //      logger?.Error(LogCode.TextureInvalidType, "emission", material.name );
                    //		success = false;
                    //  }
                    //                }
                }
            }
            if (
                uMaterial.HasProperty(k_BumpMap) &&
                (uMaterial.IsKeywordEnabled(Materials.Constants.kwNormalMap) ||
                 uMaterial.IsKeywordEnabled(k_KeywordBumpMap))
                )
            {
                var normalTex = uMaterial.GetTexture(k_BumpMap);

                if (normalTex != null)
                {
                    if (normalTex is Texture2D)
                    {
                        material.normalTexture = ExportNormalTextureInfo(normalTex, TextureMapType.Bump, uMaterial, gltf);
                        ExportTextureTransform(material.normalTexture, uMaterial, k_BumpMap, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "normal", uMaterial.name);
                        success = false;
                    }
                }
            }

            if (uMaterial.HasProperty(k_OcclusionMap))
            {
                var occTex = uMaterial.GetTexture(k_OcclusionMap);
                if (occTex != null)
                {
                    // if(occTex is Texture2D) {
                    //  material.occlusionTexture = ExportOcclusionTextureInfo(occTex, TextureMapType.Occlusion, uMaterial);
                    //  ExportTextureTransform(material.OcclusionTexture, uMaterial, "_OcclusionMap");
                    // } else {
                    //  logger?.Error(LogCode.TextureInvalidType, "occlusion", material.name );
                    //  success = false;
                    // }
                }
            }
            if (IsUnlit(uMaterial))
            {
                ExportUnlit(material, uMaterial, gltf, logger);
            }
            else if (IsPbrMetallicRoughness(uMaterial))
            {
                success &= ExportPbrMetallicRoughness(uMaterial, out material.pbrMetallicRoughness, gltf, logger);
            }
            else if (IsPbrSpecularGlossiness(uMaterial))
            {
                // ExportPBRSpecularGlossiness(material, uMaterial);
            }
            else if (uMaterial.HasProperty(k_BaseMap))
            {
                var mainTex = uMaterial.GetTexture(k_BaseMap);
                material.pbrMetallicRoughness = new PbrMetallicRoughness {
                    baseColor = uMaterial.HasProperty(k_BaseColor)
                                                ? uMaterial.GetColor(k_BaseColor)
                                                : Color.white,
                    baseColorTexture = mainTex == null ? null : ExportTextureInfo(mainTex, TextureMapType.Main, gltf)
                };
            }
            else if (uMaterial.HasProperty(k_ColorTexture))
            {
                var mainTex = uMaterial.GetTexture(k_ColorTexture);
                material.pbrMetallicRoughness = new PbrMetallicRoughness {
                    baseColor = uMaterial.HasProperty(k_BaseColor)
                                                ? uMaterial.GetColor(k_BaseColor)
                                                : Color.white,
                    baseColorTexture = mainTex == null ? null : ExportTextureInfo(mainTex, TextureMapType.Main, gltf)
                };
            }
            else if (uMaterial.HasProperty(k_MainTex)) //else export main texture
            {
                var mainTex = uMaterial.GetTexture(k_MainTex);

                if (mainTex != null)
                {
                    material.pbrMetallicRoughness = new PbrMetallicRoughness {
                        metallicFactor   = 0, roughnessFactor = 1.0f,
                        baseColorTexture = ExportTextureInfo(mainTex, TextureMapType.Main, gltf)
                    };

                    // ExportTextureTransform(material.pbrMetallicRoughness.baseColorTexture, uMaterial, "_MainTex");
                }
                if (uMaterial.HasProperty(k_TintColor))
                {
                    //particles use _TintColor instead of _Color
                    material.pbrMetallicRoughness = material.pbrMetallicRoughness ?? new PbrMetallicRoughness {
                        metallicFactor = 0, roughnessFactor = 1.0f
                    };

                    material.pbrMetallicRoughness.baseColor = uMaterial.GetColor(k_TintColor);
                }
                material.doubleSided = true;
            }
            return(success);
        }
        static bool ExportPbrMetallicRoughness(UnityEngine.Material material, out PbrMetallicRoughness pbr, IGltfWritable gltf, ICodeLogger logger)
        {
            var success = true;

            pbr = new PbrMetallicRoughness {
                metallicFactor = 0, roughnessFactor = 1.0f
            };

            if (material.HasProperty(k_BaseColor))
            {
                pbr.baseColor = material.GetColor(k_BaseColor);
            }
            else
            if (material.HasProperty(k_Color))
            {
                pbr.baseColor = material.GetColor(k_Color);
            }

            if (material.HasProperty(k_TintColor))
            {
                //particles use _TintColor instead of _Color
                float white = 1;
                if (material.HasProperty(k_Color))
                {
                    var c = material.GetColor(k_Color);
                    white = (c.r + c.g + c.b) / 3.0f; //multiply alpha by overall whiteness of TintColor
                }

                pbr.baseColor = material.GetColor(k_TintColor) * white;
            }

            if (material.HasProperty(k_MainTex) || material.HasProperty("_BaseMap"))
            {
                // TODO if additive particle, render black into alpha
                // TODO use private Material.GetFirstPropertyNameIdByAttribute here, supported from 2020.1+
                var mainTexProperty = material.HasProperty(k_BaseMap) ? k_BaseMap : k_MainTex;
                var mainTex         = material.GetTexture(mainTexProperty);

                if (mainTex)
                {
                    if (mainTex is Texture2D)
                    {
                        pbr.baseColorTexture = ExportTextureInfo(mainTex, TextureMapType.Main, gltf);
                        ExportTextureTransform(pbr.baseColorTexture, material, mainTexProperty, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "main", material.name);
                        success = false;
                    }
                }
            }

            if (material.HasProperty(k_Metallic) && !material.IsKeywordEnabled("_METALLICGLOSSMAP"))
            {
                pbr.metallicFactor = material.GetFloat(k_Metallic);
            }

            if (material.HasProperty(k_Glossiness) || material.HasProperty(k_Smoothness))
            {
                var   smoothnessPropertyName = material.HasProperty(k_Smoothness) ?  k_Smoothness : k_Glossiness;
                var   metallicGlossMap       = material.GetTexture(k_MetallicGlossMap);
                float smoothness             = material.GetFloat(smoothnessPropertyName);
                // legacy workaround: the UnityGLTF shaders misuse k_Glossiness as roughness but don't have a keyword for it.
                if (material.shader.name.Equals("GLTF/PbrMetallicRoughness", StringComparison.Ordinal))
                {
                    smoothness = 1 - smoothness;
                }
                pbr.roughnessFactor = (metallicGlossMap != null && material.HasProperty(k_GlossMapScale))
                                        ? material.GetFloat(k_GlossMapScale)
                                        : 1f - smoothness;
            }

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

                if (mrTex != null)
                {
                    if (mrTex is Texture2D)
                    {
                        // pbr.metallicRoughnessTexture = ExportTextureInfo(mrTex, TextureMapType.MetallicGloss);
                        // if (material.IsKeywordEnabled("_METALLICGLOSSMAP"))
                        //  pbr.metallicFactor = 1.0f;
                        // ExportTextureTransform(pbr.MetallicRoughnessTexture, material, k_MetallicGlossMap);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "metallic/gloss", material.name);
                        success = false;
                    }
                }
            }

            return(success);
        }
Esempio n. 10
0
        /// <summary>
        /// Converts a Unity material to a glTF material.
        /// </summary>
        /// <param name="uMaterial">Source material</param>
        /// <param name="material">Resulting material</param>
        /// <param name="gltf">Associated IGltfWriter. Is used for adding images and textures.</param>
        /// <param name="logger">Logger used for reporting</param>
        /// <returns>True if no errors occured, false otherwise</returns>
        public override bool ConvertMaterial(UnityEngine.Material uMaterial, out Material material, IGltfWritable gltf, ICodeLogger logger)
        {
            var success = true;

            material = new Material {
                name = uMaterial.name,
                pbrMetallicRoughness = new PbrMetallicRoughness {
                    metallicFactor  = 0,
                    roughnessFactor = 1.0f
                }
            };

            SetAlphaModeAndCutoff(uMaterial, material);
            material.doubleSided = IsDoubleSided(uMaterial);

            if (uMaterial.IsKeywordEnabled(k_KeywordEmission))
            {
                if (uMaterial.HasProperty(k_EmissionColor))
                {
                    var emissionColor = uMaterial.GetColor(k_EmissionColor);

                    // Clamp emissionColor to 0..1
                    var maxFactor = math.max(emissionColor.r, math.max(emissionColor.g, emissionColor.b));
                    if (maxFactor > 1f)
                    {
                        emissionColor.r /= maxFactor;
                        emissionColor.g /= maxFactor;
                        emissionColor.b /= maxFactor;
                        // TODO: use maxFactor as emissiveStrength (KHR_materials_emissive_strength)
                    }

                    material.emissive = emissionColor;
                }

                if (uMaterial.HasProperty(k_EmissionMap))
                {
                    var emissionTex = uMaterial.GetTexture(k_EmissionMap);

                    if (emissionTex != null)
                    {
                        if (emissionTex is Texture2D)
                        {
                            material.emissiveTexture = ExportTextureInfo(emissionTex, gltf);
                            if (material.emissiveTexture != null)
                            {
                                ExportTextureTransform(material.emissiveTexture, uMaterial, k_EmissionMap, gltf);
                            }
                        }
                        else
                        {
                            logger?.Error(LogCode.TextureInvalidType, "emission", material.name);
                            success = false;
                        }
                    }
                }
            }
            if (
                uMaterial.HasProperty(k_BumpMap) &&
                (uMaterial.IsKeywordEnabled(Materials.Constants.kwNormalMap) ||
                 uMaterial.IsKeywordEnabled(k_KeywordBumpMap))
                )
            {
                var normalTex = uMaterial.GetTexture(k_BumpMap);

                if (normalTex != null)
                {
                    if (normalTex is Texture2D)
                    {
                        material.normalTexture = ExportNormalTextureInfo(normalTex, uMaterial, gltf);
                        if (material.normalTexture != null)
                        {
                            ExportTextureTransform(material.normalTexture, uMaterial, k_BumpMap, gltf);
                        }
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "normal", uMaterial.name);
                        success = false;
                    }
                }
            }

            var isPbrMetallicRoughness = IsPbrMetallicRoughness(uMaterial);
            var needsMetalRoughTexture =
                isPbrMetallicRoughness &&
                (
                    HasMetallicGlossMap(uMaterial) ||
                    uMaterial.IsKeywordEnabled(k_KeywordSmoothnessTextureAlbedoChannelA)
                );

            OrmImageExport ormImageExport  = null;
            var            mainTexProperty = k_MainTex;

            if (uMaterial.HasProperty(k_BaseMap))
            {
                mainTexProperty = k_BaseMap;
            }
            else if (uMaterial.HasProperty(k_ColorTexture))
            {
                mainTexProperty = k_ColorTexture;
            }

            if (needsMetalRoughTexture)
            {
                ormImageExport = new OrmImageExport();
            }

            if (IsUnlit(uMaterial))
            {
                ExportUnlit(material, uMaterial, mainTexProperty, gltf, logger);
            }
            else if (isPbrMetallicRoughness)
            {
                success &= ExportPbrMetallicRoughness(
                    uMaterial,
                    material,
                    mainTexProperty,
                    ormImageExport,
                    gltf,
                    logger
                    );
            }
            else if (uMaterial.HasProperty(mainTexProperty))
            {
                var mainTex = uMaterial.GetTexture(mainTexProperty);
                material.pbrMetallicRoughness = new PbrMetallicRoughness {
                    metallicFactor  = 0,
                    roughnessFactor = 1.0f,
                    baseColor       = uMaterial.HasProperty(k_BaseColor)
                        ? uMaterial.GetColor(k_BaseColor)
                        : Color.white
                };
                if (mainTex != null)
                {
                    material.pbrMetallicRoughness.baseColorTexture = ExportTextureInfo(mainTex, gltf);
                    if (material.pbrMetallicRoughness.baseColorTexture != null)
                    {
                        ExportTextureTransform(material.pbrMetallicRoughness.baseColorTexture, uMaterial, mainTexProperty, gltf);
                    }
                }
                if (uMaterial.HasProperty(k_TintColor))
                {
                    //particles use _TintColor instead of _Color
                    material.pbrMetallicRoughness.baseColor = uMaterial.GetColor(k_TintColor);
                }
            }

            if (uMaterial.HasProperty(k_OcclusionMap))
            {
                var occTex = uMaterial.GetTexture(k_OcclusionMap);
                if (occTex != null)
                {
                    if (occTex is Texture2D occTex2d)
                    {
                        if (ormImageExport == null)
                        {
                            material.occlusionTexture = ExportOcclusionTextureInfo(occTex2d, uMaterial, gltf);
                        }
                        else
                        {
                            material.occlusionTexture = new OcclusionTextureInfo();
                            ormImageExport.SetOcclusionTexture(occTex2d);
                        }
                        if (material.occlusionTexture != null)
                        {
                            ExportTextureTransform(
                                material.occlusionTexture,
                                uMaterial,
                                mainTexProperty, // Standard and Lit re-use main texture transform
                                gltf
                                );
                        }
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "occlusion", material.name);
                        success = false;
                    }
                }
            }

            if (ormImageExport != null && material.pbrMetallicRoughness != null)
            {
                if (AddImageExport(gltf, ormImageExport, out var ormTextureId))
                {
                    if (material.pbrMetallicRoughness.metallicRoughnessTexture != null)
                    {
                        material.pbrMetallicRoughness.metallicRoughnessTexture.index = ormTextureId;
                        ExportTextureTransform(material.pbrMetallicRoughness.metallicRoughnessTexture, uMaterial, k_MetallicGlossMap, gltf);
                    }

                    if (ormImageExport.hasOcclusion)
                    {
                        material.occlusionTexture.index = ormTextureId;
                    }
                }
#if UNITY_IMAGECONVERSION
                else
                {
                    logger?.Error(LogCode.ExportImageFailed);
                }
#endif
            }

            if (material.occlusionTexture != null)
            {
                if (uMaterial.HasProperty(MaterialGenerator.occlusionStrengthPropId))
                {
                    material.occlusionTexture.strength = uMaterial.GetFloat(MaterialGenerator.occlusionStrengthPropId);
                }
            }

            return(success);
        }
Esempio n. 11
0
        static bool ExportPbrMetallicRoughness(
            UnityEngine.Material uMaterial,
            Material material,
            int mainTexProperty,
            OrmImageExport ormImageExport,
            IGltfWritable gltf,
            ICodeLogger logger
            )
        {
            var success = true;
            var pbr     = new PbrMetallicRoughness {
                metallicFactor = 0, roughnessFactor = 1.0f
            };

            var hasAlphaSmoothness = uMaterial.IsKeywordEnabled(k_KeywordSmoothnessTextureAlbedoChannelA);

            if (uMaterial.HasProperty(k_BaseColor))
            {
                pbr.baseColor = uMaterial.GetColor(k_BaseColor);
            }
            else
            if (uMaterial.HasProperty(k_Color))
            {
                pbr.baseColor = uMaterial.GetColor(k_Color);
            }

            if (uMaterial.HasProperty(k_TintColor))
            {
                //particles use _TintColor instead of _Color
                float white = 1;
                if (uMaterial.HasProperty(k_Color))
                {
                    var c = uMaterial.GetColor(k_Color);
                    white = (c.r + c.g + c.b) / 3.0f; //multiply alpha by overall whiteness of TintColor
                }

                pbr.baseColor = uMaterial.GetColor(k_TintColor) * white;
            }

            if (uMaterial.HasProperty(mainTexProperty))
            {
                // TODO if additive particle, render black into alpha
                // TODO use private Material.GetFirstPropertyNameIdByAttribute here, supported from 2020.1+
                var mainTex = uMaterial.GetTexture(mainTexProperty);

                if (mainTex)
                {
                    if (mainTex is Texture2D)
                    {
                        pbr.baseColorTexture = ExportTextureInfo(
                            mainTex,
                            gltf,
                            // Force RGB for the baseColor, so that the alpha (which is smoothness)
                            // is not used for alpha-opacity
                            hasAlphaSmoothness
                                ? ImageExportBase.Format.Jpg
                                : ImageExportBase.Format.Unknown
                            );
                        if (pbr.baseColorTexture != null)
                        {
                            ExportTextureTransform(pbr.baseColorTexture, uMaterial, mainTexProperty, gltf);
                        }
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "main", uMaterial.name);
                        success = false;
                    }
                }
            }

            if (uMaterial.HasProperty(k_Metallic) && !HasMetallicGlossMap(uMaterial))
            {
                pbr.metallicFactor = uMaterial.GetFloat(k_Metallic);
            }

            if (uMaterial.HasProperty(k_Glossiness) || uMaterial.HasProperty(k_Smoothness))
            {
                var smoothnessPropId = uMaterial.HasProperty(k_Smoothness) ?  k_Smoothness : k_Glossiness;
                var metallicGlossMap = uMaterial.HasProperty(k_MetallicGlossMap) ? uMaterial.GetTexture(k_MetallicGlossMap) : null;
                var smoothness       = uMaterial.GetFloat(smoothnessPropId);
                pbr.roughnessFactor = (metallicGlossMap != null || hasAlphaSmoothness) && uMaterial.HasProperty(k_GlossMapScale)
                    ? uMaterial.GetFloat(k_GlossMapScale)
                    : 1f - smoothness;
            }

            if (uMaterial.HasProperty(k_MetallicGlossMap))
            {
                var mrTex = uMaterial.GetTexture(k_MetallicGlossMap);
                if (mrTex != null)
                {
                    if (mrTex is Texture2D mrTex2d)
                    {
                        pbr.metallicRoughnessTexture = pbr.metallicRoughnessTexture ?? new TextureInfo();
                        ormImageExport.SetMetalGlossTexture(mrTex2d);
                        if (HasMetallicGlossMap(uMaterial))
                        {
                            pbr.metallicFactor = 1.0f;
                        }
                        ExportTextureTransform(pbr.metallicRoughnessTexture, uMaterial, k_MetallicGlossMap, gltf);
                    }
                    else
                    {
                        logger?.Error(LogCode.TextureInvalidType, "metallic/gloss", uMaterial.name);
                        success = false;
                    }
                }
            }

            if (uMaterial.IsKeywordEnabled(k_KeywordSmoothnessTextureAlbedoChannelA))
            {
                var smoothnessTex = uMaterial.GetTexture(mainTexProperty) as Texture2D;
                if (smoothnessTex != null)
                {
                    pbr.metallicRoughnessTexture = pbr.metallicRoughnessTexture ?? new TextureInfo();
                    ormImageExport.SetSmoothnessTexture(smoothnessTex);
                    ExportTextureTransform(pbr.metallicRoughnessTexture, uMaterial, mainTexProperty, gltf);
                }
            }

            material.pbrMetallicRoughness = pbr;
            return(success);
        }