WebGLNodeBuilder.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. import { defaultShaderStages, NodeFrame, MathNode, GLSLNodeParser, NodeBuilder } from 'three/nodes';
  2. import SlotNode from './SlotNode.js';
  3. import { PerspectiveCamera, ShaderChunk, ShaderLib, UniformsUtils, UniformsLib } from 'three';
  4. const nodeFrame = new NodeFrame();
  5. nodeFrame.camera = new PerspectiveCamera();
  6. const nodeShaderLib = {
  7. LineBasicNodeMaterial: ShaderLib.basic,
  8. MeshBasicNodeMaterial: ShaderLib.basic,
  9. PointsNodeMaterial: ShaderLib.points,
  10. MeshStandardNodeMaterial: ShaderLib.standard,
  11. MeshPhysicalNodeMaterial: ShaderLib.physical
  12. };
  13. const glslMethods = {
  14. [ MathNode.ATAN2 ]: 'atan'
  15. };
  16. const precisionLib = {
  17. low: 'lowp',
  18. medium: 'mediump',
  19. high: 'highp'
  20. };
  21. function getIncludeSnippet( name ) {
  22. return `#include <${name}>`;
  23. }
  24. function getShaderStageProperty( shaderStage ) {
  25. return `${shaderStage}Shader`;
  26. }
  27. class WebGLNodeBuilder extends NodeBuilder {
  28. constructor( object, renderer, shader ) {
  29. super( object, renderer, new GLSLNodeParser() );
  30. this.shader = shader;
  31. this.slots = { vertex: [], fragment: [] };
  32. this._parseShaderLib();
  33. this._parseInclude( 'fragment', 'lights_physical_fragment', 'clearcoat_normal_fragment_begin', 'transmission_fragment' );
  34. this._parseObject();
  35. this._sortSlotsToFlow();
  36. }
  37. getMethod( method ) {
  38. return glslMethods[ method ] || method;
  39. }
  40. addSlot( shaderStage, slotNode ) {
  41. this.slots[ shaderStage ].push( slotNode );
  42. }
  43. _parseShaderLib() {
  44. const material = this.material;
  45. let type = material.type;
  46. // see https://github.com/mrdoob/three.js/issues/23707
  47. if ( material.isMeshPhysicalNodeMaterial ) type = 'MeshPhysicalNodeMaterial';
  48. else if ( material.isMeshStandardNodeMaterial ) type = 'MeshStandardNodeMaterial';
  49. else if ( material.isMeshBasicNodeMaterial ) type = 'MeshBasicNodeMaterial';
  50. else if ( material.isPointsNodeMaterial ) type = 'PointsNodeMaterial';
  51. else if ( material.isLineBasicNodeMaterial ) type = 'LineBasicNodeMaterial';
  52. // shader lib
  53. if ( nodeShaderLib[ type ] !== undefined ) {
  54. const shaderLib = nodeShaderLib[ type ];
  55. const shader = this.shader;
  56. shader.vertexShader = shaderLib.vertexShader;
  57. shader.fragmentShader = shaderLib.fragmentShader;
  58. shader.uniforms = UniformsUtils.merge( [ shaderLib.uniforms, UniformsLib.lights ] );
  59. }
  60. }
  61. _parseObject() {
  62. const { material, renderer } = this;
  63. if ( renderer.toneMappingNode && renderer.toneMappingNode.isNode === true ) {
  64. this.addSlot( 'fragment', new SlotNode( {
  65. node: material.colorNode,
  66. nodeType: 'vec4',
  67. source: getIncludeSnippet( 'tonemapping_fragment' ),
  68. target: ''
  69. } ) );
  70. }
  71. // parse inputs
  72. if ( material.colorNode && material.colorNode.isNode ) {
  73. this.addSlot( 'fragment', new SlotNode( {
  74. node: material.colorNode,
  75. nodeType: 'vec4',
  76. source: 'vec4 diffuseColor = vec4( diffuse, opacity );',
  77. target: 'vec4 diffuseColor = %RESULT%;'
  78. } ) );
  79. }
  80. if ( material.opacityNode && material.opacityNode.isNode ) {
  81. this.addSlot( 'fragment', new SlotNode( {
  82. node: material.opacityNode,
  83. nodeType: 'float',
  84. source: getIncludeSnippet( 'alphatest_fragment' ),
  85. target: 'diffuseColor.a = %RESULT%;',
  86. inclusionType: 'append'
  87. } ) );
  88. } else {
  89. this.addCode( 'fragment', getIncludeSnippet( 'alphatest_fragment' ), 'diffuseColor.a = opacity;', this.shader );
  90. }
  91. if ( material.normalNode && material.normalNode.isNode ) {
  92. this.addSlot( 'fragment', new SlotNode( {
  93. node: material.normalNode,
  94. nodeType: 'vec3',
  95. source: getIncludeSnippet( 'normal_fragment_begin' ),
  96. target: 'normal = %RESULT%;',
  97. inclusionType: 'append'
  98. } ) );
  99. }
  100. if ( material.emissiveNode && material.emissiveNode.isNode ) {
  101. this.addSlot( 'fragment', new SlotNode( {
  102. node: material.emissiveNode,
  103. nodeType: 'vec3',
  104. source: getIncludeSnippet( 'emissivemap_fragment' ),
  105. target: 'totalEmissiveRadiance = %RESULT%;',
  106. inclusionType: 'append'
  107. } ) );
  108. }
  109. if ( material.isMeshStandardNodeMaterial ) {
  110. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  111. this.addSlot( 'fragment', new SlotNode( {
  112. node: material.metalnessNode,
  113. nodeType: 'float',
  114. source: getIncludeSnippet( 'metalnessmap_fragment' ),
  115. target: 'metalnessFactor = %RESULT%;',
  116. inclusionType: 'append'
  117. } ) );
  118. }
  119. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  120. this.addSlot( 'fragment', new SlotNode( {
  121. node: material.roughnessNode,
  122. nodeType: 'float',
  123. source: getIncludeSnippet( 'roughnessmap_fragment' ),
  124. target: 'roughnessFactor = %RESULT%;',
  125. inclusionType: 'append'
  126. } ) );
  127. }
  128. if ( material.isMeshPhysicalNodeMaterial ) {
  129. if ( material.clearcoatNode && material.clearcoatNode.isNode ) {
  130. this.addSlot( 'fragment', new SlotNode( {
  131. node: material.clearcoatNode,
  132. nodeType: 'float',
  133. source: 'material.clearcoat = clearcoat;',
  134. target: 'material.clearcoat = %RESULT%;'
  135. } ) );
  136. if ( material.clearcoatRoughnessNode && material.clearcoatRoughnessNode.isNode ) {
  137. this.addSlot( 'fragment', new SlotNode( {
  138. node: material.clearcoatRoughnessNode,
  139. nodeType: 'float',
  140. source: 'material.clearcoatRoughness = clearcoatRoughness;',
  141. target: 'material.clearcoatRoughness = %RESULT%;'
  142. } ) );
  143. }
  144. if ( material.clearcoatNormalNode && material.clearcoatNormalNode.isNode ) {
  145. this.addSlot( 'fragment', new SlotNode( {
  146. node: material.clearcoatNormalNode,
  147. nodeType: 'vec3',
  148. source: 'vec3 clearcoatNormal = geometryNormal;',
  149. target: 'vec3 clearcoatNormal = %RESULT%;'
  150. } ) );
  151. }
  152. material.defines.USE_CLEARCOAT = '';
  153. } else {
  154. delete material.defines.USE_CLEARCOAT;
  155. }
  156. if ( material.sheenNode && material.sheenNode.isNode ) {
  157. this.addSlot( 'fragment', new SlotNode( {
  158. node: material.sheenNode,
  159. nodeType: 'vec3',
  160. source: 'material.sheenColor = sheenColor;',
  161. target: 'material.sheenColor = %RESULT%;'
  162. } ) );
  163. if ( material.sheenRoughnessNode && material.sheenRoughnessNode.isNode ) {
  164. this.addSlot( 'fragment', new SlotNode( {
  165. node: material.sheenRoughnessNode,
  166. nodeType: 'float',
  167. source: 'material.sheenRoughness = clamp( sheenRoughness, 0.07, 1.0 );',
  168. target: 'material.sheenRoughness = clamp( %RESULT%, 0.07, 1.0 );'
  169. } ) );
  170. }
  171. material.defines.USE_SHEEN = '';
  172. } else {
  173. delete material.defines.USE_SHEEN;
  174. }
  175. if ( material.iridescenceNode && material.iridescenceNode.isNode ) {
  176. this.addSlot( 'fragment', new SlotNode( {
  177. node: material.iridescenceNode,
  178. nodeType: 'float',
  179. source: 'material.iridescence = iridescence;',
  180. target: 'material.iridescence = %RESULT%;'
  181. } ) );
  182. if ( material.iridescenceIORNode && material.iridescenceIORNode.isNode ) {
  183. this.addSlot( 'fragment', new SlotNode( {
  184. node: material.iridescenceIORNode,
  185. nodeType: 'float',
  186. source: 'material.iridescenceIOR = iridescenceIOR;',
  187. target: 'material.iridescenceIOR = %RESULT%;'
  188. } ) );
  189. }
  190. if ( material.iridescenceThicknessNode && material.iridescenceThicknessNode.isNode ) {
  191. this.addSlot( 'fragment', new SlotNode( {
  192. node: material.iridescenceThicknessNode,
  193. nodeType: 'float',
  194. source: 'material.iridescenceThickness = iridescenceThicknessMaximum;',
  195. target: 'material.iridescenceThickness = %RESULT%;'
  196. } ) );
  197. }
  198. material.defines.USE_IRIDESCENCE = '';
  199. } else {
  200. delete material.defines.USE_IRIDESCENCE;
  201. }
  202. if ( material.iorNode && material.iorNode.isNode ) {
  203. this.addSlot( 'fragment', new SlotNode( {
  204. node: material.iorNode,
  205. nodeType: 'float',
  206. source: 'material.ior = ior;',
  207. target: 'material.ior = %RESULT%;'
  208. } ) );
  209. }
  210. if ( material.specularColorNode && material.specularColorNode.isNode ) {
  211. this.addSlot( 'fragment', new SlotNode( {
  212. node: material.specularColorNode,
  213. nodeType: 'vec3',
  214. source: 'vec3 specularColorFactor = specularColor;',
  215. target: 'vec3 specularColorFactor = %RESULT%;'
  216. } ) );
  217. }
  218. if ( material.specularIntensityNode && material.specularIntensityNode.isNode ) {
  219. this.addSlot( 'fragment', new SlotNode( {
  220. node: material.specularIntensityNode,
  221. nodeType: 'float',
  222. source: 'float specularIntensityFactor = specularIntensity;',
  223. target: 'float specularIntensityFactor = %RESULT%;'
  224. } ) );
  225. }
  226. if ( material.transmissionNode && material.transmissionNode.isNode ) {
  227. this.addSlot( 'fragment', new SlotNode( {
  228. node: material.transmissionNode,
  229. nodeType: 'float',
  230. source: 'material.transmission = transmission;',
  231. target: 'material.transmission = %RESULT%;'
  232. } ) );
  233. if ( material.thicknessNode && material.thicknessNode.isNode ) {
  234. this.addSlot( 'fragment', new SlotNode( {
  235. node: material.thicknessNode,
  236. nodeType: 'float',
  237. source: 'material.thickness = thickness;',
  238. target: 'material.thickness = %RESULT%;'
  239. } ) );
  240. }
  241. if ( material.thicknessNode && material.thicknessNode.isNode ) {
  242. this.addSlot( 'fragment', new SlotNode( {
  243. node: material.thicknessNode,
  244. nodeType: 'float',
  245. source: 'material.thickness = thickness;',
  246. target: 'material.thickness = %RESULT%;'
  247. } ) );
  248. }
  249. if ( material.attenuationDistanceNode && material.attenuationDistanceNode.isNode ) {
  250. this.addSlot( 'fragment', new SlotNode( {
  251. node: material.attenuationDistanceNode,
  252. nodeType: 'float',
  253. source: 'material.attenuationDistance = attenuationDistance;',
  254. target: 'material.attenuationDistance = %RESULT%;'
  255. } ) );
  256. }
  257. if ( material.attenuationColorNode && material.attenuationColorNode.isNode ) {
  258. this.addSlot( 'fragment', new SlotNode( {
  259. node: material.attenuationColorNode,
  260. nodeType: 'vec3',
  261. source: 'material.attenuationColor = attenuationColor;',
  262. target: 'material.attenuationColor = %RESULT%;'
  263. } ) );
  264. }
  265. material.transmission = 1;
  266. material.defines.USE_TRANSMISSION = '';
  267. } else {
  268. material.transmission = 0;
  269. delete material.defines.USE_TRANSMISSION;
  270. }
  271. }
  272. }
  273. //
  274. if ( material.positionNode && material.positionNode.isNode ) {
  275. this.addSlot( 'vertex', new SlotNode( {
  276. node: material.positionNode,
  277. nodeType: 'vec3',
  278. source: getIncludeSnippet( 'begin_vertex' ),
  279. target: 'transformed = %RESULT%;',
  280. inclusionType: 'append'
  281. } ) );
  282. }
  283. if ( material.sizeNode && material.sizeNode.isNode ) {
  284. this.addSlot( 'vertex', new SlotNode( {
  285. node: material.sizeNode,
  286. nodeType: 'float',
  287. source: 'gl_PointSize = size;',
  288. target: 'gl_PointSize = %RESULT%;'
  289. } ) );
  290. }
  291. }
  292. getTexture( texture, textureProperty, uvSnippet ) {
  293. if ( texture.isTextureCube ) {
  294. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  295. } else {
  296. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  297. }
  298. }
  299. getTextureBias( texture, textureProperty, uvSnippet, biasSnippet ) {
  300. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  301. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  302. }
  303. getUniforms( shaderStage ) {
  304. const uniforms = this.uniforms[ shaderStage ];
  305. let output = '';
  306. for ( const uniform of uniforms ) {
  307. let snippet = null;
  308. if ( uniform.type === 'texture' ) {
  309. snippet = `sampler2D ${uniform.name}; `;
  310. } else if ( uniform.type === 'cubeTexture' ) {
  311. snippet = `samplerCube ${uniform.name}; `;
  312. } else {
  313. const vectorType = this.getVectorType( uniform.type );
  314. snippet = `${vectorType} ${uniform.name}; `;
  315. }
  316. const precision = uniform.node.precision;
  317. if ( precision !== null ) {
  318. snippet = 'uniform ' + precisionLib[ precision ] + ' ' + snippet;
  319. } else {
  320. snippet = 'uniform ' + snippet;
  321. }
  322. output += snippet;
  323. }
  324. return output;
  325. }
  326. getAttributes( shaderStage ) {
  327. let snippet = '';
  328. if ( shaderStage === 'vertex' ) {
  329. const attributes = this.attributes;
  330. for ( const attribute of attributes ) {
  331. // ignore common attributes to prevent redefinitions
  332. if ( attribute.name === 'uv' || attribute.name === 'position' || attribute.name === 'normal' )
  333. continue;
  334. snippet += `attribute ${attribute.type} ${attribute.name}; `;
  335. }
  336. }
  337. return snippet;
  338. }
  339. getVaryings( shaderStage ) {
  340. let snippet = '';
  341. const varyings = this.varyings;
  342. if ( shaderStage === 'vertex' ) {
  343. for ( const varying of varyings ) {
  344. snippet += `${varying.needsInterpolation ? 'varying' : '/*varying*/'} ${varying.type} ${varying.name}; `;
  345. }
  346. } else if ( shaderStage === 'fragment' ) {
  347. for ( const varying of varyings ) {
  348. if ( varying.needsInterpolation ) {
  349. snippet += `varying ${varying.type} ${varying.name}; `;
  350. }
  351. }
  352. }
  353. return snippet;
  354. }
  355. addCode( shaderStage, source, code, scope = this ) {
  356. const shaderProperty = getShaderStageProperty( shaderStage );
  357. let snippet = scope[ shaderProperty ];
  358. const index = snippet.indexOf( source );
  359. if ( index !== - 1 ) {
  360. const start = snippet.substring( 0, index + source.length );
  361. const end = snippet.substring( index + source.length );
  362. snippet = `${start}\n${code}\n${end}`;
  363. }
  364. scope[ shaderProperty ] = snippet;
  365. }
  366. replaceCode( shaderStage, source, target, scope = this ) {
  367. const shaderProperty = getShaderStageProperty( shaderStage );
  368. scope[ shaderProperty ] = scope[ shaderProperty ].replaceAll( source, target );
  369. }
  370. getFrontFacing() {
  371. return 'gl_FrontFacing';
  372. }
  373. getFragCoord() {
  374. return 'gl_FragCoord';
  375. }
  376. isFlipY() {
  377. return true;
  378. }
  379. buildCode() {
  380. const shaderData = {};
  381. for ( const shaderStage of defaultShaderStages ) {
  382. const uniforms = this.getUniforms( shaderStage );
  383. const attributes = this.getAttributes( shaderStage );
  384. const varyings = this.getVaryings( shaderStage );
  385. const vars = this.getVars( shaderStage );
  386. const codes = this.getCodes( shaderStage );
  387. shaderData[ shaderStage ] = `${this.getSignature()}
  388. // <node_builder>
  389. // uniforms
  390. ${uniforms}
  391. // attributes
  392. ${attributes}
  393. // varyings
  394. ${varyings}
  395. // vars
  396. ${vars}
  397. // codes
  398. ${codes}
  399. // </node_builder>
  400. ${this.shader[ getShaderStageProperty( shaderStage ) ]}
  401. `;
  402. }
  403. this.vertexShader = shaderData.vertex;
  404. this.fragmentShader = shaderData.fragment;
  405. }
  406. build() {
  407. super.build();
  408. this._addSnippets();
  409. this._addUniforms();
  410. this._updateUniforms();
  411. this.shader.vertexShader = this.vertexShader;
  412. this.shader.fragmentShader = this.fragmentShader;
  413. return this;
  414. }
  415. _parseInclude( shaderStage, ...includes ) {
  416. for ( const name of includes ) {
  417. const includeSnippet = getIncludeSnippet( name );
  418. const code = ShaderChunk[ name ];
  419. const shaderProperty = getShaderStageProperty( shaderStage );
  420. this.shader[ shaderProperty ] = this.shader[ shaderProperty ].replaceAll( includeSnippet, code );
  421. }
  422. }
  423. _sortSlotsToFlow() {
  424. for ( const shaderStage of defaultShaderStages ) {
  425. const sourceCode = this.shader[ getShaderStageProperty( shaderStage ) ];
  426. const slots = this.slots[ shaderStage ].sort( ( slotA, slotB ) => {
  427. return sourceCode.indexOf( slotA.source ) > sourceCode.indexOf( slotB.source ) ? 1 : - 1;
  428. } );
  429. for ( const slotNode of slots ) {
  430. this.addFlow( shaderStage, slotNode );
  431. }
  432. }
  433. }
  434. _addSnippets() {
  435. for ( const shaderStage of defaultShaderStages ) {
  436. for ( const slotNode of this.slots[ shaderStage ] ) {
  437. const flowData = this.getFlowData( slotNode/*, shaderStage*/ );
  438. const inclusionType = slotNode.inclusionType;
  439. const source = slotNode.source;
  440. const target = flowData.code + '\n\t' + slotNode.target.replace( '%RESULT%', flowData.result );
  441. if ( inclusionType === 'append' ) {
  442. this.addCode( shaderStage, source, target );
  443. } else if ( inclusionType === 'replace' ) {
  444. this.replaceCode( shaderStage, source, target );
  445. } else {
  446. console.warn( `Inclusion type "${ inclusionType }" not compatible.` );
  447. }
  448. }
  449. this.addCode(
  450. shaderStage,
  451. 'main() {',
  452. '\n\t' + this.flowCode[ shaderStage ]
  453. );
  454. }
  455. }
  456. _addUniforms() {
  457. for ( const shaderStage of defaultShaderStages ) {
  458. // uniforms
  459. for ( const uniform of this.uniforms[ shaderStage ] ) {
  460. this.shader.uniforms[ uniform.name ] = uniform;
  461. }
  462. }
  463. }
  464. _updateUniforms() {
  465. nodeFrame.object = this.object;
  466. nodeFrame.renderer = this.renderer;
  467. for ( const node of this.updateNodes ) {
  468. nodeFrame.updateNode( node );
  469. }
  470. }
  471. }
  472. export { WebGLNodeBuilder };