Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p5.js Shader generation API #7622

Merged
merged 62 commits into from
Mar 22, 2025
Merged
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
75f252a
local dev server vite path changed
lukeplowden Feb 13, 2025
80b5351
import system working
lukeplowden Feb 13, 2025
76eef1b
Most of the code from original file ported here
lukeplowden Feb 13, 2025
4ba46ee
example sketch
lukeplowden Feb 14, 2025
253f6fe
reduce vector2,3, and 4 classes to a single vector class to avoid rep…
lukeplowden Feb 14, 2025
f513b05
ComponentNode.toGLSL() currently broken. improving vector API.
lukeplowden Feb 17, 2025
9ef9cc7
updating sketch
lukeplowden Feb 17, 2025
59b443e
put temporary fix in comment
lukeplowden Feb 17, 2025
807d26c
Merge branch 'shadergen' of https://github.com/lukeplowden/p5.js into…
lukeplowden Feb 17, 2025
17fb0a6
adding proxy for vectors to make swizzling simpler
lukeplowden Feb 17, 2025
a1b51b0
add estraverse
lukeplowden Feb 18, 2025
fb9fd94
add AST modules
lukeplowden Feb 18, 2025
e8435ce
remove walk
lukeplowden Feb 18, 2025
aaa8aac
refactor binary operator nodes to remove redundant classes and repiti…
lukeplowden Feb 18, 2025
057dc0e
add type specific uniforms
lukeplowden Feb 18, 2025
2322079
swap parameter's order in processOperand
lukeplowden Feb 18, 2025
95d9da3
remove comment
lukeplowden Feb 18, 2025
d212480
change temporary variable logic (testing)
lukeplowden Feb 18, 2025
1b75608
Start working on Proxy for swizzles
lukeplowden Feb 18, 2025
9813cb5
Whitespace
lukeplowden Feb 18, 2025
9ca999e
add escodegen, remove estree-walker
lukeplowden Feb 20, 2025
2d79ac1
first AST conversion (binary operators partly working)
lukeplowden Feb 20, 2025
17af912
slight refactoras will need a more general addComponent function
lukeplowden Feb 28, 2025
297d806
add some more built in functions (see TODO, unfinished)
lukeplowden Feb 28, 2025
1593916
comment for clarity
lukeplowden Feb 28, 2025
c4f3e4e
adding Conditionals (should cover ternaries and if statements)
lukeplowden Feb 28, 2025
168a50b
whitespace
lukeplowden Feb 28, 2025
50e3c58
Merge branch 'processing:dev-2.0' into dev-2.0
lukeplowden Mar 2, 2025
cdd6a84
Merge branch 'dev-2.0' of https://github.com/lukeplowden/p5.js into s…
lukeplowden Mar 2, 2025
1f0a50f
comment proxy for vectors until further work can be done on it
lukeplowden Mar 2, 2025
62107b7
switch to simple walk
lukeplowden Mar 2, 2025
620d4fb
Merge branch 'processing:dev-2.0' into dev-2.0
lukeplowden Mar 2, 2025
14c4c3d
Merge branch 'dev-2.0' of https://github.com/lukeplowden/p5.js into s…
lukeplowden Mar 2, 2025
41068cb
getting the options object populated properly. Create uniform functio…
lukeplowden Mar 5, 2025
6c582b9
build shader hooks from existing API
lukeplowden Mar 5, 2025
7bae495
added automatic uniform names
lukeplowden Mar 5, 2025
d609240
fixing component assignments for binary operators and variables
lukeplowden Mar 11, 2025
b473ac8
got the swizzle assignments working
lukeplowden Mar 11, 2025
a0199d0
dev sketch
lukeplowden Mar 11, 2025
79602a7
Console log the whole modify argument
lukeplowden Mar 11, 2025
0797a1b
refactor / add qualifiers to hook arguments (i.e. in sampler2D etc). …
lukeplowden Mar 11, 2025
98a78a7
adding types to builtin functions
lukeplowden Mar 12, 2025
b8072f9
sketch example
lukeplowden Mar 12, 2025
b751657
Merge branch 'shadergen' of https://github.com/lukeplowden/p5.js into…
lukeplowden Mar 12, 2025
926f79f
allow literals on lhs before transpile, dont transpile uniform defaul…
lukeplowden Mar 13, 2025
01ef185
example sketch
lukeplowden Mar 13, 2025
9cac072
input structs now working
lukeplowden Mar 13, 2025
951ee60
input structs now working
lukeplowden Mar 13, 2025
c485a10
Merge branch 'shadergen' of https://github.com/lukeplowden/p5.js into…
lukeplowden Mar 13, 2025
a6101c7
fix merge issues
lukeplowden Mar 13, 2025
435a718
add builtin GLSL functions & change file name
lukeplowden Mar 13, 2025
9932797
sketch example
lukeplowden Mar 13, 2025
a41f773
remove console log
lukeplowden Mar 14, 2025
2655de2
infer types from function calls
lukeplowden Mar 14, 2025
3d9156b
filter shader example
lukeplowden Mar 14, 2025
9b78c4b
set default uniforms on filters outside of WebGL mode
lukeplowden Mar 14, 2025
91ea2ac
fix user fill shaders overriding filters
lukeplowden Mar 14, 2025
9c4aef8
Change FES behaviour while executing a shader generator
davepagurek Mar 14, 2025
65ea279
Merge branch 'shadergen' of https://github.com/lukeplowden/p5.js into…
lukeplowden Mar 20, 2025
d4f6cef
add toFloat() for ease of use with instanceID()
lukeplowden Mar 20, 2025
a5e76dd
remove uvCoords function as vTexCoord is not always defined in a give…
lukeplowden Mar 20, 2025
3dc08f6
working on tutorial sketch
lukeplowden Mar 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
"acorn": "^8.12.1",
"acorn-walk": "^8.3.4",
"colorjs.io": "^0.5.2",
"escodegen": "^2.1.0",
"file-saver": "^1.3.8",
"gifenc": "^1.0.3",
"i18next": "^19.0.2",
4 changes: 2 additions & 2 deletions preview/global/index.html
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@
}
</style>

<script src="./p5.js"></script>
<script src="/p5.js"></script>
</head>
<body>
<script src="./sketch.js"></script>
<script src="/sketch.js"></script>
</body>
</html>
91 changes: 43 additions & 48 deletions preview/global/sketch.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,50 @@
const vertSrc = `#version 300 es
precision mediump float;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;

in vec3 aPosition;
in vec2 aOffset;

void main(){
vec4 positionVec4 = vec4(aPosition.xyz, 1.0);
positionVec4.xy += aOffset;
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
}
`;

const fragSrc = `#version 300 es
precision mediump float;
out vec4 outColor;
void main(){
outColor = vec4(0.0, 1.0, 1.0, 1.0);
}
`;

let myShader;
function setup(){
createCanvas(100, 100, WEBGL);
p5.disableFriendlyErrors = true;
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}

// Create and use the custom shader.
myShader = createShader(vertSrc, fragSrc);
let myModel;
let starShader;
let starStrokeShader;
let stars;

function starShaderCallback() {
const time = uniformFloat(() => millis());
getWorldInputs((inputs) => {
inputs.position.y += instanceID() * 20 - 1000;
inputs.position.x += 40*sin(time * 0.001 + instanceID());
return inputs;
});
getObjectInputs((inputs) => {
inputs.position *= sin(time*0.001 + instanceID());
return inputs;
})
}

describe('A wobbly, cyan circle on a gray background.');
async function setup(){
createCanvas(windowWidth, windowHeight, WEBGL);
stars = buildGeometry(() => sphere(20, 7, 4))
starShader = baseMaterialShader().modify(starShaderCallback);
starStrokeShader = baseStrokeShader().modify(starShaderCallback)
}

function draw(){
// Set the styles
background(125);
background(0,200,240);
orbitControl();
// noStroke();

push();
stroke(255,0,255)
fill(255,200,255)
strokeShader(starStrokeShader)
shader(starShader);
model(stars, 100);
pop();
push();
shader(baseMaterialShader());
noStroke();
shader(myShader);

// Draw the circle.
beginShape();
for (let i = 0; i < 30; i++){
const x = 40 * cos(i/30 * TWO_PI);
const y = 40 * sin(i/30 * TWO_PI);

// Apply some noise to the coordinates.
const xOff = 10 * noise(x + millis()/1000) - 5;
const yOff = 10 * noise(y + millis()/1000) - 5;

// Apply these noise values to the following vertex.
vertexProperty('aOffset', [xOff, yOff]);
vertex(x, y);
}
endShape(CLOSE);
rotateX(HALF_PI);
translate(0, 0, -250);
plane(10000)
pop();
}
2 changes: 1 addition & 1 deletion preview/global/vite.config.mjs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ export default defineConfig({
name: 'reload',
configureServer(server) {
const { ws, watcher } = server;
const buildLibPath = path.resolve(libPath, './p5.rollup.js');
const buildLibPath = path.resolve(libPath, './p5.js');
watcher.add(buildLibPath);
watcher.on('change', file => {
if(file === buildLibPath){
3 changes: 2 additions & 1 deletion src/image/filterRenderer2D.js
Original file line number Diff line number Diff line change
@@ -234,7 +234,8 @@ class FilterRenderer2D {
this._shader.setUniform('canvasSize', [this.pInst.width, this.pInst.height]);
this._shader.setUniform('radius', Math.max(1, this.filterParameter));
this._shader.setUniform('filterParameter', this.filterParameter);

this._shader.setDefaultUniforms();

this.pInst.states.setValue('rectMode', constants.CORNER);
this.pInst.states.setValue('imageMode', constants.CORNER);
this.pInst.blendMode(constants.BLEND);
Loading