Skip to content

Commit afa1fd8

Browse files
committed
Partially revert "NodeMaterial: Remove WebGLNodeBuilder (mrdoob#28167)"
1 parent 1344ada commit afa1fd8

File tree

4 files changed

+1187
-0
lines changed

4 files changed

+1187
-0
lines changed
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
import { MathNode, GLSLNodeParser, NodeBuilder } from '../../../nodes/Nodes.js';
2+
3+
const glslMethods = {
4+
[ MathNode.ATAN2 ]: 'atan'
5+
};
6+
7+
const precisionLib = {
8+
low: 'lowp',
9+
medium: 'mediump',
10+
high: 'highp'
11+
};
12+
13+
class GLSL1NodeBuilder extends NodeBuilder {
14+
15+
constructor( object, renderer, scene = null ) {
16+
17+
super( object, renderer, new GLSLNodeParser(), scene );
18+
19+
}
20+
21+
getMethod( method ) {
22+
23+
return glslMethods[ method ] || method;
24+
25+
}
26+
27+
getTexture( texture, textureProperty, uvSnippet ) {
28+
29+
if ( texture.isTextureCube ) {
30+
31+
return `textureCube( ${textureProperty}, ${uvSnippet} )`;
32+
33+
} else {
34+
35+
return `texture2D( ${textureProperty}, ${uvSnippet} )`;
36+
37+
}
38+
39+
}
40+
41+
getTextureBias( texture, textureProperty, uvSnippet, biasSnippet ) {
42+
43+
return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
44+
45+
}
46+
47+
getVars( shaderStage ) {
48+
49+
const snippets = [];
50+
51+
const vars = this.vars[ shaderStage ];
52+
53+
for ( const variable of vars ) {
54+
55+
snippets.push( `${ this.getVar( variable.type, variable.name ) };` );
56+
57+
}
58+
59+
return snippets.join( '\n\t' );
60+
61+
}
62+
63+
getUniforms( shaderStage ) {
64+
65+
const uniforms = this.uniforms[ shaderStage ];
66+
67+
let output = '';
68+
69+
for ( const uniform of uniforms ) {
70+
71+
let snippet = null;
72+
73+
if ( uniform.type === 'texture' ) {
74+
75+
snippet = `sampler2D ${uniform.name};\n`;
76+
77+
} else if ( uniform.type === 'cubeTexture' ) {
78+
79+
snippet = `samplerCube ${uniform.name};\n`;
80+
81+
} else {
82+
83+
const vectorType = this.getVectorType( uniform.type );
84+
85+
snippet = `${vectorType} ${uniform.name};\n`;
86+
87+
}
88+
89+
const precision = uniform.node.precision;
90+
91+
if ( precision !== null ) {
92+
93+
snippet = 'uniform ' + precisionLib[ precision ] + ' ' + snippet;
94+
95+
} else {
96+
97+
snippet = 'uniform ' + snippet;
98+
99+
}
100+
101+
output += snippet;
102+
103+
}
104+
105+
return output;
106+
107+
}
108+
109+
getAttributes( shaderStage ) {
110+
111+
let snippet = '';
112+
113+
if ( shaderStage === 'vertex' ) {
114+
115+
const attributes = this.attributes;
116+
117+
for ( const attribute of attributes ) {
118+
119+
snippet += `attribute ${attribute.type} ${attribute.name};\n`;
120+
121+
}
122+
123+
}
124+
125+
return snippet;
126+
127+
}
128+
129+
getVaryings( shaderStage ) {
130+
131+
let snippet = '';
132+
133+
const varyings = this.varyings;
134+
135+
if ( shaderStage === 'vertex' ) {
136+
137+
for ( const varying of varyings ) {
138+
139+
snippet += `${varying.needsInterpolation ? 'varying' : '/*varying*/'} ${varying.type} ${varying.name};\n`;
140+
141+
}
142+
143+
} else if ( shaderStage === 'fragment' ) {
144+
145+
for ( const varying of varyings ) {
146+
147+
if ( varying.needsInterpolation ) {
148+
149+
snippet += `varying ${varying.type} ${varying.name};\n`;
150+
151+
}
152+
153+
}
154+
155+
}
156+
157+
return snippet;
158+
159+
}
160+
161+
getVertexIndex() {
162+
163+
return 'gl_VertexID';
164+
165+
}
166+
167+
getFrontFacing() {
168+
169+
return 'gl_FrontFacing';
170+
171+
}
172+
173+
getFragCoord() {
174+
175+
return 'gl_FragCoord';
176+
177+
}
178+
179+
isFlipY() {
180+
181+
return true;
182+
183+
}
184+
185+
_getGLSLVertexCode( shaderData ) {
186+
187+
return `${ this.getSignature() }
188+
189+
// uniforms
190+
${shaderData.uniforms}
191+
192+
// varyings
193+
${shaderData.varyings}
194+
195+
// attributes
196+
${shaderData.attributes}
197+
198+
// codes
199+
${shaderData.codes}
200+
201+
void main() {
202+
203+
// vars
204+
${shaderData.vars}
205+
206+
// flow
207+
${shaderData.flow}
208+
209+
}
210+
`;
211+
212+
}
213+
214+
_getGLSLFragmentCode( shaderData ) {
215+
216+
return `${ this.getSignature() }
217+
218+
// precision
219+
precision highp float;
220+
precision highp int;
221+
222+
// uniforms
223+
${shaderData.uniforms}
224+
225+
// varyings
226+
${shaderData.varyings}
227+
228+
// codes
229+
${shaderData.codes}
230+
231+
void main() {
232+
233+
// vars
234+
${shaderData.vars}
235+
236+
// flow
237+
${shaderData.flow}
238+
239+
}
240+
`;
241+
242+
}
243+
244+
buildCode() {
245+
246+
const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
247+
248+
for ( const shaderStage in shadersData ) {
249+
250+
let flow = '// code\n\n';
251+
flow += this.flowCode[ shaderStage ];
252+
253+
const flowNodes = this.flowNodes[ shaderStage ];
254+
const mainNode = flowNodes[ flowNodes.length - 1 ];
255+
256+
for ( const node of flowNodes ) {
257+
258+
const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
259+
const slotName = node.name;
260+
261+
if ( slotName ) {
262+
263+
if ( flow.length > 0 ) flow += '\n';
264+
265+
flow += `\t// flow -> ${ slotName }\n\t`;
266+
267+
}
268+
269+
flow += `${ flowSlotData.code }\n\t`;
270+
271+
if ( node === mainNode && shaderStage !== 'compute' ) {
272+
273+
flow += '// result\n\t';
274+
275+
if ( shaderStage === 'vertex' ) {
276+
277+
flow += 'gl_Position = ';
278+
279+
} else if ( shaderStage === 'fragment' ) {
280+
281+
flow += 'gl_FragColor = ';
282+
283+
}
284+
285+
flow += `${ flowSlotData.result };`;
286+
287+
}
288+
289+
}
290+
291+
const stageData = shadersData[ shaderStage ];
292+
293+
stageData.uniforms = this.getUniforms( shaderStage );
294+
stageData.attributes = this.getAttributes( shaderStage );
295+
stageData.varyings = this.getVaryings( shaderStage );
296+
stageData.vars = this.getVars( shaderStage );
297+
stageData.codes = this.getCodes( shaderStage );
298+
stageData.flow = flow;
299+
300+
}
301+
302+
if ( this.material !== null ) {
303+
304+
this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
305+
this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
306+
307+
} else {
308+
309+
console.warn( 'GLSLNodeBuilder: compute shaders are not supported.' );
310+
//this.computeShader = this._getGLSLComputeCode( shadersData.compute );
311+
312+
}
313+
314+
}
315+
316+
}
317+
318+
export default GLSL1NodeBuilder;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Node } from '../../../nodes/Nodes.js';
2+
3+
class SlotNode extends Node {
4+
5+
constructor( params ) {
6+
7+
super( params.nodeType );
8+
9+
this.node = null;
10+
this.source = null;
11+
this.target = null;
12+
this.inclusionType = 'replace';
13+
14+
Object.assign( this, params );
15+
16+
}
17+
18+
generate( builder ) {
19+
20+
return this.node.build( builder, this.getNodeType( builder ) );
21+
22+
}
23+
24+
}
25+
26+
export default SlotNode;

0 commit comments

Comments
 (0)