WebGLNodeBuilder.js 12 KB

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