VFXTypes.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.VFX;
  4. namespace UnityEditor.VFX
  5. {
  6. enum SpaceableType
  7. {
  8. None,
  9. Position,
  10. Direction,
  11. Matrix,
  12. Vector
  13. }
  14. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
  15. class VFXSpaceAttribute : PropertyAttribute
  16. {
  17. public readonly SpaceableType type;
  18. public VFXSpaceAttribute(SpaceableType type)
  19. {
  20. this.type = type;
  21. }
  22. }
  23. class ShowAsColorAttribute : Attribute
  24. { }
  25. class MinMaxAttribute : PropertyAttribute
  26. {
  27. public readonly float min;
  28. public readonly float max;
  29. // Attribute used to make a float or int variable in a script be restricted to a specific range.
  30. public MinMaxAttribute(float min, float max)
  31. {
  32. this.min = min;
  33. this.max = max;
  34. }
  35. }
  36. class CoordinateSpaceInfo
  37. {
  38. public static readonly int SpaceCount = Enum.GetValues(typeof(VFXCoordinateSpace)).Length;
  39. }
  40. [VFXType(VFXTypeAttribute.Usage.Default, "Circle"), Serializable]
  41. struct TCircle
  42. {
  43. [Tooltip("Sets the transform of the circle.")]
  44. public Transform transform;
  45. [Tooltip("Sets the radius of the circle.")]
  46. public float radius;
  47. public static TCircle defaultValue = new TCircle { transform = Transform.defaultValue, radius = 1.0f };
  48. }
  49. [VFXType(VFXTypeAttribute.Usage.Default, "Arc Circle"), Serializable]
  50. struct TArcCircle
  51. {
  52. [Tooltip("Sets the Circle shape input.")]
  53. public TCircle circle;
  54. [Angle, Range(0, Mathf.PI * 2.0f), Tooltip("Controls how much of the circle is used. The value is in radians.")]
  55. public float arc;
  56. public static TArcCircle defaultValue = new TArcCircle { circle = TCircle.defaultValue, arc = 2.0f * Mathf.PI };
  57. }
  58. //This type is only used in DistanceToSphere
  59. [VFXType(VFXTypeAttribute.Usage.ExcludeFromProperty, "Simple Sphere"), Serializable]
  60. struct Sphere
  61. {
  62. [Tooltip("Sets the center of the sphere."), VFXSpace(SpaceableType.Position)]
  63. public Vector3 center;
  64. [Tooltip("Sets the radius of the sphere.")]
  65. public float radius;
  66. public static Sphere defaultValue = new Sphere { radius = 1.0f };
  67. }
  68. [VFXType(VFXTypeAttribute.Usage.Default, "Sphere"), Serializable]
  69. struct TSphere
  70. {
  71. [Tooltip("Sets the transform of the sphere.")]
  72. public Transform transform;
  73. [Tooltip("Sets the radius of the sphere.")]
  74. public float radius;
  75. public static implicit operator TSphere(Sphere v)
  76. {
  77. return new TSphere()
  78. {
  79. transform = new Transform()
  80. {
  81. position = v.center,
  82. scale = Vector3.one
  83. },
  84. radius = v.radius
  85. };
  86. }
  87. public static TSphere defaultValue = new TSphere { transform = Transform.defaultValue, radius = 1.0f };
  88. }
  89. [VFXType(VFXTypeAttribute.Usage.Default), Serializable]
  90. struct TArcSphere
  91. {
  92. public TSphere sphere;
  93. [Angle, Range(0, Mathf.PI * 2.0f), Tooltip("Controls how much of the sphere is used. The value is in radians.")]
  94. public float arc;
  95. public static TArcSphere defaultValue = new TArcSphere { sphere = TSphere.defaultValue, arc = 2.0f * Mathf.PI };
  96. }
  97. [VFXType(VFXTypeAttribute.Usage.Default, "Cone"), Serializable]
  98. struct TCone
  99. {
  100. [Tooltip("Sets the transform of the cone.")]
  101. public Transform transform;
  102. [Min(0.0f), Tooltip("Sets the base radius of the cone.")]
  103. public float baseRadius;
  104. [Min(0.0f), Tooltip("Sets the top radius of the cone.")]
  105. public float topRadius;
  106. [Tooltip("Sets the height of the cone.")]
  107. public float height;
  108. [Angle, Range(0, Mathf.PI * 2.0f), Tooltip("Controls how much of the cone is used. The value is in radians.")]
  109. public static TCone defaultValue = new TCone { transform = Transform.defaultValue, baseRadius = 1.0f, topRadius = 0.1f, height = 1.0f };
  110. }
  111. [VFXType(VFXTypeAttribute.Usage.Default, "Arc Cone"), Serializable]
  112. struct TArcCone
  113. {
  114. [Tooltip("Sets the cone.")]
  115. public TCone cone;
  116. [Angle, Range(0, Mathf.PI * 2.0f), Tooltip("Controls how much of the cone is used. The value is in radians.")]
  117. public float arc;
  118. public static TArcCone defaultValue = new TArcCone { cone = TCone.defaultValue, arc = 2.0f * Mathf.PI };
  119. }
  120. [VFXType(VFXTypeAttribute.Usage.Default, "Torus"), Serializable]
  121. struct TTorus
  122. {
  123. [Tooltip("Sets the transform of the torus.")]
  124. public Transform transform;
  125. [Tooltip("Sets the radius of the torus ring.")]
  126. public float majorRadius;
  127. [Tooltip("Sets the thickness of the torus ring.")]
  128. public float minorRadius;
  129. public static TTorus defaultValue = new TTorus { transform = Transform.defaultValue, majorRadius = 1.0f, minorRadius = 0.1f };
  130. }
  131. [VFXType(VFXTypeAttribute.Usage.Default, "Arc Torus"), Serializable]
  132. struct TArcTorus
  133. {
  134. [Tooltip("Sets the cone.")]
  135. public TTorus torus;
  136. [Angle, Range(0, Mathf.PI * 2.0f), Tooltip("Controls how much of the torus is used.")]
  137. public float arc;
  138. public static TArcTorus defaultValue = new TArcTorus { torus = TTorus.defaultValue, arc = 2.0f * Mathf.PI };
  139. }
  140. [VFXType(VFXTypeAttribute.Usage.Default, "Oriented Box"), VFXSpace(SpaceableType.Matrix), Serializable]
  141. struct OrientedBox
  142. {
  143. [Tooltip("Sets the center of the box."), VFXSpace(SpaceableType.Position)]
  144. public Vector3 center;
  145. [Angle, Tooltip("Sets the orientation of the box.")]
  146. public Vector3 angles;
  147. [Tooltip("Sets the size of the box along each axis.")]
  148. public Vector3 size;
  149. public static OrientedBox defaultValue = new OrientedBox { size = Vector3.one };
  150. }
  151. [VFXType(VFXTypeAttribute.Usage.Default, "Axis Aligned Box"), Serializable]
  152. struct AABox
  153. {
  154. [Tooltip("Sets the center of the box."), VFXSpace(SpaceableType.Position)]
  155. public Vector3 center;
  156. [Tooltip("Sets the size of the box along each axis.")]
  157. public Vector3 size;
  158. public static AABox defaultValue = new AABox { size = Vector3.one };
  159. }
  160. [VFXType, Serializable]
  161. struct Plane
  162. {
  163. public Plane(Vector3 direction) { position = Vector3.zero; normal = direction; }
  164. [Tooltip("Sets the position of the plane."), VFXSpace(SpaceableType.Position)]
  165. public Vector3 position;
  166. [Normalize, Tooltip("Sets the direction of the plane."), VFXSpace(SpaceableType.Direction)]
  167. public Vector3 normal;
  168. public static Plane defaultValue = new Plane { normal = Vector3.up };
  169. }
  170. [VFXType, Serializable]
  171. struct Line
  172. {
  173. [Tooltip("Sets the start position of the line."), VFXSpace(SpaceableType.Position)]
  174. public Vector3 start;
  175. [Tooltip("Sets the end position of the line."), VFXSpace(SpaceableType.Position)]
  176. public Vector3 end;
  177. public static Line defaultValue = new Line { start = Vector3.zero, end = Vector3.left };
  178. }
  179. [VFXType, VFXSpace(SpaceableType.Matrix), Serializable]
  180. struct Transform
  181. {
  182. [Tooltip("Sets the transform position."), VFXSpace(SpaceableType.Position)]
  183. public Vector3 position;
  184. [Angle, Tooltip("Sets the euler angles of the transform.")]
  185. public Vector3 angles;
  186. [Tooltip("Sets the scale of the transform along each axis.")]
  187. public Vector3 scale;
  188. public static Transform defaultValue = new Transform { scale = Vector3.one };
  189. public static implicit operator Matrix4x4(Transform t)
  190. {
  191. return Matrix4x4.TRS(t.position, Quaternion.Euler(t.angles), t.scale);
  192. }
  193. public static implicit operator Transform(Matrix4x4 m)
  194. {
  195. return new Transform()
  196. {
  197. position = m.GetPosition(),
  198. angles = m.rotation.eulerAngles,
  199. scale = m.lossyScale
  200. };
  201. }
  202. }
  203. [VFXType, Serializable]
  204. struct Position
  205. {
  206. [Tooltip("The position."), VFXSpace(SpaceableType.Position)]
  207. public Vector3 position;
  208. public static implicit operator Position(Vector3 v)
  209. {
  210. return new Position() { position = v };
  211. }
  212. public static implicit operator Vector3(Position v)
  213. {
  214. return v.position;
  215. }
  216. public static Position defaultValue = new Position { position = Vector3.zero };
  217. }
  218. [VFXType(VFXTypeAttribute.Usage.Default, "Direction"), Serializable]
  219. struct DirectionType
  220. {
  221. [Tooltip("The normalized direction."), VFXSpace(SpaceableType.Direction)]
  222. public Vector3 direction;
  223. public static implicit operator DirectionType(Vector3 v)
  224. {
  225. return new DirectionType() { direction = v };
  226. }
  227. public static implicit operator Vector3(DirectionType v)
  228. {
  229. return v.direction;
  230. }
  231. public static DirectionType defaultValue = new DirectionType { direction = Vector3.up };
  232. }
  233. [VFXType, Serializable]
  234. struct Vector
  235. {
  236. [Tooltip("The vector."), VFXSpace(SpaceableType.Vector)]
  237. public Vector3 vector;
  238. public static implicit operator Vector(Vector3 v)
  239. {
  240. return new Vector() { vector = v };
  241. }
  242. public static implicit operator Vector3(Vector v)
  243. {
  244. return v.vector;
  245. }
  246. public static Vector defaultValue = new Vector { vector = Vector3.zero };
  247. }
  248. [VFXType, Serializable]
  249. struct FlipBook
  250. {
  251. public int x;
  252. public int y;
  253. public static FlipBook defaultValue = new FlipBook { x = 4, y = 4 };
  254. }
  255. [VFXType(VFXTypeAttribute.Usage.ExcludeFromProperty), Serializable]
  256. struct CameraBuffer
  257. {
  258. private Texture texture;
  259. public CameraBuffer(Texture texture)
  260. {
  261. this.texture = texture;
  262. }
  263. public static implicit operator Texture(CameraBuffer cameraBuffer)
  264. {
  265. return cameraBuffer.texture;
  266. }
  267. public static implicit operator CameraBuffer(Texture texture)
  268. {
  269. return new CameraBuffer(texture);
  270. }
  271. public static implicit operator int(CameraBuffer cameraBuffer)
  272. {
  273. return cameraBuffer.texture?.GetInstanceID() ?? 0;
  274. }
  275. public static implicit operator CameraBuffer(int id)
  276. {
  277. return new CameraBuffer((Texture)EditorUtility.InstanceIDToObject(id));
  278. }
  279. }
  280. [VFXType(VFXTypeAttribute.Usage.Default, "Camera"), Serializable]
  281. struct CameraType
  282. {
  283. [Tooltip("The camera's Transform in the world.")]
  284. public Transform transform;
  285. [Tooltip("Uses Orthographic projection.")]
  286. public bool orthographic;
  287. [Angle, Range(0.0f, Mathf.PI), Tooltip("The field of view is the height of the camera’s view angle, measured in degrees along the local Y axis.")]
  288. public float fieldOfView;
  289. [Min(0.0f), Tooltip("The near plane is the closest plane relative to the camera where drawing occurs.")]
  290. public float nearPlane;
  291. [Min(0.0f), Tooltip("The far plane is the furthest plane relative to the camera where drawing occurs.")]
  292. public float farPlane;
  293. [Min(0.0f), Tooltip("The orthographic size is half the size of the vertical viewing volume.")]
  294. public float orthographicSize;
  295. [Min(0.0f), Tooltip("The aspect ratio is the proportional relationship between the camera’s width and height.")]
  296. public float aspectRatio;
  297. [Min(0.0f), Tooltip("The width and height of the camera in pixels.")]
  298. public Vector2 pixelDimensions;
  299. [Tooltip("The depth buffer of the camera, containing the rendered depth information.")]
  300. public CameraBuffer depthBuffer;
  301. [Tooltip("The color buffer of the camera, containing the rendered color information.")]
  302. public CameraBuffer colorBuffer;
  303. public static CameraType defaultValue = new CameraType { transform = Transform.defaultValue, fieldOfView = 60.0f * Mathf.Deg2Rad, nearPlane = 0.3f, farPlane = 1000.0f, aspectRatio = 1.0f, orthographicSize = 5.0f, pixelDimensions = new Vector2(1920, 1080) };
  304. }
  305. [VFXType, Serializable]
  306. struct TerrainType
  307. {
  308. [Tooltip("Sets the bounds of the Terrain.")]
  309. public AABox Bounds;
  310. [Tooltip("Sets the height map of the Terrain.")]
  311. public Texture2D HeightMap;
  312. [Tooltip("Sets the height of the Terrain.")]
  313. public float Height;
  314. }
  315. }