WebGLNodeBuilder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import NodeBuilder from '../../nodes/core/NodeBuilder.js';
  2. import SlotNode from './SlotNode.js';
  3. import GLSLNodeParser from '../../nodes/parsers/GLSLNodeParser.js';
  4. import WebGLPhysicalContextNode from './WebGLPhysicalContextNode.js';
  5. import { ShaderChunk, LinearEncoding, RGBAFormat, UnsignedByteType, sRGBEncoding } from 'three';
  6. const shaderStages = [ 'vertex', 'fragment' ];
  7. function getIncludeSnippet( name ) {
  8. return `#include <${name}>`;
  9. }
  10. function getShaderStageProperty( shaderStage ) {
  11. return `${shaderStage}Shader`;
  12. }
  13. class WebGLNodeBuilder extends NodeBuilder {
  14. constructor( object, renderer, shader ) {
  15. super( object, renderer, new GLSLNodeParser() );
  16. this.shader = shader;
  17. this.slots = { vertex: [], fragment: [] };
  18. this._parseObject();
  19. }
  20. addSlot( shaderStage, slotNode ) {
  21. this.slots[ shaderStage ].push( slotNode );
  22. return this.addFlow( shaderStage, slotNode );
  23. }
  24. addFlowCode( code ) {
  25. if ( ! /;\s*$/.test( code ) ) {
  26. code += ';';
  27. }
  28. super.addFlowCode( code + '\n\t' );
  29. }
  30. _parseObject() {
  31. const material = this.material;
  32. // parse inputs
  33. if ( material.colorNode && material.colorNode.isNode ) {
  34. this.addSlot( 'fragment', new SlotNode( material.colorNode, 'COLOR', 'vec4' ) );
  35. }
  36. if ( material.opacityNode && material.opacityNode.isNode ) {
  37. this.addSlot( 'fragment', new SlotNode( material.opacityNode, 'OPACITY', 'float' ) );
  38. }
  39. if ( material.normalNode && material.normalNode.isNode ) {
  40. this.addSlot( 'fragment', new SlotNode( material.normalNode, 'NORMAL', 'vec3' ) );
  41. }
  42. if ( material.emissiveNode && material.emissiveNode.isNode ) {
  43. this.addSlot( 'fragment', new SlotNode( material.emissiveNode, 'EMISSIVE', 'vec3' ) );
  44. }
  45. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  46. this.addSlot( 'fragment', new SlotNode( material.metalnessNode, 'METALNESS', 'float' ) );
  47. }
  48. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  49. this.addSlot( 'fragment', new SlotNode( material.roughnessNode, 'ROUGHNESS', 'float' ) );
  50. }
  51. if ( material.clearcoatNode && material.clearcoatNode.isNode ) {
  52. this.addSlot( 'fragment', new SlotNode( material.clearcoatNode, 'CLEARCOAT', 'float' ) );
  53. }
  54. if ( material.clearcoatRoughnessNode && material.clearcoatRoughnessNode.isNode ) {
  55. this.addSlot( 'fragment', new SlotNode( material.clearcoatRoughnessNode, 'CLEARCOAT_ROUGHNESS', 'float' ) );
  56. }
  57. if ( material.envNode && material.envNode.isNode ) {
  58. const envRadianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.RADIANCE, material.envNode );
  59. const envIrradianceNode = new WebGLPhysicalContextNode( WebGLPhysicalContextNode.IRRADIANCE, material.envNode );
  60. this.addSlot( 'fragment', new SlotNode( envRadianceNode, 'RADIANCE', 'vec3' ) );
  61. this.addSlot( 'fragment', new SlotNode( envIrradianceNode, 'IRRADIANCE', 'vec3' ) );
  62. }
  63. if ( material.positionNode && material.positionNode.isNode ) {
  64. this.addSlot( 'vertex', new SlotNode( material.positionNode, 'POSITION', 'vec3' ) );
  65. }
  66. if ( material.sizeNode && material.sizeNode.isNode ) {
  67. this.addSlot( 'vertex', new SlotNode( material.sizeNode, 'SIZE', 'float' ) );
  68. }
  69. }
  70. getTexture( textureProperty, uvSnippet, biasSnippet = null ) {
  71. if ( biasSnippet !== null ) {
  72. return `texture2D( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  73. } else {
  74. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  75. }
  76. }
  77. getCubeTexture( textureProperty, uvSnippet, biasSnippet = null ) {
  78. const textureCube = 'textureCubeLodEXT'; // textureCubeLodEXT textureLod
  79. if ( biasSnippet !== null ) {
  80. return `${textureCube}( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  81. } else {
  82. return `${textureCube}( ${textureProperty}, ${uvSnippet} )`;
  83. }
  84. }
  85. getUniforms( shaderStage ) {
  86. const uniforms = this.uniforms[ shaderStage ];
  87. let snippet = '';
  88. for ( const uniform of uniforms ) {
  89. if ( uniform.type === 'texture' ) {
  90. snippet += `uniform sampler2D ${uniform.name}; `;
  91. } else if ( uniform.type === 'cubeTexture' ) {
  92. snippet += `uniform samplerCube ${uniform.name}; `;
  93. } else {
  94. const vectorType = this.getVectorType( uniform.type );
  95. snippet += `uniform ${vectorType} ${uniform.name}; `;
  96. }
  97. }
  98. return snippet;
  99. }
  100. getAttributes( shaderStage ) {
  101. let snippet = '';
  102. if ( shaderStage === 'vertex' ) {
  103. const attributes = this.attributes;
  104. for ( let index = 0; index < attributes.length; index ++ ) {
  105. const attribute = attributes[ index ];
  106. // ignore common attributes to prevent redefinitions
  107. if ( attribute.name === 'uv' || attribute.name === 'position' || attribute.name === 'normal' )
  108. continue;
  109. snippet += `attribute ${attribute.type} ${attribute.name}; `;
  110. }
  111. }
  112. return snippet;
  113. }
  114. getVarys( /* shaderStage */ ) {
  115. let snippet = '';
  116. const varys = this.varys;
  117. for ( let index = 0; index < varys.length; index ++ ) {
  118. const vary = varys[ index ];
  119. snippet += `varying ${vary.type} ${vary.name}; `;
  120. }
  121. return snippet;
  122. }
  123. addCodeAfterSnippet( shaderStage, snippet, code ) {
  124. const shaderProperty = getShaderStageProperty( shaderStage );
  125. let source = this[ shaderProperty ];
  126. const index = source.indexOf( snippet );
  127. if ( index !== - 1 ) {
  128. const start = source.substring( 0, index + snippet.length );
  129. const end = source.substring( index + snippet.length );
  130. source = `${start}\n${code}\n${end}`;
  131. }
  132. this[ shaderProperty ] = source;
  133. }
  134. addCodeAfterInclude( shaderStage, includeName, code ) {
  135. const includeSnippet = getIncludeSnippet( includeName );
  136. this.addCodeAfterSnippet( shaderStage, includeSnippet, code );
  137. }
  138. replaceCode( shaderStage, source, target ) {
  139. const shaderProperty = getShaderStageProperty( shaderStage );
  140. this.shader[ shaderProperty ] = this.shader[ shaderProperty ].replaceAll( source, target );
  141. }
  142. parseInclude( shaderStage, ...includes ) {
  143. for ( const name of includes ) {
  144. const includeSnippet = getIncludeSnippet( name );
  145. const code = ShaderChunk[ name ];
  146. this.replaceCode( shaderStage, includeSnippet, code );
  147. }
  148. }
  149. getTextureEncodingFromMap( map ) {
  150. const isWebGL2 = this.renderer.capabilities.isWebGL2;
  151. if ( isWebGL2 && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.encoding === sRGBEncoding ) {
  152. return LinearEncoding; // disable inline decode for sRGB textures in WebGL 2
  153. }
  154. return super.getTextureEncodingFromMap( map );
  155. }
  156. buildCode() {
  157. const shaderData = {};
  158. for ( const shaderStage of shaderStages ) {
  159. const uniforms = this.getUniforms( shaderStage );
  160. const attributes = this.getAttributes( shaderStage );
  161. const varys = this.getVarys( shaderStage );
  162. const vars = this.getVars( shaderStage );
  163. const codes = this.getCodes( shaderStage );
  164. shaderData[ shaderStage ] = `${this.getSignature()}
  165. // <node_builder>
  166. // uniforms
  167. ${uniforms}
  168. // attributes
  169. ${attributes}
  170. // varys
  171. ${varys}
  172. // vars
  173. ${vars}
  174. // codes
  175. ${codes}
  176. // </node_builder>
  177. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  178. `;
  179. }
  180. this.vertexShader = shaderData.vertex;
  181. this.fragmentShader = shaderData.fragment;
  182. }
  183. build() {
  184. super.build();
  185. this._addSnippets();
  186. this._addUniforms();
  187. this.shader.vertexShader = this.vertexShader;
  188. this.shader.fragmentShader = this.fragmentShader;
  189. return this;
  190. }
  191. getSlot( shaderStage, name ) {
  192. const slots = this.slots[ shaderStage ];
  193. for ( const node of slots ) {
  194. if ( node.name === name ) {
  195. return this.getFlowData( shaderStage, node );
  196. }
  197. }
  198. }
  199. _addSnippets() {
  200. this.parseInclude( 'fragment', 'lights_physical_fragment' );
  201. const colorSlot = this.getSlot( 'fragment', 'COLOR' );
  202. const opacityNode = this.getSlot( 'fragment', 'OPACITY' );
  203. const normalSlot = this.getSlot( 'fragment', 'NORMAL' );
  204. const emissiveNode = this.getSlot( 'fragment', 'EMISSIVE' );
  205. const roughnessNode = this.getSlot( 'fragment', 'ROUGHNESS' );
  206. const metalnessNode = this.getSlot( 'fragment', 'METALNESS' );
  207. const clearcoatNode = this.getSlot( 'fragment', 'CLEARCOAT' );
  208. const clearcoatRoughnessNode = this.getSlot( 'fragment', 'CLEARCOAT_ROUGHNESS' );
  209. const positionNode = this.getSlot( 'vertex', 'POSITION' );
  210. const sizeNode = this.getSlot( 'vertex', 'SIZE' );
  211. if ( colorSlot !== undefined ) {
  212. this.addCodeAfterInclude(
  213. 'fragment',
  214. 'color_fragment',
  215. `${colorSlot.code}\n\tdiffuseColor = ${colorSlot.result};`
  216. );
  217. }
  218. if ( opacityNode !== undefined ) {
  219. this.addCodeAfterInclude(
  220. 'fragment',
  221. 'alphatest_fragment',
  222. `${opacityNode.code}\n\tdiffuseColor.a = ${opacityNode.result};`
  223. );
  224. }
  225. if ( normalSlot !== undefined ) {
  226. this.addCodeAfterInclude(
  227. 'fragment',
  228. 'normal_fragment_begin',
  229. `${normalSlot.code}\n\tnormal = ${normalSlot.result};`
  230. );
  231. }
  232. if ( emissiveNode !== undefined ) {
  233. this.addCodeAfterInclude(
  234. 'fragment',
  235. 'emissivemap_fragment',
  236. `${emissiveNode.code}\n\ttotalEmissiveRadiance = ${emissiveNode.result};`
  237. );
  238. }
  239. if ( roughnessNode !== undefined ) {
  240. this.addCodeAfterInclude(
  241. 'fragment',
  242. 'roughnessmap_fragment',
  243. `${roughnessNode.code}\n\troughnessFactor = ${roughnessNode.result};`
  244. );
  245. }
  246. if ( metalnessNode !== undefined ) {
  247. this.addCodeAfterInclude(
  248. 'fragment',
  249. 'metalnessmap_fragment',
  250. `${metalnessNode.code}\n\tmetalnessFactor = ${metalnessNode.result};`
  251. );
  252. }
  253. if ( clearcoatNode !== undefined ) {
  254. this.addCodeAfterSnippet(
  255. 'fragment',
  256. 'material.clearcoatRoughness = clearcoatRoughness;',
  257. `${clearcoatNode.code}\n\tmaterial.clearcoat = ${clearcoatNode.result};`
  258. );
  259. }
  260. if ( clearcoatRoughnessNode !== undefined ) {
  261. this.addCodeAfterSnippet(
  262. 'fragment',
  263. 'material.clearcoatRoughness = clearcoatRoughness;',
  264. `${clearcoatRoughnessNode.code}\n\tmaterial.clearcoatRoughness = ${clearcoatRoughnessNode.result};`
  265. );
  266. }
  267. if ( positionNode !== undefined ) {
  268. this.addCodeAfterInclude(
  269. 'vertex',
  270. 'begin_vertex',
  271. `${positionNode.code}\n\ttransformed = ${positionNode.result};`
  272. );
  273. }
  274. if ( sizeNode !== undefined ) {
  275. this.addCodeAfterSnippet(
  276. 'vertex',
  277. 'gl_PointSize = size;',
  278. `${sizeNode.code}\n\tgl_PointSize = ${sizeNode.result};`
  279. );
  280. }
  281. for ( const shaderStage of shaderStages ) {
  282. this.addCodeAfterSnippet(
  283. shaderStage,
  284. 'main() {',
  285. this.flowCode[ shaderStage ]
  286. );
  287. }
  288. }
  289. _addUniforms() {
  290. for ( const shaderStage of shaderStages ) {
  291. // uniforms
  292. for ( const uniform of this.uniforms[ shaderStage ] ) {
  293. this.shader.uniforms[ uniform.name ] = uniform;
  294. }
  295. }
  296. }
  297. }
  298. export { WebGLNodeBuilder };