WebGLNodeBuilder.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import NodeBuilder from '../../nodes/core/NodeBuilder.js';
  2. import NodeSlot from '../../nodes/core/NodeSlot.js';
  3. class WebGLNodeBuilder extends NodeBuilder {
  4. constructor( material, renderer, properties ) {
  5. super( material, renderer );
  6. this.properties = properties;
  7. this._parseMaterial();
  8. }
  9. _parseMaterial() {
  10. const material = this.material;
  11. // parse inputs
  12. if ( material.colorNode !== undefined ) {
  13. this.addSlot( 'fragment', new NodeSlot( material.colorNode, 'COLOR', 'vec4' ) );
  14. }
  15. }
  16. getVaryFromNode( node, type ) {
  17. const vary = super.getVaryFromNode( node, type );
  18. if ( node.isUVNode ) {
  19. vary.name = 'vUv';
  20. }
  21. return vary;
  22. }
  23. getTexture( textureProperty, uvSnippet ) {
  24. return `sRGBToLinear( texture2D( ${textureProperty}, ${uvSnippet} ) )`;
  25. }
  26. getUniformsHeaderSnippet( shaderStage ) {
  27. const uniforms = this.uniforms[ shaderStage ];
  28. let snippet = '';
  29. for ( const uniform of uniforms ) {
  30. if ( uniform.type === 'texture' ) {
  31. snippet += `uniform sampler2D ${uniform.name};`;
  32. } else {
  33. const vectorType = this.getVectorType( uniform.type );
  34. snippet += `uniform ${vectorType} ${uniform.name};`;
  35. }
  36. }
  37. return snippet;
  38. }
  39. getAttributesHeaderSnippet( /*shaderStage*/ ) {
  40. }
  41. getVarsHeaderSnippet( /*shaderStage*/ ) {
  42. }
  43. getVarsBodySnippet( /*shaderStage*/ ) {
  44. }
  45. getVarysHeaderSnippet( /*shaderStage*/ ) {
  46. }
  47. getVarysBodySnippet( /*shaderStage*/ ) {
  48. }
  49. composeUniforms() {
  50. const uniforms = this.uniforms[ 'fragment' ];
  51. for ( const uniform of uniforms ) {
  52. this.properties.uniforms[ uniform.name ] = uniform;
  53. }
  54. }
  55. build() {
  56. super.build();
  57. this.properties.defines[ 'NODE_HEADER_UNIFORMS' ] = this.defines[ 'fragment' ][ 'NODE_HEADER_UNIFORMS' ];
  58. this.properties.defines[ 'NODE_COLOR' ] = this.defines[ 'fragment' ][ 'NODE_COLOR' ];
  59. this.composeUniforms();
  60. return this;
  61. }
  62. }
  63. export { WebGLNodeBuilder };