설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreviewProperty.cs 12KB

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