defaultNodeModel.ts 4.3 KB

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