A game about forced loneliness, made by TACStudios
at master 6.6 kB view raw
1using System; 2using System.Collections.Generic; 3using System.Globalization; 4using System.Text; 5using System.Linq; 6using UnityEditor.Graphing; 7using UnityEngine; 8 9namespace UnityEditor.ShaderGraph.Internal 10{ 11 [Serializable] 12 [FormerName("UnityEditor.ShaderGraph.FloatShaderProperty")] 13 [FormerName("UnityEditor.ShaderGraph.Vector1ShaderProperty")] 14 [BlackboardInputInfo(0, "Float")] 15 public sealed class Vector1ShaderProperty : AbstractShaderProperty<float> 16 { 17 internal Vector1ShaderProperty() 18 { 19 displayName = "Float"; 20 } 21 22 public override PropertyType propertyType => PropertyType.Float; 23 24 internal override bool isExposable => true; 25 internal override bool isRenamable => true; 26 27 public override float value 28 { 29 get 30 { 31 if (floatType == FloatType.Integer) 32 { 33 return (int)base.value; 34 } 35 36 return base.value; 37 } 38 39 set => base.value = value; 40 } 41 42 string enumTagString 43 { 44 get 45 { 46 switch (enumType) 47 { 48 case EnumType.CSharpEnum: 49 return $"[Enum({m_CSharpEnumType.ToString()})]"; 50 case EnumType.KeywordEnum: 51 return $"[KeywordEnum({string.Join(", ", enumNames)})]"; 52 default: 53 string enumValuesString = ""; 54 for (int i = 0; i < enumNames.Count; i++) 55 { 56 int value = (i < enumValues.Count) ? enumValues[i] : i; 57 enumValuesString += (enumNames[i] + ", " + value + ((i != enumNames.Count - 1) ? ", " : "")); 58 } 59 return $"[Enum({enumValuesString})]"; 60 } 61 } 62 } 63 64 internal override string GetHLSLVariableName(bool isSubgraphProperty, GenerationMode mode) 65 { 66 HLSLDeclaration decl = GetDefaultHLSLDeclaration(); 67 if (decl == HLSLDeclaration.HybridPerInstance) 68 return $"UNITY_ACCESS_HYBRID_INSTANCED_PROP({referenceName}, {concretePrecision.ToShaderString()})"; 69 else 70 return base.GetHLSLVariableName(isSubgraphProperty, mode); 71 } 72 73 internal override string GetPropertyBlockString() 74 { 75 string valueString = NodeUtils.FloatToShaderValueShaderLabSafe(value); 76 77 switch (floatType) 78 { 79 case FloatType.Slider: 80 return $"{hideTagString}{referenceName}(\"{displayName}\", Range({NodeUtils.FloatToShaderValueShaderLabSafe(m_RangeValues.x)}, {NodeUtils.FloatToShaderValueShaderLabSafe(m_RangeValues.y)})) = {valueString}"; 81 case FloatType.Integer: 82 return $"{hideTagString}{referenceName}(\"{displayName}\", Int) = {((int)value).ToString(CultureInfo.InvariantCulture)}"; 83 case FloatType.Enum: 84 return $"{hideTagString}{enumTagString}{referenceName}(\"{displayName}\", Float) = {valueString}"; 85 default: 86 return $"{hideTagString}{referenceName}(\"{displayName}\", Float) = {valueString}"; 87 } 88 } 89 90 internal override string GetPropertyAsArgumentString(string precisionString) 91 { 92 return $"{concreteShaderValueType.ToShaderString(precisionString)} {referenceName}"; 93 } 94 95 internal override void ForeachHLSLProperty(Action<HLSLProperty> action) 96 { 97 HLSLDeclaration decl = GetDefaultHLSLDeclaration(); 98 action(new HLSLProperty(HLSLType._float, referenceName, decl, concretePrecision)); 99 } 100 101 [SerializeField] 102 FloatType m_FloatType = FloatType.Default; 103 104 public FloatType floatType 105 { 106 get => m_FloatType; 107 set => m_FloatType = value; 108 } 109 110 [SerializeField] 111 Vector2 m_RangeValues = new Vector2(0, 1); 112 113 public Vector2 rangeValues 114 { 115 get => m_RangeValues; 116 set => m_RangeValues = value; 117 } 118 119 EnumType m_EnumType = EnumType.Enum; 120 121 public EnumType enumType 122 { 123 get => m_EnumType; 124 set => m_EnumType = value; 125 } 126 127 Type m_CSharpEnumType; 128 129 public Type cSharpEnumType 130 { 131 get => m_CSharpEnumType; 132 set => m_CSharpEnumType = value; 133 } 134 135 List<string> m_EnumNames = new List<string>(); 136 137 public List<string> enumNames 138 { 139 get => m_EnumNames; 140 set => m_EnumNames = value; 141 } 142 143 List<int> m_EnumValues = new List<int>(); 144 145 public List<int> enumValues 146 { 147 get => m_EnumValues; 148 set => m_EnumValues = value; 149 } 150 151 internal override AbstractMaterialNode ToConcreteNode() 152 { 153 switch (m_FloatType) 154 { 155 case FloatType.Slider: 156 return new SliderNode { value = new Vector3(value, m_RangeValues.x, m_RangeValues.y) }; 157 case FloatType.Integer: 158 return new IntegerNode { value = (int)value }; 159 default: 160 var node = new Vector1Node(); 161 node.FindInputSlot<Vector1MaterialSlot>(Vector1Node.InputSlotXId).value = value; 162 return node; 163 } 164 } 165 166 internal override PreviewProperty GetPreviewMaterialProperty() 167 { 168 return new PreviewProperty(propertyType) 169 { 170 name = referenceName, 171 floatValue = value 172 }; 173 } 174 175 internal override ShaderInput Copy() 176 { 177 return new Vector1ShaderProperty() 178 { 179 displayName = displayName, 180 value = value, 181 floatType = floatType, 182 rangeValues = rangeValues, 183 enumType = enumType, 184 enumNames = enumNames, 185 enumValues = enumValues, 186 }; 187 } 188 189 public override int latestVersion => 1; 190 public override void OnAfterDeserialize(string json) 191 { 192 if (sgVersion == 0) 193 { 194 LegacyShaderPropertyData.UpgradeToHLSLDeclarationOverride(json, this); 195 ChangeVersion(1); 196 } 197 } 198 } 199 200 public enum FloatType { Default, Slider, Integer, Enum } 201 202 public enum EnumType { Enum, CSharpEnum, KeywordEnum, } 203}