|
| 1 | +import NodeBuilder from '../../nodes/core/NodeBuilder.js'; |
| 2 | + |
| 3 | +import NodeSlot from '../../nodes/core/NodeSlot.js'; |
| 4 | + |
| 5 | +class WebGLNodeBuilder extends NodeBuilder { |
| 6 | + |
| 7 | + constructor( material, renderer, properties ) { |
| 8 | + |
| 9 | + super( material, renderer ); |
| 10 | + |
| 11 | + this.properties = properties; |
| 12 | + |
| 13 | + this._parseMaterial(); |
| 14 | + |
| 15 | + } |
| 16 | + |
| 17 | + _parseMaterial() { |
| 18 | + |
| 19 | + const material = this.material; |
| 20 | + |
| 21 | + // parse inputs |
| 22 | + |
| 23 | + if ( material.colorNode !== undefined ) { |
| 24 | + |
| 25 | + this.addSlot( 'fragment', new NodeSlot( material.colorNode, 'COLOR', 'vec4' ) ); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + getTexture( textureProperty, uvSnippet ) { |
| 32 | + |
| 33 | + return `sRGBToLinear( texture2D( ${textureProperty}, ${uvSnippet} ) )`; |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + getUniformsHeaderSnippet( shaderStage ) { |
| 38 | + |
| 39 | + const uniforms = this.uniforms[ shaderStage ]; |
| 40 | + |
| 41 | + let snippet = ''; |
| 42 | + |
| 43 | + for ( let uniform of uniforms ) { |
| 44 | + |
| 45 | + if ( uniform.type === 'texture' ) { |
| 46 | + |
| 47 | + snippet += `uniform sampler2D ${uniform.name};`; |
| 48 | + |
| 49 | + } else { |
| 50 | + |
| 51 | + let vectorType = this.getVectorType( uniform.type ); |
| 52 | + |
| 53 | + snippet += `uniform ${vectorType} ${uniform.name};`; |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + return snippet; |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + composeUniforms() { |
| 64 | + |
| 65 | + const uniforms = this.uniforms[ 'fragment' ]; |
| 66 | + |
| 67 | + for ( let uniform of uniforms ) { |
| 68 | + |
| 69 | + this.properties.uniforms[ uniform.name ] = uniform; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + build() { |
| 76 | + |
| 77 | + super.build(); |
| 78 | + |
| 79 | + this.properties.defines['NODE_HEADER_UNIFORMS'] = this.defines['fragment']['NODE_HEADER_UNIFORMS']; |
| 80 | + this.properties.defines['NODE_COLOR'] = this.defines['fragment']['NODE_COLOR']; |
| 81 | + |
| 82 | + this.composeUniforms(); |
| 83 | + |
| 84 | + return this; |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +export { WebGLNodeBuilder }; |
0 commit comments