WebGLNodeBuilder.js 12 KB

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