WebGLNodeBuilder.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. buildCode() {
  181. const shaderData = {};
  182. for ( const shaderStage of defaultShaderStages ) {
  183. const uniforms = this.getUniforms( shaderStage );
  184. const attributes = this.getAttributes( shaderStage );
  185. const varys = this.getVarys( shaderStage );
  186. const vars = this.getVars( shaderStage );
  187. const codes = this.getCodes( shaderStage );
  188. shaderData[ shaderStage ] = `${this.getSignature()}
  189. // <node_builder>
  190. // uniforms
  191. ${uniforms}
  192. // attributes
  193. ${attributes}
  194. // varys
  195. ${varys}
  196. // vars
  197. ${vars}
  198. // codes
  199. ${codes}
  200. // </node_builder>
  201. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  202. `;
  203. }
  204. this.vertexShader = shaderData.vertex;
  205. this.fragmentShader = shaderData.fragment;
  206. }
  207. build() {
  208. super.build();
  209. this._addSnippets();
  210. this._addUniforms();
  211. this._updateUniforms();
  212. this.shader.vertexShader = this.vertexShader;
  213. this.shader.fragmentShader = this.fragmentShader;
  214. return this;
  215. }
  216. getSlot( shaderStage, name ) {
  217. const slots = this.slots[ shaderStage ];
  218. for ( const node of slots ) {
  219. if ( node.name === name ) {
  220. return this.getFlowData( shaderStage, node );
  221. }
  222. }
  223. }
  224. _addSnippets() {
  225. this.parseInclude( 'fragment', 'lights_physical_fragment' );
  226. const colorSlot = this.getSlot( 'fragment', 'COLOR' );
  227. const opacityNode = this.getSlot( 'fragment', 'OPACITY' );
  228. const normalSlot = this.getSlot( 'fragment', 'NORMAL' );
  229. const emissiveNode = this.getSlot( 'fragment', 'EMISSIVE' );
  230. const roughnessNode = this.getSlot( 'fragment', 'ROUGHNESS' );
  231. const metalnessNode = this.getSlot( 'fragment', 'METALNESS' );
  232. const clearcoatNode = this.getSlot( 'fragment', 'CLEARCOAT' );
  233. const clearcoatRoughnessNode = this.getSlot( 'fragment', 'CLEARCOAT_ROUGHNESS' );
  234. const positionNode = this.getSlot( 'vertex', 'POSITION' );
  235. const sizeNode = this.getSlot( 'vertex', 'SIZE' );
  236. if ( colorSlot !== undefined ) {
  237. this.addCodeAfterInclude(
  238. 'fragment',
  239. 'color_fragment',
  240. `${colorSlot.code}\n\tdiffuseColor = ${colorSlot.result};`
  241. );
  242. }
  243. if ( opacityNode !== undefined ) {
  244. this.addCodeAfterInclude(
  245. 'fragment',
  246. 'alphatest_fragment',
  247. `${opacityNode.code}\n\tdiffuseColor.a = ${opacityNode.result};`
  248. );
  249. }
  250. if ( normalSlot !== undefined ) {
  251. this.addCodeAfterInclude(
  252. 'fragment',
  253. 'normal_fragment_begin',
  254. `${normalSlot.code}\n\tnormal = ${normalSlot.result};`
  255. );
  256. }
  257. if ( emissiveNode !== undefined ) {
  258. this.addCodeAfterInclude(
  259. 'fragment',
  260. 'emissivemap_fragment',
  261. `${emissiveNode.code}\n\ttotalEmissiveRadiance = ${emissiveNode.result};`
  262. );
  263. }
  264. if ( roughnessNode !== undefined ) {
  265. this.addCodeAfterInclude(
  266. 'fragment',
  267. 'roughnessmap_fragment',
  268. `${roughnessNode.code}\n\troughnessFactor = ${roughnessNode.result};`
  269. );
  270. }
  271. if ( metalnessNode !== undefined ) {
  272. this.addCodeAfterInclude(
  273. 'fragment',
  274. 'metalnessmap_fragment',
  275. `${metalnessNode.code}\n\tmetalnessFactor = ${metalnessNode.result};`
  276. );
  277. }
  278. if ( clearcoatNode !== undefined ) {
  279. this.addCodeAfterSnippet(
  280. 'fragment',
  281. 'material.clearcoatRoughness = clearcoatRoughness;',
  282. `${clearcoatNode.code}\n\tmaterial.clearcoat = ${clearcoatNode.result};`
  283. );
  284. }
  285. if ( clearcoatRoughnessNode !== undefined ) {
  286. this.addCodeAfterSnippet(
  287. 'fragment',
  288. 'material.clearcoatRoughness = clearcoatRoughness;',
  289. `${clearcoatRoughnessNode.code}\n\tmaterial.clearcoatRoughness = ${clearcoatRoughnessNode.result};`
  290. );
  291. }
  292. if ( positionNode !== undefined ) {
  293. this.addCodeAfterInclude(
  294. 'vertex',
  295. 'begin_vertex',
  296. `${positionNode.code}\n\ttransformed = ${positionNode.result};`
  297. );
  298. }
  299. if ( sizeNode !== undefined ) {
  300. this.addCodeAfterSnippet(
  301. 'vertex',
  302. 'gl_PointSize = size;',
  303. `${sizeNode.code}\n\tgl_PointSize = ${sizeNode.result};`
  304. );
  305. }
  306. for ( const shaderStage of defaultShaderStages ) {
  307. this.addCodeAfterSnippet(
  308. shaderStage,
  309. 'main() {',
  310. this.flowCode[ shaderStage ]
  311. );
  312. }
  313. }
  314. _addUniforms() {
  315. for ( const shaderStage of defaultShaderStages ) {
  316. // uniforms
  317. for ( const uniform of this.uniforms[ shaderStage ] ) {
  318. this.shader.uniforms[ uniform.name ] = uniform;
  319. }
  320. }
  321. }
  322. _updateUniforms() {
  323. nodeFrame.object = this.object;
  324. nodeFrame.renderer = this.renderer;
  325. for ( const node of this.updateNodes ) {
  326. nodeFrame.updateNode( node );
  327. }
  328. }
  329. }
  330. export { WebGLNodeBuilder };