A game about forced loneliness, made by TACStudios
at master 162 lines 5.5 kB view raw
1using System; 2using System.Text; 3using UnityEditor.Graphing; 4using UnityEngine; 5 6namespace UnityEditor.ShaderGraph.Internal 7{ 8 [Serializable] 9 [FormerName("UnityEditor.ShaderGraph.TextureShaderProperty")] 10 [BlackboardInputInfo(50)] 11 public class Texture2DShaderProperty : AbstractShaderProperty<SerializableTexture> 12 { 13 public enum DefaultType { White, Black, Grey, NormalMap, LinearGrey, Red } 14 15 static readonly string[] k_DefaultTypeNames = new string[] 16 { 17 "white", 18 "black", 19 "grey", 20 "bump", 21 "linearGrey", 22 "red", 23 }; 24 25 internal static string ToShaderLabString(DefaultType defaultType) 26 { 27 int index = (int)defaultType; 28 if ((index >= 0) && (index < k_DefaultTypeNames.Length)) 29 return k_DefaultTypeNames[index]; 30 return string.Empty; 31 } 32 33 internal Texture2DShaderProperty() 34 { 35 displayName = "Texture2D"; 36 value = new SerializableTexture(); 37 } 38 39 public override PropertyType propertyType => PropertyType.Texture2D; 40 41 [SerializeField] 42 internal bool isMainTexture = false; 43 44 internal override bool isExposable => true; 45 internal override bool isRenamable => true; 46 47 internal string modifiableTagString => modifiable ? "" : "[NonModifiableTextureData]"; 48 49 [SerializeField] 50 internal bool useTilingAndOffset = false; 51 52 internal string useSTString => useTilingAndOffset ? "" : "[NoScaleOffset]"; 53 internal string mainTextureString => isMainTexture ? "[MainTexture]" : ""; 54 55 internal override string GetPropertyBlockString() 56 { 57 var normalTagString = (defaultType == DefaultType.NormalMap) ? "[Normal]" : ""; 58 return $"{hideTagString}{modifiableTagString}{normalTagString}{mainTextureString}{useSTString}{referenceName}(\"{displayName}\", 2D) = \"{ToShaderLabString(defaultType)}\" {{}}"; 59 } 60 61 // Texture2D properties cannot be set via Hybrid path at the moment; disallow that choice 62 internal override bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.HybridPerInstance) && (decl != HLSLDeclaration.DoNotDeclare); 63 64 internal override void ForeachHLSLProperty(Action<HLSLProperty> action) 65 { 66 HLSLDeclaration decl = (generatePropertyBlock ? HLSLDeclaration.UnityPerMaterial : HLSLDeclaration.Global); 67 68 action(new HLSLProperty(HLSLType._Texture2D, referenceName, HLSLDeclaration.Global)); 69 action(new HLSLProperty(HLSLType._SamplerState, "sampler" + referenceName, HLSLDeclaration.Global)); 70 action(new HLSLProperty(HLSLType._float4, referenceName + "_TexelSize", decl)); 71 if (useTilingAndOffset) 72 { 73 action(new HLSLProperty(HLSLType._float4, referenceName + "_ST", decl)); 74 } 75 } 76 77 internal override string GetPropertyAsArgumentString(string precisionString) 78 { 79 return "UnityTexture2D " + referenceName; 80 } 81 82 internal override string GetPropertyAsArgumentStringForVFX(string precisionString) 83 { 84 return "TEXTURE2D(" + referenceName + ")"; 85 } 86 87 internal override string GetHLSLVariableName(bool isSubgraphProperty, GenerationMode mode) 88 { 89 if (isSubgraphProperty) 90 return referenceName; 91 else 92 { 93 if (useTilingAndOffset) 94 { 95 return $"UnityBuildTexture2DStruct({referenceName})"; 96 } 97 else 98 { 99 return $"UnityBuildTexture2DStructNoScale({referenceName})"; 100 } 101 } 102 } 103 104 [SerializeField] 105 bool m_Modifiable = true; 106 107 internal bool modifiable 108 { 109 get => m_Modifiable; 110 set => m_Modifiable = value; 111 } 112 113 [SerializeField] 114 DefaultType m_DefaultType = DefaultType.White; 115 116 public DefaultType defaultType 117 { 118 get { return m_DefaultType; } 119 set { m_DefaultType = value; } 120 } 121 122 internal override AbstractMaterialNode ToConcreteNode() 123 { 124 return new Texture2DAssetNode { texture = value.texture }; 125 } 126 127 internal override PreviewProperty GetPreviewMaterialProperty() 128 { 129 return new PreviewProperty(propertyType) 130 { 131 name = referenceName, 132 textureValue = value.texture, 133 texture2DDefaultType = defaultType 134 }; 135 } 136 137 internal override ShaderInput Copy() 138 { 139 return new Texture2DShaderProperty() 140 { 141 displayName = displayName, 142 value = value, 143 defaultType = defaultType, 144 useTilingAndOffset = useTilingAndOffset, 145 isMainTexture = isMainTexture 146 }; 147 } 148 149 internal override void OnBeforePasteIntoGraph(GraphData graph) 150 { 151 if (isMainTexture) 152 { 153 Texture2DShaderProperty existingMain = graph.GetMainTexture(); 154 if (existingMain != null && existingMain != this) 155 { 156 isMainTexture = false; 157 } 158 } 159 base.OnBeforePasteIntoGraph(graph); 160 } 161 } 162}