6
6
*/
7
7
8
8
import { parse } from 'acorn' ;
9
- import * as walk from 'acorn-walk' ;
9
+ import { simple } from 'acorn-walk' ;
10
10
import escodegen from 'escodegen' ;
11
11
12
12
function shadergen ( p5 , fn ) {
13
13
let GLOBAL_SHADER ;
14
14
15
15
const oldModify = p5 . Shader . prototype . modify
16
16
17
- p5 . Shader . prototype . modify = function ( arg ) {
18
-
19
- if ( arg instanceof Function ) {
20
- const code = arg . toString ( )
21
- const ast = parse ( code , { ecmaVersion : 2021 , locations : true } ) ;
17
+ p5 . Shader . prototype . modify = function ( modifier ) {
18
+ if ( modifier instanceof Function ) {
19
+ const code = modifier . toString ( )
20
+ const ast = parse ( code , { ecmaVersion : 2021 /*, locations: true*/ } ) ;
22
21
23
- walk . ancestor ( ast , ASTCallbacks , null , { myData : 123 } ) ;
22
+ simple ( ast , ASTCallbacks ) ;
24
23
25
- const transformed = escodegen . generate ( ast ) ;
26
- console . log ( transformed )
24
+ const transpiledArg = escodegen . generate ( ast ) ;
25
+ console . log ( transpiledArg )
27
26
28
- // const program = new ShaderProgram(arg )
27
+ // const program = new ShaderProgram(modifier )
29
28
// const newArg = program.generate();
30
29
// console.log(newArg.vertex)
31
30
// return oldModify.call(this, newArg);
32
31
}
33
32
else {
34
- return oldModify . call ( this , arg )
33
+ return oldModify . call ( this , modifier )
35
34
}
36
35
}
37
36
@@ -48,11 +47,14 @@ function shadergen(p5, fn) {
48
47
}
49
48
50
49
const ASTCallbacks = {
51
- Literal ( node , state , ancestors ) {
50
+ // TODO: automatically making uniforms
51
+ Literal ( node ) {
52
52
} ,
53
- AssignmentExpression ( node , _state , ancestors ) {
54
- // Operator overloading
55
- if ( node . operator != '=' ) {
53
+
54
+ // The callbacks for AssignmentExpression and BinaryExpression handle
55
+ // operator overloading including +=, *= assignment expressions
56
+ AssignmentExpression ( node ) {
57
+ if ( node . operator !== '=' ) {
56
58
const rightReplacementNode = {
57
59
type : 'CallExpression' ,
58
60
callee : {
@@ -68,31 +70,12 @@ function shadergen(p5, fn) {
68
70
} ,
69
71
arguments : [ node . right ]
70
72
}
71
-
72
73
node . operator = '=' ;
73
74
node . right = rightReplacementNode ;
74
75
}
75
76
} ,
76
- BinaryExpression ( node , state , ancestors ) {
77
- // let i = ancestors.length - 1;
78
- // let ancestor = ancestors[i]; // ancestor === node
79
- // while (ancestor.type === 'BinaryExpression') {
80
- // ancestor = ancestors[i--];
81
- // }
82
-
83
- console . log ( "\n NEW NODE:" )
84
-
85
- const transformed = escodegen . generate ( node ) ;
86
- const l = escodegen . generate ( node . left ) ;
87
- const r = escodegen . generate ( node . right ) ;
88
- console . log ( "Transformed: " , transformed ) ;
89
- console . log ( "Left: " , l ) ;
90
- console . log ( "Right: " , r ) ;
91
-
92
- console . log ( node . left ) ;
93
-
77
+ BinaryExpression ( node ) {
94
78
node . type = 'CallExpression' ;
95
- console . log ( "OPERATOR: " , node . operator )
96
79
node . callee = {
97
80
type : "MemberExpression" ,
98
81
object : node . left ,
@@ -102,12 +85,13 @@ function shadergen(p5, fn) {
102
85
} ,
103
86
} ;
104
87
node . arguments = [ node . right ] ;
105
-
106
88
} ,
107
89
}
108
90
109
91
110
- // JS API
92
+ // Javascript Node API.
93
+ // These classes are for expressing GLSL functions in Javascript with
94
+ // needing to transpile the user's code.
111
95
112
96
class BaseNode {
113
97
constructor ( isInternal ) {
@@ -121,7 +105,7 @@ function shadergen(p5, fn) {
121
105
this . usedIn = [ ] ;
122
106
this . dependsOn = [ ] ;
123
107
this . srcLine = null ;
124
-
108
+ // Stack Capture is used to get the original line of user code for Debug purposes
125
109
if ( isInternal === false ) {
126
110
try {
127
111
throw new Error ( "StackCapture" ) ;
0 commit comments