p5.tree 0.0.2 → 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 +11 -19
- package/dist/p5.tree.esm.js +93 -61
- package/dist/p5.tree.esm.js.map +1 -1
- package/dist/p5.tree.js +93 -61
- package/dist/p5.tree.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -266,7 +266,7 @@ A small collection of helpers commonly needed in interactive 3D sketches:
|
|
|
266
266
|
Debug / teaching primitives for visualizing common 3D concepts:
|
|
267
267
|
|
|
268
268
|
1. `axes({ size, colors, bits })`
|
|
269
|
-
2. `grid({ size, subdivisions
|
|
269
|
+
2. `grid({ size, subdivisions })`
|
|
270
270
|
3. `cross({ mMatrix, x, y, size, ... })`
|
|
271
271
|
4. `bullsEye({ mMatrix, x, y, size, shape, ... })`
|
|
272
272
|
5. `viewFrustum({ pg, bits, viewer, eMatrix, pMatrix, vMatrix })`
|
|
@@ -275,19 +275,19 @@ Debug / teaching primitives for visualizing common 3D concepts:
|
|
|
275
275
|
|
|
276
276
|
# Releases
|
|
277
277
|
|
|
278
|
-
- **Latest (v0.0.
|
|
278
|
+
- **Latest (v0.0.3):**
|
|
279
279
|
These links always point to the latest published version on npm.
|
|
280
280
|
- [p5.tree.js (unminified, IIFE)](https://cdn.jsdelivr.net/npm/p5.tree/dist/p5.tree.js)
|
|
281
281
|
- [p5.tree.min.js (minified, IIFE)](https://cdn.jsdelivr.net/npm/p5.tree/dist/p5.tree.min.js)
|
|
282
282
|
- [p5.tree.esm.js (ES module)](https://cdn.jsdelivr.net/npm/p5.tree/dist/p5.tree.esm.js)
|
|
283
283
|
- [npm package](https://www.npmjs.com/package/p5.tree)
|
|
284
284
|
|
|
285
|
-
- **Current tagged version (v0.0.
|
|
285
|
+
- **Current tagged version (v0.0.3):**
|
|
286
286
|
Use these if you want to lock to a specific version.
|
|
287
|
-
- [p5.tree@0.0.
|
|
288
|
-
- [p5.tree@0.0.
|
|
289
|
-
- [p5.tree@0.0.
|
|
290
|
-
- [npm package (v0.0.
|
|
287
|
+
- [p5.tree@0.0.3.js (unminified, IIFE)](https://cdn.jsdelivr.net/npm/p5.tree@0.0.3/dist/p5.tree.js)
|
|
288
|
+
- [p5.tree@0.0.3.min.js (minified, IIFE)](https://cdn.jsdelivr.net/npm/p5.tree@0.0.3/dist/p5.tree.min.js)
|
|
289
|
+
- [p5.tree@0.0.3.esm.js (ES module)](https://cdn.jsdelivr.net/npm/p5.tree@0.0.3/dist/p5.tree.esm.js)
|
|
290
|
+
- [npm package (v0.0.3)](https://www.npmjs.com/package/p5.tree/v/0.0.3)
|
|
291
291
|
|
|
292
292
|
---
|
|
293
293
|
|
|
@@ -295,12 +295,8 @@ Debug / teaching primitives for visualizing common 3D concepts:
|
|
|
295
295
|
|
|
296
296
|
The library works in two setups:
|
|
297
297
|
|
|
298
|
-
- **[CDN](#cdn)**: Use the [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE)
|
|
299
|
-
|
|
300
|
-
along with [p5.js](https://beta.p5js.org/).
|
|
301
|
-
- **[npm](#npm-esm)**: Use the
|
|
302
|
-
[ES module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
|
|
303
|
-
version in modern projects with [Vite](https://vitejs.dev/) or another bundler.
|
|
298
|
+
- **[CDN](#cdn)**: Use the [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) (Immediately Invoked Function Expression) format with `<script>` tags directly in the browser, along with [p5.js](https://beta.p5js.org/).
|
|
299
|
+
- **[npm](#npm-esm)**: Use the [ES module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) version in modern projects with [Vite](https://vitejs.dev/) or another bundler.
|
|
304
300
|
|
|
305
301
|
## CDN
|
|
306
302
|
|
|
@@ -329,9 +325,7 @@ Include both libraries using `<script>` tags, which run in both [global](https:/
|
|
|
329
325
|
</script>
|
|
330
326
|
````
|
|
331
327
|
|
|
332
|
-
You can run the example, which uses global mode, by opening the `index.html` file in a browser, or by using
|
|
333
|
-
[VSCodium](https://vscodium.com/) (recommended) or [Visual Studio Code](https://code.visualstudio.com/)
|
|
334
|
-
with the [Live Server extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
|
|
328
|
+
You can run the example, which uses global mode, by opening the `index.html` file in a browser, or by using [VSCodium](https://vscodium.com/) (recommended) or [Visual Studio Code](https://code.visualstudio.com/) with the [Live Server extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
|
|
335
329
|
|
|
336
330
|
## npm (ESM)
|
|
337
331
|
|
|
@@ -341,9 +335,7 @@ Install both [`p5`](https://www.npmjs.com/package/p5) and [`p5.tree`](https://ww
|
|
|
341
335
|
npm i p5 p5.tree
|
|
342
336
|
```
|
|
343
337
|
|
|
344
|
-
Then import them in your project’s entry file (e.g. `main.js`) using a
|
|
345
|
-
modern bundler like [Vite](https://vitejs.dev/), which runs in
|
|
346
|
-
[instance mode](https://github.com/processing/p5.js/wiki/Global-and-instance-mode) only:
|
|
338
|
+
Then import them in your project’s entry file (e.g. `main.js`) using a modern bundler like [Vite](https://vitejs.dev/), which runs in [instance mode](https://github.com/processing/p5.js/wiki/Global-and-instance-mode) only:
|
|
347
339
|
|
|
348
340
|
```js
|
|
349
341
|
// main.js
|
package/dist/p5.tree.esm.js
CHANGED
|
@@ -2,7 +2,7 @@ import p5 from 'p5';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file Adds Tree rendering functions to the p5 prototype.
|
|
5
|
-
* @version 0.0.
|
|
5
|
+
* @version 0.0.3
|
|
6
6
|
* @author JP Charalambos
|
|
7
7
|
* @license GPL-3.0-only
|
|
8
8
|
*
|
|
@@ -38,7 +38,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
38
38
|
const CONST = value => ({ value, writable: false, enumerable: true, configurable: false });
|
|
39
39
|
|
|
40
40
|
Object.defineProperties(p5.Tree, {
|
|
41
|
-
VERSION: CONST('0.0.
|
|
41
|
+
VERSION: CONST('0.0.3'),
|
|
42
42
|
|
|
43
43
|
NONE: CONST(0),
|
|
44
44
|
|
|
@@ -48,6 +48,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
48
48
|
NDC: CONST('NDC'),
|
|
49
49
|
SCREEN: CONST('SCREEN'),
|
|
50
50
|
MODEL: CONST('MODEL'),
|
|
51
|
+
OBJECT: CONST('MODEL'), // alias of MODEL (shader terminology)
|
|
51
52
|
|
|
52
53
|
// Points and vectors
|
|
53
54
|
ORIGIN: CONST(Object.freeze([0, 0, 0])),
|
|
@@ -68,9 +69,6 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
68
69
|
Z: CONST(1 << 4),
|
|
69
70
|
_Z: CONST(1 << 5),
|
|
70
71
|
LABELS: CONST(1 << 6),
|
|
71
|
-
|
|
72
|
-
DOTS: CONST(0),
|
|
73
|
-
SOLID: CONST(1),
|
|
74
72
|
|
|
75
73
|
// bullsEye
|
|
76
74
|
CIRCLE: CONST(0),
|
|
@@ -301,7 +299,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
301
299
|
|
|
302
300
|
/**
|
|
303
301
|
* lMatrix({ from, to }):
|
|
304
|
-
*
|
|
302
|
+
* Location transform (mat4) mapping points from `from` space to `to` space.
|
|
305
303
|
* treegl semantics: to^-1 * from.
|
|
306
304
|
* @param {object} [opts]
|
|
307
305
|
* @param {p5.Matrix} [opts.from=new p5.Matrix()] Source frame matrix.
|
|
@@ -317,7 +315,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
317
315
|
|
|
318
316
|
/**
|
|
319
317
|
* lMatrix({ from, to }):
|
|
320
|
-
*
|
|
318
|
+
* Location transform (mat4) mapping points from `from` space to `to` space.
|
|
321
319
|
* Requires WEBGL.
|
|
322
320
|
* @param {object} [opts]
|
|
323
321
|
* @param {p5.Matrix} [opts.from]
|
|
@@ -1506,25 +1504,25 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1506
1504
|
to = this.mMatrix({ eMatrix });
|
|
1507
1505
|
}
|
|
1508
1506
|
if ((from == p5.Tree.WORLD) && (to == p5.Tree.SCREEN)) {
|
|
1509
|
-
return this.
|
|
1507
|
+
return this._worldToScreenLocation({ point, pMatrix, vMatrix, pvMatrix });
|
|
1510
1508
|
}
|
|
1511
1509
|
if ((from == p5.Tree.SCREEN) && (to == p5.Tree.WORLD)) {
|
|
1512
|
-
return this.
|
|
1510
|
+
return this._screenToWorldLocation({ point, pMatrix, vMatrix, pvMatrix, ipvMatrix });
|
|
1513
1511
|
}
|
|
1514
1512
|
if (from == p5.Tree.SCREEN && to == p5.Tree.NDC) {
|
|
1515
|
-
return this.
|
|
1513
|
+
return this._screenToNDCLocation(point);
|
|
1516
1514
|
}
|
|
1517
1515
|
if (from == p5.Tree.NDC && to == p5.Tree.SCREEN) {
|
|
1518
|
-
return this.
|
|
1516
|
+
return this._ndcToScreenLocation(point);
|
|
1519
1517
|
}
|
|
1520
1518
|
if (from == p5.Tree.WORLD && to == p5.Tree.NDC) {
|
|
1521
|
-
return this.
|
|
1522
|
-
this.
|
|
1519
|
+
return this._screenToNDCLocation(
|
|
1520
|
+
this._worldToScreenLocation({ point, pMatrix, vMatrix, pvMatrix })
|
|
1523
1521
|
);
|
|
1524
1522
|
}
|
|
1525
1523
|
if (from == p5.Tree.NDC && to == p5.Tree.WORLD) {
|
|
1526
|
-
return this.
|
|
1527
|
-
point: this.
|
|
1524
|
+
return this._screenToWorldLocation({
|
|
1525
|
+
point: this._ndcToScreenLocation(point),
|
|
1528
1526
|
pMatrix,
|
|
1529
1527
|
vMatrix,
|
|
1530
1528
|
pvMatrix,
|
|
@@ -1536,8 +1534,8 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1536
1534
|
? (vMatrix ?? this.vMatrix())
|
|
1537
1535
|
: to.copy().invert(to)
|
|
1538
1536
|
).mult4(
|
|
1539
|
-
this.
|
|
1540
|
-
point: this.
|
|
1537
|
+
this._screenToWorldLocation({
|
|
1538
|
+
point: this._ndcToScreenLocation(point),
|
|
1541
1539
|
pMatrix,
|
|
1542
1540
|
vMatrix,
|
|
1543
1541
|
pvMatrix,
|
|
@@ -1546,8 +1544,8 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1546
1544
|
);
|
|
1547
1545
|
}
|
|
1548
1546
|
if ((from instanceof p5.Matrix || from == p5.Tree.EYE) && to == p5.Tree.NDC) {
|
|
1549
|
-
return this.
|
|
1550
|
-
this.
|
|
1547
|
+
return this._screenToNDCLocation(
|
|
1548
|
+
this._worldToScreenLocation({
|
|
1551
1549
|
point: (from == p5.Tree.EYE
|
|
1552
1550
|
? (eMatrix ?? this.eMatrix())
|
|
1553
1551
|
: from
|
|
@@ -1578,11 +1576,11 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1578
1576
|
? (vMatrix ?? this.vMatrix())
|
|
1579
1577
|
: to.copy().invert(to)
|
|
1580
1578
|
).mult4(
|
|
1581
|
-
this.
|
|
1579
|
+
this._screenToWorldLocation({ point, pMatrix, vMatrix, pvMatrix, ipvMatrix })
|
|
1582
1580
|
);
|
|
1583
1581
|
}
|
|
1584
1582
|
if ((from instanceof p5.Matrix || from == p5.Tree.EYE) && to == p5.Tree.SCREEN) {
|
|
1585
|
-
return this.
|
|
1583
|
+
return this._worldToScreenLocation({
|
|
1586
1584
|
point: (from == p5.Tree.EYE
|
|
1587
1585
|
? (eMatrix ?? this.eMatrix())
|
|
1588
1586
|
: from
|
|
@@ -1602,7 +1600,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1602
1600
|
return point;
|
|
1603
1601
|
};
|
|
1604
1602
|
|
|
1605
|
-
p5.RendererGL.prototype.
|
|
1603
|
+
p5.RendererGL.prototype._ndcToScreenLocation = function (point) {
|
|
1606
1604
|
return new p5.Vector(
|
|
1607
1605
|
p5.prototype.map(point.x, -1, 1, 0, this.width),
|
|
1608
1606
|
p5.prototype.map(point.y, -1, 1, 0, this.height),
|
|
@@ -1610,7 +1608,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1610
1608
|
);
|
|
1611
1609
|
};
|
|
1612
1610
|
|
|
1613
|
-
p5.RendererGL.prototype.
|
|
1611
|
+
p5.RendererGL.prototype._screenToNDCLocation = function (point) {
|
|
1614
1612
|
return new p5.Vector(
|
|
1615
1613
|
p5.prototype.map(point.x, 0, this.width, -1, 1),
|
|
1616
1614
|
p5.prototype.map(point.y, 0, this.height, -1, 1),
|
|
@@ -1618,7 +1616,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1618
1616
|
);
|
|
1619
1617
|
};
|
|
1620
1618
|
|
|
1621
|
-
p5.RendererGL.prototype.
|
|
1619
|
+
p5.RendererGL.prototype._worldToScreenLocation = function ({
|
|
1622
1620
|
point = new p5.Vector(0, 0, 0.5),
|
|
1623
1621
|
pMatrix,
|
|
1624
1622
|
vMatrix,
|
|
@@ -1641,7 +1639,7 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1641
1639
|
return new p5.Vector(target[0], target[1], target[2]);
|
|
1642
1640
|
};
|
|
1643
1641
|
|
|
1644
|
-
p5.RendererGL.prototype.
|
|
1642
|
+
p5.RendererGL.prototype._screenToWorldLocation = function ({
|
|
1645
1643
|
point = new p5.Vector(this.width / 2, this.height / 2, 0.5),
|
|
1646
1644
|
pMatrix,
|
|
1647
1645
|
vMatrix,
|
|
@@ -1965,6 +1963,49 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
1965
1963
|
return this;
|
|
1966
1964
|
};
|
|
1967
1965
|
|
|
1966
|
+
/**
|
|
1967
|
+
* Draws 3D reference axes (X, Y, Z) centered at the origin in model space.
|
|
1968
|
+
*
|
|
1969
|
+
* Each axis can be enabled independently using bitwise flags, and optional
|
|
1970
|
+
* axis labels (X, Y, Z) can be rendered near the positive ends.
|
|
1971
|
+
*
|
|
1972
|
+
* This is a WEBGL-only utility intended for debugging, teaching, and spatial
|
|
1973
|
+
* orientation. Axes are drawn on the current Z=0 plane using the current
|
|
1974
|
+
* stroke settings.
|
|
1975
|
+
*
|
|
1976
|
+
* @method axes
|
|
1977
|
+
* @for p5.RendererGL
|
|
1978
|
+
* @param {Object} [opts] Axes options.
|
|
1979
|
+
* @param {Number} [opts.size=100] Length of each axis in world units.
|
|
1980
|
+
* @param {Array<String>} [opts.colors=['Red','Lime','DodgerBlue']]
|
|
1981
|
+
* Stroke colors for X, Y, and Z axes respectively.
|
|
1982
|
+
* @param {Number} [opts.bits=p5.Tree.LABELS | p5.Tree.X | p5.Tree.Y | p5.Tree.Z]
|
|
1983
|
+
* Bitmask controlling which axes and labels are drawn.
|
|
1984
|
+
*
|
|
1985
|
+
* @example
|
|
1986
|
+
* function draw() {
|
|
1987
|
+
* background(30);
|
|
1988
|
+
* orbitControl();
|
|
1989
|
+
* axes({ size: 300 });
|
|
1990
|
+
* }
|
|
1991
|
+
*
|
|
1992
|
+
* @example
|
|
1993
|
+
* // Draw only X and Z axes, no labels
|
|
1994
|
+
* axes({
|
|
1995
|
+
* size: 200,
|
|
1996
|
+
* bits: p5.Tree.X | p5.Tree.Z
|
|
1997
|
+
* });
|
|
1998
|
+
*
|
|
1999
|
+
* @example
|
|
2000
|
+
* // Draw full axes in both positive and negative directions
|
|
2001
|
+
* axes({
|
|
2002
|
+
* size: 150,
|
|
2003
|
+
* bits: p5.Tree.X | p5.Tree._X |
|
|
2004
|
+
* p5.Tree.Y | p5.Tree._Y |
|
|
2005
|
+
* p5.Tree.Z | p5.Tree._Z |
|
|
2006
|
+
* p5.Tree.LABELS
|
|
2007
|
+
* });
|
|
2008
|
+
*/
|
|
1968
2009
|
p5.RendererGL.prototype.axes = function ({
|
|
1969
2010
|
size = 100,
|
|
1970
2011
|
colors = ['Red', 'Lime', 'DodgerBlue'],
|
|
@@ -2014,47 +2055,38 @@ p5.registerAddon((p5, fn, lifecycles) => {
|
|
|
2014
2055
|
return this;
|
|
2015
2056
|
};
|
|
2016
2057
|
|
|
2058
|
+
/**
|
|
2059
|
+
* Draws a simple X/Y reference grid on the Z=0 plane in the current model space.
|
|
2060
|
+
*
|
|
2061
|
+
* The grid is centered at the origin and spans from `-size` to `+size` on both X and Y.
|
|
2062
|
+
* It draws `subdivisions + 1` lines in each direction (including the borders).
|
|
2063
|
+
*
|
|
2064
|
+
* This is a WEBGL-only utility intended for debugging and spatial reference.
|
|
2065
|
+
*
|
|
2066
|
+
* @method grid
|
|
2067
|
+
* @for p5.RendererGL
|
|
2068
|
+
* @param {Object} [opts] Grid options.
|
|
2069
|
+
* @param {Number} [opts.size=100] Half-extent of the grid in world units.
|
|
2070
|
+
* @param {Number} [opts.subdivisions=10] Number of subdivisions per side (must be >= 1).
|
|
2071
|
+
* @example
|
|
2072
|
+
* function draw() {
|
|
2073
|
+
* background(30);
|
|
2074
|
+
* orbitControl();
|
|
2075
|
+
* grid({ size: 300, subdivisions: 20 });
|
|
2076
|
+
* }
|
|
2077
|
+
*/
|
|
2017
2078
|
p5.RendererGL.prototype.grid = function ({
|
|
2018
2079
|
size = 100,
|
|
2019
|
-
subdivisions = 10
|
|
2020
|
-
style = p5.Tree.SOLID,
|
|
2021
|
-
weight = 1,
|
|
2022
|
-
minorSubdivisions = 5
|
|
2080
|
+
subdivisions = 10
|
|
2023
2081
|
} = {}) {
|
|
2024
2082
|
const p = this._pInst;
|
|
2025
|
-
if (!p) return;
|
|
2083
|
+
if (!p) return;
|
|
2084
|
+
subdivisions = Math.max(1, subdivisions);
|
|
2026
2085
|
p.push();
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
p.
|
|
2031
|
-
p.beginShape(p.POINTS);
|
|
2032
|
-
for (let i = 0; i <= subdivisions; ++i) {
|
|
2033
|
-
posi = size * (2.0 * i / subdivisions - 1.0);
|
|
2034
|
-
for (let j = 0; j <= subdivisions; ++j) {
|
|
2035
|
-
posj = size * (2.0 * j / subdivisions - 1.0);
|
|
2036
|
-
p.vertex(posi, posj, 0);
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
p.endShape();
|
|
2040
|
-
const internalSub = Math.max(1, minorSubdivisions | 0);
|
|
2041
|
-
const subSubdivisions = subdivisions * internalSub;
|
|
2042
|
-
p.strokeWeight(weight);
|
|
2043
|
-
p.beginShape(p.POINTS);
|
|
2044
|
-
for (let i = 0; i <= subSubdivisions; ++i) {
|
|
2045
|
-
posi = size * (2.0 * i / subSubdivisions - 1.0);
|
|
2046
|
-
for (let j = 0; j <= subSubdivisions; ++j) {
|
|
2047
|
-
posj = size * (2.0 * j / subSubdivisions - 1.0);
|
|
2048
|
-
((i % internalSub) !== 0 || (j % internalSub) !== 0) && p.vertex(posi, posj, 0);
|
|
2049
|
-
}
|
|
2050
|
-
}
|
|
2051
|
-
p.endShape();
|
|
2052
|
-
} else {
|
|
2053
|
-
for (let i = 0; i <= subdivisions; ++i) {
|
|
2054
|
-
const pos = size * (2.0 * i / subdivisions - 1.0);
|
|
2055
|
-
p.line(pos, -size, 0, pos, +size, 0);
|
|
2056
|
-
p.line(-size, pos, 0, size, pos, 0);
|
|
2057
|
-
}
|
|
2086
|
+
for (let i = 0; i <= subdivisions; ++i) {
|
|
2087
|
+
const pos = size * (2.0 * i / subdivisions - 1.0);
|
|
2088
|
+
p.line(pos, -size, 0, pos, +size, 0);
|
|
2089
|
+
p.line(-size, pos, 0, +size, pos, 0);
|
|
2058
2090
|
}
|
|
2059
2091
|
p.pop();
|
|
2060
2092
|
};
|