Skip to content

Commit 41068cb

Browse files
committed
getting the options object populated properly. Create uniform functions programatically
1 parent 14c4c3d commit 41068cb

File tree

2 files changed

+63
-80
lines changed

2 files changed

+63
-80
lines changed

preview/global/sketch.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,43 @@ function setup(){
99
createCanvas(windowWidth, windowHeight, WEBGL);
1010

1111
// Raw example
12-
myShader = baseMaterialShader().modify(() => {
13-
const offset = calculateOffset();
12+
// myShader = baseMaterialShader().modify(() => {
13+
// const offset = calculateOffset();
1414

15-
getWorldPosition((pos) => {
16-
let a = createVector3(1, 2, 3);
17-
let b = createVector3(3, 4, 5);
15+
// getWorldPosition((pos) => {
16+
// let a = createVector3(1, 2, 3);
17+
// let b = createVector3(3, 4, 5);
1818

19-
a = (a * b + offset) / 10;
19+
// a = (a * b + offset) / 10;
2020

21-
pos += a;
21+
// pos += a;
2222

23-
return pos;
24-
});
25-
});
23+
// return pos;
24+
// });
25+
// });
2626

2727

2828
// Create and use the custom shader.
29-
// myShader = baseMaterialShader().modify(
30-
// () => {
31-
// const offset = uniform('offset', () => calculateOffset)
29+
myShader = baseMaterialShader().modify(
30+
() => {
31+
const offset = uniformFloat('offset', () => calculateOffset)
3232

33-
// getWorldPosition((pos) => {
34-
// let a = createVector3(1, 2, 3);
35-
// let b = createVector3(3, 4, 5);
36-
// a = a.add(b);
33+
getWorldPosition((pos) => {
34+
let a = createVector3(1, 2, 3);
35+
let b = createVector3(3, 4, 5);
36+
a = a.add(b);
3737

38-
// let c = a.add(b);
39-
// c = c.add(offset);
40-
// c.x = b.x.add(1);
38+
let c = a.add(b);
39+
c += c.add(offset);
40+
c.x = b.x.add(1);
4141

4242

43-
// pos = pos.add(c);
43+
pos = pos.add(c);
4444

45-
// return pos;
46-
// })
47-
// }
48-
// );
45+
return pos;
46+
})
47+
}
48+
);
4949
}
5050

5151
function draw(){

src/webgl/ShaderGen.js

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ function shadergen(p5, fn) {
1818
if (modifier instanceof Function) {
1919
const code = modifier.toString()
2020
const ast = parse(code, { ecmaVersion: 2021 /*, locations: true*/ });
21-
2221
simple(ast, ASTCallbacks);
23-
2422
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)
3032
// return oldModify.call(this, newArg);
3133
}
3234
else {
@@ -111,6 +113,7 @@ function shadergen(p5, fn) {
111113
throw new Error("StackCapture");
112114
} catch (e) {
113115
const lines = e.stack.split("\n");
116+
// console.log(lines);
114117
let index = 5;
115118
if (isBinaryOperatorNode(this)) { index--; };
116119
this.srcLine = lines[index].trim();
@@ -527,24 +530,18 @@ function shadergen(p5, fn) {
527530
// Shader program
528531
// This class is responsible for converting the nodes into an object containing GLSL code, to be used by p5.Shader.modify
529532

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();
543538
GLOBAL_SHADER = this;
544-
this.generator = modifyFunction;
539+
this.uniforms = {};
540+
this.functions = {};
541+
this.resetGLSLContext();
545542
}
546-
generate() {
547-
this.generator();
543+
callModifyFunction() {
544+
this.modifyFunction();
548545
return {
549546
uniforms: this.uniforms,
550547
functions: this.functions,
@@ -560,35 +557,6 @@ function shadergen(p5, fn) {
560557
}
561558
}
562559
// 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-
592560
buildFunction(argumentName, argumentType, callback) {
593561
let functionArgument = new VariableNode(argumentName, argumentType, true);
594562
const finalLine = callback(functionArgument).toGLSLBase(this.context);
@@ -649,9 +617,24 @@ function shadergen(p5, fn) {
649617
return new VariableNode('discard', 'keyword');
650618
}
651619

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+
};
655638
}
656639

657640
function getWorldPosition(func){

0 commit comments

Comments
 (0)