WebGLNodeBuilder.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. import { defaultShaderStages, NodeFrame, MathNode, GLSLNodeParser, NodeBuilder } from 'three/nodes';
  2. import SlotNode from './SlotNode.js';
  3. import { PerspectiveCamera, ShaderChunk, ShaderLib, UniformsUtils, UniformsLib } from 'three';
  4. const nodeFrame = new NodeFrame();
  5. nodeFrame.camera = new PerspectiveCamera();
  6. const nodeShaderLib = {
  7. LineBasicNodeMaterial: ShaderLib.basic,
  8. MeshBasicNodeMaterial: ShaderLib.basic,
  9. PointsNodeMaterial: ShaderLib.points,
  10. MeshStandardNodeMaterial: ShaderLib.standard,
  11. MeshPhysicalNodeMaterial: ShaderLib.physical
  12. };
  13. const glslMethods = {
  14. [ MathNode.ATAN2 ]: 'atan'
  15. };
  16. function getIncludeSnippet( name ) {
  17. return `#include <${name}>`;
  18. }
  19. function getShaderStageProperty( shaderStage ) {
  20. return `${shaderStage}Shader`;
  21. }
  22. class WebGLNodeBuilder extends NodeBuilder {
  23. constructor( object, renderer, shader ) {
  24. super( object, renderer, new GLSLNodeParser() );
  25. this.shader = shader;
  26. this.slots = { vertex: [], fragment: [] };
  27. this._parseShaderLib();
  28. this._parseInclude( 'fragment', 'lights_physical_fragment', 'clearcoat_normal_fragment_begin', 'transmission_fragment' );
  29. this._parseObject();
  30. this._sortSlotsToFlow();
  31. }
  32. getMethod( method ) {
  33. return glslMethods[ method ] || method;
  34. }
  35. addSlot( shaderStage, slotNode ) {
  36. this.slots[ shaderStage ].push( slotNode );
  37. }
  38. addFlowCode( code ) {
  39. if ( ! /;\s*$/.test( code ) ) {
  40. code += ';';
  41. }
  42. super.addFlowCode( code + '\n\t' );
  43. }
  44. _parseShaderLib() {
  45. const material = this.material;
  46. let type = material.type;
  47. // see https://github.com/mrdoob/three.js/issues/23707
  48. if ( material.isMeshPhysicalNodeMaterial ) type = 'MeshPhysicalNodeMaterial';
  49. else if ( material.isMeshStandardNodeMaterial ) type = 'MeshStandardNodeMaterial';
  50. else if ( material.isMeshBasicNodeMaterial ) type = 'MeshBasicNodeMaterial';
  51. else if ( material.isPointsNodeMaterial ) type = 'PointsNodeMaterial';
  52. else if ( material.isLineBasicNodeMaterial ) type = 'LineBasicNodeMaterial';
  53. // shader lib
  54. if ( nodeShaderLib[ type ] !== undefined ) {
  55. const shaderLib = nodeShaderLib[ type ];
  56. const shader = this.shader;
  57. shader.vertexShader = shaderLib.vertexShader;
  58. shader.fragmentShader = shaderLib.fragmentShader;
  59. shader.uniforms = UniformsUtils.merge( [ shaderLib.uniforms, UniformsLib.lights ] );
  60. }
  61. }
  62. _parseObject() {
  63. const { material, renderer } = this;
  64. if ( renderer.toneMappingNode && renderer.toneMappingNode.isNode === true ) {
  65. this.addSlot( 'fragment', new SlotNode( {
  66. node: material.colorNode,
  67. nodeType: 'vec4',
  68. source: getIncludeSnippet( 'tonemapping_fragment' ),
  69. target: ''
  70. } ) );
  71. }
  72. // parse inputs
  73. if ( material.colorNode && material.colorNode.isNode ) {
  74. this.addSlot( 'fragment', new SlotNode( {
  75. node: material.colorNode,
  76. nodeType: 'vec4',
  77. source: 'vec4 diffuseColor = vec4( diffuse, opacity );',
  78. target: 'vec4 diffuseColor = %RESULT%;'
  79. } ) );
  80. }
  81. if ( material.opacityNode && material.opacityNode.isNode ) {
  82. this.addSlot( 'fragment', new SlotNode( {
  83. node: material.opacityNode,
  84. nodeType: 'float',
  85. source: getIncludeSnippet( 'alphatest_fragment' ),
  86. target: 'diffuseColor.a = %RESULT%;',
  87. inclusionType: 'append'
  88. } ) );
  89. } else {
  90. this.addCode( 'fragment', getIncludeSnippet( 'alphatest_fragment' ), 'diffuseColor.a = opacity;', this.shader );
  91. }
  92. if ( material.normalNode && material.normalNode.isNode ) {
  93. this.addSlot( 'fragment', new SlotNode( {
  94. node: material.normalNode,
  95. nodeType: 'vec3',
  96. source: getIncludeSnippet( 'normal_fragment_begin' ),
  97. target: 'normal = %RESULT%;',
  98. inclusionType: 'append'
  99. } ) );
  100. }
  101. if ( material.emissiveNode && material.emissiveNode.isNode ) {
  102. this.addSlot( 'fragment', new SlotNode( {
  103. node: material.emissiveNode,
  104. nodeType: 'vec3',
  105. source: getIncludeSnippet( 'emissivemap_fragment' ),
  106. target: 'totalEmissiveRadiance = %RESULT%;',
  107. inclusionType: 'append'
  108. } ) );
  109. }
  110. if ( material.isMeshStandardNodeMaterial ) {
  111. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  112. this.addSlot( 'fragment', new SlotNode( {
  113. node: material.metalnessNode,
  114. nodeType: 'float',
  115. source: getIncludeSnippet( 'metalnessmap_fragment' ),
  116. target: 'metalnessFactor = %RESULT%;',
  117. inclusionType: 'append'
  118. } ) );
  119. }
  120. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  121. this.addSlot( 'fragment', new SlotNode( {
  122. node: material.roughnessNode,
  123. nodeType: 'float',
  124. source: getIncludeSnippet( 'roughnessmap_fragment' ),
  125. target: 'roughnessFactor = %RESULT%;',
  126. inclusionType: 'append'
  127. } ) );
  128. }
  129. if ( material.isMeshPhysicalNodeMaterial ) {
  130. if ( material.clearcoatNode && material.clearcoatNode.isNode ) {
  131. this.addSlot( 'fragment', new SlotNode( {
  132. node: material.clearcoatNode,
  133. nodeType: 'float',
  134. source: 'material.clearcoat = clearcoat;',
  135. target: 'material.clearcoat = %RESULT%;'
  136. } ) );
  137. if ( material.clearcoatRoughnessNode && material.clearcoatRoughnessNode.isNode ) {
  138. this.addSlot( 'fragment', new SlotNode( {
  139. node: material.clearcoatRoughnessNode,
  140. nodeType: 'float',
  141. source: 'material.clearcoatRoughness = clearcoatRoughness;',
  142. target: 'material.clearcoatRoughness = %RESULT%;'
  143. } ) );
  144. }
  145. if ( material.clearcoatNormalNode && material.clearcoatNormalNode.isNode ) {
  146. this.addSlot( 'fragment', new SlotNode( {
  147. node: material.clearcoatNormalNode,
  148. nodeType: 'vec3',
  149. source: 'vec3 clearcoatNormal = geometryNormal;',
  150. target: 'vec3 clearcoatNormal = %RESULT%;'
  151. } ) );
  152. }
  153. material.defines.USE_CLEARCOAT = '';
  154. } else {
  155. delete material.defines.USE_CLEARCOAT;
  156. }
  157. if ( material.sheenNode && material.sheenNode.isNode ) {
  158. this.addSlot( 'fragment', new SlotNode( {
  159. node: material.sheenNode,
  160. nodeType: 'vec3',
  161. source: 'material.sheenColor = sheenColor;',
  162. target: 'material.sheenColor = %RESULT%;'
  163. } ) );
  164. if ( material.sheenRoughnessNode && material.sheenRoughnessNode.isNode ) {
  165. this.addSlot( 'fragment', new SlotNode( {
  166. node: material.sheenRoughnessNode,
  167. nodeType: 'float',
  168. source: 'material.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 );',
  169. target: 'material.sheenRoughness = clamp( %RESULT%, 0.07, 1.0 );'
  170. } ) );
  171. }
  172. material.defines.USE_SHEEN = '';
  173. } else {
  174. delete material.defines.USE_SHEEN;
  175. }
  176. if ( material.iridescenceNode && material.iridescenceNode.isNode ) {
  177. this.addSlot( 'fragment', new SlotNode( {
  178. node: material.iridescenceNode,
  179. nodeType: 'float',
  180. source: 'material.iridescence = iridescence;',
  181. target: 'material.iridescence = %RESULT%;'
  182. } ) );
  183. if ( material.iridescenceIORNode && material.iridescenceIORNode.isNode ) {
  184. this.addSlot( 'fragment', new SlotNode( {
  185. node: material.iridescenceIORNode,
  186. nodeType: 'float',
  187. source: 'material.iridescenceIOR = iridescenceIOR;',
  188. target: 'material.iridescenceIOR = %RESULT%;'
  189. } ) );
  190. }
  191. if ( material.iridescenceThicknessNode && material.iridescenceThicknessNode.isNode ) {
  192. this.addSlot( 'fragment', new SlotNode( {
  193. node: material.iridescenceThicknessNode,
  194. nodeType: 'float',
  195. source: 'material.iridescenceThickness = iridescenceThicknessMaximum;',
  196. target: 'material.iridescenceThickness = %RESULT%;'
  197. } ) );
  198. }
  199. material.defines.USE_IRIDESCENCE = '';
  200. } else {
  201. delete material.defines.USE_IRIDESCENCE;
  202. }
  203. if ( material.iorNode && material.iorNode.isNode ) {
  204. this.addSlot( 'fragment', new SlotNode( {
  205. node: material.iorNode,
  206. nodeType: 'float',
  207. source: 'material.ior = ior;',
  208. target: 'material.ior = %RESULT%;'
  209. } ) );
  210. }
  211. if ( material.specularColorNode && material.specularColorNode.isNode ) {
  212. this.addSlot( 'fragment', new SlotNode( {
  213. node: material.specularColorNode,
  214. nodeType: 'vec3',
  215. source: 'vec3 specularColorFactor = specularColor;',
  216. target: 'vec3 specularColorFactor = %RESULT%;'
  217. } ) );
  218. }
  219. if ( material.specularIntensityNode && material.specularIntensityNode.isNode ) {
  220. this.addSlot( 'fragment', new SlotNode( {
  221. node: material.specularIntensityNode,
  222. nodeType: 'float',
  223. source: 'float specularIntensityFactor = specularIntensity;',
  224. target: 'float specularIntensityFactor = %RESULT%;'
  225. } ) );
  226. }
  227. if ( material.transmissionNode && material.transmissionNode.isNode ) {
  228. this.addSlot( 'fragment', new SlotNode( {
  229. node: material.transmissionNode,
  230. nodeType: 'float',
  231. source: 'material.transmission = transmission;',
  232. target: 'material.transmission = %RESULT%;'
  233. } ) );
  234. if ( material.thicknessNode && material.thicknessNode.isNode ) {
  235. this.addSlot( 'fragment', new SlotNode( {
  236. node: material.thicknessNode,
  237. nodeType: 'float',
  238. source: 'material.thickness = thickness;',
  239. target: 'material.thickness = %RESULT%;'
  240. } ) );
  241. }
  242. if ( material.thicknessNode && material.thicknessNode.isNode ) {
  243. this.addSlot( 'fragment', new SlotNode( {
  244. node: material.thicknessNode,
  245. nodeType: 'float',
  246. source: 'material.thickness = thickness;',
  247. target: 'material.thickness = %RESULT%;'
  248. } ) );
  249. }
  250. if ( material.attenuationDistanceNode && material.attenuationDistanceNode.isNode ) {
  251. this.addSlot( 'fragment', new SlotNode( {
  252. node: material.attenuationDistanceNode,
  253. nodeType: 'float',
  254. source: 'material.attenuationDistance = attenuationDistance;',
  255. target: 'material.attenuationDistance = %RESULT%;'
  256. } ) );
  257. }
  258. if ( material.attenuationColorNode && material.attenuationColorNode.isNode ) {
  259. this.addSlot( 'fragment', new SlotNode( {
  260. node: material.attenuationColorNode,
  261. nodeType: 'vec3',
  262. source: 'material.attenuationColor = attenuationColor;',
  263. target: 'material.attenuationColor = %RESULT%;'
  264. } ) );
  265. }
  266. material.transmission = 1;
  267. material.defines.USE_TRANSMISSION = '';
  268. } else {
  269. material.transmission = 0;
  270. delete material.defines.USE_TRANSMISSION;
  271. }
  272. }
  273. }
  274. //
  275. if ( material.positionNode && material.positionNode.isNode ) {
  276. this.addSlot( 'vertex', new SlotNode( {
  277. node: material.positionNode,
  278. nodeType: 'vec3',
  279. source: getIncludeSnippet( 'begin_vertex' ),
  280. target: 'transformed = %RESULT%;',
  281. inclusionType: 'append'
  282. } ) );
  283. }
  284. if ( material.sizeNode && material.sizeNode.isNode ) {
  285. this.addSlot( 'vertex', new SlotNode( {
  286. node: material.sizeNode,
  287. nodeType: 'float',
  288. source: 'gl_PointSize = size;',
  289. target: 'gl_PointSize = %RESULT%;'
  290. } ) );
  291. }
  292. }
  293. getTexture( textureProperty, uvSnippet ) {
  294. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  295. }
  296. getTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  297. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  298. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  299. }
  300. getCubeTexture( textureProperty, uvSnippet ) {
  301. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  302. }
  303. getCubeTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  304. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  305. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  306. }
  307. getUniforms( shaderStage ) {
  308. const uniforms = this.uniforms[ shaderStage ];
  309. let snippet = '';
  310. for ( const uniform of uniforms ) {
  311. if ( uniform.type === 'texture' ) {
  312. snippet += `uniform sampler2D ${uniform.name}; `;
  313. } else if ( uniform.type === 'cubeTexture' ) {
  314. snippet += `uniform samplerCube ${uniform.name}; `;
  315. } else {
  316. const vectorType = this.getVectorType( uniform.type );
  317. snippet += `uniform ${vectorType} ${uniform.name}; `;
  318. }
  319. }
  320. return snippet;
  321. }
  322. getAttributes( shaderStage ) {
  323. let snippet = '';
  324. if ( shaderStage === 'vertex' ) {
  325. const attributes = this.attributes;
  326. for ( const attribute of attributes ) {
  327. // ignore common attributes to prevent redefinitions
  328. if ( attribute.name === 'uv' || attribute.name === 'position' || attribute.name === 'normal' )
  329. continue;
  330. snippet += `attribute ${attribute.type} ${attribute.name}; `;
  331. }
  332. }
  333. return snippet;
  334. }
  335. getVaryings( shaderStage ) {
  336. let snippet = '';
  337. const varyings = this.varyings;
  338. if ( shaderStage === 'vertex' ) {
  339. for ( const varying of varyings ) {
  340. snippet += `${varying.needsInterpolation ? 'varying' : '/*varying*/'} ${varying.type} ${varying.name}; `;
  341. }
  342. } else if ( shaderStage === 'fragment' ) {
  343. for ( const varying of varyings ) {
  344. if ( varying.needsInterpolation ) {
  345. snippet += `varying ${varying.type} ${varying.name}; `;
  346. }
  347. }
  348. }
  349. return snippet;
  350. }
  351. addCode( shaderStage, source, code, scope = this ) {
  352. const shaderProperty = getShaderStageProperty( shaderStage );
  353. let snippet = scope[ shaderProperty ];
  354. const index = snippet.indexOf( source );
  355. if ( index !== - 1 ) {
  356. const start = snippet.substring( 0, index + source.length );
  357. const end = snippet.substring( index + source.length );
  358. snippet = `${start}\n${code}\n${end}`;
  359. }
  360. scope[ shaderProperty ] = snippet;
  361. }
  362. replaceCode( shaderStage, source, target, scope = this ) {
  363. const shaderProperty = getShaderStageProperty( shaderStage );
  364. scope[ shaderProperty ] = scope[ shaderProperty ].replaceAll( source, target );
  365. }
  366. getFrontFacing() {
  367. return 'gl_FrontFacing';
  368. }
  369. getFragCoord() {
  370. return 'gl_FragCoord';
  371. }
  372. isFlipY() {
  373. return true;
  374. }
  375. buildCode() {
  376. const shaderData = {};
  377. for ( const shaderStage of defaultShaderStages ) {
  378. const uniforms = this.getUniforms( shaderStage );
  379. const attributes = this.getAttributes( shaderStage );
  380. const varyings = this.getVaryings( shaderStage );
  381. const vars = this.getVars( shaderStage );
  382. const codes = this.getCodes( shaderStage );
  383. shaderData[ shaderStage ] = `${this.getSignature()}
  384. // <node_builder>
  385. // uniforms
  386. ${uniforms}
  387. // attributes
  388. ${attributes}
  389. // varyings
  390. ${varyings}
  391. // vars
  392. ${vars}
  393. // codes
  394. ${codes}
  395. // </node_builder>
  396. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  397. `;
  398. }
  399. this.vertexShader = shaderData.vertex;
  400. this.fragmentShader = shaderData.fragment;
  401. }
  402. build() {
  403. super.build();
  404. this._addSnippets();
  405. this._addUniforms();
  406. this._updateUniforms();
  407. this.shader.vertexShader = this.vertexShader;
  408. this.shader.fragmentShader = this.fragmentShader;
  409. return this;
  410. }
  411. _parseInclude( shaderStage, ...includes ) {
  412. for ( const name of includes ) {
  413. const includeSnippet = getIncludeSnippet( name );
  414. const code = ShaderChunk[ name ];
  415. const shaderProperty = getShaderStageProperty( shaderStage );
  416. this.shader[ shaderProperty ] = this.shader[ shaderProperty ].replaceAll( includeSnippet, code );
  417. }
  418. }
  419. _sortSlotsToFlow() {
  420. for ( const shaderStage of defaultShaderStages ) {
  421. const sourceCode = this.shader[ getShaderStageProperty( shaderStage ) ];
  422. const slots = this.slots[ shaderStage ].sort( ( slotA, slotB ) => {
  423. return sourceCode.indexOf( slotA.source ) > sourceCode.indexOf( slotB.source ) ? 1 : - 1;
  424. } );
  425. for ( const slotNode of slots ) {
  426. this.addFlow( shaderStage, slotNode );
  427. }
  428. }
  429. }
  430. _addSnippets() {
  431. for ( const shaderStage of defaultShaderStages ) {
  432. for ( const slotNode of this.slots[ shaderStage ] ) {
  433. const flowData = this.getFlowData( slotNode/*, shaderStage*/ );
  434. const inclusionType = slotNode.inclusionType;
  435. const source = slotNode.source;
  436. const target = flowData.code + '\n\t' + slotNode.target.replace( '%RESULT%', flowData.result );
  437. if ( inclusionType === 'append' ) {
  438. this.addCode( shaderStage, source, target );
  439. } else if ( inclusionType === 'replace' ) {
  440. this.replaceCode( shaderStage, source, target );
  441. } else {
  442. console.warn( `Inclusion type "${ inclusionType }" not compatible.` );
  443. }
  444. }
  445. this.addCode(
  446. shaderStage,
  447. 'main() {',
  448. '\n\t' + this.flowCode[ shaderStage ]
  449. );
  450. }
  451. }
  452. _addUniforms() {
  453. for ( const shaderStage of defaultShaderStages ) {
  454. // uniforms
  455. for ( const uniform of this.uniforms[ shaderStage ] ) {
  456. this.shader.uniforms[ uniform.name ] = uniform;
  457. }
  458. }
  459. }
  460. _updateUniforms() {
  461. nodeFrame.object = this.object;
  462. nodeFrame.renderer = this.renderer;
  463. for ( const node of this.updateNodes ) {
  464. nodeFrame.updateNode( node );
  465. }
  466. }
  467. }
  468. export { WebGLNodeBuilder };