Skip to content

Commit d37f10f

Browse files
committed
draft of WebGLNodeBuilder
1 parent b0a7b5d commit d37f10f

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 };

examples/webgl_materials_standard.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import { OBJLoader } from './jsm/loaders/OBJLoader.js';
2525
import { RGBELoader } from './jsm/loaders/RGBELoader.js';
2626

27+
import TextureNode from './jsm/renderers/nodes/inputs/TextureNode.js';
28+
import Vector3Node from './jsm/renderers/nodes/inputs/Vector3Node.js';
29+
import OperatorNode from './jsm/renderers/nodes/math/OperatorNode.js';
30+
2731
const statsEnabled = true;
2832

2933
let container, stats;
@@ -75,13 +79,15 @@
7579
material.metalness = 1; // attenuates metalnessMap
7680

7781
const diffuseMap = loader.load( 'Cerberus_A.jpg' );
82+
diffuseMap.wrapS = THREE.RepeatWrapping;
7883
diffuseMap.encoding = THREE.sRGBEncoding;
79-
material.map = diffuseMap;
84+
//material.map = diffuseMap;
85+
86+
material.colorNode = new OperatorNode( '*', new TextureNode( diffuseMap ), new Vector3Node( new THREE.Vector3( 1, 1, 1 ) ) );
8087
// roughness is in G channel, metalness is in B channel
8188
material.metalnessMap = material.roughnessMap = loader.load( 'Cerberus_RM.jpg' );
8289
material.normalMap = loader.load( 'Cerberus_N.jpg' );
8390

84-
material.map.wrapS = THREE.RepeatWrapping;
8591
material.roughnessMap.wrapS = THREE.RepeatWrapping;
8692
material.metalnessMap.wrapS = THREE.RepeatWrapping;
8793
material.normalMap.wrapS = THREE.RepeatWrapping;

src/renderers/WebGLRenderer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import { WebGLUtils } from './webgl/WebGLUtils.js';
4040
import { WebXRManager } from './webxr/WebXRManager.js';
4141
import { WebGLMaterials } from './webgl/WebGLMaterials.js';
4242

43+
import { WebGLNodeBuilder } from '../../examples/jsm/renderers/webgl/nodes/WebGLNodeBuilder.js';
44+
4345
function createCanvasElement() {
4446

4547
const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
@@ -1364,6 +1366,8 @@ function WebGLRenderer( parameters ) {
13641366

13651367
material.onBeforeCompile( parameters, _this );
13661368

1369+
new WebGLNodeBuilder( material, this, parameters ).build();
1370+
13671371
program = programCache.acquireProgram( parameters, programCacheKey );
13681372

13691373
materialProperties.program = program;

src/renderers/shaders/ShaderChunk/color_fragment.glsl.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@ export default /* glsl */`
33
44
diffuseColor.rgb *= vColor;
55
6+
#endif
7+
8+
#ifdef NODE_COLOR
9+
10+
diffuseColor *= NODE_COLOR;
11+
612
#endif
713
`;

src/renderers/webgl/WebGLProgram.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
650650

651651
parameters.depthPacking ? '#define DEPTH_PACKING ' + parameters.depthPacking : '',
652652

653+
`#ifdef NODE_HEADER_UNIFORMS
654+
655+
NODE_HEADER_UNIFORMS
656+
657+
#endif`,
658+
653659
'\n'
654660

655661
].filter( filterEmptyLine ).join( '\n' );

0 commit comments

Comments
 (0)