A game about forced loneliness, made by TACStudios
at master 3.9 kB view raw
1using System; 2using System.Collections.Generic; 3using UnityEditor.Graphing; 4using UnityEditor.ShaderGraph.Drawing.Slots; 5using UnityEditor.ShaderGraph.Internal; 6using UnityEngine; 7using UnityEngine.UIElements; 8 9namespace UnityEditor.ShaderGraph 10{ 11 [Serializable] 12 [HasDependencies(typeof(MinimalTexture2DInputMaterialSlot))] 13 class Texture2DInputMaterialSlot : Texture2DMaterialSlot 14 { 15 [SerializeField] 16 private SerializableTexture m_Texture = new SerializableTexture(); 17 18 [SerializeField] 19 private Texture2DShaderProperty.DefaultType m_DefaultType = Texture2DShaderProperty.DefaultType.White; 20 21 public Texture texture 22 { 23 get { return m_Texture.texture; } 24 set { m_Texture.texture = value; } 25 } 26 27 public Texture2DShaderProperty.DefaultType defaultType 28 { 29 get { return m_DefaultType; } 30 set { m_DefaultType = value; } 31 } 32 33 public override bool isDefaultValue => texture == null; 34 35 public Texture2DInputMaterialSlot() 36 { } 37 38 public Texture2DInputMaterialSlot( 39 int slotId, 40 string displayName, 41 string shaderOutputName, 42 ShaderStageCapability stageCapability = ShaderStageCapability.All, 43 bool hidden = false) 44 : base(slotId, displayName, shaderOutputName, SlotType.Input, stageCapability, hidden) 45 { } 46 47 public override VisualElement InstantiateControl() 48 { 49 return new TextureSlotControlView(this); 50 } 51 52 public override string GetDefaultValue(GenerationMode generationMode) 53 { 54 var nodeOwner = owner as AbstractMaterialNode; 55 if (nodeOwner == null) 56 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); 57 58 return $"UnityBuildTexture2DStructNoScale({nodeOwner.GetVariableNameForSlot(id)})"; 59 } 60 61 public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) 62 { 63 var nodeOwner = owner as AbstractMaterialNode; 64 if (nodeOwner == null) 65 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); 66 67 var prop = new Texture2DShaderProperty(); 68 prop.overrideReferenceName = nodeOwner.GetVariableNameForSlot(id); 69 prop.modifiable = false; 70 prop.generatePropertyBlock = true; 71 prop.value.texture = texture; 72 prop.defaultType = defaultType; 73 properties.AddShaderProperty(prop); 74 } 75 76 public override void GetPreviewProperties(List<PreviewProperty> properties, string name) 77 { 78 var pp = new PreviewProperty(PropertyType.Texture2D) 79 { 80 name = name, 81 textureValue = texture, 82 texture2DDefaultType = defaultType 83 }; 84 properties.Add(pp); 85 } 86 87 public override void CopyValuesFrom(MaterialSlot foundSlot) 88 { 89 var slot = foundSlot as Texture2DInputMaterialSlot; 90 if (slot != null) 91 { 92 m_Texture = slot.m_Texture; 93 bareResource = slot.bareResource; 94 } 95 } 96 } 97 98 class MinimalTexture2DInputMaterialSlot : IHasDependencies 99 { 100 [SerializeField] 101 private SerializableTexture m_Texture = null; 102 103 public void GetSourceAssetDependencies(AssetCollection assetCollection) 104 { 105 var guidString = m_Texture.guid; 106 if (!string.IsNullOrEmpty(guidString) && GUID.TryParse(guidString, out var guid)) 107 { 108 assetCollection.AddAssetDependency(guid, AssetCollection.Flags.IncludeInExportPackage); 109 } 110 } 111 } 112}