Last active
January 9, 2019 23:35
-
-
Save Fewes/43f2b97fe8785e9a69e22836c7d71f4e to your computer and use it in GitHub Desktop.
The custom GUI needed to use my vr_extended shader for The Lab Renderer: https://gist.github.com/Fewes/503951a86bc6825ea622c4b6c94e2643.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Valve Corporation, All rights reserved. ====================================================================================================== | |
#if ( UNITY_EDITOR ) | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace UnityEditor | |
{ | |
internal class ValveExtendedShaderGUI : ShaderGUI | |
{ | |
public enum BlendMode | |
{ | |
Opaque, | |
AlphaTest, | |
AlphaBlend, | |
Glass, | |
Additive | |
// TODO: MaskedGlass that will require an additional grayscale texture to act as a standard alpha blend mask | |
} | |
public enum SpecularMode | |
{ | |
None, | |
//BlinnPhong, | |
Metallic | |
//Anisotropic | |
} | |
private static class Styles | |
{ | |
public static GUIStyle optionsButton = "PaneOptions"; | |
public static GUIContent uvSetLabel = new GUIContent("UV Set"); | |
public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") }; | |
public static GUIContent unlitText = new GUIContent("Unlit", ""); | |
public static GUIContent VCText = new GUIContent("Vertex Colors", ""); | |
public static GUIContent linearVCText = new GUIContent("Linearize Vertex Colors", ""); | |
public static string emptyTootip = ""; | |
public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)"); | |
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff"); | |
public static GUIContent colorOvrText = new GUIContent("Color Override", "Override final shaded color"); | |
public static GUIContent metallicText = new GUIContent("Metallic", ""); | |
public static GUIContent smoothnessText = new GUIContent("Gloss", ""); | |
public static GUIContent normalMapText = new GUIContent("Normal", "Normal Map"); | |
public static GUIContent maskMapText = new GUIContent("Mask", "Metallic (R), Roughness (G), AO (B), Detail Mask (A)"); | |
public static GUIContent cubeMapScalarText = new GUIContent("Cube Map Scalar", ""); | |
public static GUIContent occlusionStrength = new GUIContent("Occlusion Strength", ""); | |
public static GUIContent occlusionStrengthDirectDiffuseText = new GUIContent("Direct Diffuse", ""); | |
public static GUIContent occlusionStrengthDirectSpecularText = new GUIContent("Direct Specular", ""); | |
public static GUIContent occlusionStrengthIndirectDiffuseText = new GUIContent("Indirect Diffuse", ""); | |
public static GUIContent occlusionStrengthIndirectSpecularText = new GUIContent("Indirect Specular", ""); | |
public static GUIContent emissionText = new GUIContent("Emission", "Emission (RGB)"); | |
public static GUIContent detailAlbedoText = new GUIContent("Detail Albedo", "Detail Albedo (RGB) multiplied by 2"); | |
public static GUIContent detailNormalMapText = new GUIContent("Detail Normal", "Detail Normal Map"); | |
public static GUIContent detail1MapText = new GUIContent("Detail Map 1", "Detail Normal (RG), Detail Albedo (B), Detail Roughness (A)"); | |
public static GUIContent detail2MapText = new GUIContent("Detail Map 2", "Detail Normal (RG), Detail Albedo (B), Detail Roughness (A)"); | |
public static GUIContent detail3MapText = new GUIContent("Detail Map 3", "Detail Normal (RG), Detail Albedo (B), Detail Roughness (A)"); | |
public static GUIContent detail4MapText = new GUIContent("Detail Map 4", "Detail Normal (RG), Detail Albedo (B), Detail Roughness (A)"); | |
public static GUIContent detailUVScaleText = new GUIContent("UV Scale", ""); | |
public static GUIContent receiveShadowsText = new GUIContent("Receive Shadows", ""); | |
public static GUIContent renderBackfacesText = new GUIContent("Render Backfaces", ""); | |
public static GUIContent overrideLightmapText = new GUIContent("Override Lightmap", "Requires ValveOverrideLightmap.cs scrip on object"); | |
public static GUIContent worldAlignedTextureText = new GUIContent("World Aligned Texture", ""); | |
public static GUIContent worldAlignedTextureSizeText = new GUIContent("Size", ""); | |
public static GUIContent worldAlignedTextureNormalText = new GUIContent("Normal", ""); | |
public static GUIContent worldAlignedTexturePositionText = new GUIContent("World Position", ""); | |
public static string whiteSpaceString = " "; | |
public static string primaryMapsText = "Main Maps"; | |
public static string secondaryMapsText = "Detail Maps"; | |
public static string renderingMode = "Rendering Mode"; | |
public static string specularModeText = "Specular Mode"; | |
public static GUIContent emissiveWarning = new GUIContent("Emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive."); | |
public static GUIContent emissiveColorWarning = new GUIContent("Ensure emissive color is non-black for emission to have effect."); | |
public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode)); | |
public static readonly string[] specularNames = Enum.GetNames(typeof(SpecularMode)); | |
} | |
MaterialProperty unlit = null; | |
MaterialProperty VC = null; | |
MaterialProperty linearVC = null; | |
MaterialProperty blendMode = null; | |
MaterialProperty specularMode = null; | |
MaterialProperty albedoMap = null; | |
MaterialProperty albedoColor = null; | |
MaterialProperty overrideColor = null; | |
MaterialProperty alphaCutoff = null; | |
MaterialProperty maskMap = null; | |
MaterialProperty metallic = null; | |
MaterialProperty smoothness = null; | |
MaterialProperty bumpScale = null; | |
MaterialProperty bumpMap = null; | |
MaterialProperty cubeMapScalar = null; | |
MaterialProperty occlusionStrength = null; | |
MaterialProperty occlusionStrengthDirectDiffuse = null; | |
MaterialProperty occlusionStrengthDirectSpecular = null; | |
MaterialProperty occlusionStrengthIndirectDiffuse = null; | |
MaterialProperty occlusionStrengthIndirectSpecular = null; | |
//MaterialProperty heigtMapScale = null; | |
//MaterialProperty heightMap = null; | |
MaterialProperty emissionColorForRendering = null; | |
MaterialProperty emissionMap = null; | |
MaterialProperty detail1Map = null; | |
MaterialProperty detail2Map = null; | |
MaterialProperty detail3Map = null; | |
MaterialProperty detail4Map = null; | |
MaterialProperty detail1Scale = null; | |
MaterialProperty detail2Scale = null; | |
MaterialProperty detail3Scale = null; | |
MaterialProperty detail4Scale = null; | |
MaterialProperty detail1UVScale = null; | |
MaterialProperty detail2UVScale = null; | |
MaterialProperty detail3UVScale = null; | |
MaterialProperty detail4UVScale = null; | |
MaterialProperty uvSetSecondary = null; | |
MaterialProperty receiveShadows = null; | |
MaterialProperty renderBackfaces = null; | |
MaterialProperty overrideLightmap = null; | |
MaterialProperty worldAlignedTexture = null; | |
MaterialProperty worldAlignedTextureSize = null; | |
MaterialProperty worldAlignedTextureNormal = null; | |
MaterialProperty worldAlignedTexturePosition = null; | |
MaterialEditor m_MaterialEditor; | |
ColorPickerHDRConfig m_ColorPickerHDRConfig = new ColorPickerHDRConfig(0f, 99f, 1 / 99f, 3f); | |
bool m_FirstTimeApply = true; | |
public void FindProperties(MaterialProperty[] props) | |
{ | |
unlit = FindProperty("g_bUnlit", props); | |
VC = FindProperty("g_bVC", props); | |
linearVC = FindProperty("g_bLinearVC", props); | |
blendMode = FindProperty("_Mode", props); | |
specularMode = FindProperty("_SpecularMode", props); | |
albedoMap = FindProperty("_MainTex", props); | |
albedoColor = FindProperty("_Color", props); | |
overrideColor = FindProperty("_ColorOvr", props); | |
alphaCutoff = FindProperty("_Cutoff", props); | |
maskMap = FindProperty("_MaskMap", props, true); | |
metallic = FindProperty("_Metallic", props); | |
smoothness = FindProperty("_Glossiness", props); | |
bumpScale = FindProperty("_BumpScale", props); | |
bumpMap = FindProperty("_BumpMap", props); | |
//heigtMapScale = FindProperty ("_Parallax", props); | |
//heightMap = FindProperty("_ParallaxMap", props); | |
cubeMapScalar = FindProperty("g_flCubeMapScalar", props); | |
occlusionStrength = FindProperty("_OcclusionStrength", props); | |
occlusionStrengthDirectDiffuse = FindProperty("_OcclusionStrengthDirectDiffuse", props); | |
occlusionStrengthDirectSpecular = FindProperty("_OcclusionStrengthDirectSpecular", props); | |
occlusionStrengthIndirectDiffuse = FindProperty("_OcclusionStrengthIndirectDiffuse", props); | |
occlusionStrengthIndirectSpecular = FindProperty("_OcclusionStrengthIndirectSpecular", props); | |
emissionColorForRendering = FindProperty("_EmissionColor", props); | |
emissionMap = FindProperty("_EmissionMap", props); | |
detail1Map = FindProperty("_Detail1Map", props); | |
detail2Map = FindProperty("_Detail2Map", props); | |
detail3Map = FindProperty("_Detail3Map", props); | |
detail4Map = FindProperty("_Detail4Map", props); | |
detail1Scale = FindProperty("_Detail1Scale", props); | |
detail2Scale = FindProperty("_Detail2Scale", props); | |
detail3Scale = FindProperty("_Detail3Scale", props); | |
detail4Scale = FindProperty("_Detail4Scale", props); | |
detail1UVScale = FindProperty("_Detail1UVScale", props); | |
detail2UVScale = FindProperty("_Detail2UVScale", props); | |
detail3UVScale = FindProperty("_Detail3UVScale", props); | |
detail4UVScale = FindProperty("_Detail4UVScale", props); | |
uvSetSecondary = FindProperty("_UVSec", props); | |
receiveShadows = FindProperty("g_bReceiveShadows", props); | |
renderBackfaces = FindProperty("g_bRenderBackfaces", props); | |
overrideLightmap = FindProperty("g_tOverrideLightmap", props); | |
worldAlignedTexture = FindProperty("g_bWorldAlignedTexture", props, false); | |
worldAlignedTextureSize = FindProperty("g_vWorldAlignedTextureSize", props, worldAlignedTexture != null); | |
worldAlignedTextureNormal = FindProperty("g_vWorldAlignedTextureNormal", props, worldAlignedTexture != null); | |
worldAlignedTexturePosition = FindProperty("g_vWorldAlignedTexturePosition", props, worldAlignedTexture != null); | |
} | |
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) | |
{ | |
FindProperties(props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly | |
m_MaterialEditor = materialEditor; | |
Material material = materialEditor.target as Material; | |
ShaderPropertiesGUI(material); | |
// Make sure that needed keywords are set up if we're switching some existing | |
// material to a standard shader. | |
if (m_FirstTimeApply) | |
{ | |
SetMaterialKeywords(material); | |
m_FirstTimeApply = false; | |
} | |
} | |
public void Vector3GUI(GUIContent label, MaterialProperty materialProperty) | |
{ | |
Vector4 v4 = materialProperty.vectorValue; | |
Vector3 v3 = EditorGUILayout.Vector3Field(label, new Vector3(v4.x, v4.y, v4.z)); | |
materialProperty.vectorValue = new Vector4(v3.x, v3.y, v3.z, 0.0f); | |
} | |
public void ShaderPropertiesGUI(Material material) | |
{ | |
// Use default labelWidth | |
EditorGUIUtility.labelWidth = 0f; | |
// Detect any changes to the material | |
EditorGUI.BeginChangeCheck(); | |
{ | |
m_MaterialEditor.ShaderProperty(unlit, Styles.unlitText.text); | |
bool bUnlit = (unlit.floatValue != 0.0f); | |
BlendModePopup(); | |
if (!bUnlit) | |
{ | |
SpecularModePopup(); | |
} | |
if (!bUnlit) | |
{ | |
m_MaterialEditor.ShaderProperty(receiveShadows, Styles.receiveShadowsText.text); | |
} | |
m_MaterialEditor.ShaderProperty(renderBackfaces, Styles.renderBackfacesText.text); | |
m_MaterialEditor.ShaderProperty(VC, Styles.VCText.text); | |
m_MaterialEditor.ShaderProperty(linearVC, Styles.linearVCText.text); | |
EditorGUILayout.Space(); | |
//GUILayout.Label( Styles.primaryMapsText, EditorStyles.boldLabel ); | |
DoAlbedoArea(material); | |
if (!bUnlit) | |
{ | |
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null); | |
DoSpecularMetallicArea(material); | |
m_MaterialEditor.ShaderProperty(occlusionStrength, Styles.occlusionStrength.text); | |
m_MaterialEditor.ShaderProperty(occlusionStrengthDirectDiffuse, Styles.occlusionStrengthDirectDiffuseText.text, 1); | |
m_MaterialEditor.ShaderProperty(occlusionStrengthDirectSpecular, Styles.occlusionStrengthDirectSpecularText.text, 1); | |
m_MaterialEditor.ShaderProperty(occlusionStrengthIndirectDiffuse, Styles.occlusionStrengthIndirectDiffuseText.text, 1); | |
m_MaterialEditor.ShaderProperty(occlusionStrengthIndirectSpecular, Styles.occlusionStrengthIndirectSpecularText.text, 1); | |
m_MaterialEditor.ShaderProperty(cubeMapScalar, Styles.cubeMapScalarText.text, 0); | |
} | |
//m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap, heightMap.textureValue != null ? heigtMapScale : null); | |
DoEmissionArea(material); | |
if (!bUnlit) | |
{ | |
m_MaterialEditor.TexturePropertySingleLine(Styles.overrideLightmapText, overrideLightmap); | |
} | |
EditorGUI.BeginChangeCheck(); // !!! AV - This is from Unity's script. Can these Begin/End calls be nested like this? | |
m_MaterialEditor.TextureScaleOffsetProperty(albedoMap); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
emissionMap.textureScaleAndOffset = albedoMap.textureScaleAndOffset; // Apply the main texture scale and offset to the emission texture as well, for Enlighten's sake | |
} | |
if (worldAlignedTexture != null) | |
{ | |
m_MaterialEditor.ShaderProperty(worldAlignedTexture, Styles.worldAlignedTextureText.text); | |
if (worldAlignedTexture.floatValue != 0.0f) | |
{ | |
EditorGUI.indentLevel = 2; | |
Vector3GUI(Styles.worldAlignedTextureSizeText, worldAlignedTextureSize); | |
Vector3GUI(Styles.worldAlignedTextureNormalText, worldAlignedTextureNormal); | |
Vector3GUI(Styles.worldAlignedTexturePositionText, worldAlignedTexturePosition); | |
EditorGUI.indentLevel = 0; | |
} | |
} | |
m_MaterialEditor.ShaderProperty(overrideColor, Styles.colorOvrText.text); | |
EditorGUILayout.Space(); | |
// Secondary properties | |
GUILayout.Label(Styles.secondaryMapsText, EditorStyles.boldLabel); | |
m_MaterialEditor.ShaderProperty(uvSetSecondary, Styles.uvSetLabel.text); | |
EditorGUILayout.Space(); | |
if (!bUnlit) | |
{ | |
m_MaterialEditor.TexturePropertySingleLine(Styles.detail1MapText, detail1Map, detail1Scale); | |
if (detail1Map.textureValue != null) | |
m_MaterialEditor.ShaderProperty(detail1UVScale, Styles.detailUVScaleText.text); | |
m_MaterialEditor.TexturePropertySingleLine(Styles.detail2MapText, detail2Map, detail2Scale); | |
if (detail2Map.textureValue != null) | |
m_MaterialEditor.ShaderProperty(detail2UVScale, Styles.detailUVScaleText.text); | |
m_MaterialEditor.TexturePropertySingleLine(Styles.detail3MapText, detail3Map, detail3Scale); | |
if (detail3Map.textureValue != null) | |
m_MaterialEditor.ShaderProperty(detail3UVScale, Styles.detailUVScaleText.text); | |
m_MaterialEditor.TexturePropertySingleLine(Styles.detail4MapText, detail4Map, detail4Scale); | |
if (detail4Map.textureValue != null) | |
m_MaterialEditor.ShaderProperty(detail4UVScale, Styles.detailUVScaleText.text); | |
}; | |
} | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
foreach (var obj in blendMode.targets) | |
{ | |
MaterialChanged((Material)obj); | |
} | |
foreach (var obj in specularMode.targets) | |
{ | |
MaterialChanged((Material)obj); | |
} | |
} | |
} | |
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader) | |
{ | |
base.AssignNewShaderToMaterial(material, oldShader, newShader); | |
if (oldShader == null) | |
return; | |
// Convert to vr_standard | |
if (newShader.name.Equals("Valve/vr_standard")) | |
{ | |
List<string> unknownShaders = new List<string>(); | |
ValveMenuTools.StandardToValveSingleMaterial(material, oldShader, newShader, false, unknownShaders); | |
} | |
// Legacy shaders | |
if (!oldShader.name.Contains("Legacy Shaders/")) | |
return; | |
BlendMode blendMode = BlendMode.Opaque; | |
if (oldShader.name.Contains("/Transparent/Cutout/")) | |
{ | |
blendMode = BlendMode.AlphaTest; | |
} | |
else if (oldShader.name.Contains("/Transparent/")) | |
{ | |
// NOTE: legacy shaders did not provide physically based transparency | |
// therefore Fade mode | |
blendMode = BlendMode.AlphaBlend; | |
} | |
material.SetFloat("_Mode", (float)blendMode); | |
MaterialChanged(material); | |
} | |
void BlendModePopup() | |
{ | |
EditorGUI.showMixedValue = blendMode.hasMixedValue; | |
var mode = (BlendMode)blendMode.floatValue; | |
EditorGUI.BeginChangeCheck(); | |
mode = (BlendMode)EditorGUILayout.Popup(Styles.renderingMode, (int)mode, Styles.blendNames); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode"); | |
blendMode.floatValue = (float)mode; | |
} | |
EditorGUI.showMixedValue = false; | |
} | |
void SpecularModePopup() | |
{ | |
EditorGUI.showMixedValue = specularMode.hasMixedValue; | |
var mode = (SpecularMode)specularMode.floatValue; | |
EditorGUI.BeginChangeCheck(); | |
mode = (SpecularMode)EditorGUILayout.Popup(Styles.specularModeText, (int)mode, Styles.specularNames); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
m_MaterialEditor.RegisterPropertyChangeUndo("Specular Mode"); | |
specularMode.floatValue = (float)mode; | |
} | |
EditorGUI.showMixedValue = false; | |
} | |
void DoAlbedoArea(Material material) | |
{ | |
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoMap, albedoColor); | |
if (((BlendMode)material.GetFloat("_Mode") == BlendMode.AlphaTest)) | |
{ | |
m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText.text, MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1); | |
} | |
} | |
void DoEmissionArea(Material material) | |
{ | |
float brightness = emissionColorForRendering.colorValue.maxColorComponent; | |
bool showHelpBox = !HasValidEmissiveKeyword(material); | |
bool showEmissionColorAndGIControls = brightness > 0.0f; | |
bool hadEmissionTexture = emissionMap.textureValue != null; | |
// Texture and HDR color controls | |
m_MaterialEditor.TexturePropertyWithHDRColor(Styles.emissionText, emissionMap, emissionColorForRendering, m_ColorPickerHDRConfig, false); | |
// If texture was assigned and color was black set color to white | |
if (emissionMap.textureValue != null && !hadEmissionTexture && brightness <= 0f) | |
emissionColorForRendering.colorValue = Color.white; | |
// Dynamic Lightmapping mode | |
if (showEmissionColorAndGIControls) | |
{ | |
bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled(emissionColorForRendering.colorValue); | |
using (new EditorGUI.DisabledScope(!shouldEmissionBeEnabled)) | |
{ | |
m_MaterialEditor.LightmapEmissionProperty(MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1); | |
} | |
} | |
if (showHelpBox) | |
{ | |
EditorGUILayout.HelpBox(Styles.emissiveWarning.text, MessageType.Warning); | |
} | |
} | |
void DoSpecularMetallicArea(Material material) | |
{ | |
SpecularMode specularMode = (SpecularMode)material.GetInt("_SpecularMode"); | |
if (specularMode == SpecularMode.Metallic) | |
{ | |
m_MaterialEditor.TexturePropertySingleLine(Styles.maskMapText, maskMap); | |
if (maskMap.textureValue == null) | |
{ | |
m_MaterialEditor.ShaderProperty(metallic, Styles.metallicText); | |
m_MaterialEditor.ShaderProperty(smoothness, Styles.smoothnessText); | |
} | |
} | |
} | |
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; | |
} | |
} | |
static bool ShouldEmissionBeEnabled(Color color) | |
{ | |
return color.maxColorComponent > (0.1f / 255.0f); | |
} | |
static void SetMaterialKeywords(Material material) | |
{ | |
// Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation | |
// (MaterialProperty value might come from renderer material property block) | |
SetKeyword(material, "_ALBEDO", material.GetTexture("_MainTex")); | |
SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap") || material.GetTexture("_DetailNormalMap")); | |
SpecularMode specularMode = (SpecularMode)material.GetInt("_SpecularMode"); | |
SetKeyword(material, "_MASKMAP", material.GetTexture("_MaskMap")); | |
SetKeyword(material, "S_SPECULAR_NONE", specularMode == SpecularMode.None); | |
SetKeyword(material, "S_SPECULAR_METALLIC", specularMode == SpecularMode.Metallic); | |
SetKeyword(material, "_DETAIL_MULX2", material.GetTexture("_DetailAlbedoMap") || material.GetTexture("_DetailNormalMap")); | |
SetKeyword(material, "S_OVERRIDE_LIGHTMAP", material.GetTexture("g_tOverrideLightmap")); | |
SetKeyword(material, "S_UNLIT", material.GetInt("g_bUnlit") == 1); | |
SetKeyword(material, "S_LINEAR_VC", material.GetInt("g_bLinearVC") == 1); | |
SetKeyword(material, "S_RECEIVE_SHADOWS", material.GetInt("g_bReceiveShadows") == 1); | |
SetKeyword(material, "S_WORLD_ALIGNED_TEXTURE", material.GetInt("g_bWorldAlignedTexture") == 1); | |
SetKeyword(material, "_DETAIL1", material.GetTexture("_Detail1Map")); | |
SetKeyword(material, "_DETAIL2", material.GetTexture("_Detail2Map")); | |
SetKeyword(material, "_DETAIL3", material.GetTexture("_Detail3Map")); | |
SetKeyword(material, "_DETAIL4", material.GetTexture("_Detail4Map")); | |
bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled(material.GetColor("_EmissionColor")); | |
SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled); | |
if (material.IsKeywordEnabled("S_RENDER_BACKFACES")) | |
{ | |
material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); | |
} | |
else | |
{ | |
material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Back); | |
} | |
// Setup lightmap emissive flags | |
MaterialGlobalIlluminationFlags flags = material.globalIlluminationFlags; | |
if ((flags & (MaterialGlobalIlluminationFlags.BakedEmissive | MaterialGlobalIlluminationFlags.RealtimeEmissive)) != 0) | |
{ | |
flags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; | |
if (!shouldEmissionBeEnabled) | |
flags |= MaterialGlobalIlluminationFlags.EmissiveIsBlack; | |
material.globalIlluminationFlags = flags; | |
} | |
// World aligned texture constants | |
Vector4 worldAlignedTextureNormal = material.GetVector("g_vWorldAlignedTextureNormal"); | |
Vector3 normal = new Vector3(worldAlignedTextureNormal.x, worldAlignedTextureNormal.y, worldAlignedTextureNormal.z); | |
normal = (normal.sqrMagnitude > 0.0f) ? normal : Vector3.up; | |
Vector3 tangentU = Vector3.zero, tangentV = Vector3.zero; | |
Vector3.OrthoNormalize(ref normal, ref tangentU, ref tangentV); | |
material.SetVector("g_vWorldAlignedNormalTangentU", new Vector4(tangentU.x, tangentU.y, tangentU.z, 0.0f)); | |
material.SetVector("g_vWorldAlignedNormalTangentV", new Vector4(tangentV.x, tangentV.y, tangentV.z, 0.0f)); | |
// Static combo skips | |
if (material.GetInt("g_bUnlit") == 1) | |
{ | |
material.DisableKeyword("_NORMALMAP"); | |
material.EnableKeyword("S_SPECULAR_NONE"); | |
material.DisableKeyword("S_SPECULAR_METALLIC"); | |
material.DisableKeyword("_MASKMAP"); | |
material.DisableKeyword("S_OVERRIDE_LIGHTMAP"); | |
material.DisableKeyword("S_RECEIVE_SHADOWS"); | |
} | |
} | |
bool HasValidEmissiveKeyword(Material material) | |
{ | |
// Material animation might be out of sync with the material keyword. | |
// So if the emission support is disabled on the material, but the property blocks have a value that requires it, then we need to show a warning. | |
// (note: (Renderer MaterialPropertyBlock applies its values to emissionColorForRendering)) | |
bool hasEmissionKeyword = material.IsKeywordEnabled("_EMISSION"); | |
if (!hasEmissionKeyword && ShouldEmissionBeEnabled(emissionColorForRendering.colorValue)) | |
return false; | |
else | |
return true; | |
} | |
static void MaterialChanged(Material material) | |
{ | |
SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode")); | |
SetMaterialKeywords(material); | |
} | |
static void SetKeyword(Material m, string keyword, bool state) | |
{ | |
if (state) | |
m.EnableKeyword(keyword); | |
else | |
m.DisableKeyword(keyword); | |
} | |
} | |
} // namespace UnityEditor | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment