WebGLNodeBuilder.js 11 KB

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