A game about forced loneliness, made by TACStudios
1using System; 2using System.Runtime.InteropServices; 3using UnityEngine; 4using UnityEditor.ShaderGraph.Internal; 5 6namespace UnityEditor.ShaderGraph 7{ 8 struct PreviewProperty 9 { 10 public string name { get; set; } 11 public PropertyType propType { get; private set; } 12 13 public PreviewProperty(PropertyType type) : this() 14 { 15 propType = type; 16 } 17 18 [StructLayout(LayoutKind.Explicit)] 19 struct ClassData 20 { 21 [FieldOffset(0)] 22 public Texture textureValue; 23 [FieldOffset(0)] 24 public Cubemap cubemapValue; 25 [FieldOffset(0)] 26 public Gradient gradientValue; 27 [FieldOffset(0)] 28 public VirtualTextureShaderProperty vtProperty; 29 } 30 31 [StructLayout(LayoutKind.Explicit)] 32 struct StructData 33 { 34 [FieldOffset(0)] 35 public Color colorValue; 36 [FieldOffset(0)] 37 public Vector4 vector4Value; 38 [FieldOffset(0)] 39 public float floatValue; 40 [FieldOffset(0)] 41 public bool booleanValue; 42 [FieldOffset(0)] 43 public Matrix4x4 matrixValue; 44 } 45 46 ClassData m_ClassData; 47 StructData m_StructData; 48 Texture2DShaderProperty.DefaultType m_texture2dDefaultType; 49 50 public Color colorValue 51 { 52 get 53 { 54 if (propType != PropertyType.Color) 55 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Color, propType)); 56 return m_StructData.colorValue; 57 } 58 set 59 { 60 if (propType != PropertyType.Color) 61 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Color, propType)); 62 m_StructData.colorValue = value; 63 } 64 } 65 66 public Texture textureValue 67 { 68 get 69 { 70 if (propType != PropertyType.Texture2D && propType != PropertyType.Texture2DArray && propType != PropertyType.Texture3D) 71 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Texture2D, propType)); 72 return m_ClassData.textureValue; 73 } 74 set 75 { 76 if (propType != PropertyType.Texture2D && propType != PropertyType.Texture2DArray && propType != PropertyType.Texture3D) 77 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Texture2D, propType)); 78 m_ClassData.textureValue = value; 79 } 80 } 81 82 public Texture2DShaderProperty.DefaultType texture2DDefaultType 83 { 84 get 85 { 86 if (propType != PropertyType.Texture2D) 87 throw new ArgumentException(string.Format(k_GetErrorMessage, "Texture2DShaderProperty.DefaultType", propType)); 88 return m_texture2dDefaultType; 89 } 90 set 91 { 92 if (propType != PropertyType.Texture2D) 93 throw new ArgumentException(string.Format(k_GetErrorMessage, "Texture2DShaderProperty.DefaultType", propType)); 94 m_texture2dDefaultType = value; 95 } 96 } 97 98 public Cubemap cubemapValue 99 { 100 get 101 { 102 if (propType != PropertyType.Cubemap) 103 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Cubemap, propType)); 104 return m_ClassData.cubemapValue; 105 } 106 set 107 { 108 if (propType != PropertyType.Cubemap) 109 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Cubemap, propType)); 110 m_ClassData.cubemapValue = value; 111 } 112 } 113 114 public Gradient gradientValue 115 { 116 get 117 { 118 if (propType != PropertyType.Gradient) 119 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Gradient, propType)); 120 return m_ClassData.gradientValue; 121 } 122 set 123 { 124 if (propType != PropertyType.Gradient) 125 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Gradient, propType)); 126 m_ClassData.gradientValue = value; 127 } 128 } 129 130 public VirtualTextureShaderProperty vtProperty 131 { 132 get 133 { 134 if (propType != PropertyType.VirtualTexture) 135 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Gradient, propType)); 136 return m_ClassData.vtProperty; 137 } 138 set 139 { 140 if (propType != PropertyType.VirtualTexture) 141 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Gradient, propType)); 142 m_ClassData.vtProperty = value; 143 } 144 } 145 146 public Vector4 vector4Value 147 { 148 get 149 { 150 if (propType != PropertyType.Vector2 && propType != PropertyType.Vector3 && propType != PropertyType.Vector4) 151 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Vector4, propType)); 152 return m_StructData.vector4Value; 153 } 154 set 155 { 156 if (propType != PropertyType.Vector2 && propType != PropertyType.Vector3 && propType != PropertyType.Vector4 157 && propType != PropertyType.Matrix2 && propType != PropertyType.Matrix3 && propType != PropertyType.Matrix4) 158 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Vector4, propType)); 159 m_StructData.vector4Value = value; 160 } 161 } 162 163 public float floatValue 164 { 165 get 166 { 167 if (propType != PropertyType.Float) 168 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Float, propType)); 169 return m_StructData.floatValue; 170 } 171 set 172 { 173 if (propType != PropertyType.Float) 174 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Float, propType)); 175 m_StructData.floatValue = value; 176 } 177 } 178 179 public bool booleanValue 180 { 181 get 182 { 183 if (propType != PropertyType.Boolean) 184 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Boolean, propType)); 185 return m_StructData.booleanValue; 186 } 187 set 188 { 189 if (propType != PropertyType.Boolean) 190 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Boolean, propType)); 191 m_StructData.booleanValue = value; 192 } 193 } 194 195 public Matrix4x4 matrixValue 196 { 197 get 198 { 199 if (propType != PropertyType.Matrix2 && propType != PropertyType.Matrix3 && propType != PropertyType.Matrix4) 200 throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Boolean, propType)); 201 return m_StructData.matrixValue; 202 } 203 set 204 { 205 if (propType != PropertyType.Matrix2 && propType != PropertyType.Matrix3 && propType != PropertyType.Matrix4) 206 throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Boolean, propType)); 207 m_StructData.matrixValue = value; 208 } 209 } 210 211 const string k_SetErrorMessage = "Cannot set a {0} property on a PreviewProperty with type {1}."; 212 const string k_GetErrorMessage = "Cannot get a {0} property on a PreviewProperty with type {1}."; 213 214 public void SetValueOnMaterialPropertyBlock(MaterialPropertyBlock mat) 215 { 216 if ((propType == PropertyType.Texture2D || propType == PropertyType.Texture2DArray || propType == PropertyType.Texture3D)) 217 { 218 if (m_ClassData.textureValue == null) 219 { 220 // there's no way to set the texture back to NULL 221 // and no way to delete the property either 222 // so instead we set the value to what we know the default will be 223 // (all textures in ShaderGraph default to white) 224 switch (m_texture2dDefaultType) 225 { 226 case Texture2DShaderProperty.DefaultType.White: 227 mat.SetTexture(name, Texture2D.whiteTexture); 228 break; 229 case Texture2DShaderProperty.DefaultType.Black: 230 mat.SetTexture(name, Texture2D.blackTexture); 231 break; 232 case Texture2DShaderProperty.DefaultType.Grey: 233 mat.SetTexture(name, Texture2D.grayTexture); 234 break; 235 case Texture2DShaderProperty.DefaultType.NormalMap: 236 mat.SetTexture(name, Texture2D.normalTexture); 237 break; 238 case Texture2DShaderProperty.DefaultType.LinearGrey: 239 mat.SetTexture(name, Texture2D.linearGrayTexture); 240 break; 241 case Texture2DShaderProperty.DefaultType.Red: 242 mat.SetTexture(name, Texture2D.redTexture); 243 break; 244 } 245 } 246 else 247 mat.SetTexture(name, m_ClassData.textureValue); 248 } 249 else if (propType == PropertyType.Cubemap) 250 { 251 if (m_ClassData.cubemapValue == null) 252 { 253 // there's no way to set the texture back to NULL 254 // and no way to delete the property either 255 // so instead we set the value to what we know the default will be 256 // (all textures in ShaderGraph default to white) 257 // there's no Cubemap.whiteTexture, but this seems to work 258 mat.SetTexture(name, Texture2D.whiteTexture); 259 } 260 else 261 mat.SetTexture(name, m_ClassData.cubemapValue); 262 } 263 else if (propType == PropertyType.Color) 264 mat.SetColor(name, m_StructData.colorValue); 265 else if (propType == PropertyType.Vector2 || propType == PropertyType.Vector3 || propType == PropertyType.Vector4) 266 mat.SetVector(name, m_StructData.vector4Value); 267 else if (propType == PropertyType.Float) 268 mat.SetFloat(name, m_StructData.floatValue); 269 else if (propType == PropertyType.Boolean) 270 mat.SetFloat(name, m_StructData.booleanValue ? 1 : 0); 271 else if (propType == PropertyType.Matrix2 || propType == PropertyType.Matrix3 || propType == PropertyType.Matrix4) 272 mat.SetMatrix(name, m_StructData.matrixValue); 273 else if (propType == PropertyType.Gradient) 274 { 275 mat.SetFloat(string.Format("{0}_Type", name), (int)m_ClassData.gradientValue.mode); 276 mat.SetFloat(string.Format("{0}_ColorsLength", name), m_ClassData.gradientValue.colorKeys.Length); 277 mat.SetFloat(string.Format("{0}_AlphasLength", name), m_ClassData.gradientValue.alphaKeys.Length); 278 for (int i = 0; i < 8; i++) 279 mat.SetVector(string.Format("{0}_ColorKey{1}", name, i), i < m_ClassData.gradientValue.colorKeys.Length ? GradientUtil.ColorKeyToVector(m_ClassData.gradientValue.colorKeys[i]) : Vector4.zero); 280 for (int i = 0; i < 8; i++) 281 mat.SetVector(string.Format("{0}_AlphaKey{1}", name, i), i < m_ClassData.gradientValue.alphaKeys.Length ? GradientUtil.AlphaKeyToVector(m_ClassData.gradientValue.alphaKeys[i]) : Vector2.zero); 282 } 283 else if (propType == PropertyType.VirtualTexture) 284 { 285 // virtual texture assignments are not supported via the material property block, we must assign them to the materials 286 } 287 } 288 } 289}