StructNode.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import CodeNode from './CodeNode.js';
  2. import StructVarNode from './StructVarNode.js';
  3. class StructNode extends CodeNode {
  4. constructor( inputs = {}, name = '' ) {
  5. super();
  6. this.inputs = inputs;
  7. this.name = name;
  8. }
  9. getType( builder ) {
  10. if ( this.name !== '' ) {
  11. return this.name;
  12. } else {
  13. const codeNode = builder.getCodeFromNode( this, 'code' );
  14. return codeNode.name;
  15. }
  16. }
  17. create( inputs = {} ) {
  18. return new StructVarNode( this, inputs );
  19. }
  20. generate( builder, output ) {
  21. const type = this.getType( builder );
  22. const inputs = this.inputs;
  23. const shaderStage = builder.getShaderStage();
  24. let code = `struct ${type} {\n`;
  25. for ( const inputName in inputs ) {
  26. const inputType = inputs[ inputName ];
  27. code += `\t${inputType} ${inputName};\n`;
  28. }
  29. code += `};`;
  30. this.code = code;
  31. super.generate( builder, output );
  32. if ( output === 'var' ) {
  33. const nodeData = builder.getDataFromNode( this );
  34. if ( nodeData.index === undefined ) {
  35. nodeData.index = 0;
  36. }
  37. return `structVar${nodeData.index ++}`;
  38. } else {
  39. return code;
  40. }
  41. }
  42. }
  43. export default StructNode;