Skip to content

Commit b523b73

Browse files
committed
add mat4Persp + mat4Ortho to @nakednous/tree and p5.tree bridge
deps/tree/src/form.js: - remove mat4Perspective (symmetric fov+aspect is user-space sugar) - rename mat4Frustum → mat4Persp (general off-centre perspective) src/matrix.js: - mat4Proj remains pure live-state reader (persp or ortho) - add mat4Persp(out, l,r,b,t,near,far) standalone constructor - add mat4Ortho(out, l,r,b,t,near,far) standalone constructor - both auto-supply NDC convention via getNdcZ() src/visibility.js: - update sign contract comment: mat4Frustum → mat4Persp
1 parent b77c968 commit b523b73

File tree

11 files changed

+54
-50
lines changed

11 files changed

+54
-50
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,14 @@ Accepted types for `out` and override params: `Float32Array` | `ArrayLike` | `p5
244244
**Simple queries** — read from live renderer state:
245245
246246
```js
247-
mat4Eye(out) // eye matrix (inverse view) — eye→world
248-
mat4Eye(out, ex,ey,ez, cx,cy,cz, ux,uy,uz) // standalone lookat eye — no camera state
249-
mat4Proj(out) // projection matrix
250-
mat4View(out) // view matrix — world→eye
251-
mat4View(out, ex,ey,ez, cx,cy,cz, ux,uy,uz) // standalone lookat view — no camera state
252-
mat4Model(out) // model matrix — local→world
247+
mat4Model(out) // model matrix — local→world
248+
mat4View(out) // view matrix — world→eye
249+
mat4View(out, ex,ey,ez, cx,cy,cz, ux,uy,uz) // standalone lookat view — no camera state
250+
mat4Eye(out) // eye matrix (inverse view) — eye→world
251+
mat4Eye(out, ex,ey,ez, cx,cy,cz, ux,uy,uz) // standalone lookat eye — no camera state
252+
mat4Proj(out) // projection matrix (live state — persp or ortho)
253+
mat4Persp(out, l,r,b,t, near,far) // standalone perspective (general frustum)
254+
mat4Ortho(out, l,r,b,t, near,far) // standalone orthographic
253255
```
254256
255257
**Composite queries** — `out` first, optional overrides in an opts object:
@@ -607,7 +609,7 @@ Both accept the same options object:
607609
# Utilities
608610
609611
```js
610-
p5.Tree.VERSION // '0.0.36'
612+
p5.Tree.VERSION // '0.0.37'
611613
```
612614
613615
## Shader helpers
@@ -682,9 +684,9 @@ Latest:
682684
683685
Tagged:
684686
685-
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.js)
686-
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.min.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.min.js)
687-
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.esm.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.36/dist/p5.tree.esm.js)
687+
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.js)
688+
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.min.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.min.js)
689+
* [https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.esm.js](https://cdn.jsdelivr.net/npm/p5.tree@0.0.37/dist/p5.tree.esm.js)
688690
689691
---
690692

deps/tree/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,8 @@ mat4Eye — eye matrix (eye→world) from lookat params
320320
mat4FromTRS — column-major mat4 from flat TRS scalars
321321
mat4FromTranslation — translation-only mat4
322322
mat4FromScale — scale-only mat4
323-
mat4Perspective — perspective projection (ndcZMin, ndcYSign)
324-
mat4Ortho — orthographic projection (ndcZMin, ndcYSign)
325-
mat4Frustum — off-centre perspective (ndcZMin, ndcYSign)
323+
mat4Persp — perspective projection, general frustum (ndcZMin, ndcYSign)
324+
mat4Ortho — orthographic projection (ndcZMin, ndcYSign)
326325
mat4Bias — NDC→texture/UV remap [0,1] for shadow mapping
327326
mat4Reflect — reflection across a plane
328327
```

deps/tree/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nakednous/tree",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "tree — pure numeric core. Zero dependencies.",
55
"type": "module",
66
"main": "dist/index.js",

deps/tree/src/form.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,30 +164,6 @@ export function mat4FromScale(out, sx,sy,sz) {
164164
// Projection construction
165165
// =========================================================================
166166

167-
/**
168-
* Perspective projection matrix.
169-
*
170-
* @param {Float32Array|number[]} out 16-element destination.
171-
* @param {number} fov Vertical field of view (radians).
172-
* @param {number} aspect Width / height.
173-
* @param {number} near Near plane distance (positive).
174-
* @param {number} far Far plane distance (positive, > near).
175-
* @param {number} ndcZMin −1 (WEBGL) or 0 (WEBGPU).
176-
* @param {number} [ndcYSign=1] +1 = NDC y-up (default); −1 = NDC y-down (native Vulkan).
177-
*/
178-
export function mat4Perspective(out, fov, aspect, near, far, ndcZMin, ndcYSign=1) {
179-
const f = 1 / Math.tan(fov * 0.5);
180-
out[0]=f/aspect; out[1]=0; out[2]=0; out[3]=0;
181-
out[4]=0; out[5]=ndcYSign*f; out[6]=0; out[7]=0;
182-
out[8]=0; out[9]=0;
183-
out[10]=(ndcZMin*near-far)/(far-near);
184-
out[11]=-1;
185-
out[12]=0; out[13]=0;
186-
out[14]=(ndcZMin-1)*far*near/(far-near);
187-
out[15]=0;
188-
return out;
189-
}
190-
191167
/**
192168
* Orthographic projection matrix.
193169
*
@@ -209,15 +185,18 @@ export function mat4Ortho(out, left, right, bottom, top, near, far, ndcZMin, ndc
209185
}
210186

211187
/**
212-
* Frustum (off-centre perspective) projection matrix.
188+
* Perspective projection matrix (general / off-centre frustum).
189+
* Symmetric case: left=-right, bottom=-top — derive from fov+aspect in user space:
190+
* top = near * Math.tan(fov / 2); right = top * aspect
191+
* mat4Persp(out, -right, right, -top, top, near, far, ndcZMin)
213192
*
214193
* @param {Float32Array|number[]} out 16-element destination.
215-
* @param {number} left,right,bottom,top Near-plane extents.
194+
* @param {number} left,right,bottom,top Near-plane extents (signed, y-up: top>0, bottom<0).
216195
* @param {number} near,far Clip plane distances (positive).
217196
* @param {number} ndcZMin −1 (WEBGL) or 0 (WEBGPU).
218197
* @param {number} [ndcYSign=1] +1 = NDC y-up (default); −1 = NDC y-down (native Vulkan).
219198
*/
220-
export function mat4Frustum(out, left, right, bottom, top, near, far, ndcZMin, ndcYSign=1) {
199+
export function mat4Persp(out, left, right, bottom, top, near, far, ndcZMin, ndcYSign=1) {
221200
const rl=1/(right-left), tb=1/(top-bottom);
222201
out[0]=2*near*rl; out[1]=0; out[2]=0; out[3]=0;
223202
out[4]=0; out[5]=ndcYSign*2*near*tb; out[6]=0; out[7]=0;

deps/tree/src/track.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ export const hermiteVec3 = (out, p0, m0, p1, m1, t) => {
121121
};
122122

123123
// Centripetal CR outgoing tangent at p1 for segment p1→p2, scaled by dt1.
124-
const _crTanOut = (out, p0, p1, p2, p3) => {
124+
const _crTanOut = (out, p0, p1, p2) => {
125125
const dt0=Math.pow(_dist3(p0,p1),0.5)||1, dt1=Math.pow(_dist3(p1,p2),0.5)||1;
126126
for (let i=0;i<3;i++) out[i]=((p1[i]-p0[i])/dt0-(p2[i]-p0[i])/(dt0+dt1)+(p2[i]-p1[i])/dt1)*dt1;
127127
return out;
128128
};
129129

130-
const _crTanIn = (out, p0, p1, p2, p3) => {
130+
const _crTanIn = (out, p1, p2, p3) => {
131131
const dt1=Math.pow(_dist3(p1,p2),0.5)||1, dt2=Math.pow(_dist3(p2,p3),0.5)||1;
132132
for (let i=0;i<3;i++) out[i]=((p2[i]-p1[i])/dt1-(p3[i]-p1[i])/(dt1+dt2)+(p3[i]-p2[i])/dt2)*dt1;
133133
return out;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "p5.tree",
3-
"version": "0.0.36",
3+
"version": "0.0.37",
44
"description": "Render pipeline for p5.js v2 — pose and camera interpolation, space transforms, frustum visibility, HUD, post-processing pipe, picking, and declarative control panels.",
55
"type": "module",
66
"main": "dist/p5.tree.esm.js",
@@ -49,7 +49,7 @@
4949
"p5": "^2.2.3"
5050
},
5151
"dependencies": {
52-
"@nakednous/tree": "^0.0.17",
52+
"@nakednous/tree": "^0.0.18",
5353
"@nakednous/ui": "^0.0.9"
5454
},
5555
"devDependencies": {

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function installConstants(p5) {
1212
const CONST = value => ({ value, writable: false, enumerable: true, configurable: false });
1313

1414
Object.defineProperties(p5.Tree, {
15-
VERSION: CONST('0.0.36'),
15+
VERSION: CONST('0.0.37'),
1616
NONE: CONST(0),
1717

1818
// Core constants (spaces, visibility, NDC, basis vectors)

src/gizmos.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
} from '@nakednous/tree';
1919

2020
import { getNdcZ } from './matrix.js';
21-
import { computePlanes } from './visibility.js';
2221

2322
// ═══════════════════════════════════════════════════════════════════════════
2423
// Module-level working buffers — never returned to caller

src/matrix.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
pixelRatio as corePixelRatio,
3535
mat4View as _mat4View,
3636
mat4Eye as _mat4Eye,
37+
mat4Persp as _mat4Persp,
38+
mat4Ortho as _mat4Ortho,
3739
mat4ToTranslation,
3840
mat4ToScale,
3941
mat4ToRotation,
@@ -102,14 +104,37 @@ export function installMatrix(p5, fn) {
102104
// ── Simple matrix queries ─────────────────────────────────────────────────
103105
// out: Float32Array | ArrayLike | p5.Matrix — 16-element destination.
104106

105-
/** Projection matrix (eye → clip). */
107+
/** Projection matrix (eye → clip) — reads live renderer state (perspective or ortho). */
106108
p5.Renderer3D.prototype.mat4Proj = function (out) {
107109
const buf = _rawMat4(out), s = _projMat4(this);
108110
for (let i = 0; i < 16; i++) buf[i] = s[i];
109111
return out;
110112
};
111113
fn.mat4Proj = function (out) { return this._renderer.mat4Proj(out); };
112114

115+
/**
116+
* Perspective projection matrix (standalone constructor, general frustum).
117+
* mat4Persp(out, left, right, bottom, top, near, far[, ndcZMin[, ndcYSign]])
118+
* Symmetric: left=-right, bottom=-top — derive from fov+aspect in user space.
119+
* @param {Float32Array|ArrayLike|p5.Matrix} out
120+
*/
121+
fn.mat4Persp = function (out, ...args) {
122+
if (args[6] == null) args[6] = _ndcZ;
123+
_mat4Persp(_rawMat4(out), ...args);
124+
return out;
125+
};
126+
127+
/**
128+
* Orthographic projection matrix (standalone constructor).
129+
* mat4Ortho(out, left, right, bottom, top, near, far[, ndcZMin[, ndcYSign]])
130+
* @param {Float32Array|ArrayLike|p5.Matrix} out
131+
*/
132+
fn.mat4Ortho = function (out, ...args) {
133+
if (args[6] == null) args[6] = _ndcZ;
134+
_mat4Ortho(_rawMat4(out), ...args);
135+
return out;
136+
};
137+
113138
/** Model matrix (local → world). */
114139
p5.Renderer3D.prototype.mat4Model = function (out) {
115140
const buf = _rawMat4(out), s = _modelMat4(this);

src/panel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
mapLocation as coreMapLocation,
3232
mat4Mul, mat4Invert,
3333
} from '@nakednous/tree';
34-
import { registerPlayer, unregisterPlayer } from './track.js';
34+
import { registerPlayer } from './track.js';
3535
import { CameraTrack } from '@nakednous/tree';
3636

3737
// ── Module-level scratch (allocated once at import time) ──────────────────────

0 commit comments

Comments
 (0)