123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- using UnityEngine;
- using UnityEditor;
- using System;
- namespace AmplifyShaderEditor
- {
- public enum VirtualPreset
- {
- Unity_Legacy,
- Unity5,
- Alloy,
- UBER,
- Skyshop,
- Lux
- }
- public enum VirtualChannel
- {
- Albedo = 0,
- Base,
- Normal,
- Height,
- Occlusion,
- Displacement,
- Specular,
- SpecMet,
- Material,
- }
- [Serializable]
- [NodeAttributes( "Virtual Texture Object", "Textures", "Represents a Virtual Texture Asset", SortOrderPriority = 1 )]
- public class VirtualTextureObject : TexturePropertyNode
- {
- protected const string VirtualPresetStr = "Layout Preset";
- protected const string VirtualChannelStr = "Virtual Layer";
- private const string VirtualTextureObjectInfo = "Can only be used alongside a Texture Sample node by connecting to its Tex Input Port.\n" +
- "\nProperty name must match the value set on your Virtual Texture.\n" +
- "Default e.g Albedo = _MainTex\n" +
- "\nName your node according to the respective channel property in your Virtual Texture. The Albedo must be set to _MainTex ( temporary requirement ).";
- private readonly string[] ChannelTypeStr = {
- "Albedo - D.RGBA",
- "Base - D.RGBA",
- "Normal - N.GA",
- "Height - N.B",
- "Occlusion - N.R",
- "Displacement - N.B",
- "Specular - S.RGBA",
- "Specular|Metallic - S.RGBA",
- "Material - S.RGBA",};
- private readonly string[] Dummy = { string.Empty };
- private string[] m_channelTypeStr;
- [SerializeField]
- protected VirtualPreset m_virtualPreset = VirtualPreset.Unity5;
- [SerializeField]
- protected VirtualChannel m_virtualChannel = VirtualChannel.Albedo;
- [SerializeField]
- private int m_selectedChannelInt = 0;
- protected override void CommonInit( int uniqueId )
- {
- base.CommonInit( uniqueId );
- ChangeChannels();
- }
- protected override void OnUniqueIDAssigned()
- {
- base.OnUniqueIDAssigned();
- if ( UniqueId != -1 )
- UIUtils.AddVirtualTextureCount();
- }
- public override void DrawSubProperties()
- {
- ShowDefaults();
- base.DrawSubProperties();
- }
- public override void DrawMaterialProperties()
- {
- ShowDefaults();
- base.DrawMaterialProperties();
- }
- new void ShowDefaults()
- {
- EditorGUI.BeginChangeCheck();
- m_virtualPreset = ( VirtualPreset ) EditorGUILayoutEnumPopup( VirtualPresetStr, m_virtualPreset );
- if ( EditorGUI.EndChangeCheck() )
- {
- ChangeChannels();
- }
- EditorGUI.BeginChangeCheck();
- m_selectedChannelInt = EditorGUILayoutPopup( VirtualChannelStr, m_selectedChannelInt, m_channelTypeStr );
- if ( EditorGUI.EndChangeCheck() )
- {
- m_virtualChannel = GetChannel( m_selectedChannelInt );
- }
- }
- public override void DrawProperties()
- {
- base.DrawProperties();
- EditorGUILayout.HelpBox( VirtualTextureObjectInfo, MessageType.Info );
- }
- private VirtualChannel GetChannel( int popupInt )
- {
- int remapInt = 0;
- switch ( m_virtualPreset )
- {
- case VirtualPreset.Unity_Legacy:
- remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 4 : popupInt == 3 ? 5 : 0;
- break;
- default:
- case VirtualPreset.Unity5:
- case VirtualPreset.UBER:
- remapInt = popupInt == 0 ? 0 : popupInt == 1 ? 7 : popupInt == 2 ? 2 : popupInt == 3 ? 3 : popupInt == 4 ? 4 : 0;
- break;
- case VirtualPreset.Alloy:
- remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 8 : popupInt == 3 ? 3 : 0;
- break;
- case VirtualPreset.Skyshop:
- case VirtualPreset.Lux:
- remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 6 : 0;
- break;
- }
- return ( VirtualChannel ) remapInt;
- }
- private void ChangeChannels()
- {
- m_channelTypeStr = Dummy;
- switch ( m_virtualPreset )
- {
- case VirtualPreset.Unity_Legacy:
- m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 4 ], ChannelTypeStr[ 5 ] };
- break;
- default:
- case VirtualPreset.Unity5:
- case VirtualPreset.UBER:
- m_channelTypeStr = new string[] { ChannelTypeStr[ 0 ], ChannelTypeStr[ 7 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 3 ], ChannelTypeStr[ 4 ] };
- break;
- case VirtualPreset.Alloy:
- m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 8 ], ChannelTypeStr[ 3 ] };
- break;
- case VirtualPreset.Skyshop:
- case VirtualPreset.Lux:
- m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 6 ] };
- break;
- }
- }
- public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
- {
- base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
- dataCollector.AddToProperties( UniqueId, "[HideInInspector] _VTInfoBlock( \"VT( auto )\", Vector ) = ( 0, 0, 0, 0 )", -1 );
- return PropertyName;
- }
- public override string GetPropertyValue()
- {
- string propertyValue = string.Empty;
- switch ( m_virtualChannel )
- {
- default:
- case VirtualChannel.Albedo:
- case VirtualChannel.Base:
- propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
- break;
- case VirtualChannel.Normal:
- propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
- break;
- case VirtualChannel.SpecMet:
- propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
- break;
- }
- return PropertyAttributes + propertyValue;
- }
- public override string GetUniformValue()
- {
- return "uniform sampler2D " + PropertyName + ";";
- }
- public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
- {
- dataType = "sampler2D";
- dataName = PropertyName;
- return true;
- }
- public override void ReadFromString( ref string[] nodeParams )
- {
- base.ReadFromString( ref nodeParams );
- string defaultTextureGUID = GetCurrentParam( ref nodeParams );
- //m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( textureName );
- if( UIUtils.CurrentShaderVersion() > 14101 )
- {
- m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( defaultTextureGUID ) );
- string materialTextureGUID = GetCurrentParam( ref nodeParams );
- m_materialValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( materialTextureGUID ) );
- }
- else
- {
- m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( defaultTextureGUID );
- }
- m_isNormalMap = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
- m_defaultTextureValue = ( TexturePropertyValues ) Enum.Parse( typeof( TexturePropertyValues ), GetCurrentParam( ref nodeParams ) );
- m_autocastMode = ( AutoCastType ) Enum.Parse( typeof( AutoCastType ), GetCurrentParam( ref nodeParams ) );
- m_virtualPreset = ( VirtualPreset ) Enum.Parse( typeof( VirtualPreset ), GetCurrentParam( ref nodeParams ) );
- m_selectedChannelInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
- ChangeChannels();
- m_virtualChannel = GetChannel( m_selectedChannelInt );
- //m_forceNodeUpdate = true;
- //ConfigFromObject( m_defaultValue );
- if( m_materialValue == null )
- {
- ConfigFromObject( m_defaultValue );
- }
- else
- {
- CheckTextureImporter( true, true );
- }
- ConfigureInputPorts();
- ConfigureOutputPorts();
- }
- public override void ReadAdditionalData( ref string[] nodeParams ) { }
- public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
- {
- base.WriteToString( ref nodeInfo, ref connectionsInfo );
- //IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.GetAssetPath( m_defaultValue ) : Constants.NoStringValue );
- IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_defaultValue ) ) : Constants.NoStringValue );
- IOUtils.AddFieldValueToString( ref nodeInfo, ( m_materialValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_materialValue ) ) : Constants.NoStringValue );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_isNormalMap.ToString() );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultTextureValue );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_autocastMode );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_virtualPreset );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedChannelInt );
- }
- public override void WriteAdditionalToString( ref string nodeInfo, ref string connectionsInfo ) { }
- //public override string PropertyName
- //{
- // get
- // {
- // string propertyName = string.Empty;
- // switch ( m_virtualChannel )
- // {
- // default:
- // case VirtualChannel.Albedo:
- // case VirtualChannel.Base:
- // propertyName = "_MainTex";
- // break;
- // case VirtualChannel.Normal:
- // propertyName = "_BumpMap";
- // break;
- // case VirtualChannel.SpecMet:
- // propertyName = "_MetallicGlossMap";
- // break;
- // case VirtualChannel.Occlusion:
- // propertyName = "_OcclusionMap";
- // break;
- // }
- // return propertyName;
- // }
- //}
- public override void Destroy()
- {
- base.Destroy();
- UIUtils.RemoveVirtualTextureCount();
- }
- public override bool IsNormalMap { get { return m_isNormalMap || m_virtualChannel == VirtualChannel.Normal; } }
- public VirtualChannel Channel { get { return m_virtualChannel; } }
- }
- }
|