WebGLNodeBuilder.js 11 KB

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