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

Bug fixes for Shader API #7675

Draft
wants to merge 50 commits into
base: dev-2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
eed0a30
example sketch
lukeplowden Mar 25, 2025
9db6d15
example sketch
lukeplowden Mar 25, 2025
4b7399e
Merge branch 'dev-2.0' of github.com:processing/p5.js into shadergen.
lukeplowden Mar 26, 2025
e9b361e
ensure property nodes are nodes when generating glsl
lukeplowden Mar 26, 2025
fd707a5
Couple of bug fixes
lukeplowden Mar 26, 2025
a75f9b2
remove console debug logs
lukeplowden Mar 26, 2025
1938df2
fix bug, now possible vector uniform defaults to be defined by functions
lukeplowden Mar 26, 2025
ca66bf9
example sketch final
lukeplowden Mar 26, 2025
a462487
Allow for swizzling on arrays
lukeplowden Mar 27, 2025
cd94ec6
Fixed some problems with temporary variables. work in progress
lukeplowden Mar 27, 2025
4cc5956
clarify helper functions for types/ nodes, more accurate naming
lukeplowden Mar 27, 2025
7daa0bc
use camera space instead of sending camera position as a uniform
lukeplowden Mar 27, 2025
744d4a1
update function call node to prioritize vectors as gentypes, as some …
lukeplowden Mar 27, 2025
a66ff2c
fix some issues with components and using temp vars before theyre dec…
lukeplowden Mar 27, 2025
6cad8d0
update temp var scoring system
lukeplowden Mar 27, 2025
decdc05
fix some issues with constructing vectors. allow vector construction …
lukeplowden Mar 27, 2025
ea6b414
differentiate betwen VariableNode and nodes with a temporary variable
lukeplowden Mar 27, 2025
362cdd1
code formatting
lukeplowden Mar 27, 2025
8aafa73
added function overloads. working but will need extending in the future
lukeplowden Mar 27, 2025
4f3acf8
function overloads are working, including parameter validation. Can b…
lukeplowden Mar 28, 2025
ec165ab
convert float -> vec
lukeplowden Mar 28, 2025
c0729eb
add support for unary operators
lukeplowden Mar 28, 2025
fd152a1
simplify unary overload
lukeplowden Mar 28, 2025
e1e437c
utilise new unary operator
lukeplowden Mar 28, 2025
40fd5a3
add in/ out variables
lukeplowden Mar 28, 2025
9dfd454
use varying in sketch
lukeplowden Mar 28, 2025
c3eebca
swizzles semi working
lukeplowden Mar 28, 2025
0aafb75
testing swizzles
lukeplowden Mar 28, 2025
2f8adb1
swizzles work to reconstruct kinda
lukeplowden Mar 28, 2025
f49f6aa
testing swizzles
lukeplowden Mar 28, 2025
7144dcd
Vector swizzling supported
lukeplowden Mar 29, 2025
687f788
fixed name clashes, and unary node swizzling bugs
lukeplowden Mar 29, 2025
cb96271
swizzling for lh values added. fixed bug with unary swizzles
lukeplowden Mar 29, 2025
57a11b4
code gen changes, remove extra semicolons, add state obj
lukeplowden Mar 31, 2025
0b22cb6
IF statements added to js node API
lukeplowden Mar 31, 2025
75318ce
testing conditionals
lukeplowden Mar 31, 2025
ad6c0df
helper func for condition dependencies
lukeplowden Mar 31, 2025
22ea418
put updateComponents into context
lukeplowden Apr 1, 2025
e54fa0f
update components for vector nodes properly
lukeplowden Apr 1, 2025
e512e2e
more dependency fixes
lukeplowden Apr 1, 2025
ecbf061
fix updatecomponents
lukeplowden Apr 1, 2025
4863a4d
make copy of components which have actually changed before updating
lukeplowden Apr 1, 2025
9529a80
properly generate float components in conditionals
lukeplowden Apr 1, 2025
fa21c5a
vec components added after proxy creation for correct parenting
lukeplowden Apr 2, 2025
94dc159
temp
lukeplowden Apr 2, 2025
b89c506
ifStatement dependents fixed
lukeplowden Apr 2, 2025
6adfbe0
dependsOn prototype simplified
lukeplowden Apr 4, 2025
1985d1d
Merge branch 'processing:dev-2.0' into dev-2.0
lukeplowden Apr 4, 2025
fa277e5
fix typo in documentation for vertexProperty
lukeplowden Apr 4, 2025
b666366
Merge branch 'dev-2.0' of https://github.com/lukeplowden/p5.js into d…
lukeplowden Apr 4, 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
148 changes: 92 additions & 56 deletions preview/global/sketch.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,130 @@
p5.disableFriendlyErrors = true;

function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}

let myModel;
let starShader;
let starStrokeShader;
let stars;
let ditheringShader;
let originalFrameBuffer;
let blurredFrameBuffer;
let pixellizeShader;
let fresnelShader;
let bloomShader;


function fresnelShaderCallback() {
const fresnelPower = uniformFloat(2);
const fresnelBias = uniformFloat(-0.1);
const fresnelScale = uniformFloat(2);
getCameraInputs((inputs) => {
let myValue = [1,0,0];
let n = normalize(inputs.normal);
If(n == n, () => assign(myValue, 1));
let v = normalize(-inputs.position);
let base = 1.0 - dot(n, v);
let fresnel = fresnelScale * pow(base, fresnelPower) + fresnelBias;


let col = mix(myValue, [1, .5, .7], fresnel);

inputs.color = [col, 1];
return inputs;
});
}

function starShaderCallback() {
const time = uniformFloat(() => millis());
const skyRadius = uniformFloat(1000);

function rand2(st) {
return fract(sin(dot(st, [12.9898, 78.233])) * 43758.5453123);
}

function semiSphere() {
let id = instanceID();
let theta = rand2([id, 0.1234]) * TWO_PI;
let phi = rand2([id, 3.321]) * PI+time/10000;
let r = skyRadius;
r *= 1.5 * sin(phi);
let x = r * sin(phi) * cos(theta);
let y = r *1.5* cos(phi);
let z = r * sin(phi) * sin(theta);
return [x, y, z];
}

getWorldInputs((inputs) => {
inputs.position.y += instanceID() * 20 - 1000;
inputs.position.x += 40 * sin(time * 0.001 + instanceID());
inputs.position += semiSphere();
return inputs;
});

getObjectInputs((inputs) => {
inputs.position *= sin(time*0.001 + instanceID());
let scale = 1 + 0.1 * sin(time * 0.002 + instanceID());
inputs.position *= scale;
return inputs;
})
});
}

function ditheringCallback() {
const time = uniformFloat(() => millis())

function rand(co) {
return fract(sin(dot(co, [12.9898, 78.233])) * 43758.5453);
}

function grayscale(col) {
return dot([col.x, col.y, col.z], [0.21, 0.72, 0.07])
}

function pixellizeShaderCallback() {
const pixelSize = uniformFloat(()=> width/2);
getColor((input, canvasContent) => {
let col = texture(canvasContent, input.texCoord);
col.z = 0.55;
col += rand(input.texCoord + time/10000000000) * 0.15 - 0.05;
let greyscaleValue = grayscale(col);
col.x = greyscaleValue
col.y = greyscaleValue
let coord = input.texCoord;
coord = floor(coord * pixelSize) / pixelSize;
let col = texture(canvasContent, coord);
return col;
});
}

function bloom() {
const blurred = uniformTexture(() => blurredFrameBuffer);
const original = uniformTexture(() => originalFrameBuffer);

function bloomShaderCallback() {
const preBlur = uniformTexture(() => originalFrameBuffer);
getColor((input, canvasContent) => {
const blurredCol = texture(blurred, input.texCoord);
const originalCol = texture(original, input.texCoord);
const brightPass = max(originalCol - 0.0, 0.0) * 3.0;
// const bloom = original + blurred * brightPass;
// return bloom;
return texture(blurred, input.texCoord) + texture(original, input.texCoord);
const blurredCol = texture(canvasContent, input.texCoord);
const originalCol = texture(preBlur, input.texCoord);
const brightPass = max(originalCol - 0.3, 0.7) * 1.2;
const bloom = originalCol + blurredCol * brightPass;
return bloom;
});
}

async function setup(){
createCanvas(windowWidth, windowHeight, WEBGL);
stars = buildGeometry(() => sphere(20, 3, 3))
starShader = baseMaterialShader().modify(starShaderCallback);
starStrokeShader = baseStrokeShader().modify(starShaderCallback)
ditheringShader = baseFilterShader().modify(ditheringCallback);
stars = buildGeometry(() => sphere(20, 4, 2))
originalFrameBuffer = createFramebuffer();
blurredFrameBuffer = createFramebuffer();
bloomShader = baseFilterShader().modify(bloom);

// starShader = baseMaterialShader().modify(starShaderCallback);
// starStrokeShader = baseStrokeShader().modify(starShaderCallback)
fresnelShader = baseColorShader().modify(fresnelShaderCallback);
// bloomShader = baseFilterShader().modify(bloomShaderCallback);
// pixellizeShader = baseFilterShader().modify(pixellizeShaderCallback);
}

function draw(){
originalFrameBuffer.begin();
orbitControl();
background(0,0,0);
push();
stroke(255,0,255)
fill(255,200,255)
strokeShader(starStrokeShader)
shader(starShader);
model(stars, 100);
pop();
background(0);
orbitControl();

// push()
// strokeWeight(4)
// stroke(255,0,0)
// rotateX(PI/2 + millis() * 0.0005);
// fill(255,100, 150)
// strokeShader(starStrokeShader)
// shader(starShader);
// model(stars, 5000);
// pop()

push()
shader(fresnelShader)
noStroke()
sphere(500);
pop()

// filter(pixellizeShader);
originalFrameBuffer.end();

blurredFrameBuffer.begin();
image(originalFrameBuffer, -windowWidth/2, -windowHeight/2)
filter(BLUR)
blurredFrameBuffer.end();

// image(originalFrameBuffer, -windowWidth/2, -windowHeight/2)
filter(bloomShader);
imageMode(CENTER)
image(originalFrameBuffer, 0, 0)

// filter(BLUR, 20)
// filter(bloomShader);
}
2 changes: 1 addition & 1 deletion src/shape/vertex.js
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ function vertex(p5, fn){
/**
* Sets the shader's vertex property or attribute variables.
*
* An vertex property or vertex attribute is a variable belonging to a vertex in a shader. p5.js provides some
* A vertex property, or vertex attribute, is a variable belonging to a vertex in a shader. p5.js provides some
* default properties, such as `aPosition`, `aNormal`, `aVertexColor`, etc. These are
* set using <a href="#/p5/vertex">vertex()</a>, <a href="#/p5/normal">normal()</a>
* and <a href="#/p5/fill">fill()</a> respectively. Custom properties can also
Expand Down
Loading