GLSLNodeBuilder.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial } from '../../../nodes/Nodes.js';
  2. import UniformsGroup from '../../common/UniformsGroup.js';
  3. import { NodeSampledTexture, NodeSampledCubeTexture } from '../../common/nodes/NodeSampledTexture.js';
  4. const glslMethods = {
  5. [ MathNode.ATAN2 ]: 'atan'
  6. };
  7. const precisionLib = {
  8. low: 'lowp',
  9. medium: 'mediump',
  10. high: 'highp'
  11. };
  12. class GLSLNodeBuilder extends NodeBuilder {
  13. constructor( object, renderer, scene = null ) {
  14. super( object, renderer, new GLSLNodeParser(), scene );
  15. this.uniformsGroup = {};
  16. }
  17. getMethod( method ) {
  18. return glslMethods[ method ] || method;
  19. }
  20. getTexture( texture, textureProperty, uvSnippet ) {
  21. if ( texture.isTextureCube ) {
  22. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  23. } else {
  24. return `texture( ${textureProperty}, ${uvSnippet} )`;
  25. }
  26. }
  27. getTextureLevel( texture, textureProperty, uvSnippet, biasSnippet ) {
  28. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  29. }
  30. getVars( shaderStage ) {
  31. const snippets = [];
  32. const vars = this.vars[ shaderStage ];
  33. for ( const variable of vars ) {
  34. snippets.push( `${ this.getVar( variable.type, variable.name ) };` );
  35. }
  36. return snippets.join( '\n\t' );
  37. }
  38. getUniforms( shaderStage ) {
  39. const uniforms = this.uniforms[ shaderStage ];
  40. const bindingSnippets = [];
  41. const groupSnippets = [];
  42. for ( const uniform of uniforms ) {
  43. let snippet = null;
  44. let group = false;
  45. if ( uniform.type === 'texture' ) {
  46. snippet = `sampler2D ${uniform.name};`;
  47. } else if ( uniform.type === 'cubeTexture' ) {
  48. snippet = `samplerCube ${uniform.name};`;
  49. } else {
  50. const vectorType = this.getVectorType( uniform.type );
  51. snippet = `${vectorType} ${uniform.name};`;
  52. group = true;
  53. }
  54. const precision = uniform.node.precision;
  55. if ( precision !== null ) {
  56. snippet = precisionLib[ precision ] + ' ' + snippet;
  57. }
  58. if ( group ) {
  59. snippet = '\t' + snippet;
  60. groupSnippets.push( snippet );
  61. } else {
  62. snippet = 'uniform ' + snippet;
  63. bindingSnippets.push( snippet );
  64. }
  65. }
  66. let output = '';
  67. if ( groupSnippets.length > 0 ) {
  68. output += this._getGLSLUniformStruct( shaderStage + 'NodeUniforms', groupSnippets.join( '\n' ) ) + '\n';
  69. }
  70. output += bindingSnippets.join( '\n' );
  71. return output;
  72. }
  73. getAttributes( shaderStage ) {
  74. let snippet = '';
  75. if ( shaderStage === 'vertex' ) {
  76. const attributes = this.attributes;
  77. let location = 0;
  78. for ( const attribute of attributes ) {
  79. snippet += `layout( location = ${ location ++ } ) in ${ attribute.type } ${ attribute.name };\n`;
  80. }
  81. }
  82. return snippet;
  83. }
  84. getVaryings( shaderStage ) {
  85. let snippet = '';
  86. const varyings = this.varyings;
  87. if ( shaderStage === 'vertex' ) {
  88. for ( const varying of varyings ) {
  89. snippet += `${varying.needsInterpolation ? 'out' : '/*out*/'} ${varying.type} ${varying.name};\n`;
  90. }
  91. } else if ( shaderStage === 'fragment' ) {
  92. for ( const varying of varyings ) {
  93. if ( varying.needsInterpolation ) {
  94. snippet += `in ${varying.type} ${varying.name};\n`;
  95. }
  96. }
  97. }
  98. return snippet;
  99. }
  100. getVertexIndex() {
  101. return 'gl_VertexID';
  102. }
  103. getFrontFacing() {
  104. return 'gl_FrontFacing';
  105. }
  106. getFragCoord() {
  107. return 'gl_FragCoord';
  108. }
  109. isFlipY() {
  110. return true;
  111. }
  112. _getGLSLUniformStruct( name, vars ) {
  113. return `
  114. layout( std140 ) uniform ${name} {
  115. ${vars}
  116. };`;
  117. }
  118. _getGLSLVertexCode( shaderData ) {
  119. return `#version 300 es
  120. ${ this.getSignature() }
  121. // precision
  122. precision highp float;
  123. precision highp int;
  124. // uniforms
  125. ${shaderData.uniforms}
  126. // varyings
  127. ${shaderData.varyings}
  128. // attributes
  129. ${shaderData.attributes}
  130. // codes
  131. ${shaderData.codes}
  132. void main() {
  133. // vars
  134. ${shaderData.vars}
  135. // flow
  136. ${shaderData.flow}
  137. gl_PointSize = 1.0;
  138. }
  139. `;
  140. }
  141. _getGLSLFragmentCode( shaderData ) {
  142. return `#version 300 es
  143. ${ this.getSignature() }
  144. // precision
  145. precision highp float;
  146. precision highp int;
  147. // uniforms
  148. ${shaderData.uniforms}
  149. // varyings
  150. ${shaderData.varyings}
  151. // codes
  152. ${shaderData.codes}
  153. layout( location = 0 ) out vec4 fragColor;
  154. void main() {
  155. // vars
  156. ${shaderData.vars}
  157. // flow
  158. ${shaderData.flow}
  159. }
  160. `;
  161. }
  162. buildCode() {
  163. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  164. for ( const shaderStage in shadersData ) {
  165. let flow = '// code\n\n';
  166. flow += this.flowCode[ shaderStage ];
  167. const flowNodes = this.flowNodes[ shaderStage ];
  168. const mainNode = flowNodes[ flowNodes.length - 1 ];
  169. for ( const node of flowNodes ) {
  170. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  171. const slotName = node.name;
  172. if ( slotName ) {
  173. if ( flow.length > 0 ) flow += '\n';
  174. flow += `\t// flow -> ${ slotName }\n\t`;
  175. }
  176. flow += `${ flowSlotData.code }\n\t`;
  177. if ( node === mainNode && shaderStage !== 'compute' ) {
  178. flow += '// result\n\t';
  179. if ( shaderStage === 'vertex' ) {
  180. flow += 'gl_Position = ';
  181. } else if ( shaderStage === 'fragment' ) {
  182. flow += 'fragColor = ';
  183. }
  184. flow += `${ flowSlotData.result };`;
  185. }
  186. }
  187. const stageData = shadersData[ shaderStage ];
  188. stageData.uniforms = this.getUniforms( shaderStage );
  189. stageData.attributes = this.getAttributes( shaderStage );
  190. stageData.varyings = this.getVaryings( shaderStage );
  191. stageData.vars = this.getVars( shaderStage );
  192. stageData.codes = this.getCodes( shaderStage );
  193. stageData.flow = flow;
  194. }
  195. if ( this.material !== null ) {
  196. this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
  197. this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
  198. //console.log( this.vertexShader );
  199. //console.log( this.fragmentShader );
  200. } else {
  201. console.warn( 'GLSLNodeBuilder: compute shaders are not supported.' );
  202. //this.computeShader = this._getGLSLComputeCode( shadersData.compute );
  203. }
  204. }
  205. getUniformFromNode( node, type, shaderStage, name = null ) {
  206. const uniformNode = super.getUniformFromNode( node, type, shaderStage, name );
  207. const nodeData = this.getDataFromNode( node, shaderStage );
  208. let uniformGPU = nodeData.uniformGPU;
  209. if ( uniformGPU === undefined ) {
  210. if ( type === 'texture' ) {
  211. uniformGPU = new NodeSampledTexture( uniformNode.name, uniformNode.node );
  212. this.bindings[ shaderStage ].push( uniformGPU );
  213. } else if ( type === 'cubeTexture' ) {
  214. uniformGPU = new NodeSampledCubeTexture( uniformNode.name, uniformNode.node );
  215. this.bindings[ shaderStage ].push( uniformGPU );
  216. } else {
  217. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  218. if ( uniformsGroup === undefined ) {
  219. uniformsGroup = new UniformsGroup( shaderStage + 'NodeUniforms' );
  220. //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] );
  221. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  222. this.bindings[ shaderStage ].push( uniformsGroup );
  223. }
  224. uniformGPU = this.getNodeUniform( uniformNode, type );
  225. uniformsGroup.addUniform( uniformGPU );
  226. }
  227. nodeData.uniformGPU = uniformGPU;
  228. }
  229. return uniformNode;
  230. }
  231. build() {
  232. // @TODO: Move this code to super.build()
  233. const { object, material } = this;
  234. if ( material !== null ) {
  235. NodeMaterial.fromMaterial( material ).build( this );
  236. } else {
  237. this.addFlow( 'compute', object );
  238. }
  239. return super.build();
  240. }
  241. }
  242. export default GLSLNodeBuilder;