VFXExpressionTransform.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEngine.VFX;
  6. namespace UnityEditor.VFX
  7. {
  8. class VFXExpressionTRSToMatrix : VFXExpression
  9. {
  10. public VFXExpressionTRSToMatrix() : this(new VFXExpression[] { VFXValue<Vector3>.Default, VFXValue<Vector3>.Default, VFXValue<Vector3>.Default }
  11. )
  12. {
  13. }
  14. public VFXExpressionTRSToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
  15. {
  16. }
  17. public override VFXExpressionOperation operation
  18. {
  19. get
  20. {
  21. return VFXExpressionOperation.TRSToMatrix;
  22. }
  23. }
  24. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  25. {
  26. var posReduce = constParents[0];
  27. var rotReduce = constParents[1];
  28. var scaleReduce = constParents[2];
  29. var pos = posReduce.Get<Vector3>();
  30. var rot = rotReduce.Get<Vector3>();
  31. var scale = scaleReduce.Get<Vector3>();
  32. var quat = Quaternion.Euler(rot);
  33. Matrix4x4 matrix = new Matrix4x4();
  34. matrix.SetTRS(pos, quat, scale);
  35. return VFXValue.Constant(matrix);
  36. }
  37. public override string GetCodeString(string[] parents)
  38. {
  39. return string.Format("GetTRSMatrix({0}, {1}, {2})", parents[0], parents[1], parents[2]);
  40. }
  41. }
  42. class VFXExpressionInverseMatrix : VFXExpression
  43. {
  44. public VFXExpressionInverseMatrix() : this(VFXValue<Matrix4x4>.Default)
  45. { }
  46. public VFXExpressionInverseMatrix(VFXExpression parent) : base(VFXExpression.Flags.InvalidOnGPU, parent)
  47. { }
  48. sealed public override VFXExpressionOperation operation
  49. {
  50. get
  51. {
  52. return VFXExpressionOperation.InverseMatrix;
  53. }
  54. }
  55. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  56. {
  57. var matrix = constParents[0].Get<Matrix4x4>();
  58. return VFXValue.Constant(matrix.inverse);
  59. }
  60. }
  61. class VFXExpressionTransposeMatrix : VFXExpression
  62. {
  63. public VFXExpressionTransposeMatrix() : this(VFXValue<Matrix4x4>.Default)
  64. { }
  65. public VFXExpressionTransposeMatrix(VFXExpression parent) : base(Flags.None, parent)
  66. { }
  67. public sealed override VFXExpressionOperation operation
  68. {
  69. get
  70. {
  71. return VFXExpressionOperation.TransposeMatrix;
  72. }
  73. }
  74. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  75. {
  76. var matrix = constParents[0].Get<Matrix4x4>();
  77. return VFXValue.Constant(matrix.transpose);
  78. }
  79. public override string GetCodeString(string[] parents)
  80. {
  81. return string.Format("transpose({0})", parents[0]);
  82. }
  83. }
  84. class VFXExpressionInverseTRSMatrix : VFXExpression
  85. {
  86. public VFXExpressionInverseTRSMatrix() : this(VFXValue<Matrix4x4>.Default)
  87. { }
  88. public VFXExpressionInverseTRSMatrix(VFXExpression parent) : base(VFXExpression.Flags.None, parent)
  89. { }
  90. sealed public override VFXExpressionOperation operation
  91. {
  92. get
  93. {
  94. return VFXExpressionOperation.InverseTRSMatrix;
  95. }
  96. }
  97. // Invert 3D transformation matrix (not perspective). Adapted from graphics gems 2.
  98. // Inverts upper left by calculating its determinant and multiplying it to the symmetric
  99. // adjust matrix of each element. Finally deals with the translation by transforming the
  100. // original translation using by the calculated inverse.
  101. //https://github.com/erich666/GraphicsGems/blob/master/gemsii/inverse.c
  102. static bool inputvertMatrix4x4_General3D(Matrix4x4 input, ref Matrix4x4 output)
  103. {
  104. return Matrix4x4.Inverse3DAffine(input, ref output);
  105. }
  106. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  107. {
  108. var matrix = constParents[0].Get<Matrix4x4>();
  109. var result = Matrix4x4.identity;
  110. inputvertMatrix4x4_General3D(matrix, ref result);
  111. return VFXValue.Constant(result);
  112. }
  113. public override string GetCodeString(string[] parents)
  114. {
  115. return string.Format("VFXInverseTRSMatrix({0})", parents[0]);
  116. }
  117. }
  118. class VFXExpressionExtractPositionFromMatrix : VFXExpression
  119. {
  120. public VFXExpressionExtractPositionFromMatrix() : this(VFXValue<Matrix4x4>.Default)
  121. {
  122. }
  123. public VFXExpressionExtractPositionFromMatrix(VFXExpression parent) : base(VFXExpression.Flags.None, new VFXExpression[] { parent })
  124. {
  125. }
  126. public override VFXExpressionOperation operation
  127. {
  128. get
  129. {
  130. return VFXExpressionOperation.ExtractPositionFromMatrix;
  131. }
  132. }
  133. protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
  134. {
  135. var parent = reducedParents[0];
  136. if (parent is VFXExpressionTRSToMatrix)
  137. return parent.parents[0];
  138. return base.Reduce(reducedParents);
  139. }
  140. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  141. {
  142. var matrixReduce = constParents[0];
  143. var matrix = matrixReduce.Get<Matrix4x4>();
  144. return VFXValue.Constant<Vector3>(matrix.GetColumn(3));
  145. }
  146. public override string GetCodeString(string[] parents)
  147. {
  148. return string.Format("{0}[3].xyz", parents[0]);
  149. }
  150. }
  151. class VFXExpressionExtractAnglesFromMatrix : VFXExpression
  152. {
  153. public VFXExpressionExtractAnglesFromMatrix() : this(VFXValue<Matrix4x4>.Default)
  154. {
  155. }
  156. public VFXExpressionExtractAnglesFromMatrix(VFXExpression parent) : base(VFXExpression.Flags.InvalidOnGPU, new VFXExpression[] { parent })
  157. {
  158. }
  159. public override VFXExpressionOperation operation
  160. {
  161. get
  162. {
  163. return VFXExpressionOperation.ExtractAnglesFromMatrix;
  164. }
  165. }
  166. protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
  167. {
  168. var parent = reducedParents[0];
  169. if (parent is VFXExpressionTRSToMatrix)
  170. return parent.parents[1];
  171. return base.Reduce(reducedParents);
  172. }
  173. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  174. {
  175. var matrixReduce = constParents[0];
  176. var matrix = matrixReduce.Get<Matrix4x4>();
  177. matrix.SetRow(3, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  178. return VFXValue.Constant(matrix.rotation.eulerAngles);
  179. }
  180. }
  181. class VFXExpressionExtractScaleFromMatrix : VFXExpression
  182. {
  183. public VFXExpressionExtractScaleFromMatrix() : this(VFXValue<Matrix4x4>.Default)
  184. {
  185. }
  186. public VFXExpressionExtractScaleFromMatrix(VFXExpression parent) : base(VFXExpression.Flags.None, new VFXExpression[] { parent })
  187. {
  188. }
  189. public override VFXExpressionOperation operation
  190. {
  191. get
  192. {
  193. return VFXExpressionOperation.ExtractScaleFromMatrix;
  194. }
  195. }
  196. protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
  197. {
  198. var parent = reducedParents[0];
  199. if (parent is VFXExpressionTRSToMatrix)
  200. return parent.parents[2];
  201. return base.Reduce(reducedParents);
  202. }
  203. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  204. {
  205. var matrixReduce = constParents[0];
  206. var matrix = matrixReduce.Get<Matrix4x4>();
  207. matrix.SetRow(3, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  208. return VFXValue.Constant(matrix.lossyScale);
  209. }
  210. public override string GetCodeString(string[] parents)
  211. {
  212. return string.Format("float3(length({0}[0]),length({0}[1]),length({0}[2]))", parents[0]);
  213. }
  214. }
  215. class VFXExpressionTransformMatrix : VFXExpression
  216. {
  217. public VFXExpressionTransformMatrix() : this(VFXValue<Matrix4x4>.Default, VFXValue<Matrix4x4>.Default)
  218. {
  219. }
  220. public VFXExpressionTransformMatrix(VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
  221. {
  222. }
  223. public override VFXExpressionOperation operation
  224. {
  225. get
  226. {
  227. return VFXExpressionOperation.TransformMatrix;
  228. }
  229. }
  230. protected sealed override VFXExpression Evaluate(VFXExpression[] constParents)
  231. {
  232. var left = constParents[0].Get<Matrix4x4>();
  233. var right = constParents[1].Get<Matrix4x4>();
  234. return VFXValue.Constant(left * right);
  235. }
  236. public override string GetCodeString(string[] parents)
  237. {
  238. return string.Format("mul({0}, {1})", parents[0], parents[1]);
  239. }
  240. }
  241. class VFXExpressionTransformPosition : VFXExpression
  242. {
  243. public VFXExpressionTransformPosition() : this(VFXValue<Matrix4x4>.Default, VFXValue<Vector3>.Default)
  244. {
  245. }
  246. public VFXExpressionTransformPosition(VFXExpression matrix, VFXExpression position) : base(VFXExpression.Flags.None, new VFXExpression[] { matrix, position })
  247. {
  248. }
  249. public override VFXExpressionOperation operation
  250. {
  251. get
  252. {
  253. return VFXExpressionOperation.TransformPos;
  254. }
  255. }
  256. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  257. {
  258. var matrixReduce = constParents[0];
  259. var positionReduce = constParents[1];
  260. var matrix = matrixReduce.Get<Matrix4x4>();
  261. var position = positionReduce.Get<Vector3>();
  262. return VFXValue.Constant(matrix.MultiplyPoint(position));
  263. }
  264. public override string GetCodeString(string[] parents)
  265. {
  266. return string.Format("mul({0}, float4({1}, 1.0)).xyz", parents[0], parents[1]);
  267. }
  268. }
  269. class VFXExpressionTransformVector : VFXExpression
  270. {
  271. public VFXExpressionTransformVector() : this(VFXValue<Matrix4x4>.Default, VFXValue<Vector3>.Default)
  272. {
  273. }
  274. public VFXExpressionTransformVector(VFXExpression matrix, VFXExpression vector) : base(VFXExpression.Flags.None, new VFXExpression[] { matrix, vector })
  275. {
  276. }
  277. public override VFXExpressionOperation operation
  278. {
  279. get
  280. {
  281. return VFXExpressionOperation.TransformVec;
  282. }
  283. }
  284. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  285. {
  286. var matrixReduce = constParents[0];
  287. var positionReduce = constParents[1];
  288. var matrix = matrixReduce.Get<Matrix4x4>();
  289. var vector = positionReduce.Get<Vector3>();
  290. return VFXValue.Constant(matrix.MultiplyVector(vector));
  291. }
  292. public override string GetCodeString(string[] parents)
  293. {
  294. return string.Format("mul((float3x3){0}, {1})", parents[0], parents[1]);
  295. }
  296. }
  297. class VFXExpressionTransformVector4 : VFXExpression
  298. {
  299. public VFXExpressionTransformVector4()
  300. : this(VFXValue<Matrix4x4>.Default, VFXValue<Vector4>.Default)
  301. {
  302. }
  303. public VFXExpressionTransformVector4(VFXExpression matrix, VFXExpression vec)
  304. : base(VFXExpression.Flags.None, new VFXExpression[] { matrix, vec })
  305. {
  306. }
  307. public override VFXExpressionOperation operation
  308. {
  309. get
  310. {
  311. return VFXExpressionOperation.TransformVector4;
  312. }
  313. }
  314. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  315. {
  316. var matrixReduce = constParents[0];
  317. var vReduce = constParents[1];
  318. var matrix = matrixReduce.Get<Matrix4x4>();
  319. var vec = vReduce.Get<Vector4>();
  320. // No multiply Vector4 in Unity API :(
  321. Vector4 dst = new Vector4();
  322. dst.x = Vector4.Dot(matrix.GetRow(0), vec);
  323. dst.y = Vector4.Dot(matrix.GetRow(1), vec);
  324. dst.z = Vector4.Dot(matrix.GetRow(2), vec);
  325. dst.w = Vector4.Dot(matrix.GetRow(3), vec);
  326. return VFXValue.Constant(dst);
  327. }
  328. public override string GetCodeString(string[] parents)
  329. {
  330. return string.Format("mul({0}, {1})", parents[0], parents[1]);
  331. }
  332. }
  333. class VFXExpressionTransformDirection : VFXExpression
  334. {
  335. public VFXExpressionTransformDirection() : this(VFXValue<Matrix4x4>.Default, VFXValue<Vector3>.Default)
  336. {
  337. }
  338. public VFXExpressionTransformDirection(VFXExpression matrix, VFXExpression vector) : base(VFXExpression.Flags.None, new VFXExpression[] { matrix, vector })
  339. {
  340. }
  341. public override VFXExpressionOperation operation
  342. {
  343. get
  344. {
  345. return VFXExpressionOperation.TransformDir;
  346. }
  347. }
  348. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  349. {
  350. var matrixReduce = constParents[0];
  351. var positionReduce = constParents[1];
  352. var matrix = matrixReduce.Get<Matrix4x4>();
  353. var vector = positionReduce.Get<Vector3>();
  354. return VFXValue.Constant(matrix.MultiplyVector(vector).normalized);
  355. }
  356. public override string GetCodeString(string[] parents)
  357. {
  358. return string.Format("normalize(mul((float3x3){0}, {1}))", parents[0], parents[1]);
  359. }
  360. }
  361. class VFXExpressionVector3sToMatrix : VFXExpression
  362. {
  363. public VFXExpressionVector3sToMatrix() : this(new VFXExpression[] { new VFXValue<Vector3>(Vector3.right), new VFXValue<Vector3>(Vector3.up), new VFXValue<Vector3>(Vector3.forward), VFXValue<Vector3>.Default }
  364. )
  365. {
  366. }
  367. public VFXExpressionVector3sToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
  368. {
  369. }
  370. public override VFXExpressionOperation operation
  371. {
  372. get
  373. {
  374. return VFXExpressionOperation.Vector3sToMatrix;
  375. }
  376. }
  377. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  378. {
  379. var xReduce = constParents[0];
  380. var yReduce = constParents[1];
  381. var zReduce = constParents[2];
  382. var wReduce = constParents[3];
  383. var x = xReduce.Get<Vector3>();
  384. var y = yReduce.Get<Vector3>();
  385. var z = zReduce.Get<Vector3>();
  386. var w = wReduce.Get<Vector3>();
  387. Matrix4x4 matrix = new Matrix4x4();
  388. matrix.SetColumn(0, new Vector4(x.x, x.y, x.z, 0.0f));
  389. matrix.SetColumn(1, new Vector4(y.x, y.y, y.z, 0.0f));
  390. matrix.SetColumn(2, new Vector4(z.x, z.y, z.z, 0.0f));
  391. matrix.SetColumn(3, new Vector4(w.x, w.y, w.z, 1.0f));
  392. return VFXValue.Constant(matrix);
  393. }
  394. public override string GetCodeString(string[] parents)
  395. {
  396. return string.Format("VFXCreateMatrixFromColumns(float4({0}, 0.0), float4({1}, 0.0), float4({2}, 0.0), float4({3}, 1.0));", parents[0], parents[1], parents[2], parents[3]);
  397. }
  398. }
  399. class VFXExpressionVector4sToMatrix : VFXExpression
  400. {
  401. public VFXExpressionVector4sToMatrix() : this(new VFXExpression[] { new VFXValue<Vector4>(new Vector4(1, 0, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 1, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 1, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 0, 1)) }
  402. )
  403. {
  404. }
  405. public VFXExpressionVector4sToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
  406. {
  407. }
  408. public override VFXExpressionOperation operation
  409. {
  410. get
  411. {
  412. return VFXExpressionOperation.Vector4sToMatrix;
  413. }
  414. }
  415. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  416. {
  417. var xReduce = constParents[0];
  418. var yReduce = constParents[1];
  419. var zReduce = constParents[2];
  420. var wReduce = constParents[3];
  421. var x = xReduce.Get<Vector4>();
  422. var y = yReduce.Get<Vector4>();
  423. var z = zReduce.Get<Vector4>();
  424. var w = wReduce.Get<Vector4>();
  425. Matrix4x4 matrix = new Matrix4x4();
  426. matrix.SetColumn(0, x);
  427. matrix.SetColumn(1, y);
  428. matrix.SetColumn(2, z);
  429. matrix.SetColumn(3, w);
  430. return VFXValue.Constant(matrix);
  431. }
  432. public override string GetCodeString(string[] parents)
  433. {
  434. return string.Format("VFXCreateMatrixFromColumns({0}, {1}, {2}, {3});", parents[0], parents[1], parents[2], parents[3]);
  435. }
  436. }
  437. class VFXExpressionMatrixToVector3s : VFXExpression
  438. {
  439. public VFXExpressionMatrixToVector3s() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) } // TODO row index should not be an expression!
  440. )
  441. {
  442. }
  443. public VFXExpressionMatrixToVector3s(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
  444. {
  445. }
  446. public override VFXExpressionOperation operation
  447. {
  448. get
  449. {
  450. return VFXExpressionOperation.MatrixToVector3s;
  451. }
  452. }
  453. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  454. {
  455. var matReduce = constParents[0];
  456. var axisReduce = constParents[1];
  457. var mat = matReduce.Get<Matrix4x4>();
  458. var axis = axisReduce.Get<int>();
  459. return VFXValue.Constant<Vector3>(mat.GetColumn(axis));
  460. }
  461. public override string GetCodeString(string[] parents)
  462. {
  463. return string.Format("VFXGetColumnFromMatrix({0}, {1}).xyz", parents[0], parents[1]);
  464. }
  465. }
  466. class VFXExpressionMatrixToVector4s : VFXExpression
  467. {
  468. public VFXExpressionMatrixToVector4s() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) } // TODO row index should not be an expression!
  469. )
  470. {
  471. }
  472. public VFXExpressionMatrixToVector4s(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
  473. {
  474. }
  475. public override VFXExpressionOperation operation
  476. {
  477. get
  478. {
  479. return VFXExpressionOperation.MatrixToVector4s;
  480. }
  481. }
  482. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  483. {
  484. var matReduce = constParents[0];
  485. var axisReduce = constParents[1];
  486. var mat = matReduce.Get<Matrix4x4>();
  487. var axis = axisReduce.Get<int>();
  488. return VFXValue.Constant(mat.GetColumn(axis));
  489. }
  490. public override string GetCodeString(string[] parents)
  491. {
  492. return string.Format("VFXGetColumnFromMatrix({0}, {1})", parents[0], parents[1]);
  493. }
  494. }
  495. }