genericNodeModel.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as React from "react";
  2. import { Nullable } from 'babylonjs/types';
  3. import { Vector2, Vector3, Vector4, Matrix } from 'babylonjs/Maths/math';
  4. import { DefaultNodeModel } from '../defaultNodeModel';
  5. import { DiagramModel } from 'storm-react-diagrams/dist/@types/src/models/DiagramModel';
  6. import { GraphEditor, NodeCreationOptions } from '../../../graphEditor';
  7. import { NodeMaterialConnectionPoint } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPoint';
  8. import { Vector2PropertyTabComponent } from '../../../components/propertyTab/properties/vector2PropertyTabComponent';
  9. import { GlobalState } from '../../../globalState';
  10. import { Vector3PropertyTabComponent } from '../../../components/propertyTab/properties/vector3PropertyTabComponent';
  11. import { DefaultPortModel } from '../defaultPortModel';
  12. /**
  13. * Generic node model which stores information about a node editor block
  14. */
  15. export class GenericNodeModel extends DefaultNodeModel {
  16. /**
  17. * Labels for the block
  18. */
  19. public header = "";
  20. /**
  21. * Vector2 for the node if it exists
  22. */
  23. public vector2: Nullable<Vector2> = null;
  24. /**
  25. * Vector3 for the node if it exists
  26. */
  27. public vector3: Nullable<Vector3> = null;
  28. /**
  29. * Vector4 for the node if it exists
  30. */
  31. public vector4: Nullable<Vector4> = null;
  32. /**
  33. * Matrix for the node if it exists
  34. */
  35. public matrix: Nullable<Matrix> = null;
  36. /**
  37. * Constructs the node model
  38. */
  39. constructor() {
  40. super("generic");
  41. }
  42. prepareConnection(type: string, outPort: DefaultPortModel, connection?: NodeMaterialConnectionPoint) {
  43. switch (type) {
  44. case "Vector2":
  45. outPort.getValue = () => {
  46. return this.vector2;
  47. }
  48. if (connection && connection.value) {
  49. this.vector2 = connection.value
  50. } else {
  51. this.vector2 = new Vector2()
  52. }
  53. break;
  54. case "Vector3":
  55. outPort.getValue = () => {
  56. return this.vector3;
  57. }
  58. if (connection && connection.value) {
  59. this.vector3 = connection.value
  60. } else {
  61. this.vector3 = new Vector3()
  62. }
  63. break;
  64. case "Vector4":
  65. outPort.getValue = () => {
  66. return this.vector4;
  67. }
  68. if (connection && connection.value) {
  69. this.vector4 = connection.value
  70. } else {
  71. this.vector4 = new Vector4(0, 0, 0, 1)
  72. }
  73. break;
  74. case "Matrix":
  75. outPort.getValue = () => {
  76. return this.matrix;
  77. }
  78. if (connection && connection.value) {
  79. this.matrix = connection.value
  80. } else {
  81. this.matrix = new Matrix();
  82. }
  83. break;
  84. }
  85. }
  86. prepare(options: NodeCreationOptions, nodes: Array<DefaultNodeModel>, model: DiagramModel, graphEditor: GraphEditor, filterInputs: string[]) {
  87. if (options.nodeMaterialBlock) {
  88. this.header = options.nodeMaterialBlock.name;
  89. }
  90. super.prepare(options, nodes, model, graphEditor, filterInputs);
  91. }
  92. renderProperties(globalState: GlobalState) {
  93. if (this.vector2) {
  94. return (
  95. <Vector2PropertyTabComponent globalState={globalState} node={this} />
  96. );
  97. }
  98. if (this.vector3) {
  99. return (
  100. <Vector3PropertyTabComponent globalState={globalState} node={this} />
  101. );
  102. }
  103. return null;
  104. }
  105. }