public enum SurfaceType
{
Opaque ,
Cutout ,
Transparent
}
public enum BlendMode
Premultiply
}
public enum SmoothnessMapChannel
{
MaskAlpha ,
AlbedoAlpha ,
}
public enum EmissiveColorMode
{
UseEmissiveColor ,
UseEmissiveMask ,
}
public enum DoubleSidedMode
{
None ,
DoubleSided ,
DoubleSidedLightingFlip ,
DoubleSidedLightingMirror ,
}
public static string SurfaceTypeText = "Surface Type" ;
public static string BlendModeText = "Blend Mode" ;
public static GUIContent alphaCutoffEnableText = new GUIContent ( "Alpha Cutoff Enable" , "Threshold for alpha cutoff" ) ;
public static GUIContent doubleSidedText = new GUIContent ( "Double Sided" , "This will render the two face of the objects (disable backface culling)" ) ;
public static GUIContent doubleSidedLightingText = new GUIContent ( "Double Sided Lighting" , "Enable lighting on both side of the objects (flip normal)" ) ;
public static GUIContent doubleSidedModeText = new GUIContent ( "Double Sided" , "This will render the two face of the objects (disable backface culling)" ) ;
public static readonly string [ ] surfaceTypeNames = Enum . GetNames ( typeof ( SurfaceType ) ) ;
public static readonly string [ ] blendModeNames = Enum . GetNames ( typeof ( BlendMode ) ) ;
public static GUIContent baseColorText = new GUIContent ( "Base Color" , "Base Color scale factor" ) ;
public static GUIContent baseColorMapText = new GUIContent ( "Base Color Map" , "Albedo (RGB) and Transparency (A)" ) ;
public static GUIContent ambientOcclusionText = new GUIContent ( "Ambient Occlusion" , "Ambient Occlusion (R)" ) ;
public static string InputsOptionsText = "Inputs options" ;
public static GUIContent smoothnessMapChannelText = new GUIContent ( "Smoothness Source" , "Smoothness texture and channel" ) ;
public static GUIContent emissiveColorModeText = new GUIContent ( "Emissive Color Usage" , "Use emissive color or emissive mask" ) ;
public static string InputsText = "Inputs" ;
public static string InputsMapText = "" ;
public static GUIContent baseColorText = new GUIContent ( "Base Color" , "Albedo (RGB) and Smoothness (A)" ) ;
public static GUIContent baseColorSmoothnessText = new GUIContent ( "Base Color + Smoothness" , "Albedo (RGB) and Smoothness (A)" ) ;
public static GUIContent mettalicMapText = new GUIContent ( "Mettalic Map" , "Mettalic Map" ) ;
public static GUIContent smoothnessMapText = new GUIContent ( "Smoothness Map" , "Base Color scale factor" ) ;
public static GUIContent specularOcclusionMapText = new GUIContent ( "Specular Occlusion Map" , "Specular Occlusion Map" ) ;
public static GUIContent maskMapESText = new GUIContent ( "Mask Map - M(R), AO(G), E(B), S(A)" , "Mask map" ) ;
public static GUIContent maskMapEText = new GUIContent ( "Mask Map - M(R), AO(G), E(B)" , "Mask map" ) ;
public static GUIContent maskMapText = new GUIContent ( "Mask Map - M(R), AO(G)" , "Mask map" ) ;
public static GUIContent maskMapSText = new GUIContent ( "Mask Map - M(R), AO(G), S(A)" , "Mask map" ) ;
public static GUIContent specularOcclusionMapText = new GUIContent ( "Specular Occlusion Map (RGBA)" , "Specular Occlusion Map" ) ;
public static GUIContent heightScaleText = new GUIContent ( "Height Scale" , "eight Map" ) ;
public static GUIContent heightBiasText = new GUIContent ( "Height Bias" , "eight Map" ) ;
public static GUIContent emissiveColorMapText = new GUIContent ( "Emissive Color Map" , "Emissive" ) ;
public static string SurfaceTypeText = "Surface Type" ;
public static string BlendModeText = "Blend Mode" ;
public static string InputsText = "Inputs" ;
public static readonly string [ ] surfaceTypeNames = Enum . GetNames ( typeof ( SurfaceType ) ) ;
public static readonly string [ ] blendModeNames = Enum . GetNames ( typeof ( BlendMode ) ) ;
MaterialProperty doubleSided = null ;
MaterialProperty doubleSidedLighting = null ;
MaterialProperty alphaCutoffEnable = null ;
MaterialProperty doubleSidedMode = null ;
MaterialProperty smoothnessMapChannel = null ;
MaterialProperty emissiveColorMode = null ;
MaterialProperty ambientOcclusionMap = null ;
MaterialProperty mettalicMap = null ;
MaterialProperty smoothnessMap = null ;
MaterialProperty maskMap = null ;
MaterialProperty specularOcclusionMap = null ;
MaterialProperty normalMap = null ;
MaterialProperty heightMap = null ;
surfaceType = FindProperty ( "_SurfaceType" , props ) ;
blendMode = FindProperty ( "_BlendMode" , props ) ;
alphaCutoff = FindProperty ( "_Cutoff" , props ) ;
doubleSided = FindProperty ( "_DoubleSided " , props ) ;
doubleSidedLighting = FindProperty ( "_DoubleSidedLigthing " , props ) ;
alphaCutoffEnable = FindProperty ( "_AlphaCutoffEnable " , props ) ;
doubleSidedMode = FindProperty ( "_DoubleSidedMode " , props ) ;
smoothnessMapChannel = FindProperty ( "_SmoothnessTextureChannel" , props ) ;
emissiveColorMode = FindProperty ( "_EmissiveColorMode" , props ) ;
ambientOcclusionMap = FindProperty ( "_AmbientOcclusionMap" , props ) ;
mettalicMap = FindProperty ( "_MettalicMap" , props ) ;
smoothness Map = FindProperty ( "_Smoothness Map" , props ) ;
mask Map = FindProperty ( "_Mask Map" , props ) ;
specularOcclusionMap = FindProperty ( "_SpecularOcclusionMap" , props ) ;
normalMap = FindProperty ( "_NormalMap" , props ) ;
heightMap = FindProperty ( "_HeightMap" , props ) ;
{
GUILayout . Label ( Styles . OptionText , EditorStyles . boldLabel ) ;
SurfaceTypePopup ( ) ;
BlendModePopup ( ) ;
m_MaterialEditor . ShaderProperty ( alphaCutoffEnable , Styles . alphaCutoffEnableText . text ) ;
if ( alphaCutoffEnable . floatValue = = 1.0 )
{
m_MaterialEditor . ShaderProperty ( alphaCutoff , Styles . alphaCutoffText . text ) ;
}
if ( ( SurfaceType ) surfaceType . floatValue = = SurfaceType . Transparent )
{
BlendModePopup ( ) ;
}
m_MaterialEditor . ShaderProperty ( doubleSidedMode , Styles . doubleSidedModeText . text ) ;
GUILayout . Label ( Styles . InputsOptionsText , EditorStyles . boldLabel ) ;
m_MaterialEditor . ShaderProperty ( smoothnessMapChannel , Styles . smoothnessMapChannelText . text ) ;
m_MaterialEditor . ShaderProperty ( emissiveColorMode , Styles . emissiveColorModeText . text ) ;
bool isAlbedoAlpha = ( SmoothnessMapChannel ) smoothnessMapChannel . floatValue = = SmoothnessMapChannel . AlbedoAlpha ;
bool useEmissiveMask = ( EmissiveColorMode ) emissiveColorMode . floatValue = = EmissiveColorMode . UseEmissiveMask ;
m_MaterialEditor . TexturePropertySingleLine ( isAlbedoAlpha ? Styles . baseColorSmoothnessText : Styles . baseColorText , baseColorMap , baseColor ) ;
m_MaterialEditor . ShaderProperty ( mettalic , Styles . mettalicText ) ;
m_MaterialEditor . ShaderProperty ( smoothness , Styles . smoothnessText ) ;
/ *
m_MaterialEditor . TexturePropertySingleLine ( Styles . normalMapText , bumpMap , bumpMap . textureValue ! = null ? bumpScale : null ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . heightMapText , heightMap , heightMap . textureValue ! = null ? heigtMapScale : null ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . occlusionText , occlusionMap , occlusionMap . textureValue ! = null ? occlusionStrength : null ) ;
DoEmissionArea ( material ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . detailMaskText , detailMask ) ;
EditorGUI . BeginChangeCheck ( ) ;
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 ( isAlbedoAlpha & & useEmissiveMask )
m_MaterialEditor . TexturePropertySingleLine ( Styles . maskMapESText , maskMap ) ;
else if ( useEmissiveMask )
m_MaterialEditor . TexturePropertySingleLine ( Styles . maskMapEText , maskMap ) ;
else if ( isAlbedoAlpha )
m_MaterialEditor . TexturePropertySingleLine ( Styles . maskMapSText , maskMap ) ;
else
m_MaterialEditor . TexturePropertySingleLine ( Styles . maskMapText , maskMap ) ;
EditorGUILayout . Space ( ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . specularOcclusionMapText , specularOcclusionMap ) ;
// Secondary properties
GUILayout . Label ( Styles . secondaryMapsText , EditorStyles . boldLabel ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . detailAlbedoText , detailAlbedoMap ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . detailNormalMapText , detailNormalMap , detailNormalMapScale ) ;
m_MaterialEditor . TextureScaleOffsetProperty ( detailAlbedoMap ) ;
m_MaterialEditor . ShaderProperty ( uvSetSecondary , Styles . uvSetLabel . text ) ;
m_MaterialEditor . TexturePropertySingleLine ( Styles . normalMapText , normalMap ) ;
// Third properties
m_MaterialEditor . TexturePropertySingleLine ( Styles . heightMapText , heightMap , heightScale , heightBias ) ;
if ( highlights ! = null )
m_MaterialEditor . ShaderProperty ( highlights , Styles . highlightsText ) ;
if ( reflections ! = null )
m_MaterialEditor . ShaderProperty ( reflections , Styles . reflectionsText ) ;
* /
if ( ! useEmissiveMask )
{
m_MaterialEditor . TexturePropertySingleLine ( Styles . emissiveText , emissiveColorMap , emissiveColor ) ;
}
m_MaterialEditor . ShaderProperty ( emissiveIntensity , Styles . emissiveIntensityText ) ;
//foreach (var obj in blendMode.targets)
// MaterialChanged((Material)obj, m_WorkflowMode);
foreach ( var obj in blendMode . targets )
MaterialChanged ( ( Material ) obj ) ;
}
}
EditorGUI . showMixedValue = false ;
}
public static void SetupMaterialWithBlendMode ( Material material , SurfaceType surfaceType , BlendMode blendMode )
public void SetupMaterialWithBlendMode ( Material material , bool alphaTestEnable , SurfaceType surfaceType , BlendMode blendMode , DoubleSidedMode doubleSidedMode )
if ( alphaTestEnable )
material . EnableKeyword ( "_ALPHATEST_ON" ) ;
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 ;
}
else if ( surfaceType = = SurfaceType . Cutout )
{
material . SetOverrideTag ( "RenderType" , "TransparentCutout" ) ;
material . SetOverrideTag ( "RenderType" , alphaTestEnable ? "TransparentCutout" : "" ) ;
material . EnableKeyword ( "_ALPHATEST_ON" ) ;
material . DisableKeyword ( "_ALPHABLEND_ON" ) ;
material . DisableKeyword ( "_ALPHAPREMULTIPLY_ON" ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . AlphaTest ;
material . renderQueue = alphaTestEnable ? ( int ) UnityEngine . Rendering . RenderQueue . AlphaTest : - 1 ;
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
material . SetInt ( "_ZWrite" , 0 ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . Transparent ;
case BlendMode . Lerp :
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
case BlendMode . Lerp :
material . SetInt ( "_ZWrite" , 0 ) ;
material . DisableKeyword ( "_ALPHATEST_ON" ) ;
material . EnableKeyword ( "_ALPHABLEND_ON" ) ;
material . DisableKeyword ( "_ALPHAPREMULTIPLY_ON" ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . Transparent ;
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
material . SetInt ( "_ZWrite" , 0 ) ;
material . DisableKeyword ( "_ALPHATEST_ON" ) ;
material . DisableKeyword ( "_ALPHABLEND_ON" ) ;
material . DisableKeyword ( "_ALPHAPREMULTIPLY_ON" ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . Transparent ;
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
material . SetInt ( "_ZWrite" , 0 ) ;
material . DisableKeyword ( "_ALPHATEST_ON" ) ;
material . DisableKeyword ( "_ALPHABLEND_ON" ) ;
material . DisableKeyword ( "_ALPHAPREMULTIPLY_ON" ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . Transparent ;
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
material . SetInt ( "_ZWrite" , 0 ) ;
material . DisableKeyword ( "_ALPHATEST_ON" ) ;
material . DisableKeyword ( "_ALPHABLEND_ON" ) ;
material . DisableKeyword ( "_ALPHAPREMULTIPLY_ON" ) ;
material . renderQueue = ( int ) UnityEngine . Rendering . RenderQueue . Transparent ;
material . SetOverrideTag ( "RenderType" , "Transparent" ) ;
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 ;
material . SetInt ( "_DstBlend" , ( int ) UnityEngine . Rendering . BlendMode . OneMinusSrcAlpha ) ;
if ( doubleSidedMode = = DoubleSidedMode . DoubleSided )
{
material . SetInt ( "_CullMode" , ( int ) UnityEngine . Rendering . CullMode . Off ) ;
}
else if ( doubleSidedMode = = DoubleSidedMode . DoubleSided )
{
material . SetInt ( "_CullMode" , ( int ) UnityEngine . Rendering . CullMode . Off ) ;
material . EnableKeyword ( "_DOUBLESIDED_LIGHTING_FLIP" ) ;
}
else
{
material . SetInt ( "_CullMode" , ( int ) UnityEngine . Rendering . CullMode . Off ) ;
material . EnableKeyword ( "_DOUBLESIDED_LIGHTING_MIRROR" ) ;
}
}
static bool ShouldEmissionBeEnabled ( Material mat , Color color )
return false ;
}
static void SetMaterialKeywords ( Material material )
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 , "_NORMALMAP" , material . GetTexture ( "_BumpMap" ) | | material . GetTexture ( "_DetailNormalMap" ) ) ;
if ( workflowMode = = WorkflowMode . Specular )
SetKeyword ( material , "_SPECGLOSSMAP" , material . GetTexture ( "_SpecGlossMap" ) ) ;
else if ( workflowMode = = WorkflowMode . Metallic )
SetKeyword ( material , "_METALLICGLOSSMAP" , material . GetTexture ( "_MetallicGlossMap" ) ) ;
SetKeyword ( material , "_PARALLAXMAP" , material . GetTexture ( "_ParallaxMap" ) ) ;
SetKeyword ( material , "_DETAIL_MULX2" , material . GetTexture ( "_DetailAlbedoMap" ) | | material . GetTexture ( "_DetailNormalMap" ) ) ;
bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled ( material , material . GetColor ( "_EmissionColor" ) ) ;
SetKeyword ( material , "_EMISSION" , shouldEmissionBeEnabled ) ;
if ( material . HasProperty ( "_SmoothnessTextureChannel" ) )
{
SetKeyword ( material , "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A" , GetSmoothnessMapChannel ( material ) = = SmoothnessMapChannel . AlbedoAlpha ) ;
}
// 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 , "_NORMALMAP" , material . GetTexture ( "_NormalMap" ) ) ;
SetKeyword ( material , "_MASKMAP" , material . GetTexture ( "_MaskMap" ) ) ;
SetKeyword ( material , "_SPECULAROCCLUSIONMAP" , material . GetTexture ( "_SpecularOcclusionMap" ) ) ;
SetKeyword ( material , "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A" , ( ( SmoothnessMapChannel ) material . GetFloat ( "_SmoothnessTextureChannel" ) ) = = SmoothnessMapChannel . AlbedoAlpha ) ;
SetKeyword ( material , "_EMISSIVE_COLOR" , ( ( EmissiveColorMode ) material . GetFloat ( "_EmissiveColorMode" ) ) = = EmissiveColorMode . UseEmissiveColor ) ;
/ *
// Setup lightmap emissive flags
MaterialGlobalIlluminationFlags flags = material . globalIlluminationFlags ;
if ( ( flags & ( MaterialGlobalIlluminationFlags . BakedEmissive | MaterialGlobalIlluminationFlags . RealtimeEmissive ) ) ! = 0 )
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))
else
return true ;
* /
* /
return true ;
return true ;
static void MaterialChanged ( Material material )
void MaterialChanged ( Material material )
SetupMaterialWithBlendMode ( material , ( SurfaceType ) material . GetFloat ( "_SurfaceType" ) , ( BlendMode ) material . GetFloat ( "_BlendMode" ) ) ;
SetupMaterialWithBlendMode ( material , alphaCutoffEnable . floatValue = = 1.0 , ( SurfaceType ) surfaceType . floatValue , ( BlendMode ) blendMode . floatValue , ( DoubleSidedMode ) doubleSidedMode . floatValue ) ;
static void SetKeyword ( Material m , string keyword , bool state )
void SetKeyword ( Material m , string keyword , bool state )
{
if ( state )
m . EnableKeyword ( keyword ) ;