@@ -18,15 +18,17 @@ function shadergen(p5, fn) {
18
18
if ( modifier instanceof Function ) {
19
19
const code = modifier . toString ( )
20
20
const ast = parse ( code , { ecmaVersion : 2021 /*, locations: true*/ } ) ;
21
-
22
21
simple ( ast , ASTCallbacks ) ;
23
-
24
22
const transpiledArg = escodegen . generate ( ast ) ;
25
- console . log ( transpiledArg )
26
-
27
- // const program = new ShaderProgram(modifier)
28
- // const newArg = program.generate();
29
- // console.log(newArg.vertex)
23
+ const transpiledFn = new Function ( transpiledArg
24
+ . slice ( transpiledArg . indexOf ( "{" ) + 1 , transpiledArg . lastIndexOf ( "}" ) )
25
+ ) ;
26
+
27
+ const generator = new ShaderGenerator ( transpiledFn , this )
28
+ const newArg = generator . callModifyFunction ( ) ;
29
+ console . log ( code ) ;
30
+ console . log ( transpiledArg ) ;
31
+ console . log ( newArg )
30
32
// return oldModify.call(this, newArg);
31
33
}
32
34
else {
@@ -111,6 +113,7 @@ function shadergen(p5, fn) {
111
113
throw new Error ( "StackCapture" ) ;
112
114
} catch ( e ) {
113
115
const lines = e . stack . split ( "\n" ) ;
116
+ // console.log(lines);
114
117
let index = 5 ;
115
118
if ( isBinaryOperatorNode ( this ) ) { index -- ; } ;
116
119
this . srcLine = lines [ index ] . trim ( ) ;
@@ -527,24 +530,18 @@ function shadergen(p5, fn) {
527
530
// Shader program
528
531
// This class is responsible for converting the nodes into an object containing GLSL code, to be used by p5.Shader.modify
529
532
530
- class ShaderProgram {
531
- constructor ( modifyFunction ) {
532
- this . uniforms = {
533
- int : { } ,
534
- float : { } ,
535
- vec2 : { } ,
536
- vec3 : { } ,
537
- vec4 : { } ,
538
- texture : { } ,
539
- }
540
- this . functions = {
541
- }
542
- this . resetGLSLContext ( ) ;
533
+ class ShaderGenerator {
534
+ constructor ( modifyFunction , shaderToModify ) {
535
+ this . modifyFunction = modifyFunction ;
536
+ this . shaderToModify = shaderToModify ;
537
+ shaderToModify . inspectHooks ( ) ;
543
538
GLOBAL_SHADER = this ;
544
- this . generator = modifyFunction ;
539
+ this . uniforms = { } ;
540
+ this . functions = { } ;
541
+ this . resetGLSLContext ( ) ;
545
542
}
546
- generate ( ) {
547
- this . generator ( ) ;
543
+ callModifyFunction ( ) {
544
+ this . modifyFunction ( ) ;
548
545
return {
549
546
uniforms : this . uniforms ,
550
547
functions : this . functions ,
@@ -560,35 +557,6 @@ function shadergen(p5, fn) {
560
557
}
561
558
}
562
559
// TODO:
563
- uniformInt ( name , defaultValue ) {
564
- this . uniforms . int [ name ] = defaultValue ;
565
- return new VariableNode ( name , 'int' ) ;
566
- }
567
- uniformFloat ( name , defaultValue ) {
568
- this . uniforms . float [ name ] = defaultValue ;
569
- return new VariableNode ( name , 'float' ) ;
570
- }
571
- uniformVector2 ( name , defaultValue ) {
572
- this . uniforms . vec2 [ name ] = defaultValue ;
573
- return new VariableNode ( name , 'vec2' ) ;
574
- }
575
- uniformVector2 ( name , defaultValue ) {
576
- this . uniforms . vec3 [ name ] = defaultValue ;
577
- return new VariableNode ( name , 'vec3' ) ;
578
- }
579
- uniformVector2 ( name , defaultValue ) {
580
- this . uniforms . vec4 [ name ] = defaultValue ;
581
- return new VariableNode ( name , 'vec4' ) ;
582
- }
583
- uniformTexture ( name , defaultValue ) {
584
- this . uniforms . texture [ name ] = defaultValue ;
585
- return new VariableNode ( name , 'vec4' ) ;
586
- }
587
- uniform ( name , defaultValue ) {
588
- this . uniforms [ name ] = defaultValue ;
589
- return new VariableNode ( name , defaultValue . type ) ;
590
- }
591
-
592
560
buildFunction ( argumentName , argumentType , callback ) {
593
561
let functionArgument = new VariableNode ( argumentName , argumentType , true ) ;
594
562
const finalLine = callback ( functionArgument ) . toGLSLBase ( this . context ) ;
@@ -649,9 +617,24 @@ function shadergen(p5, fn) {
649
617
return new VariableNode ( 'discard' , 'keyword' ) ;
650
618
}
651
619
652
- fn . uniform = function ( name , value ) {
653
- let result = GLOBAL_SHADER . uniform ( name , value )
654
- return result ;
620
+ // Uniforms and attributes
621
+ const uniformFns = {
622
+ 'int' : 'Int' ,
623
+ 'float' : 'Float' ,
624
+ 'vec2' : 'Vector2' ,
625
+ 'vec3' : 'Vector3' ,
626
+ 'vec4' : 'Vector4' ,
627
+ 'sampler2D' : 'Texture' ,
628
+ } ;
629
+ for ( const type in uniformFns ) {
630
+ const uniformFnVariant = `uniform${ uniformFns [ type ] } ` ;
631
+ ShaderGenerator . prototype [ uniformFnVariant ] = function ( name , defaultValue ) {
632
+ this . uniforms [ `${ type } ${ name } ` ] = defaultValue ;
633
+ return new VariableNode ( name , type ) ;
634
+ } ;
635
+ fn [ uniformFnVariant ] = function ( name , value ) {
636
+ return GLOBAL_SHADER [ uniformFnVariant ] ( name , value ) ;
637
+ } ;
655
638
}
656
639
657
640
function getWorldPosition ( func ) {
0 commit comments