WebGPUNodeBuilder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import WebGPUUniformsGroup from './WebGPUUniformsGroup.js';
  2. import { FloatUniform, Vector3Uniform } from './WebGPUUniform.js';
  3. import NodeSlot from '../nodes/core/NodeSlot.js';
  4. import NodeBuilder from '../nodes/core/NodeBuilder.js';
  5. class WebGPUNodeUniformsGroup extends WebGPUUniformsGroup {
  6. constructor( shaderStage ) {
  7. super( 'nodeUniforms' );
  8. let shaderStageVisibility;
  9. if ( shaderStage === 'vertex' ) shaderStageVisibility = GPUShaderStage.VERTEX;
  10. else if ( shaderStage === 'fragment' ) shaderStageVisibility = GPUShaderStage.FRAGMENT;
  11. this.setVisibility( shaderStageVisibility );
  12. this.setOnBeforeUpdate( this._onBeforeUpdate );
  13. }
  14. _onBeforeUpdate( object, camera ) {
  15. const material = object.material;
  16. console.log( 1 );
  17. }
  18. }
  19. class WebGPUNodeBuilder extends NodeBuilder {
  20. constructor( material, renderer ) {
  21. super( material, renderer );
  22. this.uniformsGroup = {};
  23. this._parseMaterial();
  24. }
  25. _parseMaterial() {
  26. const material = this.material;
  27. if ( material.isMeshBasicMaterial ) {
  28. if ( material.color.isNode ) {
  29. this.addSlot( 'fragment', new NodeSlot( material.color, 'COLOR', 'vec3' ) );
  30. }
  31. }
  32. }
  33. getUniformNSName( nodeUniform ) {
  34. return `nodeUniforms.${nodeUniform.name}`;
  35. }
  36. getBindings() {
  37. const bindings = [];
  38. const uniformsVertexGroup = this.uniformsGroup[ 'vertex' ];
  39. const uniformsFragmentGroup = this.uniformsGroup[ 'fragment' ];
  40. if ( uniformsVertexGroup ) bindings.push( uniformsVertexGroup );
  41. if ( uniformsFragmentGroup ) bindings.push( uniformsFragmentGroup );
  42. return bindings;
  43. }
  44. createUniformFromNode( node, shaderStage, type ) {
  45. const uniformNode = super.createUniformFromNode( node, shaderStage, type )
  46. const nodeData = this.createDataFromNode( node, shaderStage );
  47. if ( nodeData.uniformGroup === undefined ) {
  48. let uniformsGroup = this.uniformsGroup[shaderStage];
  49. if (uniformsGroup === undefined) {
  50. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  51. this.uniformsGroup[shaderStage] = uniformsGroup;
  52. }
  53. let uniformGroup;
  54. if ( type === 'float' ) {
  55. uniformGroup = new FloatUniform( uniformNode.name, uniformNode.value );
  56. } else if ( type === 'vec3' ) {
  57. uniformGroup = new Vector3Uniform( uniformNode.name, uniformNode.value );
  58. } else {
  59. console.error( `Uniform "${type}" not declared.` );
  60. }
  61. uniformsGroup.addUniform( uniformGroup );
  62. nodeData.uniformGroup = uniformGroup;
  63. }
  64. return uniformNode;
  65. }
  66. buildShader( shaderStage, code ) {
  67. // use regex maybe for security?
  68. const versionStrIndex = code.indexOf("\n");
  69. let finalCode = code.substr( 0, versionStrIndex ) + "\n\n";
  70. const shaderCodes = this.build( shaderStage );
  71. finalCode += shaderCodes.defines;
  72. finalCode += code.substr( versionStrIndex );
  73. console.log( finalCode );
  74. return finalCode;
  75. }
  76. parse( vertexShader, fragmentShader ) {
  77. vertexShader = this.buildShader( 'vertex', vertexShader );
  78. fragmentShader = this.buildShader( 'fragment', fragmentShader );
  79. return {
  80. vertexShader,
  81. fragmentShader
  82. };
  83. }
  84. }
  85. export default WebGPUNodeBuilder;