WebGLNodeBuilder.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. import NodeBuilder, { defaultShaderStages } from 'three-nodes/core/NodeBuilder.js';
  2. import NodeFrame from 'three-nodes/core/NodeFrame.js';
  3. import SlotNode from './SlotNode.js';
  4. import GLSLNodeParser from 'three-nodes/parsers/GLSLNodeParser.js';
  5. import WebGLPhysicalContextNode from './WebGLPhysicalContextNode.js';
  6. import { PerspectiveCamera, ShaderChunk, ShaderLib, UniformsUtils, UniformsLib,
  7. LinearEncoding, RGBAFormat, UnsignedByteType, sRGBEncoding } from 'three';
  8. const nodeFrame = new NodeFrame();
  9. nodeFrame.camera = new PerspectiveCamera();
  10. const nodeShaderLib = {
  11. LineBasicNodeMaterial: ShaderLib.basic,
  12. MeshBasicNodeMaterial: ShaderLib.basic,
  13. PointsNodeMaterial: ShaderLib.points,
  14. MeshStandardNodeMaterial: ShaderLib.standard,
  15. MeshPhysicalMaterial: ShaderLib.physical
  16. };
  17. function getIncludeSnippet( name ) {
  18. return `#include <${name}>`;
  19. }
  20. function getShaderStageProperty( shaderStage ) {
  21. return `${shaderStage}Shader`;
  22. }
  23. class WebGLNodeBuilder extends NodeBuilder {
  24. constructor( object, renderer, shader ) {
  25. super( object, renderer, new GLSLNodeParser() );
  26. this.shader = shader;
  27. this.slots = { vertex: [], fragment: [] };
  28. this._parseObject();
  29. }
  30. addSlot( shaderStage, slotNode ) {
  31. this.slots[ shaderStage ].push( slotNode );
  32. return this.addFlow( shaderStage, slotNode );
  33. }
  34. addFlowCode( code ) {
  35. if ( ! /;\s*$/.test( code ) ) {
  36. code += ';';
  37. }
  38. super.addFlowCode( code + '\n\t' );
  39. }
  40. _parseObject() {
  41. const { material, renderer } = this;
  42. let type = material.type;
  43. // shader lib
  44. if ( material.isMeshPhysicalNodeMaterial ) type = 'MeshPhysicalMaterial';
  45. else if ( material.isMeshStandardNodeMaterial ) type = 'MeshStandardNodeMaterial';
  46. else if ( material.isMeshBasicNodeMaterial ) type = 'MeshBasicNodeMaterial';
  47. else if ( material.isPointsNodeMaterial ) type = 'PointsNodeMaterial';
  48. else if ( material.isLineBasicNodeMaterial ) type = 'LineBasicNodeMaterial';
  49. if ( nodeShaderLib[ type ] !== undefined ) {
  50. const shaderLib = nodeShaderLib[ type ];
  51. const shader = this.shader;
  52. shader.vertexShader = shaderLib.vertexShader;
  53. shader.fragmentShader = shaderLib.fragmentShader;
  54. shader.uniforms = UniformsUtils.merge( [ shaderLib.uniforms, UniformsLib.lights ] );
  55. }
  56. if ( renderer.toneMappingNode?.isNode === true ) {
  57. this.replaceCode( 'fragment', getIncludeSnippet( 'tonemapping_fragment' ), '' );
  58. }
  59. // parse inputs
  60. if ( material.colorNode && material.colorNode.isNode ) {
  61. this.addSlot( 'fragment', new SlotNode( material.colorNode, 'COLOR', 'vec4' ) );
  62. }
  63. if ( material.opacityNode && material.opacityNode.isNode ) {
  64. this.addSlot( 'fragment', new SlotNode( material.opacityNode, 'OPACITY', 'float' ) );
  65. }
  66. if ( material.normalNode && material.normalNode.isNode ) {
  67. this.addSlot( 'fragment', new SlotNode( material.normalNode, 'NORMAL', 'vec3' ) );
  68. }
  69. if ( material.emissiveNode && material.emissiveNode.isNode ) {
  70. this.addSlot( 'fragment', new SlotNode( material.emissiveNode, 'EMISSIVE', 'vec3' ) );
  71. }
  72. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  73. this.addSlot( 'fragment', new SlotNode( material.metalnessNode, 'METALNESS', 'float' ) );
  74. }
  75. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  76. this.addSlot( 'fragment', new SlotNode( material.roughnessNode, 'ROUGHNESS', 'float' ) );
  77. }
  78. if ( material.isMeshPhysicalNodeMaterial ) {
  79. if ( material.clearcoatNode && material.clearcoatNode.isNode ) {
  80. this.addSlot( 'fragment', new SlotNode( material.clearcoatNode, 'CLEARCOAT', 'float' ) );
  81. if ( material.clearcoatRoughnessNode && material.clearcoatRoughnessNode.isNode ) {
  82. this.addSlot( 'fragment', new SlotNode( material.clearcoatRoughnessNode, 'CLEARCOAT_ROUGHNESS', 'float' ) );
  83. }
  84. if ( material.clearcoatNormalNode && material.clearcoatNormalNode.isNode ) {
  85. this.addSlot( 'fragment', new SlotNode( material.clearcoatNormalNode, 'CLEARCOAT_NORMAL', 'vec3' ) );
  86. }
  87. material.defines.USE_CLEARCOAT = '';
  88. } else {
  89. delete material.defines.USE_CLEARCOAT;
  90. }
  91. if ( material.sheenNode && material.sheenNode.isNode ) {
  92. this.addSlot( 'fragment', new SlotNode( material.sheenNode, 'SHEEN', 'vec3' ) );
  93. if ( material.sheenRoughnessNode && material.sheenRoughnessNode.isNode ) {
  94. this.addSlot( 'fragment', new SlotNode( material.sheenRoughnessNode, 'SHEEN_ROUGHNESS', 'float' ) );
  95. }
  96. material.defines.USE_SHEEN = '';
  97. } else {
  98. delete material.defines.USE_SHEEN;
  99. }
  100. if ( material.iridescenceNode && material.iridescenceNode.isNode ) {
  101. this.addSlot( 'fragment', new SlotNode( material.iridescenceNode, 'IRIDESCENCE', 'float' ) );
  102. if ( material.iridescenceIORNode && material.iridescenceIORNode.isNode ) {
  103. this.addSlot( 'fragment', new SlotNode( material.iridescenceIORNode, 'IRIDESCENCE_IOR', 'float' ) );
  104. }
  105. if ( material.iridescenceThicknessNode && material.iridescenceThicknessNode.isNode ) {
  106. this.addSlot( 'fragment', new SlotNode( material.iridescenceThicknessNode, 'IRIDESCENCE_THICKNESS', 'float' ) );
  107. }
  108. material.defines.USE_IRIDESCENCE = '';
  109. } else {
  110. delete material.defines.USE_IRIDESCENCE;
  111. }
  112. }
  113. if ( material.envNode && material.envNode.isNode ) {
  114. const envRadianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.RADIANCE, material.envNode );
  115. const envIrradianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.IRRADIANCE, material.envNode );
  116. this.addSlot( 'fragment', new SlotNode( envRadianceNode, 'RADIANCE', 'vec3' ) );
  117. this.addSlot( 'fragment', new SlotNode( envIrradianceNode, 'IRRADIANCE', 'vec3' ) );
  118. }
  119. if ( material.positionNode && material.positionNode.isNode ) {
  120. this.addSlot( 'vertex', new SlotNode( material.positionNode, 'POSITION', 'vec3' ) );
  121. }
  122. if ( material.sizeNode && material.sizeNode.isNode ) {
  123. this.addSlot( 'vertex', new SlotNode( material.sizeNode, 'SIZE', 'float' ) );
  124. }
  125. }
  126. getTexture( textureProperty, uvSnippet ) {
  127. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  128. }
  129. getTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  130. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  131. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  132. }
  133. getCubeTexture( textureProperty, uvSnippet ) {
  134. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  135. }
  136. getCubeTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  137. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  138. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  139. }
  140. getUniforms( shaderStage ) {
  141. const uniforms = this.uniforms[ shaderStage ];
  142. let snippet = '';
  143. for ( const uniform of uniforms ) {
  144. if ( uniform.type === 'texture' ) {
  145. snippet += `uniform sampler2D ${uniform.name}; `;
  146. } else if ( uniform.type === 'cubeTexture' ) {
  147. snippet += `uniform samplerCube ${uniform.name}; `;
  148. } else {
  149. const vectorType = this.getVectorType( uniform.type );
  150. snippet += `uniform ${vectorType} ${uniform.name}; `;
  151. }
  152. }
  153. return snippet;
  154. }
  155. getAttributes( shaderStage ) {
  156. let snippet = '';
  157. if ( shaderStage === 'vertex' ) {
  158. const attributes = this.attributes;
  159. for ( const attribute of attributes ) {
  160. // ignore common attributes to prevent redefinitions
  161. if ( attribute.name === 'uv' || attribute.name === 'position' || attribute.name === 'normal' )
  162. continue;
  163. snippet += `attribute ${attribute.type} ${attribute.name}; `;
  164. }
  165. }
  166. return snippet;
  167. }
  168. getVaryings( /* shaderStage */ ) {
  169. let snippet = '';
  170. const varyings = this.varyings;
  171. for ( const varying of varyings ) {
  172. snippet += `varying ${varying.type} ${varying.name}; `;
  173. }
  174. return snippet;
  175. }
  176. addCodeAfterSnippet( shaderStage, snippet, code ) {
  177. const shaderProperty = getShaderStageProperty( shaderStage );
  178. let source = this[ shaderProperty ];
  179. const index = source.indexOf( snippet );
  180. if ( index !== - 1 ) {
  181. const start = source.substring( 0, index + snippet.length );
  182. const end = source.substring( index + snippet.length );
  183. source = `${start}\n${code}\n${end}`;
  184. }
  185. this[ shaderProperty ] = source;
  186. }
  187. addCodeAfterInclude( shaderStage, includeName, code ) {
  188. const includeSnippet = getIncludeSnippet( includeName );
  189. this.addCodeAfterSnippet( shaderStage, includeSnippet, code );
  190. }
  191. replaceCode( shaderStage, source, target ) {
  192. const shaderProperty = getShaderStageProperty( shaderStage );
  193. this[ shaderProperty ] = this[ shaderProperty ].replaceAll( source, target );
  194. }
  195. parseInclude( shaderStage, ...includes ) {
  196. for ( const name of includes ) {
  197. const includeSnippet = getIncludeSnippet( name );
  198. const code = ShaderChunk[ name ];
  199. this.replaceCode( shaderStage, includeSnippet, code );
  200. }
  201. }
  202. getTextureEncodingFromMap( map ) {
  203. const isWebGL2 = this.renderer.capabilities.isWebGL2;
  204. if ( isWebGL2 && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.encoding === sRGBEncoding ) {
  205. return LinearEncoding; // disable inline decode for sRGB textures in WebGL 2
  206. }
  207. return super.getTextureEncodingFromMap( map );
  208. }
  209. getFrontFacing() {
  210. return 'gl_FrontFacing';
  211. }
  212. buildCode() {
  213. const shaderData = {};
  214. for ( const shaderStage of defaultShaderStages ) {
  215. const uniforms = this.getUniforms( shaderStage );
  216. const attributes = this.getAttributes( shaderStage );
  217. const varyings = this.getVaryings( shaderStage );
  218. const vars = this.getVars( shaderStage );
  219. const codes = this.getCodes( shaderStage );
  220. shaderData[ shaderStage ] = `${this.getSignature()}
  221. // <node_builder>
  222. // uniforms
  223. ${uniforms}
  224. // attributes
  225. ${attributes}
  226. // varyings
  227. ${varyings}
  228. // vars
  229. ${vars}
  230. // codes
  231. ${codes}
  232. // </node_builder>
  233. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  234. `;
  235. }
  236. this.vertexShader = shaderData.vertex;
  237. this.fragmentShader = shaderData.fragment;
  238. }
  239. build() {
  240. super.build();
  241. this._addSnippets();
  242. this._addUniforms();
  243. this._updateUniforms();
  244. this.shader.vertexShader = this.vertexShader;
  245. this.shader.fragmentShader = this.fragmentShader;
  246. return this;
  247. }
  248. getSlot( shaderStage, name ) {
  249. const slots = this.slots[ shaderStage ];
  250. for ( const node of slots ) {
  251. if ( node.name === name ) {
  252. return this.getFlowData( node/*, shaderStage*/ );
  253. }
  254. }
  255. }
  256. _addSnippets() {
  257. this.parseInclude( 'fragment', 'lights_physical_fragment' );
  258. this.parseInclude( 'fragment', 'clearcoat_normal_fragment_begin' );
  259. const colorSlot = this.getSlot( 'fragment', 'COLOR' );
  260. const opacityNode = this.getSlot( 'fragment', 'OPACITY' );
  261. const normalSlot = this.getSlot( 'fragment', 'NORMAL' );
  262. const emissiveNode = this.getSlot( 'fragment', 'EMISSIVE' );
  263. const roughnessNode = this.getSlot( 'fragment', 'ROUGHNESS' );
  264. const metalnessNode = this.getSlot( 'fragment', 'METALNESS' );
  265. const clearcoatNode = this.getSlot( 'fragment', 'CLEARCOAT' );
  266. const clearcoatRoughnessNode = this.getSlot( 'fragment', 'CLEARCOAT_ROUGHNESS' );
  267. const clearcoatNormalNode = this.getSlot( 'fragment', 'CLEARCOAT_NORMAL' );
  268. const sheenNode = this.getSlot( 'fragment', 'SHEEN' );
  269. const sheenRoughnessNode = this.getSlot( 'fragment', 'SHEEN_ROUGHNESS' );
  270. const iridescenceNode = this.getSlot( 'fragment', 'IRIDESCENCE' );
  271. const iridescenceIORNode = this.getSlot( 'fragment', 'IRIDESCENCE_IOR' );
  272. const iridescenceThicknessNode = this.getSlot( 'fragment', 'IRIDESCENCE_THICKNESS' );
  273. const positionNode = this.getSlot( 'vertex', 'POSITION' );
  274. const sizeNode = this.getSlot( 'vertex', 'SIZE' );
  275. if ( colorSlot !== undefined ) {
  276. this.addCodeAfterInclude(
  277. 'fragment',
  278. 'color_fragment',
  279. `${colorSlot.code}\n\tdiffuseColor = ${colorSlot.result};`
  280. );
  281. }
  282. if ( opacityNode !== undefined ) {
  283. this.addCodeAfterInclude(
  284. 'fragment',
  285. 'alphatest_fragment',
  286. `${opacityNode.code}\n\tdiffuseColor.a = ${opacityNode.result};`
  287. );
  288. }
  289. if ( normalSlot !== undefined ) {
  290. this.addCodeAfterInclude(
  291. 'fragment',
  292. 'normal_fragment_begin',
  293. `${normalSlot.code}\n\tnormal = ${normalSlot.result};`
  294. );
  295. }
  296. if ( emissiveNode !== undefined ) {
  297. this.addCodeAfterInclude(
  298. 'fragment',
  299. 'emissivemap_fragment',
  300. `${emissiveNode.code}\n\ttotalEmissiveRadiance = ${emissiveNode.result};`
  301. );
  302. }
  303. if ( roughnessNode !== undefined ) {
  304. this.addCodeAfterInclude(
  305. 'fragment',
  306. 'roughnessmap_fragment',
  307. `${roughnessNode.code}\n\troughnessFactor = ${roughnessNode.result};`
  308. );
  309. }
  310. if ( metalnessNode !== undefined ) {
  311. this.addCodeAfterInclude(
  312. 'fragment',
  313. 'metalnessmap_fragment',
  314. `${metalnessNode.code}\n\tmetalnessFactor = ${metalnessNode.result};`
  315. );
  316. }
  317. if ( clearcoatNode !== undefined ) {
  318. this.addCodeAfterSnippet(
  319. 'fragment',
  320. 'material.clearcoat = clearcoat;',
  321. `${clearcoatNode.code}\n\tmaterial.clearcoat = ${clearcoatNode.result};`
  322. );
  323. if ( clearcoatRoughnessNode !== undefined ) {
  324. this.addCodeAfterSnippet(
  325. 'fragment',
  326. 'material.clearcoatRoughness = clearcoatRoughness;',
  327. `${clearcoatRoughnessNode.code}\n\tmaterial.clearcoatRoughness = ${clearcoatRoughnessNode.result};`
  328. );
  329. }
  330. if ( clearcoatNormalNode !== undefined ) {
  331. this.addCodeAfterSnippet(
  332. 'fragment',
  333. 'vec3 clearcoatNormal = geometryNormal;',
  334. `${clearcoatNormalNode.code}\n\tclearcoatNormal = ${clearcoatNormalNode.result};`
  335. );
  336. }
  337. }
  338. if ( sheenNode !== undefined ) {
  339. this.addCodeAfterSnippet(
  340. 'fragment',
  341. 'material.sheenColor = sheenColor;',
  342. `${sheenNode.code}\n\tmaterial.sheenColor = ${sheenNode.result};`
  343. );
  344. if ( sheenRoughnessNode !== undefined ) {
  345. this.replaceCode(
  346. 'fragment',
  347. 'material.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 );',
  348. `${sheenRoughnessNode.code}\n\tmaterial.sheenRoughness = clamp( ${sheenRoughnessNode.result}, 0.07, 1.0 );`
  349. );
  350. }
  351. }
  352. if ( iridescenceNode !== undefined ) {
  353. this.addCodeAfterSnippet(
  354. 'fragment',
  355. 'material.iridescence = iridescence;',
  356. `${iridescenceNode.code}\n\tmaterial.iridescence = ${iridescenceNode.result};`
  357. );
  358. }
  359. if ( iridescenceIORNode !== undefined ) {
  360. this.addCodeAfterSnippet(
  361. 'fragment',
  362. 'material.iridescenceIOR = iridescenceIOR;',
  363. `${iridescenceIORNode.code}\n\tmaterial.iridescenceIOR = ${iridescenceIORNode.result};`
  364. );
  365. }
  366. if ( iridescenceThicknessNode !== undefined ) {
  367. this.addCodeAfterSnippet(
  368. 'fragment',
  369. 'material.iridescenceThickness = iridescenceThicknessMaximum;',
  370. `${iridescenceThicknessNode.code}\n\tmaterial.iridescenceThickness = ${iridescenceThicknessNode.result};`
  371. );
  372. }
  373. if ( positionNode !== undefined ) {
  374. this.addCodeAfterInclude(
  375. 'vertex',
  376. 'begin_vertex',
  377. `${positionNode.code}\n\ttransformed = ${positionNode.result};`
  378. );
  379. }
  380. if ( sizeNode !== undefined ) {
  381. this.addCodeAfterSnippet(
  382. 'vertex',
  383. 'gl_PointSize = size;',
  384. `${sizeNode.code}\n\tgl_PointSize = ${sizeNode.result};`
  385. );
  386. }
  387. for ( const shaderStage of defaultShaderStages ) {
  388. this.addCodeAfterSnippet(
  389. shaderStage,
  390. 'main() {',
  391. this.flowCode[ shaderStage ]
  392. );
  393. }
  394. }
  395. _addUniforms() {
  396. for ( const shaderStage of defaultShaderStages ) {
  397. // uniforms
  398. for ( const uniform of this.uniforms[ shaderStage ] ) {
  399. this.shader.uniforms[ uniform.name ] = uniform;
  400. }
  401. }
  402. }
  403. _updateUniforms() {
  404. nodeFrame.object = this.object;
  405. nodeFrame.renderer = this.renderer;
  406. for ( const node of this.updateNodes ) {
  407. nodeFrame.updateNode( node );
  408. }
  409. }
  410. }
  411. export { WebGLNodeBuilder };