WebGLNodeBuilder.js 16 KB

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