Esempio n. 1
0
            public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings, Action <Material> onFinish)
            {
                // Shader
                Shader sh = null;

                if (alphaMode == AlphaMode.BLEND)
                {
                    sh = shaderSettings.SpecularBlend;
                }
                else
                {
                    sh = shaderSettings.Specular;
                }

                // Material
                Material mat = new Material(sh);

                mat.color = diffuseFactor;
                mat.SetColor("_SpecColor", specularFactor);
                mat.SetFloat("_GlossyReflections", glossinessFactor);

                // Assign textures
                if (textures != null)
                {
                    // Diffuse texture
                    if (diffuseTexture != null)
                    {
                        if (textures.Length <= diffuseTexture.index)
                        {
                            Debug.LogWarning("Attempted to get diffuseTexture texture index " + diffuseTexture.index + " when only " + textures.Length + " exist");
                        }
                        else
                        {
                            IEnumerator en = textures[diffuseTexture.index].GetTextureCached(false, tex => {
                                if (tex != null)
                                {
                                    mat.SetTexture("_MainTex", tex);
                                    if (diffuseTexture.extensions != null)
                                    {
                                        diffuseTexture.extensions.Apply(diffuseTexture, mat, "_MainTex");
                                    }
                                }
                            });
                            while (en.MoveNext())
                            {
                                yield return(null);
                            }
                            ;
                        }
                    }
                    // Specular texture
                    if (specularGlossinessTexture != null)
                    {
                        if (textures.Length <= specularGlossinessTexture.index)
                        {
                            Debug.LogWarning("Attempted to get specularGlossinessTexture texture index " + specularGlossinessTexture.index + " when only " + textures.Length + " exist");
                        }
                        else
                        {
                            mat.EnableKeyword("_SPECGLOSSMAP");
                            IEnumerator en = textures[specularGlossinessTexture.index].GetTextureCached(false, tex => {
                                if (tex != null)
                                {
                                    mat.SetTexture("_SpecGlossMap", tex);
                                    mat.EnableKeyword("_SPECGLOSSMAP");
                                    if (specularGlossinessTexture.extensions != null)
                                    {
                                        specularGlossinessTexture.extensions.Apply(specularGlossinessTexture, mat, "_SpecGlossMap");
                                    }
                                }
                            });
                            while (en.MoveNext())
                            {
                                yield return(null);
                            }
                            ;
                        }
                    }
                }
                onFinish(mat);
            }
Esempio n. 2
0
        public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, ShaderSettings shaderSettings, Action <Material> onFinish)
        {
            Material    mat = null;
            IEnumerator en  = null;

            // Load metallic-roughness materials
            if (pbrMetallicRoughness != null)
            {
                en = pbrMetallicRoughness.CreateMaterial(textures, alphaMode, shaderSettings, x => mat = x);
                while (en.MoveNext())
                {
                    yield return(null);
                }
                ;
            }
            // Load specular-glossiness materials
            else if (extensions != null && extensions.KHR_materials_pbrSpecularGlossiness != null)
            {
                en = extensions.KHR_materials_pbrSpecularGlossiness.CreateMaterial(textures, alphaMode, shaderSettings, x => mat = x);
                while (en.MoveNext())
                {
                    yield return(null);
                }
                ;
            }
            // Load fallback material
            else
            {
                mat = new Material(Shader.Find("Standard"));
            }
            // Normal texture
            if (normalTexture != null)
            {
                en = TryGetTexture(textures, normalTexture, true, tex => {
                    if (tex != null)
                    {
                        mat.SetTexture("_BumpMap", tex);
                        mat.EnableKeyword("_NORMALMAP");
                        mat.SetFloat("_BumpScale", normalTexture.scale);
                        if (normalTexture.extensions != null)
                        {
                            normalTexture.extensions.Apply(normalTexture, mat, "_BumpMap");
                        }
                    }
                });
                while (en.MoveNext())
                {
                    yield return(null);
                }
                ;
            }
            // Occlusion texture
            if (occlusionTexture != null)
            {
                en = TryGetTexture(textures, occlusionTexture, true, tex => {
                    if (tex != null)
                    {
                        mat.SetTexture("_OcclusionMap", tex);
                        if (occlusionTexture.extensions != null)
                        {
                            occlusionTexture.extensions.Apply(occlusionTexture, mat, "_OcclusionMap");
                        }
                    }
                });
                while (en.MoveNext())
                {
                    yield return(null);
                }
                ;
            }
            // Emissive factor
            if (emissiveFactor != Color.black)
            {
                mat.SetColor("_EmissionColor", emissiveFactor);
                mat.EnableKeyword("_EMISSION");
            }
            // Emissive texture
            if (emissiveTexture != null)
            {
                en = TryGetTexture(textures, emissiveTexture, false, tex => {
                    if (tex != null)
                    {
                        mat.SetTexture("_EmissionMap", tex);
                        mat.EnableKeyword("_EMISSION");
                        if (emissiveTexture.extensions != null)
                        {
                            emissiveTexture.extensions.Apply(emissiveTexture, mat, "_EmissionMap");
                        }
                    }
                });
                while (en.MoveNext())
                {
                    yield return(null);
                }
                ;
            }

            if (alphaMode == AlphaMode.MASK)
            {
                mat.SetFloat("_AlphaCutoff", alphaCutoff);
            }
            mat.name = name;
            onFinish(mat);
        }
Esempio n. 3
0
            public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings, Action <Material> onFinish)
            {
                // Shader
                Shader sh = null;

                if (alphaMode == AlphaMode.BLEND)
                {
                    sh = shaderSettings.MetallicBlend;
                }
                else
                {
                    sh = shaderSettings.Metallic;
                }

                // Material
                Material mat = new Material(sh);

                mat.color = baseColorFactor;
                mat.SetFloat("_Metallic", metallicFactor);
                mat.SetFloat("_Roughness", roughnessFactor);

                // Assign textures
                if (textures != null)
                {
                    // Base color texture
                    if (baseColorTexture != null && baseColorTexture.index >= 0)
                    {
                        if (textures.Length <= baseColorTexture.index)
                        {
                            Debug.LogWarning("Attempted to get basecolor texture index " + baseColorTexture.index + " when only " + textures.Length + " exist");
                        }
                        else
                        {
                            IEnumerator en = textures[baseColorTexture.index].GetTextureCached(false, tex => {
                                if (tex != null)
                                {
                                    mat.SetTexture("_MainTex", tex);
                                    if (baseColorTexture.extensions != null)
                                    {
                                        baseColorTexture.extensions.Apply(baseColorTexture, mat, "_MainTex");
                                    }
                                }
                            });
                            while (en.MoveNext())
                            {
                                yield return(null);
                            }
                            ;
                        }
                    }
                    // Metallic roughness texture
                    if (metallicRoughnessTexture != null && metallicRoughnessTexture.index >= 0)
                    {
                        if (textures.Length <= metallicRoughnessTexture.index)
                        {
                            Debug.LogWarning("Attempted to get metallicRoughness texture index " + metallicRoughnessTexture.index + " when only " + textures.Length + " exist");
                        }
                        else
                        {
                            IEnumerator en = TryGetTexture(textures, metallicRoughnessTexture, true, tex => {
                                if (tex != null)
                                {
                                    mat.SetTexture("_MetallicGlossMap", tex);
                                    mat.EnableKeyword("_METALLICGLOSSMAP");
                                    if (metallicRoughnessTexture.extensions != null)
                                    {
                                        metallicRoughnessTexture.extensions.Apply(metallicRoughnessTexture, mat, "_MetallicGlossMap");
                                    }
                                }
                            });
                            while (en.MoveNext())
                            {
                                yield return(null);
                            }
                            ;
                        }
                    }
                }

                // After the texture and color is extracted from the glTFObject
                if (mat.HasProperty("_BaseMap"))
                {
                    mat.SetTexture("_BaseMap", mat.mainTexture);
                }
                if (mat.HasProperty("_BaseColor"))
                {
                    mat.SetColor("_BaseColor", baseColorFactor);
                }
                onFinish(mat);
            }