p5.env 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -19
- package/p5.env.js +23 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,10 +15,10 @@ The angular SDFs here take in a surface normal and return two things:
|
|
|
15
15
|
## Builder API
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
myShader =
|
|
18
|
+
myShader = buildEnvMaterial(() => {
|
|
19
19
|
envColor.begin()
|
|
20
20
|
const l = envLight(baseColor, envColor.dir, envColor.blur)
|
|
21
|
-
l.mix(l.
|
|
21
|
+
l.mix(l.circle(...), lightColor)
|
|
22
22
|
envColor.set(l.get())
|
|
23
23
|
envColor.end()
|
|
24
24
|
})
|
|
@@ -27,16 +27,16 @@ myShader = buildEnvLightShader(() => {
|
|
|
27
27
|
`envLight(baseColor, dir, blur)` creates a builder. `dir` and `blur` are captured once and used by all subsequent method calls.
|
|
28
28
|
|
|
29
29
|
**Shape methods** (return an SDF result to pass to `mix`):
|
|
30
|
-
- `l.
|
|
31
|
-
- `l.
|
|
32
|
-
- `l.
|
|
33
|
-
- `l.
|
|
34
|
-
- `l.
|
|
30
|
+
- `l.circle(center, radius)` - spherical cap; `center` is a unit vec3, `radius` in radians
|
|
31
|
+
- `l.capsule(a, b, radius)` - capsule between two unit vec3 endpoints, `radius` in radians
|
|
32
|
+
- `l.star(center, n, innerRadius, outerRadius, rotation?)` - n-pointed star; radii in radians, `n` is a plain JS integer
|
|
33
|
+
- `l.rect(center, size, rotation?)` - rectangle; `size` is `[halfWidth, halfHeight]` as chord lengths (sine of angle, not radians), `rotation` in radians
|
|
34
|
+
- `l.window(center, size, panes, barWidth)` - rectangle subdivided into panes; `panes` is `[nx, ny]`; `size` and `barWidth` are chord lengths (sine of angle, not radians -- for small shapes the difference is negligible)
|
|
35
35
|
|
|
36
36
|
**Color methods** (return a scalar/vec3 usable in expressions or as a base color):
|
|
37
|
-
- `l.
|
|
38
|
-
- `l.
|
|
39
|
-
- `l.
|
|
37
|
+
- `l.gradient(center, ...stops)` - radial gradient; each stop is `{ t, color }` where `t` is angle from `center` in radians and `color` is a vec3; spread stops as individual arguments
|
|
38
|
+
- `l.noise(size)` - blur-aware fractal noise value; `size` is the angular scale of the largest octave
|
|
39
|
+
- `l.noisePlane(planeNormal, h, size, { rotation?, offset? })` - projects a planar noise field onto the sphere; `h` is the plane's height, `size` is the noise scale; `offset` is a vec2 that shifts the noise coordinate (use for animation)
|
|
40
40
|
|
|
41
41
|
**Builder methods**:
|
|
42
42
|
- `l.mix(shape, color)` - blends `color` into the accumulated result using the shape's SDF; returns `l` for chaining
|
|
@@ -44,28 +44,27 @@ myShader = buildEnvLightShader(() => {
|
|
|
44
44
|
|
|
45
45
|
## Panorama
|
|
46
46
|
|
|
47
|
-
The same `envColor` hook can also
|
|
47
|
+
The same `envColor` hook can also drive a background panorama, so the environment visible on surfaces matches the environment visible in the background.
|
|
48
48
|
|
|
49
49
|
```js
|
|
50
|
-
|
|
50
|
+
const envHooks = () => {
|
|
51
51
|
envColor.begin()
|
|
52
|
-
//
|
|
52
|
+
// ... same hook body as before ...
|
|
53
53
|
envColor.end()
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
let envShader,
|
|
56
|
+
let envShader, pano
|
|
57
57
|
|
|
58
58
|
function setup() {
|
|
59
|
-
envShader =
|
|
60
|
-
|
|
59
|
+
envShader = buildEnvMaterial(envHooks)
|
|
60
|
+
pano = buildEnvPanorama(envHooks)
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
function draw() {
|
|
64
|
-
|
|
65
|
-
panoramaEnv(panoramaShader)
|
|
64
|
+
pano()
|
|
66
65
|
shader(envShader)
|
|
67
66
|
sphere(150)
|
|
68
67
|
}
|
|
69
68
|
```
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
`buildEnvPanorama` returns a function. Calling it each frame sets the camera uniforms and applies the panorama as a post-process filter. Pass a blur value in radians to match a rough material's specular blur: `pano(blur)`.
|
package/p5.env.js
CHANGED
|
@@ -37,7 +37,7 @@ function envLight(p5, fn) {
|
|
|
37
37
|
return this._baseEnvLightShader
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
fn.
|
|
40
|
+
fn.buildEnvMaterial = function(...args) {
|
|
41
41
|
return this.baseEnvLightShader().modify(...args)
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -71,19 +71,18 @@ function envLight(p5, fn) {
|
|
|
71
71
|
return this._baseEnvLightPanoramaShader
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
fn.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.filter(panoramaShader)
|
|
74
|
+
fn.buildEnvPanorama = function(...args) {
|
|
75
|
+
const panoramaShader = this.baseEnvLightPanoramaShader().modify(...args)
|
|
76
|
+
return (blur = 0) => {
|
|
77
|
+
const renderer = this._renderer
|
|
78
|
+
renderer.scratchMat3.inverseTranspose4x4(renderer.states.uViewMatrix)
|
|
79
|
+
renderer.scratchMat3.invert(renderer.scratchMat3)
|
|
80
|
+
panoramaShader.setUniform('uFovY', renderer.states.curCamera.cameraFOV)
|
|
81
|
+
panoramaShader.setUniform('uAspect', renderer.states.curCamera.aspectRatio)
|
|
82
|
+
panoramaShader.setUniform('uCameraRotation', renderer.scratchMat3.mat3)
|
|
83
|
+
panoramaShader.setUniform('uBlur', Math.min(blur, Math.PI / 2))
|
|
84
|
+
this.filter(panoramaShader)
|
|
85
|
+
}
|
|
87
86
|
}
|
|
88
87
|
|
|
89
88
|
// Color helpers
|
|
@@ -207,7 +206,7 @@ function envLight(p5, fn) {
|
|
|
207
206
|
rotation = p5.strandsNode(rotation)
|
|
208
207
|
h = p5.strandsNode(h)
|
|
209
208
|
|
|
210
|
-
let up = p5.strandsTernary(this.abs(planeNormal.y).
|
|
209
|
+
let up = p5.strandsTernary(this.abs(planeNormal.y).lessThan(0.99), this.vec3(0, 1, 0), this.vec3(1, 0, 0))
|
|
211
210
|
let xLocal = this.normalize(this.cross(up, planeNormal))
|
|
212
211
|
let yLocal = this.cross(planeNormal, xLocal)
|
|
213
212
|
|
|
@@ -240,7 +239,7 @@ function envLight(p5, fn) {
|
|
|
240
239
|
outerRadius = p5.strandsNode(outerRadius)
|
|
241
240
|
rotation = p5.strandsNode(rotation)
|
|
242
241
|
|
|
243
|
-
let up = p5.strandsTernary(this.abs(center.y).
|
|
242
|
+
let up = p5.strandsTernary(this.abs(center.y).lessThan(0.99), this.vec3(0, 1, 0), this.vec3(1, 0, 0))
|
|
244
243
|
let xLocal = this.normalize(this.cross(up, center))
|
|
245
244
|
let yLocal = this.cross(center, xLocal)
|
|
246
245
|
|
|
@@ -400,28 +399,28 @@ function envLight(p5, fn) {
|
|
|
400
399
|
let c = p5.strandsNode(baseColor)
|
|
401
400
|
|
|
402
401
|
return {
|
|
403
|
-
|
|
402
|
+
circle(center, radius) {
|
|
404
403
|
return sketch.envCircle(dir, center, radius)
|
|
405
404
|
},
|
|
406
|
-
|
|
405
|
+
capsule(a, b, radius) {
|
|
407
406
|
return sketch.envCapsule(dir, a, b, radius)
|
|
408
407
|
},
|
|
409
|
-
|
|
408
|
+
star(center, n, innerRadius, outerRadius, rotation) {
|
|
410
409
|
return sketch.envStar(dir, center, n, innerRadius, outerRadius, rotation)
|
|
411
410
|
},
|
|
412
|
-
|
|
411
|
+
rect(center, size, rotation) {
|
|
413
412
|
return sketch.envRect(dir, center, size, rotation)
|
|
414
413
|
},
|
|
415
|
-
|
|
414
|
+
window(center, size, panes, barWidth) {
|
|
416
415
|
return sketch.envWindow(dir, center, size, panes, barWidth)
|
|
417
416
|
},
|
|
418
|
-
|
|
417
|
+
noise(size) {
|
|
419
418
|
return sketch.envNoise(dir, size, blur)
|
|
420
419
|
},
|
|
421
|
-
|
|
420
|
+
gradient(center, ...stops) {
|
|
422
421
|
return sketch.envGradient(dir, center, blur, ...stops)
|
|
423
422
|
},
|
|
424
|
-
|
|
423
|
+
noisePlane(planeNormal, h, size, { rotation = 0, offset = [0, 0] } = {}) {
|
|
425
424
|
return sketch.envNoisePlane(dir, planeNormal, h, size, blur, { rotation, offset })
|
|
426
425
|
},
|
|
427
426
|
mix(shape, materialColor) {
|