defaultNodeModel.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { NodeModel, DiagramModel } from "storm-react-diagrams";
  2. import { Nullable } from 'babylonjs/types';
  3. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  4. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  5. import { GraphEditor, NodeCreationOptions } from '../../graphEditor';
  6. import { GlobalState } from '../../globalState';
  7. import { DefaultPortModel } from './defaultPortModel';
  8. /**
  9. * Generic node model which stores information about a node editor block
  10. */
  11. export class DefaultNodeModel extends NodeModel {
  12. /**
  13. * The babylon block this node represents
  14. */
  15. public block: Nullable<NodeMaterialBlock> = null;
  16. public ports: { [s: string]: DefaultPortModel };
  17. /**
  18. * Constructs the node model
  19. */
  20. constructor(key: string) {
  21. super(key);
  22. }
  23. prepare(options: NodeCreationOptions, nodes: Array<DefaultNodeModel>, model: DiagramModel, graphEditor: GraphEditor, filterInputs: string[]) {
  24. this.block = options.nodeMaterialBlock || null;
  25. if (!options.nodeMaterialBlock) {
  26. return;
  27. }
  28. // Create output ports
  29. options.nodeMaterialBlock._outputs.forEach((connection: any) => {
  30. var outputPort = new DefaultPortModel(connection.name, "output");
  31. outputPort.syncWithNodeMaterialConnectionPoint(connection);
  32. this.addPort(outputPort)
  33. })
  34. // Create input ports and nodes if they exist
  35. options.nodeMaterialBlock._inputs.forEach((connection) => {
  36. if (filterInputs.length > 0 && filterInputs.indexOf(connection.name) === -1) {
  37. return;
  38. }
  39. var inputPort = new DefaultPortModel(connection.name, "input");
  40. inputPort.connection = connection;
  41. this.addPort(inputPort)
  42. if (connection.connectedPoint) {
  43. // Block is not a leaf node, create node for the given block type
  44. var connectedNode;
  45. var existingNodes = nodes.filter((n) => { return n.block === (connection as any)._connectedPoint._ownerBlock });
  46. if (existingNodes.length == 0) {
  47. connectedNode = graphEditor.createNodeFromObject({ column: options.column + 1, nodeMaterialBlock: connection.connectedPoint._ownerBlock });
  48. } else {
  49. connectedNode = existingNodes[0];
  50. }
  51. let link = connectedNode.ports[connection.connectedPoint.name].link(inputPort);
  52. if (graphEditor._toAdd) {
  53. graphEditor._toAdd.push(link);
  54. } else {
  55. model.addAll(link);
  56. }
  57. } else if (!connection.isUndefined) {
  58. // Create value node for the connection
  59. var type = ""
  60. if (connection.type == NodeMaterialBlockConnectionPointTypes.Texture) {
  61. type = "Texture"
  62. } else if (connection.type == NodeMaterialBlockConnectionPointTypes.Matrix) {
  63. type = "Matrix"
  64. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector3OrColor3) {
  65. type = "Vector3"
  66. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector2) {
  67. type = "Vector2"
  68. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector3OrColor3OrVector4OrColor4) {
  69. type = "Vector4"
  70. }
  71. else if (connection.type & NodeMaterialBlockConnectionPointTypes.Float) {
  72. type = "Float"
  73. }
  74. // Create links
  75. var localNode = graphEditor.addValueNode(type, options.column + 1, connection);
  76. if (localNode) {
  77. var ports = localNode.getPorts()
  78. for (var key in ports) {
  79. let link = (ports[key] as DefaultPortModel).link(inputPort);
  80. if (graphEditor._toAdd) {
  81. graphEditor._toAdd.push(link);
  82. } else {
  83. model.addAll(link);
  84. }
  85. }
  86. }
  87. }
  88. });
  89. }
  90. renderProperties(globalState: GlobalState): JSX.Element | null {
  91. return null;
  92. }
  93. }