defaultNodeModel.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { NodeModel, DiagramModel } from "storm-react-diagrams";
  2. import { Nullable } from 'babylonjs/types';
  3. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  4. import { GenericPortModel } from './generic/genericPortModel';
  5. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  6. import { GraphEditor, NodeCreationOptions } from '../../graphEditor';
  7. import { NodeMaterialConnectionPoint } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPoint';
  8. import { GlobalState } from '../../globalState';
  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]: GenericPortModel };
  18. /**
  19. * Constructs the node model
  20. */
  21. constructor(key: string) {
  22. super(key);
  23. }
  24. prepareConnection(type: string, outPort: GenericPortModel, connection?: NodeMaterialConnectionPoint) {
  25. }
  26. prepare(options: NodeCreationOptions, nodes: Array<DefaultNodeModel>, model: DiagramModel, graphEditor: GraphEditor) {
  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 GenericPortModel(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. var inputPort = new GenericPortModel(connection.name, "input");
  40. inputPort.connection = connection;
  41. this.addPort(inputPort)
  42. console.log(connection.name + " for " + options.nodeMaterialBlock!.name)
  43. if (connection.connectedPoint) {
  44. // Block is not a leaf node, create node for the given block type
  45. var connectedNode;
  46. var existingNodes = nodes.filter((n) => { return n.block === (connection as any)._connectedPoint._ownerBlock });
  47. if (existingNodes.length == 0) {
  48. connectedNode = graphEditor.createNodeFromObject({ column: options.column + 1, nodeMaterialBlock: connection.connectedPoint._ownerBlock });
  49. } else {
  50. connectedNode = existingNodes[0];
  51. }
  52. let link = connectedNode.ports[connection.connectedPoint.name].link(inputPort);
  53. model.addAll(link);
  54. } else if (connection.isAttribute) {
  55. } else {
  56. // Create value node for the connection
  57. var type = ""
  58. if (connection.type == NodeMaterialBlockConnectionPointTypes.Texture) {
  59. type = "Texture"
  60. } else if (connection.type == NodeMaterialBlockConnectionPointTypes.Matrix) {
  61. type = "Matrix"
  62. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector3OrColor3) {
  63. type = "Vector3"
  64. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector2) {
  65. type = "Vector2"
  66. } else if (connection.type & NodeMaterialBlockConnectionPointTypes.Vector3OrColor3OrVector4OrColor4) {
  67. type = "Vector4"
  68. }
  69. // Create links
  70. var localNode = graphEditor.addValueNode(type, options.column + 1, connection);
  71. if (localNode) {
  72. var ports = localNode.getPorts()
  73. for (var key in ports) {
  74. let link = (ports[key] as GenericPortModel).link(inputPort);
  75. model.addAll(link);
  76. }
  77. }
  78. }
  79. });
  80. }
  81. renderProperties(globalState: GlobalState): JSX.Element | null {
  82. return null;
  83. }
  84. }