public ImportSettings(string shader) { if (shader != null) { shaders = new ShaderSettings(shader); } }
public Material CreateMaterial(GLTFTexture.ImportResult[] textures, ShaderSettings shaderSettings) { Material mat; // Load metallic-roughness materials if (pbrMetallicRoughness != null) { mat = pbrMetallicRoughness.CreateMaterial(textures, alphaMode, shaderSettings); } // Load specular-glossiness materials else if (extensions != null && extensions.KHR_materials_pbrSpecularGlossiness != null) { mat = extensions.KHR_materials_pbrSpecularGlossiness.CreateMaterial(textures, alphaMode, shaderSettings); } // Load fallback material else { mat = new Material(Shader.Find("Standard")); } Texture2D tex; if (TryGetTexture(textures, normalTexture, out tex, x => x.GetNormalMap())) { mat.SetTexture("_BumpMap", tex); mat.EnableKeyword("_NORMALMAP"); } if (TryGetTexture(textures, occlusionTexture, out tex)) { mat.SetTexture("_OcclusionMap", tex); } if (emissiveFactor != Color.black) { mat.SetColor("_EmissionColor", emissiveFactor); mat.EnableKeyword("_EMISSION"); } if (TryGetTexture(textures, emissiveTexture, out tex)) { mat.SetTexture("_EmissionMap", tex); mat.EnableKeyword("_EMISSION"); } if (alphaMode == AlphaMode.MASK) { mat.SetFloat("_AlphaCutoff", alphaCutoff); } mat.name = name; return(mat); }
public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings, Action <Material> onFinish) { // Shader Shader sh = null; #if UNITY_2019_1_OR_NEWER // LWRP support if (GraphicsSettings.renderPipelineAsset) { sh = GraphicsSettings.renderPipelineAsset.defaultShader; } #endif if (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); } }); 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"); } }); 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); }
} //END SetScale public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, ShaderSettings shaderSettings, Action <Material> onFinish) { Material mat = null; // Load metallic-roughness materials if (pbrMetallicRoughness != null) { yield return(StaticCoroutine.Start(pbrMetallicRoughness.CreateMaterial(textures, alphaMode, shaderSettings, x => mat = x))); } // Load specular-glossiness materials else if (extensions != null && extensions.KHR_materials_pbrSpecularGlossiness != null) { yield return(StaticCoroutine.Start(extensions.KHR_materials_pbrSpecularGlossiness.CreateMaterial(textures, alphaMode, shaderSettings, x => mat = x))); } // Load fallback material else { mat = new Material(Shader.Find("Standard")); } // Normal texture if (normalTexture != null) { yield return(StaticCoroutine.Start(TryGetTexture(textures, normalTexture, true, (tex, orientation) => { if (tex != null) { mat.SetTexture("_BumpMap", tex); SetScale(ref mat, orientation, tex.name, "_BumpMap"); mat.EnableKeyword("_NORMALMAP"); mat.SetFloat("_BumpScale", normalTexture.scale); if (normalTexture.extensions != null) { normalTexture.extensions.Apply(normalTexture, mat, "_BumpMap"); } } }))); } // Occlusion texture if (occlusionTexture != null) { yield return(StaticCoroutine.Start(TryGetTexture(textures, occlusionTexture, true, (tex, orientation) => { if (tex != null) { mat.SetTexture("_OcclusionMap", tex); SetScale(ref mat, orientation, tex.name, "_OcclusionMap"); if (occlusionTexture.extensions != null) { occlusionTexture.extensions.Apply(occlusionTexture, mat, "_OcclusionMap"); } } }))); } // Emissive factor if (emissiveFactor != Color.black) { mat.SetColor("_EmissionColor", emissiveFactor); mat.EnableKeyword("_EMISSION"); } // Emissive texture if (emissiveTexture != null) { yield return(StaticCoroutine.Start(TryGetTexture(textures, emissiveTexture, false, (tex, orientation) => { if (tex != null) { mat.SetTexture("_EmissionMap", tex); SetScale(ref mat, orientation, tex.name, "_EmissionMap"); mat.EnableKeyword("_EMISSION"); if (emissiveTexture.extensions != null) { emissiveTexture.extensions.Apply(emissiveTexture, mat, "_EmissionMap"); } } }))); } if (alphaMode == AlphaMode.MASK) { mat.SetFloat("_AlphaCutoff", alphaCutoff); } mat.name = name; onFinish(mat); }
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 { yield return(StaticCoroutine.Start(textures[diffuseTexture.index].GetTextureCached(false, (tex, orientation) => { if (tex != null) { mat.SetTexture("_MainTex", tex); SetScale(ref mat, orientation, tex.name, "_MainTex"); if (diffuseTexture.extensions != null) { diffuseTexture.extensions.Apply(diffuseTexture, mat, "_MainTex"); } } }))); } } // 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"); yield return(StaticCoroutine.Start(textures[specularGlossinessTexture.index].GetTextureCached(false, (tex, orientation) => { if (tex != null) { mat.SetTexture("_SpecGlossMap", tex); SetScale(ref mat, orientation, tex.name, "_SpecGlossMap"); mat.EnableKeyword("_SPECGLOSSMAP"); if (specularGlossinessTexture.extensions != null) { specularGlossinessTexture.extensions.Apply(specularGlossinessTexture, mat, "_SpecGlossMap"); } } }))); } } } onFinish(mat); }
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 { yield return(StaticCoroutine.Start(textures[baseColorTexture.index].GetTextureCached(false, (tex, orientation) => { if (tex != null) { mat.SetTexture("_MainTex", tex); SetScale(ref mat, orientation, tex.name, "_MainTex"); if (baseColorTexture.extensions != null) { baseColorTexture.extensions.Apply(baseColorTexture, mat, "_MainTex"); } } }))); } } // 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 { yield return(StaticCoroutine.Start(TryGetTexture(textures, metallicRoughnessTexture, true, (tex, orientation) => { if (tex != null) { mat.SetTexture("_MetallicGlossMap", tex); mat.EnableKeyword("_METALLICGLOSSMAP"); SetScale(ref mat, orientation, tex.name, "_MetallicGlossMap"); if (metallicRoughnessTexture.extensions != null) { metallicRoughnessTexture.extensions.Apply(metallicRoughnessTexture, mat, "_MetallicGlossMap"); } } }))); } } } // 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); }
public Material CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings) { // Shader Shader sh = null; #if UNITY_2019_1_OR_NEWER // LWRP support if (GraphicsSettings.renderPipelineAsset) { sh = GraphicsSettings.renderPipelineAsset.defaultShader; } #endif if (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("_Glossiness", glossinessFactor); // 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 { mat.SetTexture("_MainTex", textures[diffuseTexture.index].image.texture); } } // 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.SetTexture("_SpecGlossMap", textures[specularGlossinessTexture.index].image.GetFixedMetallicRoughness()); mat.EnableKeyword("_SPECGLOSSMAP"); } } return(mat); }
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(alphaMode == AlphaMode.BLEND ? shaderSettings.MetallicBlend : shaderSettings.Metallic); } // 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); } #if UNITY_2019_1_OR_NEWER if (GraphicsSettings.renderPipelineAsset) { URPHelper.SetMaterialKeywords(mat, alphaMode, doubleSided); } #endif mat.name = name; onFinish(mat); }
public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, ShaderSettings shaderSettings, Action <Material> onFinish) { Material mat = new Material(Shader.Find("Unlit/Texture")); IEnumerator en = null; //UnityEngine.Debug.Log("textures size : " + textures.Length); Stopwatch sw = new Stopwatch(); //if (textures[0] != null) { sw.Start(); en = textures[0].GetTextureCached(false, tex => { if (tex != null) { mat.SetTexture("_MainTex", tex); } }); while (en.MoveNext()) { yield return(null); } ; sw.Stop(); UnityEngine.Debug.Log("CreateMaterial : " + sw.ElapsedMilliseconds.ToString() + "ms"); } //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("Unlit/Texture")); //en = TryGetTexture(textures, normalTexture, true, tex => { // if (tex != null) // { // mat.SetTexture("_MainTex", tex); // } //}); //while (en.MoveNext()) { yield return null; }; //// 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); yield return(null); }
public ImportSettings() { shaderOverrides = new ShaderSettings(); }
public ImportSettings(Material _overrideMaterial) { shaderOverrides = new ShaderSettings(_overrideMaterial); }
public ImportSettings(Shader defaultShader) { shaderOverrides = new ShaderSettings(defaultShader); }
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 (doubleSided) { mat.SetFloat("_Cull", 0.0f); } if (alphaMode == AlphaMode.MASK) { mat.SetFloat("_AlphaCutoff", alphaCutoff); } else if (alphaMode == AlphaMode.BLEND) { mat.SetOverrideTag("RenderType", "Transparent"); mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); mat.DisableKeyword("_ALPHAPREMULTIPLY_ON"); mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; mat.SetShaderPassEnabled("ShadowCaster", false); mat.SetFloat("_Surface", 1.0f); } mat.name = name; onFinish(mat); }
public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings, Action <Material> onFinish) { // Shader Shader sh = null; #if UNITY_2019_1_OR_NEWER // LWRP support if (GraphicsSettings.renderPipelineAsset) { sh = GraphicsSettings.renderPipelineAsset.defaultShader; } #endif if (sh == null) { if (alphaMode == AlphaMode.BLEND) { sh = shaderSettings.SpecularBlend; } else { sh = shaderSettings.Specular; } } Material mat = new Material(sh); // Material if (alphaMode == AlphaMode.BLEND) { mat.SetOverrideTag("RenderType", "Transparent"); mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); mat.SetInt("_ZWrite", 0); mat.DisableKeyword("_ALPHAPREMULTIPLY_ON"); mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; mat.SetShaderPassEnabled("ShadowCaster", false); } 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); } }); 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"); } }); while (en.MoveNext()) { yield return(null); } ; } } } onFinish(mat); }
public IEnumerator CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings, Action <Material> onFinish) { // Shader Shader sh = null; #if UNITY_2019_1_OR_NEWER // LWRP support if (GraphicsSettings.renderPipelineAsset) { sh = GraphicsSettings.renderPipelineAsset.defaultShader; } #endif if (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); } }); 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"); } }); while (en.MoveNext()) { yield return(null); } ; } } } onFinish(mat); }
public Material CreateMaterial(GLTFTexture.ImportResult[] textures, AlphaMode alphaMode, ShaderSettings shaderSettings) { // Shader Shader sh = null; #if UNITY_2019_1_OR_NEWER // LWRP support if (GraphicsSettings.renderPipelineAsset) { sh = GraphicsSettings.renderPipelineAsset.defaultShader; } #endif if (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("_Glossiness", 1 - roughnessFactor); 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 { mat.SetTexture("_MainTex", textures[baseColorTexture.index].image.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 { mat.SetTexture("_MetallicGlossMap", textures[metallicRoughnessTexture.index].image.GetFixedMetallicRoughness()); mat.EnableKeyword("_METALLICGLOSSMAP"); } } // 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); } return(mat); }
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"); } }); while (en.MoveNext()) { yield return(null); } ; } // Occlusion texture if (occlusionTexture != null) { en = TryGetTexture(textures, occlusionTexture, true, tex => { if (tex != null) { mat.SetTexture("_OcclusionMap", tex); } }); 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"); } }); while (en.MoveNext()) { yield return(null); } ; } if (alphaMode == AlphaMode.MASK) { mat.SetFloat("_AlphaCutoff", alphaCutoff); } mat.name = name; onFinish(mat); }
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; if (mat.HasProperty("_BaseColor")) { mat.SetColor("_BaseColor", 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) { if (mat.HasProperty("_BaseMap")) { mat.SetTexture("_BaseMap", tex); if (baseColorTexture.extensions != null) { baseColorTexture.extensions.Apply(baseColorTexture, mat, "_BaseMap"); } } if (mat.HasProperty("_MainTex")) { 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); } ; } } } onFinish(mat); }