WebGLNodeBuilder.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. }
  101. if ( material.iridescenceNode && material.iridescenceNode.isNode ) {
  102. this.addSlot( 'fragment', new SlotNode( material.iridescenceNode, 'IRIDESCENCE', 'float' ) );
  103. }
  104. if ( material.iridescenceIORNode && material.iridescenceIORNode.isNode ) {
  105. this.addSlot( 'fragment', new SlotNode( material.iridescenceIORNode, 'IRIDESCENCE_IOR', 'float' ) );
  106. }
  107. if ( material.iridescenceThicknessNode && material.iridescenceThicknessNode.isNode ) {
  108. this.addSlot( 'fragment', new SlotNode( material.iridescenceThicknessNode, 'IRIDESCENCE_THICKNESS', 'float' ) );
  109. }
  110. if ( material.envNode && material.envNode.isNode ) {
  111. const envRadianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.RADIANCE, material.envNode );
  112. const envIrradianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.IRRADIANCE, material.envNode );
  113. this.addSlot( 'fragment', new SlotNode( envRadianceNode, 'RADIANCE', 'vec3' ) );
  114. this.addSlot( 'fragment', new SlotNode( envIrradianceNode, 'IRRADIANCE', 'vec3' ) );
  115. }
  116. if ( material.positionNode && material.positionNode.isNode ) {
  117. this.addSlot( 'vertex', new SlotNode( material.positionNode, 'POSITION', 'vec3' ) );
  118. }
  119. if ( material.sizeNode && material.sizeNode.isNode ) {
  120. this.addSlot( 'vertex', new SlotNode( material.sizeNode, 'SIZE', 'float' ) );
  121. }
  122. }
  123. getTexture( textureProperty, uvSnippet ) {
  124. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  125. }
  126. getTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  127. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  128. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  129. }
  130. getCubeTexture( textureProperty, uvSnippet ) {
  131. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  132. }
  133. getCubeTextureBias( textureProperty, uvSnippet, biasSnippet ) {
  134. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  135. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  136. }
  137. getUniforms( shaderStage ) {
  138. const uniforms = this.uniforms[ shaderStage ];
  139. let snippet = '';
  140. for ( const uniform of uniforms ) {
  141. if ( uniform.type === 'texture' ) {
  142. snippet += `uniform sampler2D ${uniform.name}; `;
  143. } else if ( uniform.type === 'cubeTexture' ) {
  144. snippet += `uniform samplerCube ${uniform.name}; `;
  145. } else {
  146. const vectorType = this.getVectorType( uniform.type );
  147. snippet += `uniform ${vectorType} ${uniform.name}; `;
  148. }
  149. }
  150. return snippet;
  151. }
  152. getAttributes( shaderStage ) {
  153. let snippet = '';
  154. if ( shaderStage === 'vertex' ) {
  155. const attributes = this.attributes;
  156. for ( const attribute of attributes ) {
  157. // ignore common attributes to prevent redefinitions
  158. if ( attribute.name === 'uv' || attribute.name === 'position' || attribute.name === 'normal' )
  159. continue;
  160. snippet += `attribute ${attribute.type} ${attribute.name}; `;
  161. }
  162. }
  163. return snippet;
  164. }
  165. getVaryings( /* shaderStage */ ) {
  166. let snippet = '';
  167. const varyings = this.varyings;
  168. for ( const varying of varyings ) {
  169. snippet += `varying ${varying.type} ${varying.name}; `;
  170. }
  171. return snippet;
  172. }
  173. addCodeAfterSnippet( shaderStage, snippet, code ) {
  174. const shaderProperty = getShaderStageProperty( shaderStage );
  175. let source = this[ shaderProperty ];
  176. const index = source.indexOf( snippet );
  177. if ( index !== - 1 ) {
  178. const start = source.substring( 0, index + snippet.length );
  179. const end = source.substring( index + snippet.length );
  180. source = `${start}\n${code}\n${end}`;
  181. }
  182. this[ shaderProperty ] = source;
  183. }
  184. addCodeAfterInclude( shaderStage, includeName, code ) {
  185. const includeSnippet = getIncludeSnippet( includeName );
  186. this.addCodeAfterSnippet( shaderStage, includeSnippet, code );
  187. }
  188. replaceCode( shaderStage, source, target ) {
  189. const shaderProperty = getShaderStageProperty( shaderStage );
  190. this[ shaderProperty ] = this[ shaderProperty ].replaceAll( source, target );
  191. }
  192. parseInclude( shaderStage, ...includes ) {
  193. for ( const name of includes ) {
  194. const includeSnippet = getIncludeSnippet( name );
  195. const code = ShaderChunk[ name ];
  196. this.replaceCode( shaderStage, includeSnippet, code );
  197. }
  198. }
  199. getTextureEncodingFromMap( map ) {
  200. const isWebGL2 = this.renderer.capabilities.isWebGL2;
  201. if ( isWebGL2 && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.encoding === sRGBEncoding ) {
  202. return LinearEncoding; // disable inline decode for sRGB textures in WebGL 2
  203. }
  204. return super.getTextureEncodingFromMap( map );
  205. }
  206. getFrontFacing() {
  207. return 'gl_FrontFacing';
  208. }
  209. buildCode() {
  210. const shaderData = {};
  211. for ( const shaderStage of defaultShaderStages ) {
  212. const uniforms = this.getUniforms( shaderStage );
  213. const attributes = this.getAttributes( shaderStage );
  214. const varyings = this.getVaryings( shaderStage );
  215. const vars = this.getVars( shaderStage );
  216. const codes = this.getCodes( shaderStage );
  217. shaderData[ shaderStage ] = `${this.getSignature()}
  218. // <node_builder>
  219. // uniforms
  220. ${uniforms}
  221. // attributes
  222. ${attributes}
  223. // varyings
  224. ${varyings}
  225. // vars
  226. ${vars}
  227. // codes
  228. ${codes}
  229. // </node_builder>
  230. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  231. `;
  232. }
  233. this.vertexShader = shaderData.vertex;
  234. this.fragmentShader = shaderData.fragment;
  235. }
  236. build() {
  237. super.build();
  238. this._addSnippets();
  239. this._addUniforms();
  240. this._updateUniforms();
  241. this.shader.vertexShader = this.vertexShader;
  242. this.shader.fragmentShader = this.fragmentShader;
  243. return this;
  244. }
  245. getSlot( shaderStage, name ) {
  246. const slots = this.slots[ shaderStage ];
  247. for ( const node of slots ) {
  248. if ( node.name === name ) {
  249. return this.getFlowData( node/*, shaderStage*/ );
  250. }
  251. }
  252. }
  253. _addSnippets() {
  254. this.parseInclude( 'fragment', 'lights_physical_fragment' );
  255. this.parseInclude( 'fragment', 'clearcoat_normal_fragment_begin' );
  256. const colorSlot = this.getSlot( 'fragment', 'COLOR' );
  257. const opacityNode = this.getSlot( 'fragment', 'OPACITY' );
  258. const normalSlot = this.getSlot( 'fragment', 'NORMAL' );
  259. const emissiveNode = this.getSlot( 'fragment', 'EMISSIVE' );
  260. const roughnessNode = this.getSlot( 'fragment', 'ROUGHNESS' );
  261. const metalnessNode = this.getSlot( 'fragment', 'METALNESS' );
  262. const clearcoatNode = this.getSlot( 'fragment', 'CLEARCOAT' );
  263. const clearcoatRoughnessNode = this.getSlot( 'fragment', 'CLEARCOAT_ROUGHNESS' );
  264. const clearcoatNormalNode = this.getSlot( 'fragment', 'CLEARCOAT_NORMAL' );
  265. const sheenNode = this.getSlot( 'fragment', 'SHEEN' );
  266. const sheenRoughnessNode = this.getSlot( 'fragment', 'SHEEN_ROUGHNESS' );
  267. const iridescenceNode = this.getSlot( 'fragment', 'IRIDESCENCE' );
  268. const iridescenceIORNode = this.getSlot( 'fragment', 'IRIDESCENCE_IOR' );
  269. const iridescenceThicknessNode = this.getSlot( 'fragment', 'IRIDESCENCE_THICKNESS' );
  270. const positionNode = this.getSlot( 'vertex', 'POSITION' );
  271. const sizeNode = this.getSlot( 'vertex', 'SIZE' );
  272. if ( colorSlot !== undefined ) {
  273. this.addCodeAfterInclude(
  274. 'fragment',
  275. 'color_fragment',
  276. `${colorSlot.code}\n\tdiffuseColor = ${colorSlot.result};`
  277. );
  278. }
  279. if ( opacityNode !== undefined ) {
  280. this.addCodeAfterInclude(
  281. 'fragment',
  282. 'alphatest_fragment',
  283. `${opacityNode.code}\n\tdiffuseColor.a = ${opacityNode.result};`
  284. );
  285. }
  286. if ( normalSlot !== undefined ) {
  287. this.addCodeAfterInclude(
  288. 'fragment',
  289. 'normal_fragment_begin',
  290. `${normalSlot.code}\n\tnormal = ${normalSlot.result};`
  291. );
  292. }
  293. if ( emissiveNode !== undefined ) {
  294. this.addCodeAfterInclude(
  295. 'fragment',
  296. 'emissivemap_fragment',
  297. `${emissiveNode.code}\n\ttotalEmissiveRadiance = ${emissiveNode.result};`
  298. );
  299. }
  300. if ( roughnessNode !== undefined ) {
  301. this.addCodeAfterInclude(
  302. 'fragment',
  303. 'roughnessmap_fragment',
  304. `${roughnessNode.code}\n\troughnessFactor = ${roughnessNode.result};`
  305. );
  306. }
  307. if ( metalnessNode !== undefined ) {
  308. this.addCodeAfterInclude(
  309. 'fragment',
  310. 'metalnessmap_fragment',
  311. `${metalnessNode.code}\n\tmetalnessFactor = ${metalnessNode.result};`
  312. );
  313. }
  314. if ( clearcoatNode !== undefined ) {
  315. this.addCodeAfterSnippet(
  316. 'fragment',
  317. 'material.clearcoat = clearcoat;',
  318. `${clearcoatNode.code}\n\tmaterial.clearcoat = ${clearcoatNode.result};`
  319. );
  320. if ( clearcoatRoughnessNode !== undefined ) {
  321. this.addCodeAfterSnippet(
  322. 'fragment',
  323. 'material.clearcoatRoughness = clearcoatRoughness;',
  324. `${clearcoatRoughnessNode.code}\n\tmaterial.clearcoatRoughness = ${clearcoatRoughnessNode.result};`
  325. );
  326. }
  327. if ( clearcoatNormalNode !== undefined ) {
  328. this.addCodeAfterSnippet(
  329. 'fragment',
  330. 'vec3 clearcoatNormal = geometryNormal;',
  331. `${clearcoatNormalNode.code}\n\tclearcoatNormal = ${clearcoatNormalNode.result};`
  332. );
  333. }
  334. }
  335. if ( sheenNode !== undefined ) {
  336. this.addCodeAfterSnippet(
  337. 'fragment',
  338. 'material.sheenColor = sheenColor;',
  339. `${sheenNode.code}\n\tmaterial.sheenColor = ${sheenNode.result};`
  340. );
  341. if ( sheenRoughnessNode !== undefined ) {
  342. this.replaceCode(
  343. 'fragment',
  344. 'material.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 );',
  345. `${sheenRoughnessNode.code}\n\tmaterial.sheenRoughness = clamp( ${sheenRoughnessNode.result}, 0.07, 1.0 );`
  346. );
  347. }
  348. }
  349. if ( iridescenceNode !== undefined ) {
  350. this.addCodeAfterInclude(
  351. 'fragment',
  352. 'iridescence_fragment',
  353. `${iridescenceNode.code}\n\tmaterial.iridescence = ${iridescenceNode.result};`
  354. );
  355. }
  356. if ( iridescenceIORNode !== undefined ) {
  357. this.addCodeAfterInclude(
  358. 'fragment',
  359. 'iridescence_fragment',
  360. `${iridescenceIORNode.code}\n\tmaterial.iridescenceIOR = ${iridescenceIORNode.result};`
  361. );
  362. }
  363. if ( iridescenceThicknessNode !== undefined ) {
  364. this.addCodeAfterInclude(
  365. 'fragment',
  366. 'iridescence_fragment',
  367. `${iridescenceThicknessNode.code}\n\tmaterial.iridescenceThickness = ${iridescenceThicknessNode.result};`
  368. );
  369. }
  370. if ( positionNode !== undefined ) {
  371. this.addCodeAfterInclude(
  372. 'vertex',
  373. 'begin_vertex',
  374. `${positionNode.code}\n\ttransformed = ${positionNode.result};`
  375. );
  376. }
  377. if ( sizeNode !== undefined ) {
  378. this.addCodeAfterSnippet(
  379. 'vertex',
  380. 'gl_PointSize = size;',
  381. `${sizeNode.code}\n\tgl_PointSize = ${sizeNode.result};`
  382. );
  383. }
  384. for ( const shaderStage of defaultShaderStages ) {
  385. this.addCodeAfterSnippet(
  386. shaderStage,
  387. 'main() {',
  388. this.flowCode[ shaderStage ]
  389. );
  390. }
  391. }
  392. _addUniforms() {
  393. for ( const shaderStage of defaultShaderStages ) {
  394. // uniforms
  395. for ( const uniform of this.uniforms[ shaderStage ] ) {
  396. this.shader.uniforms[ uniform.name ] = uniform;
  397. }
  398. }
  399. }
  400. _updateUniforms() {
  401. nodeFrame.object = this.object;
  402. nodeFrame.renderer = this.renderer;
  403. for ( const node of this.updateNodes ) {
  404. nodeFrame.updateNode( node );
  405. }
  406. }
  407. }
  408. export { WebGLNodeBuilder };