WebGPUNodeBuilder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. import { LinearEncoding } from 'three';
  2. import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.js';
  3. import {
  4. FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
  5. ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
  6. } from './WebGPUNodeUniform.js';
  7. import WebGPUNodeSampler from './WebGPUNodeSampler.js';
  8. import { WebGPUNodeSampledTexture } from './WebGPUNodeSampledTexture.js';
  9. import WebGPUUniformBuffer from '../WebGPUUniformBuffer.js';
  10. import { getVectorLength, getStrideLength } from '../WebGPUBufferUtils.js';
  11. import VarNode from 'three-nodes/core/VarNode.js';
  12. import CodeNode from 'three-nodes/core/CodeNode.js';
  13. import BypassNode from 'three-nodes/core/BypassNode.js';
  14. import ExpressionNode from 'three-nodes/core/ExpressionNode.js';
  15. import NodeBuilder from 'three-nodes/core/NodeBuilder.js';
  16. import MaterialNode from 'three-nodes/accessors/MaterialNode.js';
  17. import PositionNode from 'three-nodes/accessors/PositionNode.js';
  18. import NormalNode from 'three-nodes/accessors/NormalNode.js';
  19. import ModelViewProjectionNode from 'three-nodes/accessors/ModelViewProjectionNode.js';
  20. import SkinningNode from 'three-nodes/accessors/SkinningNode.js';
  21. import ColorSpaceNode from 'three-nodes/display/ColorSpaceNode.js';
  22. import LightContextNode from 'three-nodes/lights/LightContextNode.js';
  23. import OperatorNode from 'three-nodes/math/OperatorNode.js';
  24. import WGSLNodeParser from 'three-nodes/parsers/WGSLNodeParser.js';
  25. import { vec3, add, join, mix, nodeObject } from 'three-nodes/ShaderNode.js';
  26. import { getRoughness } from 'three-nodes/functions/PhysicalMaterialFunctions.js';
  27. const wgslTypeLib = {
  28. float: 'f32',
  29. int: 'i32',
  30. uint: 'u32',
  31. bool: 'bool',
  32. vec2: 'vec2<f32>',
  33. vec3: 'vec3<f32>',
  34. vec4: 'vec4<f32>',
  35. uvec4: 'vec4<u32>',
  36. bvec3: 'vec3<bool>',
  37. mat3: 'mat3x3<f32>',
  38. mat4: 'mat4x4<f32>'
  39. };
  40. const wgslMethods = {
  41. dFdx: 'dpdx',
  42. dFdy: 'dpdy'
  43. };
  44. const wgslPolyfill = {
  45. lessThanEqual: new CodeNode( `
  46. fn lessThanEqual( a : vec3<f32>, b : vec3<f32> ) -> vec3<bool> {
  47. return vec3<bool>( a.x <= b.x, a.y <= b.y, a.z <= b.z );
  48. }
  49. ` ),
  50. mod: new CodeNode( `
  51. fn mod( x : f32, y : f32 ) -> f32 {
  52. return x - y * floor( x / y );
  53. }
  54. ` ),
  55. smoothstep: new CodeNode( `
  56. fn smoothstep( low : f32, high : f32, x : f32 ) -> f32 {
  57. let t = clamp( ( x - low ) / ( high - low ), 0.0, 1.0 );
  58. return t * t * ( 3.0 - 2.0 * t );
  59. }
  60. ` ),
  61. repeatWrapping: new CodeNode( `
  62. fn repeatWrapping( uv : vec2<f32>, dimension : vec2<i32> ) -> vec2<i32> {
  63. let uvScaled = vec2<i32>( uv * vec2<f32>( dimension ) );
  64. return ( ( uvScaled % dimension ) + dimension ) % dimension;
  65. }
  66. ` ),
  67. inversesqrt: new CodeNode( `
  68. fn inversesqrt( x : f32 ) -> f32 {
  69. return 1.0 / sqrt( x );
  70. }
  71. ` )
  72. };
  73. class WebGPUNodeBuilder extends NodeBuilder {
  74. constructor( object, renderer ) {
  75. super( object, renderer, new WGSLNodeParser() );
  76. this.lightNode = null;
  77. this.fogNode = null;
  78. this.bindings = { vertex: [], fragment: [] };
  79. this.bindingsOffset = { vertex: 0, fragment: 0 };
  80. this.uniformsGroup = {};
  81. }
  82. build() {
  83. this._parseObject();
  84. return super.build();
  85. }
  86. _parseObject() {
  87. const object = this.object;
  88. const material = this.material;
  89. // parse inputs
  90. if ( material.isMeshStandardMaterial || material.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
  91. let lightNode = material.lightNode;
  92. // VERTEX STAGE
  93. let vertex = new PositionNode( PositionNode.GEOMETRY );
  94. if ( lightNode === null && this.lightNode && this.lightNode.hasLights === true ) {
  95. lightNode = this.lightNode;
  96. }
  97. if ( material.positionNode && material.positionNode.isNode ) {
  98. const assignPositionNode = new OperatorNode( '=', new PositionNode( PositionNode.LOCAL ), material.positionNode );
  99. vertex = new BypassNode( vertex, assignPositionNode );
  100. }
  101. if ( object.isSkinnedMesh === true ) {
  102. vertex = new BypassNode( vertex, new SkinningNode( object ) );
  103. }
  104. this.context.vertex = vertex;
  105. this.addFlow( 'vertex', new VarNode( new ModelViewProjectionNode(), 'MVP', 'vec4' ) );
  106. // COLOR
  107. let colorNode = null;
  108. if ( material.colorNode && material.colorNode.isNode ) {
  109. colorNode = material.colorNode;
  110. } else {
  111. colorNode = new MaterialNode( MaterialNode.COLOR );
  112. }
  113. colorNode = this.addFlow( 'fragment', new VarNode( colorNode, 'Color', 'vec4' ) );
  114. const diffuseColorNode = this.addFlow( 'fragment', new VarNode( colorNode, 'DiffuseColor', 'vec4' ) );
  115. // OPACITY
  116. let opacityNode = null;
  117. if ( material.opacityNode && material.opacityNode.isNode ) {
  118. opacityNode = material.opacityNode;
  119. } else {
  120. opacityNode = new VarNode( new MaterialNode( MaterialNode.OPACITY ) );
  121. }
  122. this.addFlow( 'fragment', new VarNode( opacityNode, 'OPACITY', 'float' ) );
  123. this.addFlow( 'fragment', new ExpressionNode( 'DiffuseColor.a = DiffuseColor.a * OPACITY;' ) );
  124. // ALPHA TEST
  125. let alphaTest = null;
  126. if ( material.alphaTestNode && material.alphaTestNode.isNode ) {
  127. alphaTest = material.alphaTestNode;
  128. } else if ( material.alphaTest > 0 ) {
  129. alphaTest = new MaterialNode( MaterialNode.ALPHA_TEST );
  130. }
  131. if ( alphaTest !== null ) {
  132. this.addFlow( 'fragment', new VarNode( alphaTest, 'AlphaTest', 'float' ) );
  133. this.addFlow( 'fragment', new ExpressionNode( 'if ( DiffuseColor.a <= AlphaTest ) { discard; }' ) );
  134. }
  135. if ( material.isMeshStandardMaterial ) {
  136. // METALNESS
  137. let metalnessNode = null;
  138. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  139. metalnessNode = material.metalnessNode;
  140. } else {
  141. metalnessNode = new MaterialNode( MaterialNode.METALNESS );
  142. }
  143. this.addFlow( 'fragment', new VarNode( metalnessNode, 'Metalness', 'float' ) );
  144. this.addFlow( 'fragment', new ExpressionNode( 'DiffuseColor = vec4<f32>( DiffuseColor.rgb * ( 1.0 - Metalness ), DiffuseColor.a );' ) );
  145. // ROUGHNESS
  146. let roughnessNode = null;
  147. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  148. roughnessNode = material.roughnessNode;
  149. } else {
  150. roughnessNode = new MaterialNode( MaterialNode.ROUGHNESS );
  151. }
  152. roughnessNode = getRoughness( { roughness: roughnessNode } );
  153. this.addFlow( 'fragment', new VarNode( roughnessNode, 'Roughness', 'float' ) );
  154. // SPECULAR_TINT
  155. this.addFlow( 'fragment', new VarNode( new ExpressionNode( 'mix( vec3<f32>( 0.04 ), Color.rgb, Metalness )', 'vec3' ), 'SpecularColor', 'color' ) );
  156. // NORMAL_VIEW
  157. let normalNode = null;
  158. if ( material.normalNode && material.normalNode.isNode ) {
  159. normalNode = material.normalNode;
  160. } else {
  161. normalNode = new NormalNode( NormalNode.VIEW );
  162. }
  163. this.addFlow( 'fragment', new VarNode( normalNode, 'TransformedNormalView', 'vec3' ) );
  164. }
  165. // LIGHT
  166. let outputNode = diffuseColorNode;
  167. if ( lightNode && lightNode.isNode ) {
  168. const lightContextNode = new LightContextNode( lightNode );
  169. outputNode = this.addFlow( 'fragment', new VarNode( lightContextNode, 'Light', 'vec3' ) );
  170. }
  171. // OUTGOING LIGHT
  172. let outgoingLightNode = vec3( outputNode );
  173. // EMISSIVE
  174. const emissiveNode = material.emissiveNode;
  175. if ( emissiveNode && emissiveNode.isNode ) {
  176. outgoingLightNode = add( emissiveNode, outgoingLightNode );
  177. }
  178. // OUTPUT
  179. outputNode = join( vec3( outgoingLightNode ), nodeObject( diffuseColorNode ).w );
  180. // ENCODING
  181. const outputEncoding = this.renderer.outputEncoding;
  182. if ( outputEncoding !== LinearEncoding ) {
  183. outputNode = new ColorSpaceNode( ColorSpaceNode.LINEAR_TO_LINEAR, outputNode );
  184. outputNode.fromEncoding( outputEncoding );
  185. }
  186. // FOG
  187. const fogNode = this.fogNode;
  188. if ( fogNode && fogNode.isFogNode ) {
  189. outputNode = mix( outputNode, fogNode.colorNode, fogNode );
  190. }
  191. // RESULT
  192. this.addFlow( 'fragment', new VarNode( outputNode, 'Output', 'vec4' ) );
  193. }
  194. }
  195. addFlowCode( code ) {
  196. if ( ! /;\s*$/.test( code ) ) {
  197. code += ';';
  198. }
  199. super.addFlowCode( code + '\n\t' );
  200. }
  201. getTexture( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  202. if ( shaderStage === 'fragment' ) {
  203. return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`;
  204. } else {
  205. this._include( 'repeatWrapping' );
  206. const dimension = `textureDimensions( ${textureProperty}, 0 )`;
  207. return `textureLoad( ${textureProperty}, repeatWrapping( ${uvSnippet}, ${dimension} ), 0 )`;
  208. }
  209. }
  210. getPropertyName( node, shaderStage = this.shaderStage ) {
  211. if ( node.isNodeVary === true ) {
  212. if ( shaderStage === 'vertex' ) {
  213. return `NodeVarys.${ node.name }`;
  214. }
  215. } else if ( node.isNodeUniform === true ) {
  216. const name = node.name;
  217. const type = node.type;
  218. if ( type === 'texture' ) {
  219. return name;
  220. } else if ( type === 'buffer' ) {
  221. return `NodeBuffer.${name}`;
  222. } else {
  223. return `NodeUniforms.${name}`;
  224. }
  225. }
  226. return super.getPropertyName( node );
  227. }
  228. getBindings() {
  229. const bindings = this.bindings;
  230. return [ ...bindings.vertex, ...bindings.fragment ];
  231. }
  232. getUniformFromNode( node, shaderStage, type ) {
  233. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  234. const nodeData = this.getDataFromNode( node, shaderStage );
  235. if ( nodeData.uniformGPU === undefined ) {
  236. let uniformGPU;
  237. const bindings = this.bindings[ shaderStage ];
  238. if ( type === 'texture' ) {
  239. const sampler = new WebGPUNodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
  240. const texture = new WebGPUNodeSampledTexture( uniformNode.name, uniformNode.node );
  241. // add first textures in sequence and group for last
  242. const lastBinding = bindings[ bindings.length - 1 ];
  243. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  244. if ( shaderStage === 'fragment' ) {
  245. bindings.splice( index, 0, sampler, texture );
  246. uniformGPU = [ sampler, texture ];
  247. } else {
  248. bindings.splice( index, 0, texture );
  249. uniformGPU = [ texture ];
  250. }
  251. } else if ( type === 'buffer' ) {
  252. const buffer = new WebGPUUniformBuffer( 'NodeBuffer', node.value );
  253. // add first textures in sequence and group for last
  254. const lastBinding = bindings[ bindings.length - 1 ];
  255. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  256. bindings.splice( index, 0, buffer );
  257. uniformGPU = buffer;
  258. } else {
  259. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  260. if ( uniformsGroup === undefined ) {
  261. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  262. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  263. bindings.push( uniformsGroup );
  264. }
  265. if ( node.isArrayInputNode === true ) {
  266. uniformGPU = [];
  267. for ( const inputNode of node.nodes ) {
  268. const uniformNodeGPU = this._getNodeUniform( inputNode, type );
  269. // fit bounds to buffer
  270. uniformNodeGPU.boundary = getVectorLength( uniformNodeGPU.itemSize );
  271. uniformNodeGPU.itemSize = getStrideLength( uniformNodeGPU.itemSize );
  272. uniformsGroup.addUniform( uniformNodeGPU );
  273. uniformGPU.push( uniformNodeGPU );
  274. }
  275. } else {
  276. uniformGPU = this._getNodeUniform( uniformNode, type );
  277. uniformsGroup.addUniform( uniformGPU );
  278. }
  279. }
  280. nodeData.uniformGPU = uniformGPU;
  281. if ( shaderStage === 'vertex' ) {
  282. this.bindingsOffset[ 'fragment' ] = bindings.length;
  283. }
  284. }
  285. return uniformNode;
  286. }
  287. getAttributes( shaderStage ) {
  288. let snippet = '';
  289. if ( shaderStage === 'vertex' ) {
  290. const attributes = this.attributes;
  291. const length = attributes.length;
  292. snippet += '\n';
  293. for ( let index = 0; index < length; index ++ ) {
  294. const attribute = attributes[ index ];
  295. const name = attribute.name;
  296. const type = this.getType( attribute.type );
  297. snippet += `\t@location( ${index} ) ${ name } : ${ type }`;
  298. if ( index + 1 < length ) {
  299. snippet += ',\n';
  300. }
  301. }
  302. snippet += '\n';
  303. }
  304. return snippet;
  305. }
  306. getVars( shaderStage ) {
  307. let snippet = '';
  308. const vars = this.vars[ shaderStage ];
  309. for ( let index = 0; index < vars.length; index ++ ) {
  310. const variable = vars[ index ];
  311. const name = variable.name;
  312. const type = this.getType( variable.type );
  313. snippet += `var ${name} : ${type}; `;
  314. }
  315. return snippet;
  316. }
  317. getVarys( shaderStage ) {
  318. let snippet = '';
  319. if ( shaderStage === 'vertex' ) {
  320. snippet += '\t@builtin( position ) Vertex: vec4<f32>;\n';
  321. const varys = this.varys;
  322. for ( let index = 0; index < varys.length; index ++ ) {
  323. const vary = varys[ index ];
  324. snippet += `\t@location( ${index} ) ${ vary.name } : ${ this.getType( vary.type ) };\n`;
  325. }
  326. snippet = this._getWGSLStruct( 'NodeVarysStruct', snippet );
  327. } else if ( shaderStage === 'fragment' ) {
  328. const varys = this.varys;
  329. snippet += '\n';
  330. for ( let index = 0; index < varys.length; index ++ ) {
  331. const vary = varys[ index ];
  332. snippet += `\t@location( ${index} ) ${ vary.name } : ${ this.getType( vary.type ) }`;
  333. if ( index + 1 < varys.length ) {
  334. snippet += ',\n';
  335. }
  336. }
  337. snippet += '\n';
  338. }
  339. return snippet;
  340. }
  341. getUniforms( shaderStage ) {
  342. const uniforms = this.uniforms[ shaderStage ];
  343. let snippet = '';
  344. let groupSnippet = '';
  345. let index = this.bindingsOffset[ shaderStage ];
  346. for ( const uniform of uniforms ) {
  347. if ( uniform.type === 'texture' ) {
  348. if ( shaderStage === 'fragment' ) {
  349. snippet += `@group( 0 ) @binding( ${index ++} ) var ${uniform.name}_sampler : sampler; `;
  350. }
  351. snippet += `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_2d<f32>; `;
  352. } else if ( uniform.type === 'buffer' ) {
  353. const bufferNode = uniform.node;
  354. const bufferType = this.getType( bufferNode.bufferType );
  355. const bufferCount = bufferNode.bufferCount;
  356. const bufferSnippet = `\t${uniform.name} : array< ${bufferType}, ${bufferCount} >;\n`;
  357. snippet += this._getWGSLUniforms( 'NodeBuffer', bufferSnippet, index ++ ) + '\n\n';
  358. } else {
  359. const vectorType = this.getType( this.getVectorType( uniform.type ) );
  360. if ( Array.isArray( uniform.value ) === true ) {
  361. const length = uniform.value.length;
  362. groupSnippet += `uniform ${vectorType}[ ${length} ] ${uniform.name}; `;
  363. } else {
  364. groupSnippet += `\t${uniform.name} : ${ vectorType};\n`;
  365. }
  366. }
  367. }
  368. if ( groupSnippet ) {
  369. snippet += this._getWGSLUniforms( 'NodeUniforms', groupSnippet, index ++ );
  370. }
  371. return snippet;
  372. }
  373. buildCode() {
  374. const shadersData = { fragment: {}, vertex: {} };
  375. for ( const shaderStage in shadersData ) {
  376. let flow = '// code\n';
  377. flow += `\t${ this.flowCode[ shaderStage ] }`;
  378. flow += '\n';
  379. const flowNodes = this.flowNodes[ shaderStage ];
  380. const mainNode = flowNodes[ flowNodes.length - 1 ];
  381. for ( const node of flowNodes ) {
  382. const flowSlotData = this.getFlowData( shaderStage, node );
  383. const slotName = node.name;
  384. if ( slotName ) {
  385. if ( flow.length > 0 ) flow += '\n';
  386. flow += `\t// FLOW -> ${ slotName }\n\t`;
  387. }
  388. flow += `${ flowSlotData.code }\n\t`;
  389. if ( node === mainNode ) {
  390. flow += '// FLOW RESULT\n\t';
  391. if ( shaderStage === 'vertex' ) {
  392. flow += 'NodeVarys.Vertex = ';
  393. } else if ( shaderStage === 'fragment' ) {
  394. flow += 'return ';
  395. }
  396. flow += `${ flowSlotData.result };`;
  397. }
  398. }
  399. const stageData = shadersData[ shaderStage ];
  400. stageData.uniforms = this.getUniforms( shaderStage );
  401. stageData.attributes = this.getAttributes( shaderStage );
  402. stageData.varys = this.getVarys( shaderStage );
  403. stageData.vars = this.getVars( shaderStage );
  404. stageData.codes = this.getCodes( shaderStage );
  405. stageData.flow = flow;
  406. }
  407. this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
  408. this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
  409. }
  410. getMethod( method ) {
  411. if ( wgslPolyfill[ method ] !== undefined ) {
  412. this._include( method );
  413. }
  414. return wgslMethods[ method ] || method;
  415. }
  416. getType( type ) {
  417. return wgslTypeLib[ type ] || type;
  418. }
  419. _include( name ) {
  420. wgslPolyfill[ name ].build( this );
  421. }
  422. _getNodeUniform( uniformNode, type ) {
  423. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  424. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  425. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  426. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  427. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  428. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  429. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  430. throw new Error( `Uniform "${type}" not declared.` );
  431. }
  432. _getWGSLVertexCode( shaderData ) {
  433. return `${ this.getSignature() }
  434. // uniforms
  435. ${shaderData.uniforms}
  436. // varys
  437. ${shaderData.varys}
  438. // codes
  439. ${shaderData.codes}
  440. @stage( vertex )
  441. fn main( ${shaderData.attributes} ) -> NodeVarysStruct {
  442. // system
  443. var NodeVarys: NodeVarysStruct;
  444. // vars
  445. ${shaderData.vars}
  446. // flow
  447. ${shaderData.flow}
  448. return NodeVarys;
  449. }
  450. `;
  451. }
  452. _getWGSLFragmentCode( shaderData ) {
  453. return `${ this.getSignature() }
  454. // uniforms
  455. ${shaderData.uniforms}
  456. // codes
  457. ${shaderData.codes}
  458. @stage( fragment )
  459. fn main( ${shaderData.varys} ) -> @location( 0 ) vec4<f32> {
  460. // vars
  461. ${shaderData.vars}
  462. // flow
  463. ${shaderData.flow}
  464. }
  465. `;
  466. }
  467. _getWGSLStruct( name, vars ) {
  468. return `
  469. struct ${name} {
  470. \n${vars}
  471. };`;
  472. }
  473. _getWGSLUniforms( name, vars, binding = 0, group = 0 ) {
  474. const structName = name + 'Struct';
  475. const structSnippet = this._getWGSLStruct( structName, vars );
  476. return `${structSnippet}
  477. @binding( ${binding} ) @group( ${group} )
  478. var<uniform> ${name} : ${structName};`;
  479. }
  480. }
  481. export default WebGPUNodeBuilder;