GLSL1NodeBuilder.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { MathNode, GLSLNodeParser, NodeBuilder } from '../../../nodes/Nodes.js';
  2. const glslMethods = {
  3. [ MathNode.ATAN2 ]: 'atan'
  4. };
  5. const precisionLib = {
  6. low: 'lowp',
  7. medium: 'mediump',
  8. high: 'highp'
  9. };
  10. class GLSL1NodeBuilder extends NodeBuilder {
  11. constructor( object, renderer, scene = null ) {
  12. super( object, renderer, new GLSLNodeParser(), scene );
  13. }
  14. getMethod( method ) {
  15. return glslMethods[ method ] || method;
  16. }
  17. getTexture( texture, textureProperty, uvSnippet ) {
  18. if ( texture.isTextureCube ) {
  19. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  20. } else {
  21. return `texture2D( ${textureProperty}, ${uvSnippet} )`;
  22. }
  23. }
  24. getTextureBias( texture, textureProperty, uvSnippet, biasSnippet ) {
  25. if ( this.material.extensions !== undefined ) this.material.extensions.shaderTextureLOD = true;
  26. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  27. }
  28. getVars( shaderStage ) {
  29. const snippets = [];
  30. const vars = this.vars[ shaderStage ];
  31. for ( const variable of vars ) {
  32. snippets.push( `${ this.getVar( variable.type, variable.name ) };` );
  33. }
  34. return snippets.join( '\n\t' );
  35. }
  36. getUniforms( shaderStage ) {
  37. const uniforms = this.uniforms[ shaderStage ];
  38. let output = '';
  39. for ( const uniform of uniforms ) {
  40. let snippet = null;
  41. if ( uniform.type === 'texture' ) {
  42. snippet = `sampler2D ${uniform.name};\n`;
  43. } else if ( uniform.type === 'cubeTexture' ) {
  44. snippet = `samplerCube ${uniform.name};\n`;
  45. } else {
  46. const vectorType = this.getVectorType( uniform.type );
  47. snippet = `${vectorType} ${uniform.name};\n`;
  48. }
  49. const precision = uniform.node.precision;
  50. if ( precision !== null ) {
  51. snippet = 'uniform ' + precisionLib[ precision ] + ' ' + snippet;
  52. } else {
  53. snippet = 'uniform ' + snippet;
  54. }
  55. output += snippet;
  56. }
  57. return output;
  58. }
  59. getAttributes( shaderStage ) {
  60. let snippet = '';
  61. if ( shaderStage === 'vertex' ) {
  62. const attributes = this.attributes;
  63. for ( const attribute of attributes ) {
  64. snippet += `attribute ${attribute.type} ${attribute.name};\n`;
  65. }
  66. }
  67. return snippet;
  68. }
  69. getVaryings( shaderStage ) {
  70. let snippet = '';
  71. const varyings = this.varyings;
  72. if ( shaderStage === 'vertex' ) {
  73. for ( const varying of varyings ) {
  74. snippet += `${varying.needsInterpolation ? 'varying' : '/*varying*/'} ${varying.type} ${varying.name};\n`;
  75. }
  76. } else if ( shaderStage === 'fragment' ) {
  77. for ( const varying of varyings ) {
  78. if ( varying.needsInterpolation ) {
  79. snippet += `varying ${varying.type} ${varying.name};\n`;
  80. }
  81. }
  82. }
  83. return snippet;
  84. }
  85. getVertexIndex() {
  86. return 'gl_VertexID';
  87. }
  88. getFrontFacing() {
  89. return 'gl_FrontFacing';
  90. }
  91. getFragCoord() {
  92. return 'gl_FragCoord';
  93. }
  94. isFlipY() {
  95. return true;
  96. }
  97. _getGLSLVertexCode( shaderData ) {
  98. return `${ this.getSignature() }
  99. // uniforms
  100. ${shaderData.uniforms}
  101. // varyings
  102. ${shaderData.varyings}
  103. // attributes
  104. ${shaderData.attributes}
  105. // codes
  106. ${shaderData.codes}
  107. void main() {
  108. // vars
  109. ${shaderData.vars}
  110. // flow
  111. ${shaderData.flow}
  112. }
  113. `;
  114. }
  115. _getGLSLFragmentCode( shaderData ) {
  116. return `${ this.getSignature() }
  117. // precision
  118. precision highp float;
  119. precision highp int;
  120. // uniforms
  121. ${shaderData.uniforms}
  122. // varyings
  123. ${shaderData.varyings}
  124. // codes
  125. ${shaderData.codes}
  126. void main() {
  127. // vars
  128. ${shaderData.vars}
  129. // flow
  130. ${shaderData.flow}
  131. }
  132. `;
  133. }
  134. buildCode() {
  135. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  136. for ( const shaderStage in shadersData ) {
  137. let flow = '// code\n\n';
  138. flow += this.flowCode[ shaderStage ];
  139. const flowNodes = this.flowNodes[ shaderStage ];
  140. const mainNode = flowNodes[ flowNodes.length - 1 ];
  141. for ( const node of flowNodes ) {
  142. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  143. const slotName = node.name;
  144. if ( slotName ) {
  145. if ( flow.length > 0 ) flow += '\n';
  146. flow += `\t// flow -> ${ slotName }\n\t`;
  147. }
  148. flow += `${ flowSlotData.code }\n\t`;
  149. if ( node === mainNode && shaderStage !== 'compute' ) {
  150. flow += '// result\n\t';
  151. if ( shaderStage === 'vertex' ) {
  152. flow += 'gl_Position = ';
  153. } else if ( shaderStage === 'fragment' ) {
  154. flow += 'gl_FragColor = ';
  155. }
  156. flow += `${ flowSlotData.result };`;
  157. }
  158. }
  159. const stageData = shadersData[ shaderStage ];
  160. stageData.uniforms = this.getUniforms( shaderStage );
  161. stageData.attributes = this.getAttributes( shaderStage );
  162. stageData.varyings = this.getVaryings( shaderStage );
  163. stageData.vars = this.getVars( shaderStage );
  164. stageData.codes = this.getCodes( shaderStage );
  165. stageData.flow = flow;
  166. }
  167. if ( this.material !== null ) {
  168. this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
  169. this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
  170. } else {
  171. console.warn( 'GLSLNodeBuilder: compute shaders are not supported.' );
  172. //this.computeShader = this._getGLSLComputeCode( shadersData.compute );
  173. }
  174. }
  175. }
  176. export default GLSL1NodeBuilder;