WebGLNodeBuilder.js 14 KB

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