Skip to content

Commit 3e2d17a

Browse files
authored
Merge pull request #7675 from lukeplowden/dev-2.0
Bug fixes for Shader API
2 parents 1946523 + 73345ff commit 3e2d17a

File tree

4 files changed

+1077
-306
lines changed

4 files changed

+1077
-306
lines changed

preview/global/sketch.js

+81-51
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,124 @@
11
p5.disableFriendlyErrors = true;
2+
23
function windowResized() {
34
resizeCanvas(windowWidth, windowHeight);
45
}
56

6-
let myModel;
77
let starShader;
88
let starStrokeShader;
99
let stars;
10-
let ditheringShader;
1110
let originalFrameBuffer;
12-
let blurredFrameBuffer;
11+
let pixellizeShader;
12+
let fresnelShader;
13+
let bloomShader;
14+
15+
function fresnelShaderCallback() {
16+
const fresnelPower = uniformFloat(2);
17+
const fresnelBias = uniformFloat(-0.1);
18+
const fresnelScale = uniformFloat(2);
19+
getCameraInputs((inputs) => {
20+
let n = normalize(inputs.normal);
21+
let v = normalize(-inputs.position);
22+
let base = 1.0 - dot(n, v);
23+
let fresnel = fresnelScale * pow(base, fresnelPower) + fresnelBias;
24+
let col = mix([0, 0, 0], [1, .5, .7], fresnel);
25+
inputs.color = [col, 1];
26+
return inputs;
27+
});
28+
}
1329

1430
function starShaderCallback() {
1531
const time = uniformFloat(() => millis());
32+
const skyRadius = uniformFloat(1000);
33+
34+
function rand2(st) {
35+
return fract(sin(dot(st, [12.9898, 78.233])) * 43758.5453123);
36+
}
37+
38+
function semiSphere() {
39+
let id = instanceID();
40+
let theta = rand2([id, 0.1234]) * TWO_PI;
41+
let phi = rand2([id, 3.321]) * PI+time/10000;
42+
let r = skyRadius;
43+
r *= 1.5 * sin(phi);
44+
let x = r * sin(phi) * cos(theta);
45+
let y = r * 1.5 * cos(phi);
46+
let z = r * sin(phi) * sin(theta);
47+
return [x, y, z];
48+
}
49+
1650
getWorldInputs((inputs) => {
17-
inputs.position.y += instanceID() * 20 - 1000;
18-
inputs.position.x += 40 * sin(time * 0.001 + instanceID());
51+
inputs.position += semiSphere();
1952
return inputs;
2053
});
54+
2155
getObjectInputs((inputs) => {
22-
inputs.position *= sin(time*0.001 + instanceID());
56+
let scale = 1 + 0.1 * sin(time * 0.002 + instanceID());
57+
inputs.position *= scale;
2358
return inputs;
24-
})
59+
});
2560
}
2661

27-
function ditheringCallback() {
28-
const time = uniformFloat(() => millis())
29-
30-
function rand(co) {
31-
return fract(sin(dot(co, [12.9898, 78.233])) * 43758.5453);
32-
}
33-
34-
function grayscale(col) {
35-
return dot([col.x, col.y, col.z], [0.21, 0.72, 0.07])
36-
}
37-
62+
function pixellizeShaderCallback() {
63+
const pixelSize = uniformFloat(()=> width*.75);
3864
getColor((input, canvasContent) => {
39-
let col = texture(canvasContent, input.texCoord);
40-
col.z = 0.55;
41-
col += rand(input.texCoord + time/10000000000) * 0.15 - 0.05;
42-
let greyscaleValue = grayscale(col);
43-
col.x = greyscaleValue
44-
col.y = greyscaleValue
65+
let coord = input.texCoord;
66+
coord = floor(coord * pixelSize) / pixelSize;
67+
let col = texture(canvasContent, coord);
4568
return col;
4669
});
4770
}
4871

49-
function bloom() {
50-
const blurred = uniformTexture(() => blurredFrameBuffer);
51-
const original = uniformTexture(() => originalFrameBuffer);
52-
72+
function bloomShaderCallback() {
73+
const preBlur = uniformTexture(() => originalFrameBuffer);
5374
getColor((input, canvasContent) => {
54-
const blurredCol = texture(blurred, input.texCoord);
55-
const originalCol = texture(original, input.texCoord);
56-
const brightPass = max(originalCol - 0.0, 0.0) * 3.0;
57-
// const bloom = original + blurred * brightPass;
58-
// return bloom;
59-
return texture(blurred, input.texCoord) + texture(original, input.texCoord);
75+
const blurredCol = texture(canvasContent, input.texCoord);
76+
const originalCol = texture(preBlur, input.texCoord);
77+
const brightPass = max(originalCol, 0.3) * 1.5;
78+
const bloom = originalCol + blurredCol * brightPass;
79+
return bloom;
6080
});
6181
}
6282

6383
async function setup(){
6484
createCanvas(windowWidth, windowHeight, WEBGL);
65-
stars = buildGeometry(() => sphere(20, 3, 3))
85+
stars = buildGeometry(() => sphere(30, 4, 2))
86+
originalFrameBuffer = createFramebuffer();
87+
6688
starShader = baseMaterialShader().modify(starShaderCallback);
6789
starStrokeShader = baseStrokeShader().modify(starShaderCallback)
68-
ditheringShader = baseFilterShader().modify(ditheringCallback);
69-
originalFrameBuffer = createFramebuffer();
70-
blurredFrameBuffer = createFramebuffer();
71-
bloomShader = baseFilterShader().modify(bloom);
90+
fresnelShader = baseColorShader().modify(fresnelShaderCallback);
91+
bloomShader = baseFilterShader().modify(bloomShaderCallback);
92+
pixellizeShader = baseFilterShader().modify(pixellizeShaderCallback);
7293
}
7394

7495
function draw(){
7596
originalFrameBuffer.begin();
97+
background(0);
7698
orbitControl();
77-
background(0,0,0);
78-
push();
79-
stroke(255,0,255)
80-
fill(255,200,255)
99+
100+
push()
101+
strokeWeight(4)
102+
stroke(255,0,0)
103+
rotateX(PI/2 + millis() * 0.0005);
104+
fill(255,100, 150)
81105
strokeShader(starStrokeShader)
82106
shader(starShader);
83-
model(stars, 100);
84-
pop();
107+
model(stars, 2000);
108+
pop()
109+
110+
push()
111+
shader(fresnelShader)
112+
noStroke()
113+
sphere(500);
114+
pop()
115+
filter(pixellizeShader);
116+
85117
originalFrameBuffer.end();
86118

87-
blurredFrameBuffer.begin();
88-
image(originalFrameBuffer, -windowWidth/2, -windowHeight/2)
89-
filter(BLUR)
90-
blurredFrameBuffer.end();
91-
92-
// image(originalFrameBuffer, -windowWidth/2, -windowHeight/2)
119+
imageMode(CENTER)
120+
image(originalFrameBuffer, 0, 0)
121+
122+
filter(BLUR, 20)
93123
filter(bloomShader);
94124
}

src/shape/vertex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ function vertex(p5, fn){
14131413
/**
14141414
* Sets the shader's vertex property or attribute variables.
14151415
*
1416-
* An vertex property or vertex attribute is a variable belonging to a vertex in a shader. p5.js provides some
1416+
* A vertex property, or vertex attribute, is a variable belonging to a vertex in a shader. p5.js provides some
14171417
* default properties, such as `aPosition`, `aNormal`, `aVertexColor`, etc. These are
14181418
* set using <a href="#/p5/vertex">vertex()</a>, <a href="#/p5/normal">normal()</a>
14191419
* and <a href="#/p5/fill">fill()</a> respectively. Custom properties can also

0 commit comments

Comments
 (0)