GLSL1NodeBuilder.js 5.0 KB

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