@@ -35,7 +35,7 @@ function shadergen(p5, fn) {
35
35
generatorFunction = shaderModifier ;
36
36
}
37
37
const generator = new ShaderGenerator ( generatorFunction , this , options . srcLocations )
38
- const generatedModifyArgument = generator . hookCallbacks ( ) ;
38
+ const generatedModifyArgument = generator . generate ( ) ;
39
39
console . log ( "SRC STRING: " , generatorFunction ) ;
40
40
console . log ( "NEW OPTIONS:" , generatedModifyArgument [ 'vec4 getFinalColor' ] )
41
41
@@ -113,7 +113,9 @@ function shadergen(p5, fn) {
113
113
throw new TypeError ( "Cannot construct BaseNode instances directly. This is an abstract class." ) ;
114
114
}
115
115
this . type = type ;
116
-
116
+ this . componentNames = [ ] ;
117
+ this . swizzleAccessed = false ;
118
+ this . swizzleChanged = false ;
117
119
// For tracking recursion depth and creating temporary variables
118
120
this . isInternal = isInternal ;
119
121
this . usedIn = [ ] ;
@@ -131,7 +133,36 @@ function shadergen(p5, fn) {
131
133
}
132
134
}
133
135
}
134
-
136
+ // get type() {
137
+ // return this._type;
138
+ // }
139
+
140
+ // set type(value) {
141
+ // this._type = value;
142
+ // }
143
+
144
+ addVectorComponents ( ) {
145
+ if ( this . type . startsWith ( 'vec' ) ) {
146
+ const vectorDimensions = + this . type . slice ( 3 ) ;
147
+ this . componentNames = [ 'x' , 'y' , 'z' , 'w' ] . slice ( 0 , vectorDimensions ) ;
148
+
149
+ for ( let componentName of this . componentNames ) {
150
+ // let value = new FloatNode()
151
+ let value = new ComponentNode ( this , componentName , 'float' , true ) ;
152
+ Object . defineProperty ( this , componentName , {
153
+ get ( ) {
154
+ this . swizzleAccessed = true ;
155
+ return value ;
156
+ } ,
157
+ set ( newValue ) {
158
+ this . swizzleChanged = true ;
159
+ value = newValue ;
160
+ }
161
+ } )
162
+ }
163
+ }
164
+ }
165
+
135
166
// The base node implements a version of toGLSL which determines whether the generated code should be stored in a temporary variable.
136
167
toGLSLBase ( context ) {
137
168
if ( this . shouldUseTemporaryVariable ( ) ) {
@@ -143,7 +174,7 @@ function shadergen(p5, fn) {
143
174
}
144
175
145
176
shouldUseTemporaryVariable ( ) {
146
- if ( this . temporaryVariable )
177
+ if ( this . swizzleChanged ) { return true ; }
147
178
if ( this . isInternal || isVariableNode ( this ) ) { return false ; }
148
179
let score = 0 ;
149
180
score += isBinaryOperatorNode ( this ) ;
@@ -159,7 +190,18 @@ function shadergen(p5, fn) {
159
190
if ( this . srcLine ) {
160
191
line += `\n// From ${ this . srcLine } \n` ;
161
192
}
162
- line += this . type + " " + this . temporaryVariable + " = " + this . toGLSL ( context ) + ";" ;
193
+ if ( this . swizzleChanged ) {
194
+ const valueArgs = [ ] ;
195
+ for ( let componentName of this . componentNames ) {
196
+ valueArgs . push ( this [ componentName ] )
197
+ }
198
+ console . log ( this , valueArgs )
199
+ const replacement = nodeConstructors [ this . type ] ( valueArgs )
200
+ line += this . type + " " + this . temporaryVariable + " = " + this . toGLSL ( context ) + ";" ;
201
+ line += `\n` + this . temporaryVariable + " = " + replacement . toGLSL ( context ) + ";" ;
202
+ } else {
203
+ line += this . type + " " + this . temporaryVariable + " = " + this . toGLSL ( context ) + ";" ;
204
+ }
163
205
context . declarations . push ( line ) ;
164
206
}
165
207
return this . temporaryVariable ;
@@ -191,22 +233,6 @@ function shadergen(p5, fn) {
191
233
return new this . constructor ( other ) ;
192
234
}
193
235
}
194
-
195
- addComponent ( componentName , type = 'float' ) {
196
- this [ componentName ] = new ComponentNode ( this , componentName , type , true ) ;
197
- }
198
-
199
- autoAddVectorComponents ( ) {
200
- const options = {
201
- vec2 : [ 'x' , 'y' ] ,
202
- vec3 : [ 'x' , 'y' , 'z' ] ,
203
- vec4 : [ 'x' , 'y' , 'z' , 'w' ]
204
- } ;
205
- const componentNames = options [ this . type ] || [ ] ;
206
- for ( let componentName of componentNames ) {
207
- this . addComponent ( componentName ) ;
208
- }
209
- }
210
236
211
237
toGLSL ( context ) {
212
238
throw new TypeError ( "Not supposed to call this function on BaseNode, which is an abstract class." ) ;
@@ -254,21 +280,20 @@ function shadergen(p5, fn) {
254
280
}
255
281
}
256
282
257
-
258
283
class VectorNode extends BaseNode {
259
284
constructor ( values , type , isInternal = false ) {
260
285
super ( isInternal , type ) ;
261
- this . components = [ 'x' , 'y' , 'z' , 'w' ] . slice ( 0 , values . length ) ;
262
- this . components . forEach ( ( component , i ) => {
286
+ this . componentNames = [ 'x' , 'y' , 'z' , 'w' ] . slice ( 0 , values . length ) ;
287
+ this . componentNames . forEach ( ( component , i ) => {
263
288
this [ component ] = new FloatNode ( values [ i ] , true ) ;
264
289
} ) ;
265
290
}
266
291
267
292
toGLSL ( context ) {
268
293
let glslArgs = `` ;
269
294
270
- this . components . forEach ( ( component , i ) => {
271
- const comma = i === this . components . length - 1 ? `` : `, ` ;
295
+ this . componentNames . forEach ( ( component , i ) => {
296
+ const comma = i === this . componentNames . length - 1 ? `` : `, ` ;
272
297
glslArgs += `${ this [ component ] . toGLSLBase ( context ) } ${ comma } ` ;
273
298
} )
274
299
@@ -304,11 +329,10 @@ function shadergen(p5, fn) {
304
329
// Variables and member variable nodes
305
330
class VariableNode extends BaseNode {
306
331
constructor ( name , type , isInternal = false ) {
307
- super ( isInternal , type )
332
+ super ( isInternal , type ) ;
308
333
this . name = name ;
309
- this . autoAddVectorComponents ( ) ;
334
+ this . addVectorComponents ( ) ;
310
335
}
311
-
312
336
toGLSL ( context ) {
313
337
return `${ this . name } ` ;
314
338
}
@@ -318,13 +342,13 @@ function shadergen(p5, fn) {
318
342
constructor ( parent , componentName , type , isInternal = false ) {
319
343
super ( isInternal , type ) ;
320
344
this . parent = parent ;
321
- this . component = componentName ;
322
- this . type = 'float' ;
345
+ this . componentName = componentName ;
346
+ this . type = type ;
323
347
}
324
348
toGLSL ( context ) {
325
- // const parentName = this.parent.toGLSLBase(context);
326
- const parentName = this . parent . temporaryVariable ? this . parent . temporaryVariable : this . parent . name ;
327
- return `${ parentName } .${ this . component } ` ;
349
+ const parentName = this . parent . toGLSLBase ( context ) ;
350
+ // const parentName = this.parent.temporaryVariable ? this.parent.temporaryVariable : this.parent.name;
351
+ return `${ parentName } .${ this . componentName } ` ;
328
352
}
329
353
}
330
354
@@ -339,6 +363,7 @@ function shadergen(p5, fn) {
339
363
operand . usedIn . push ( this ) ;
340
364
}
341
365
this . type = this . determineType ( ) ;
366
+ this . addVectorComponents ( ) ;
342
367
}
343
368
344
369
// We know that both this.a and this.b are nodes because of BaseNode.enforceType
@@ -476,8 +501,9 @@ function shadergen(p5, fn) {
476
501
this . resetGLSLContext ( ) ;
477
502
}
478
503
479
- hookCallbacks ( ) {
504
+ generate ( ) {
480
505
this . modifyFunction ( ) ;
506
+ console . log ( this . output ) ;
481
507
return this . output ;
482
508
}
483
509
@@ -504,8 +530,6 @@ function shadergen(p5, fn) {
504
530
argsArray . push ( `, ${ parameter . type . typeName } ${ parameter . name } ` ) ;
505
531
}
506
532
} )
507
- console . log ( hookArgs )
508
-
509
533
const toGLSLResult = userOverride ( ...hookArgs ) . toGLSLBase ( this . context ) ;
510
534
let codeLines = [ `(${ argsArray . join ( ', ' ) } ) {` , this . context . declarations . slice ( ) ] . flat ( ) ;
511
535
codeLines . push ( `\n${ hookTypes . returnType . typeName } finalReturnValue = ${ toGLSLResult } ;
@@ -565,32 +589,45 @@ function shadergen(p5, fn) {
565
589
vec4 : 'Vector4' ,
566
590
sampler2D : 'Texture' ,
567
591
} ;
592
+
568
593
const nodeConstructors = {
569
594
int : ( value ) => new IntNode ( value ) ,
570
595
float : ( value ) => new FloatNode ( value ) ,
571
596
vec2 : ( value ) => new VectorNode ( value , 'vec2' ) ,
572
597
vec3 : ( value ) => new VectorNode ( value , 'vec3' ) ,
573
598
vec4 : ( value ) => new VectorNode ( value , 'vec4' ) ,
574
599
} ;
600
+
575
601
for ( const glslType in GLSLTypesToSuffixes ) {
576
602
// Generate uniform*() Methods for creating uniforms
577
603
const typeIdentifier = GLSLTypesToSuffixes [ glslType ] ;
578
604
const uniformMethodName = `uniform${ typeIdentifier } ` ;
605
+
579
606
ShaderGenerator . prototype [ uniformMethodName ] = function ( ...args ) {
580
607
let [ name , ...defaultValue ] = args ;
581
608
if ( glslType . startsWith ( 'vec' ) ) {
582
609
defaultValue = conformVectorParameters ( defaultValue , + glslType . slice ( 3 ) ) ;
610
+ this . output . uniforms [ `${ glslType } ${ name } ` ] = defaultValue ;
611
+ }
612
+ else {
613
+ console . log ( "defaultValue: " , defaultValue ) ;
614
+ console . log ( "defaultValue[0]: " , defaultValue [ 0 ] )
615
+ this . output . uniforms [ `${ glslType } ${ name } ` ] = defaultValue [ 0 ] ;
583
616
}
584
- this . output . uniforms [ ` ${ glslType } ${ name } ` ] = defaultValue ;
617
+
585
618
let safeType = glslType === 'sampler2D' ? 'vec4' : glslType ;
586
- return new VariableNode ( name , safeType ) ;
619
+ const uniform = new VariableNode ( name , safeType , false ) ;
620
+ return uniform ;
587
621
} ;
622
+
588
623
fn [ uniformMethodName ] = function ( ...args ) {
589
624
return GLOBAL_SHADER [ uniformMethodName ] ( ...args ) ;
590
625
} ;
591
626
592
- // Generate the create*() Methods for creating variables in shaders
627
+ // We don't need a createTexture method.
593
628
if ( glslType === 'sampler2D' ) { continue ; }
629
+
630
+ // Generate the create*() Methods for creating variables in shaders
594
631
const createMethodName = `create${ typeIdentifier } ` ;
595
632
fn [ createMethodName ] = function ( ...value ) {
596
633
if ( glslType . startsWith ( 'vec' ) ) {
0 commit comments