WebGLNodeBuilder.js 14 KB

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