public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode) { switch (blendMode) { case BlendMode.Opaque: material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.SetFloat( "_FogMultiplier", 1.0f ); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case BlendMode.AlphaTest: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.SetFloat( "_FogMultiplier", 1.0f ); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 2450; break; case BlendMode.AlphaBlend: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.SetFloat( "_FogMultiplier", 1.0f ); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; case BlendMode.Glass: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.SetFloat( "_FogMultiplier", 1.0f ); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; case BlendMode.Additive: material.SetOverrideTag( "RenderType", "Transparent" ); material.SetInt( "_SrcBlend", ( int )UnityEngine.Rendering.BlendMode.One ); material.SetInt( "_DstBlend", ( int )UnityEngine.Rendering.BlendMode.One ); material.SetInt( "_ZWrite", 0 ); material.SetFloat( "_FogMultiplier", 0.0f ); material.DisableKeyword( "_ALPHATEST_ON" ); material.DisableKeyword( "_ALPHABLEND_ON" ); material.DisableKeyword( "_ALPHAPREMULTIPLY_ON" ); material.renderQueue = 3000; break; } }
public static void SetMaterialRenderingMode(Material material, StandardShaderRenderingMode mode) { switch (mode) { case StandardShaderRenderingMode.Opaque: material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case StandardShaderRenderingMode.Cutout: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 2450; break; case StandardShaderRenderingMode.Fade: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; case StandardShaderRenderingMode.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; } }
public static void SetAlphaModeMask(UnityEngine.Material material) { material.SetFloat(modePropId, (int)StandardShaderMode.Cutout); material.SetOverrideTag("RenderType", "TransparentCutout"); material.EnableKeyword("_ALPHATEST_ON"); material.renderQueue = 2450; }
public static void SetAlphaModeBlend(UnityEngine.Material material) { material.SetFloat(modePropId, (int)StandardShaderMode.Transparent); material.SetFloat(dstBlendPropId, 10); material.SetOverrideTag("RenderType", "Transparent"); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; }
protected virtual void SetAlphaModeMask(Schema.Material gltfMaterial, Material material) { material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); #if USING_HDRP_10_OR_NEWER || USING_URP_12_OR_NEWER material.EnableKeyword(KW_ALPHATEST_ON); material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_CUTOUT); material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3 #endif }
public static void SetOpaque(Material material) { material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; }
public static void SetOpaqueMode(UnityEngine.Material material) { material.SetOverrideTag("RenderType", "Opaque"); material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.One); material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt(zWritePropId, 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; }
public static void SetTransparent(Material material) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; }
public static void SetAlphaModeBlend(UnityEngine.Material material) { material.SetFloat(modePropId, (int)StandardShaderMode.Transparent); material.SetOverrideTag("RenderType", "Transparent"); material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.SrcAlpha); //5 material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); //10 material.SetInt(zWritePropId, 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; //3000 }
public static void SetAlphaModeMask(UnityEngine.Material material, Schema.Material gltfMaterial) { material.SetFloat(modePropId, (int)StandardShaderMode.Cutout); material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt(srcBlendPropId, (int)UnityEngine.Rendering.BlendMode.One); material.SetInt(dstBlendPropId, (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt(zWritePropId, 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; //2450 material.SetFloat(cutoffPropId, gltfMaterial.alphaCutoff); }
public static void SetOpaque(Material material) { if (material.shader != StandardShader) Debug.LogWarning("Trying to set opaque mode on non-standard shader. Please use the Standard Shader instead or modify this method."); material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; }
static int SetOverrideTag(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.Material obj = (UnityEngine.Material)ToLua.CheckObject(L, 1, typeof(UnityEngine.Material)); string arg0 = ToLua.CheckString(L, 2); string arg1 = ToLua.CheckString(L, 3); obj.SetOverrideTag(arg0, arg1); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static public int SetOverrideTag(IntPtr l) { try { UnityEngine.Material self = (UnityEngine.Material)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.String a2; checkType(l, 3, out a2); self.SetOverrideTag(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
private void SetupMaterialBlendMode(UnityEngine.Material material) { bool isUsingGeometryRenderQueue = material.renderQueue == 2000; material.SetOverrideTag("RenderType", isUsingGeometryRenderQueue ? "Opaque" : "Transparent"); material.SetInt("_ZWrite", isUsingGeometryRenderQueue ? 1 : 0); if (isUsingGeometryRenderQueue) { material.SetInt("_SrcBlend", (int)BlendMode.One); material.SetInt("_DstBlend", (int)BlendMode.Zero); } else { material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha); } }
protected override void SetShaderModeBlend(Schema.Material gltfMaterial, Material material) { material.SetOverrideTag(TAG_RENDER_TYPE, TAG_RENDER_TYPE_TRANSPARENT); material.EnableKeyword(KW_SURFACE_TYPE_TRANSPARENT); material.EnableKeyword(KW_DISABLE_SSR_TRANSPARENT); material.EnableKeyword(KW_ENABLE_FOG_ON_TRANSPARENT); material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPrepass, false); material.SetShaderPassEnabled(k_ShaderPassTransparentDepthPostpass, false); material.SetShaderPassEnabled(k_ShaderPassTransparentBackface, false); material.SetShaderPassEnabled(k_ShaderPassRayTracingPrepass, false); material.SetShaderPassEnabled(k_ShaderPassDepthOnlyPass, false); material.SetFloat(srcBlendPropId, (int)BlendMode.SrcAlpha); //5 material.SetFloat(dstBlendPropId, (int)BlendMode.OneMinusSrcAlpha); //10 material.SetFloat(k_ZTestGBufferPropId, (int)CompareFunction.Equal); //3 material.SetFloat(k_AlphaDstBlendPropId, (int)BlendMode.OneMinusSrcAlpha); //10 material.SetFloat(k_Surface, 1); material.SetFloat(zWritePropId, 0); }
public static void SetTransparent(Material material, Color? newcolor = null) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.SetFloat("_Metallic", 0f); material.SetFloat("_Glossiness", 0f); material.renderQueue = 3000; material.mainTexture = null; if (newcolor != null) { material.color = newcolor.Value; } }
public static void SetTransparent(Material material, Color? newcolor = null) { if (material.shader != StandardShader) Debug.LogWarning("Trying to set transparent mode on non-standard shader. Please use the Standard Shader instead or modify this method."); material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.SetFloat("_Metallic", 0f); material.SetFloat("_Glossiness", 0f); material.renderQueue = 3000; material.mainTexture = null; if (newcolor != null) { material.color = newcolor.Value; } }
//--------------------------------------------------------------------------------------------------------------------------------------------------- public static bool StandardToValveSingleMaterial( Material m, Shader srcShader, Shader destShader, bool bRecordUnknownShaders, List<string> unknownShaders ) { string n = srcShader.name; if ( n.Equals( destShader.name ) ) { // Do nothing //Debug.Log( " Skipping " + m.name + "\n" ); return false; } else if ( n.Equals( "Standard" ) || n.Equals( "Valve/VR/Standard" ) ) { // Metallic specular Debug.Log( " Converting from \"" + n + "\"-->\"" + destShader.name + "\": " + m.name + "\n" ); m.shader = destShader; m.SetOverrideTag( "OriginalShader", n ); m.SetInt( "_SpecularMode", 2 ); m.DisableKeyword( "S_SPECULAR_NONE" ); m.DisableKeyword( "S_SPECULAR_BLINNPHONG" ); m.EnableKeyword( "S_SPECULAR_METALLIC" ); return true; } else if ( n.Equals( "Standard (Specular setup)" ) || n.Equals( "Legacy Shaders/Bumped Diffuse" ) || n.Equals( "Legacy Shaders/Transparent/Diffuse" ) ) { // Regular specular Debug.Log( " Converting from \"" + n + "\"-->\"" + destShader.name + "\": " + m.name + "\n" ); m.shader = destShader; m.SetOverrideTag( "OriginalShader", n ); m.SetInt( "_SpecularMode", 1 ); m.DisableKeyword( "S_SPECULAR_NONE" ); m.EnableKeyword( "S_SPECULAR_BLINNPHONG" ); m.DisableKeyword( "S_SPECULAR_METALLIC" ); if ( n.Equals( "Legacy Shaders/Transparent/Diffuse" ) ) { m.SetFloat( "_Mode", 2 ); m.SetOverrideTag( "RenderType", "Transparent" ); m.SetInt( "_SrcBlend", ( int )UnityEngine.Rendering.BlendMode.SrcAlpha ); m.SetInt( "_DstBlend", ( int )UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha ); m.SetInt( "_ZWrite", 0 ); m.DisableKeyword( "_ALPHATEST_ON" ); m.EnableKeyword( "_ALPHABLEND_ON" ); m.DisableKeyword( "_ALPHAPREMULTIPLY_ON" ); m.renderQueue = 3000; } return true; } else if ( n.Equals( "Unlit/Color" ) || n.Equals( "Unlit/Texture" ) || n.Equals( "Unlit/Transparent" ) || n.Equals( "Unlit/Transparent Cutout" ) ) { // Unlit Debug.Log( " Converting from \"" + n + "\"-->\"" + destShader.name + "\": " + m.name + "\n" ); m.shader = destShader; m.SetOverrideTag( "OriginalShader", n ); m.SetInt( "g_bUnlit", 1 ); m.EnableKeyword( "S_UNLIT" ); m.SetColor( "_EmissionColor", Color.black ); if ( n.Equals( "Unlit/Color" ) ) { m.SetTexture( "_MainTex", Texture2D.whiteTexture ); } else { m.SetColor( "_Color", Color.white ); if ( n.Equals( "Unlit/Transparent Cutout" ) ) { m.SetFloat( "_Mode", 1 ); m.SetOverrideTag( "RenderType", "TransparentCutout" ); m.SetInt( "_SrcBlend", ( int )UnityEngine.Rendering.BlendMode.One ); m.SetInt( "_DstBlend", ( int )UnityEngine.Rendering.BlendMode.Zero ); m.SetInt( "_ZWrite", 1 ); m.EnableKeyword( "_ALPHATEST_ON" ); m.DisableKeyword( "_ALPHABLEND_ON" ); m.DisableKeyword( "_ALPHAPREMULTIPLY_ON" ); m.renderQueue = 2450; } else if ( n.Equals( "Unlit/Transparent" ) ) { m.SetFloat( "_Mode", 2 ); m.SetOverrideTag( "RenderType", "Transparent" ); m.SetInt( "_SrcBlend", ( int )UnityEngine.Rendering.BlendMode.SrcAlpha ); m.SetInt( "_DstBlend", ( int )UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha ); m.SetInt( "_ZWrite", 0 ); m.DisableKeyword( "_ALPHATEST_ON" ); m.EnableKeyword( "_ALPHABLEND_ON" ); m.DisableKeyword( "_ALPHAPREMULTIPLY_ON" ); m.renderQueue = 3000; } } return true; } else if ( bRecordUnknownShaders ) { if ( n.StartsWith( "Valve/" ) ) { // Don't report unknown shaders if in the top level Valve folder return false; } // Don't know how to convert, so add to list to spew at the end Debug.LogWarning( " Don't know how to convert shader \"" + n + "\"" + " in material \"" + m.name + "\"" + "\n" ); if ( !unknownShaders.Contains( n ) ) { unknownShaders.Add( n ); } return false; } return false; }
protected virtual MaterialCacheData CreateMaterial(GLTF.Schema.Material def, int materialIndex) { MaterialCacheData materialWrapper = null; if (materialIndex < 0 || _assetCache.MaterialCache[materialIndex] == null) { Shader shader; // get the shader to use for this material try { if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_pbrSpecularGlossiness")) { shader = _shaderCache[MaterialType.KHR_materials_pbrSpecularGlossiness]; } else if (def.PbrMetallicRoughness != null) { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } else if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_common") && def.CommonConstant != null) { shader = _shaderCache[MaterialType.CommonConstant]; } else { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } } catch (KeyNotFoundException) { Debug.LogWarningFormat("No shader supplied for type of glTF material {0}, using Standard fallback", def.Name); shader = Shader.Find("Standard"); } shader.maximumLOD = MaximumLod; var material = new UnityEngine.Material(shader); if (def.AlphaMode == AlphaMode.MASK) { material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; material.SetFloat("_Cutoff", (float)def.AlphaCutoff); } else if (def.AlphaMode == AlphaMode.BLEND) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else { material.SetOverrideTag("RenderType", "Opaque"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; } if (def.DoubleSided) { material.SetInt("_Cull", (int)CullMode.Off); } else { material.SetInt("_Cull", (int)CullMode.Back); } if (def.PbrMetallicRoughness != null) { var pbr = def.PbrMetallicRoughness; material.SetColor("_Color", pbr.BaseColorFactor.ToUnityColor()); if (pbr.BaseColorTexture != null) { var textureDef = pbr.BaseColorTexture.Index.Value; material.SetTexture("_MainTex", CreateTexture(textureDef)); ApplyTextureTransform(pbr.BaseColorTexture, material, "_MainTex"); } material.SetFloat("_Metallic", (float)pbr.MetallicFactor); if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Value; material.SetTexture("_MetallicRoughnessMap", CreateTexture(texture)); ApplyTextureTransform(pbr.MetallicRoughnessTexture, material, "_MetallicRoughnessMap"); } material.SetFloat("_Roughness", (float)pbr.RoughnessFactor); } if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME)) { KHR_materials_pbrSpecularGlossinessExtension specGloss = def.Extensions[KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME] as KHR_materials_pbrSpecularGlossinessExtension; if (specGloss.DiffuseTexture != null) { var texture = specGloss.DiffuseTexture.Index.Value; material.SetTexture("_MainTex", CreateTexture(texture)); ApplyTextureTransform(specGloss.DiffuseTexture, material, "_MainTex"); } else { material.SetColor("_Color", specGloss.DiffuseFactor.ToUnityColor()); } if (specGloss.SpecularGlossinessTexture != null) { var texture = specGloss.SpecularGlossinessTexture.Index.Value; material.SetTexture("_SpecGlossMap", CreateTexture(texture)); material.EnableKeyword("_SPECGLOSSMAP"); ApplyTextureTransform(specGloss.SpecularGlossinessTexture, material, "_SpecGlossMap"); } else { material.SetVector("_SpecColor", specGloss.SpecularFactor.ToUnityVector3()); material.SetFloat("_Glossiness", (float)specGloss.GlossinessFactor); } } if (def.CommonConstant != null) { material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor.ToUnityColor()); if (def.CommonConstant.LightmapTexture != null) { material.EnableKeyword("LIGHTMAP_ON"); var texture = def.CommonConstant.LightmapTexture.Index.Value; material.SetTexture("_LightMap", CreateTexture(texture)); material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord); ApplyTextureTransform(def.CommonConstant.LightmapTexture, material, "_LightMap"); } material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor.ToUnityColor()); } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Value; material.SetTexture("_BumpMap", CreateTexture(texture)); material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale); material.EnableKeyword("_NORMALMAP"); ApplyTextureTransform(def.NormalTexture, material, "_BumpMap"); } if (def.OcclusionTexture != null) { var texture = def.OcclusionTexture.Index; material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); if (def.PbrMetallicRoughness != null && def.PbrMetallicRoughness.MetallicRoughnessTexture != null && def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id) { material.EnableKeyword("OCC_METAL_ROUGH_ON"); } else { material.SetTexture("_OcclusionMap", CreateTexture(texture.Value)); ApplyTextureTransform(def.OcclusionTexture, material, "_OcclusionMap"); } } if (def.EmissiveTexture != null) { var texture = def.EmissiveTexture.Index.Value; material.EnableKeyword("EMISSION_MAP_ON"); material.EnableKeyword("_EMISSION"); material.SetTexture("_EmissionMap", CreateTexture(texture)); material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord); ApplyTextureTransform(def.EmissiveTexture, material, "_EmissionMap"); } material.SetColor("_EmissionColor", def.EmissiveFactor.ToUnityColor()); materialWrapper = new MaterialCacheData { UnityMaterial = material, UnityMaterialWithVertexColor = new UnityEngine.Material(material), GLTFMaterial = def }; materialWrapper.UnityMaterialWithVertexColor.EnableKeyword("VERTEX_COLOR_ON"); if (materialIndex > 0) { _assetCache.MaterialCache[materialIndex] = materialWrapper; } } return(materialIndex > 0 ? _assetCache.MaterialCache[materialIndex] : materialWrapper); }
public static void SetupMaterialWithBlendMode(Material material, StandardShaderGUI.BlendMode blendMode) { switch (blendMode) { case StandardShaderGUI.BlendMode.Opaque: material.SetOverrideTag("RenderType", string.Empty); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 0); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case StandardShaderGUI.BlendMode.Cutout: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 0); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 2450; break; case StandardShaderGUI.BlendMode.Fade: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", 5); material.SetInt("_DstBlend", 10); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; case StandardShaderGUI.BlendMode.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 10); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 3000; break; } }
public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode) { switch (blendMode) { case BlendMode.Opaque: material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case BlendMode.Cutout: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; break; case BlendMode.Fade: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; break; case BlendMode.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; break; } }
private UnityEngine.Material CreateMaterial(Material def, bool useVertexColors) { UnityEngine.Material material = new UnityEngine.Material(useVertexColors ? ColorMaterial : NoColorMaterial); material.shader.maximumLOD = MaximumLod; if (def.AlphaMode == AlphaMode.MASK) { material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; material.SetFloat("_Cutoff", (float)def.AlphaCutoff); } else if (def.AlphaMode == AlphaMode.BLEND) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else { material.SetOverrideTag("RenderType", "Opaque"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; } if (def.DoubleSided) { material.SetInt("_Cull", (int)CullMode.Off); } else { material.SetInt("_Cull", (int)CullMode.Back); } if (useVertexColors) { material.EnableKeyword("VERTEX_COLOR_ON"); } if (def.PbrMetallicRoughness != null) { var pbr = def.PbrMetallicRoughness; material.SetColor("_Color", pbr.BaseColorFactor); if (pbr.BaseColorTexture != null) { var texture = pbr.BaseColorTexture.Index.Value; material.SetTexture("_MainTex", _imageCache[texture.Source.Value]); } material.SetFloat("_Metallic", (float)pbr.MetallicFactor); if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Value; material.SetTexture("_MetallicRoughnessMap", _imageCache[texture.Source.Value]); } material.SetFloat("_Roughness", (float)pbr.RoughnessFactor); } if (def.CommonConstant != null) { material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor); if (def.CommonConstant.LightmapTexture != null) { material.EnableKeyword("LIGHTMAP_ON"); var texture = def.CommonConstant.LightmapTexture.Index.Value; material.SetTexture("_LightMap", _imageCache[texture.Source.Value]); material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord); } material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor); } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Value; material.EnableKeyword("_NORMALMAP"); material.SetTexture("_BumpMap", _imageCache[texture.Source.Value]); material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale); } else { material.SetTexture("_BumpMap", null); material.DisableKeyword("_NORMALMAP"); } if (def.OcclusionTexture != null) { var texture = def.OcclusionTexture.Index; material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); if (def.PbrMetallicRoughness != null && def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id) { material.EnableKeyword("OCC_METAL_ROUGH_ON"); } else { material.SetTexture("_OcclusionMap", _imageCache[texture.Value.Source.Value]); } } if (def.EmissiveTexture != null) { var texture = def.EmissiveTexture.Index.Value; material.EnableKeyword("_EMISSION"); material.EnableKeyword("EMISSION_MAP_ON"); material.SetTexture("_EmissionMap", _imageCache[texture.Source.Value]); material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord); } material.SetColor("_EmissionColor", def.EmissiveFactor); return(material); }
protected virtual UnityEngine.Material CreateMaterial(Material def, bool useVertexColors) { if (def.ContentsWithVC == null || def.ContentsWithoutVC == null) { Shader shader; // get the shader to use for this material try { if (def.PbrMetallicRoughness != null) { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } else if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_common") && def.CommonConstant != null) { shader = _shaderCache[MaterialType.CommonConstant]; } else { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } } catch (KeyNotFoundException e) { Debug.LogWarningFormat("No shader supplied for type of glTF material {0}, using Standard fallback", def.Name); shader = Shader.Find("Standard"); } shader.maximumLOD = MaximumLod; var material = new UnityEngine.Material(shader); if (def.AlphaMode == AlphaMode.MASK) { material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; material.SetFloat("_Cutoff", (float)def.AlphaCutoff); } else if (def.AlphaMode == AlphaMode.BLEND) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else { material.SetOverrideTag("RenderType", "Opaque"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; } if (def.DoubleSided) { material.SetInt("_Cull", (int)CullMode.Off); } else { material.SetInt("_Cull", (int)CullMode.Back); } if (def.PbrMetallicRoughness != null) { var pbr = def.PbrMetallicRoughness; material.SetColor("_Color", pbr.BaseColorFactor); if (pbr.BaseColorTexture != null) { var texture = pbr.BaseColorTexture.Index.Value; material.SetTexture("_MainTex", CreateTexture(texture)); } material.SetFloat("_Metallic", (float)pbr.MetallicFactor); if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Value; material.SetTexture("_MetallicRoughnessMap", CreateTexture(texture)); } material.SetFloat("_Roughness", (float)pbr.RoughnessFactor); } if (def.CommonConstant != null) { material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor); if (def.CommonConstant.LightmapTexture != null) { material.EnableKeyword("LIGHTMAP_ON"); var texture = def.CommonConstant.LightmapTexture.Index.Value; material.SetTexture("_LightMap", CreateTexture(texture)); material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord); } material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor); } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Value; material.SetTexture("_BumpMap", CreateTexture(texture)); material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale); } if (def.OcclusionTexture != null) { var texture = def.OcclusionTexture.Index; material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); if (def.PbrMetallicRoughness != null && def.PbrMetallicRoughness.MetallicRoughnessTexture != null && def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id) { material.EnableKeyword("OCC_METAL_ROUGH_ON"); } else { material.SetTexture("_OcclusionMap", CreateTexture(texture.Value)); } } if (def.EmissiveTexture != null) { var texture = def.EmissiveTexture.Index.Value; material.EnableKeyword("EMISSION_MAP_ON"); material.SetTexture("_EmissionMap", CreateTexture(texture)); material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord); } material.SetColor("_EmissionColor", def.EmissiveFactor); def.ContentsWithoutVC = material; def.ContentsWithVC = new UnityEngine.Material(material); def.ContentsWithVC.EnableKeyword("VERTEX_COLOR_ON"); } return(def.GetContents(useVertexColors)); }
private void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode) { switch (blendMode) { case BlendMode.Opaque: // set render type unityMaterial.SetOverrideTag("RenderType", "Opaque"); // required for the blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero); unityMaterial.SetInt(ZWrite, 1); // properties unityMaterial.SetFloat(Surface, 0); // 0 = Opaque; 1 = Transparent // render queue unityMaterial.renderQueue = -1; break; case BlendMode.Cutout: // set render type unityMaterial.SetOverrideTag("RenderType", "TransparentCutout"); // keywords unityMaterial.EnableKeyword("_ALPHATEST_ON"); // required for blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero); unityMaterial.SetInt(ZWrite, 1); // properties unityMaterial.SetFloat(Surface, 0); // 0 = Opaque; 1 = Transparent unityMaterial.SetInt(AlphaClip, 1); // render queue unityMaterial.renderQueue = 2450; break; case BlendMode.Translucent: // disable shader passes unityMaterial.SetShaderPassEnabled("SHADOWCASTER", false); // set render type unityMaterial.SetOverrideTag("RenderType", "Transparent"); // keywords unityMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON"); // required for blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); unityMaterial.SetInt(ZWrite, 0); // properties unityMaterial.SetFloat(Surface, 1); // 0 = Opaque; 1 = Transparent unityMaterial.SetFloat(Blend, 1); // 0 = Alpha, 1 = Premultiply, 2 = Additive, 3 = Multiply // render queue const int transparentRenderQueueBase = 3000; const int transparentSortingPriority = 0; unityMaterial.SetInt(QueueOffset, transparentSortingPriority); unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority; break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Change the blend mode of a Standard material at runtime. /// </summary> public static void SetBlendMode( Material material, CustomBlendMode blendMode, CustomSmoothnessMapChannel smoothnessChannel, float metallic, float glossiness) { // Set properties material.SetFloat("_Mode", (int)blendMode); material.SetFloat("_SmoothnessTextureChannel", (int)smoothnessChannel); material.SetFloat("_Metallic", metallic); material.SetFloat("_Glossiness", glossiness); switch (blendMode) { case CustomBlendMode.Opaque: material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case CustomBlendMode.Cutout: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; break; case CustomBlendMode.Fade: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; break; case CustomBlendMode.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; break; } }
public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode) { switch (blendMode) { case BlendMode.Opaque: material.SetOverrideTag("RenderType", ""); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 0); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; break; case BlendMode.Cutout: material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 0); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 0x992; break; case BlendMode.Fade: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", 5); material.SetInt("_DstBlend", 10); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 0xbb8; break; case BlendMode.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", 1); material.SetInt("_DstBlend", 10); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.EnableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = 0xbb8; break; } }
private static void ApplyBlendMode(UnityEngine.Material unityMaterial, BlendMode blendMode) { // disable shader passes unityMaterial.SetShaderPassEnabled("DistortionVectors", false); unityMaterial.SetShaderPassEnabled("MOTIONVECTORS", false); unityMaterial.SetShaderPassEnabled("TransparentDepthPrepass", false); unityMaterial.SetShaderPassEnabled("TransparentDepthPostpass", false); unityMaterial.SetShaderPassEnabled("TransparentBackface", false); // reset existing blend modes, will be enabled explicitly unityMaterial.DisableKeyword("_BLENDMODE_ALPHA"); unityMaterial.DisableKeyword("_BLENDMODE_ADD"); unityMaterial.DisableKeyword("_BLENDMODE_PRE_MULTIPLY"); switch (blendMode) { case Engine.VPT.BlendMode.Opaque: // required for the blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero); unityMaterial.SetInt(ZWrite, 1); // properties unityMaterial.SetFloat(SurfaceType, 0); // 0 = Opaque; 1 = Transparent unityMaterial.SetFloat(AlphaCutoffEnable, 0); // render queue unityMaterial.renderQueue = -1; break; case Engine.VPT.BlendMode.Cutout: // set render type unityMaterial.SetOverrideTag("RenderType", "TransparentCutout"); // keywords unityMaterial.EnableKeyword("_ALPHATEST_ON"); unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE"); // required for the blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero); unityMaterial.SetInt(ZWrite, 1); // properties unityMaterial.SetFloat(SurfaceType, 0); // 0 = Opaque; 1 = Transparent unityMaterial.SetFloat(AlphaCutoffEnable, 1); unityMaterial.SetFloat(ZTestDepthEqualForOpaque, 3); unityMaterial.SetFloat(ZTestModeDistortion, 4); unityMaterial.SetFloat(ZTestGBuffer, 3); // render queue unityMaterial.renderQueue = 2450; break; case Engine.VPT.BlendMode.Translucent: // set render type unityMaterial.SetOverrideTag("RenderType", "Transparent"); // keywords //unityMaterial.EnableKeyword("_ALPHATEST_ON"); // required for _AlphaCutoffEnable unityMaterial.EnableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); unityMaterial.EnableKeyword("_BLENDMODE_PRE_MULTIPLY"); unityMaterial.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT"); unityMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE"); unityMaterial.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); // required for the blend mode unityMaterial.SetInt(SrcBlend, (int)UnityEngine.Rendering.BlendMode.One); unityMaterial.SetInt(DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); unityMaterial.SetInt(ZWrite, 0); // properties unityMaterial.SetFloat(SurfaceType, 1); // 0 = Opaque; 1 = Transparent //unityMaterial.SetFloat("_AlphaCutoffEnable", 1); // enable keyword _ALPHATEST_ON if this is required unityMaterial.SetFloat(BlendMode, 4); // 0 = Alpha, 1 = Additive, 4 = PreMultiply // render queue const int transparentRenderQueueBase = 3000; const int transparentSortingPriority = 0; unityMaterial.SetInt(TransparentSortPriority, transparentSortingPriority); unityMaterial.renderQueue = transparentRenderQueueBase + transparentSortingPriority; break; default: throw new ArgumentOutOfRangeException(); } }
static void SetRenderQueue (Material material, string queue) { bool meshNormal = true; if (material.HasProperty("_FixedNormal")) { bool fixedNormals = material.IsKeywordEnabled(_FIXED_NORMALS); bool fixedNormalsBackRendering = material.IsKeywordEnabled(_FIXED_NORMALS_BACK_RENDERING); meshNormal = !fixedNormals && !fixedNormalsBackRendering; } material.SetOverrideTag("RenderType", meshNormal ? queue : "Sprite"); }