@playcanvas/inspector 0.1.0
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/LICENSE +19 -0
- package/README.md +110 -0
- package/package.json +62 -0
- package/src/asset-usage.js +108 -0
- package/src/asset-view.js +370 -0
- package/src/camera-view.js +128 -0
- package/src/debug.js +31 -0
- package/src/describe.js +302 -0
- package/src/entity-inspector-script.js +130 -0
- package/src/frame-graph-view.js +767 -0
- package/src/hierarchy-view.js +475 -0
- package/src/index.js +4 -0
- package/src/input/fly-controller.js +116 -0
- package/src/input/input.js +299 -0
- package/src/input/math.js +8 -0
- package/src/input/orbit-controller.js +158 -0
- package/src/input/pose.js +207 -0
- package/src/inspector.js +3293 -0
- package/src/instance-survey.js +50 -0
- package/src/list-view.js +410 -0
- package/src/material-view.js +197 -0
- package/src/memory-view.js +344 -0
- package/src/mesh-view.js +235 -0
- package/src/model.js +170 -0
- package/src/node-model.js +404 -0
- package/src/physics-debug.js +527 -0
- package/src/physics-view.js +314 -0
- package/src/picker/picker.js +545 -0
- package/src/picker/render-pass-picker.js +206 -0
- package/src/property-view.js +628 -0
- package/src/render-target-view.js +288 -0
- package/src/renderers/texture-renderer-shaders.js +139 -0
- package/src/renderers/texture-renderer.js +430 -0
- package/src/renderers/wire-renderer.js +954 -0
- package/src/script-view.js +288 -0
- package/src/shader-view.js +287 -0
- package/src/styles.js +918 -0
- package/src/texture-view.js +186 -0
- package/src/tooltip.js +72 -0
- package/src/viewport-tools.js +695 -0
- package/types/asset-usage.d.ts +23 -0
- package/types/asset-view.d.ts +94 -0
- package/types/camera-view.d.ts +45 -0
- package/types/debug.d.ts +14 -0
- package/types/describe.d.ts +43 -0
- package/types/entity-inspector-script.d.ts +87 -0
- package/types/frame-graph-view.d.ts +155 -0
- package/types/hierarchy-view.d.ts +172 -0
- package/types/index.d.ts +3 -0
- package/types/input/fly-controller.d.ts +42 -0
- package/types/input/input.d.ts +192 -0
- package/types/input/math.d.ts +1 -0
- package/types/input/orbit-controller.d.ts +63 -0
- package/types/input/pose.d.ts +126 -0
- package/types/inspector.d.ts +1304 -0
- package/types/instance-survey.d.ts +35 -0
- package/types/list-view.d.ts +151 -0
- package/types/material-view.d.ts +63 -0
- package/types/memory-view.d.ts +95 -0
- package/types/mesh-view.d.ts +89 -0
- package/types/model.d.ts +104 -0
- package/types/node-model.d.ts +54 -0
- package/types/physics-debug.d.ts +254 -0
- package/types/physics-view.d.ts +81 -0
- package/types/picker/picker.d.ts +255 -0
- package/types/picker/render-pass-picker.d.ts +47 -0
- package/types/property-view.d.ts +189 -0
- package/types/render-target-view.d.ts +98 -0
- package/types/renderers/texture-renderer-shaders.d.ts +9 -0
- package/types/renderers/texture-renderer.d.ts +231 -0
- package/types/renderers/wire-renderer.d.ts +347 -0
- package/types/script-view.d.ts +56 -0
- package/types/shader-view.d.ts +46 -0
- package/types/styles.d.ts +22 -0
- package/types/texture-view.d.ts +30 -0
- package/types/tooltip.d.ts +17 -0
- package/types/viewport-tools.d.ts +237 -0
|
@@ -0,0 +1,954 @@
|
|
|
1
|
+
import { Color, Mat4, Vec3, VIEW_CENTER } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { Debug } from '../debug.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { AppBase } from 'playcanvas'
|
|
7
|
+
* @import { BoundingBox } from 'playcanvas'
|
|
8
|
+
* @import { CameraComponent } from 'playcanvas'
|
|
9
|
+
* @import { Layer } from 'playcanvas'
|
|
10
|
+
* @import { LightComponent } from 'playcanvas'
|
|
11
|
+
* @import { LineWriter } from 'playcanvas'
|
|
12
|
+
* @import { OrientedBox } from 'playcanvas'
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const _u = new Vec3();
|
|
16
|
+
const _v = new Vec3();
|
|
17
|
+
const _dir = new Vec3();
|
|
18
|
+
const _pos = new Vec3();
|
|
19
|
+
const _head = new Vec3();
|
|
20
|
+
const _lightDir = new Vec3();
|
|
21
|
+
const _mat = new Mat4();
|
|
22
|
+
const _view = new Mat4();
|
|
23
|
+
const _proj = new Mat4();
|
|
24
|
+
const _color = new Color();
|
|
25
|
+
|
|
26
|
+
const DEG_TO_RAD = Math.PI / 180;
|
|
27
|
+
|
|
28
|
+
// the 8 corners of a frustum in normalized device coordinates, near plane first
|
|
29
|
+
const NDC_CORNERS = [
|
|
30
|
+
-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
|
|
31
|
+
-1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// index pairs forming the 12 edges of the corners above
|
|
35
|
+
const FRUSTUM_EDGES = [
|
|
36
|
+
0, 1, 1, 2, 2, 3, 3, 0,
|
|
37
|
+
4, 5, 5, 6, 6, 7, 7, 4,
|
|
38
|
+
0, 4, 1, 5, 2, 6, 3, 7
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
// scratch storage for the unprojected frustum corners
|
|
42
|
+
const _corners = [];
|
|
43
|
+
for (let i = 0; i < 8; i++) {
|
|
44
|
+
_corners.push(new Vec3());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Builds an arbitrary orthonormal basis perpendicular to the supplied direction, storing the two
|
|
49
|
+
* tangents in `_u` and `_v`. The direction is expected to be normalized.
|
|
50
|
+
*
|
|
51
|
+
* The choice of basis is not continuous as `dir` rotates - it cannot be, as no continuous tangent
|
|
52
|
+
* field exists over a whole sphere - so it swings around as `dir.x` passes the threshold below.
|
|
53
|
+
* That is invisible for the rings of a circle, cylinder, capsule or cone, and for the barbs of an
|
|
54
|
+
* arrow, all of which are symmetric about their axis. It is visible for {@link WireRenderer#plane},
|
|
55
|
+
* whose square has a definite orientation.
|
|
56
|
+
*
|
|
57
|
+
* @param {Vec3} dir - The normalized direction to build a basis around.
|
|
58
|
+
* @ignore
|
|
59
|
+
*/
|
|
60
|
+
const buildBasis = (dir) => {
|
|
61
|
+
// pick the world axis least aligned with dir, so the cross product stays well conditioned
|
|
62
|
+
if (Math.abs(dir.x) < 0.5) {
|
|
63
|
+
_u.set(1, 0, 0);
|
|
64
|
+
} else {
|
|
65
|
+
_u.set(0, 1, 0);
|
|
66
|
+
}
|
|
67
|
+
_v.cross(dir, _u).normalize();
|
|
68
|
+
_u.cross(_v, dir).normalize();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Renders wireframe shapes for a single frame, for debugging and visualization. Shapes are
|
|
73
|
+
* submitted as line segments to the layer given by {@link WireRenderer#layer}, which defaults to
|
|
74
|
+
* the {@link LAYERID_IMMEDIATE} layer, and are discarded once the frame has been rendered, so
|
|
75
|
+
* they must be issued again on every frame they should be visible.
|
|
76
|
+
*
|
|
77
|
+
* The renderer holds the state used by the shapes it draws - {@link WireRenderer#color},
|
|
78
|
+
* {@link WireRenderer#layer}, {@link WireRenderer#depthTest}, {@link WireRenderer#segments} and
|
|
79
|
+
* {@link WireRenderer#transform}. Fields can be assigned between calls, and drawing many shapes
|
|
80
|
+
* with the same state allocates nothing:
|
|
81
|
+
*
|
|
82
|
+
* ```javascript
|
|
83
|
+
* const wire = new WireRenderer(app);
|
|
84
|
+
* wire.color = Color.RED;
|
|
85
|
+
*
|
|
86
|
+
* app.on('update', () => {
|
|
87
|
+
* for (const item of items) {
|
|
88
|
+
* wire.sphere(item.position, item.radius);
|
|
89
|
+
* }
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* A second set of state is simply a second instance. Instances hold no GPU resources, and those
|
|
94
|
+
* sharing a layer and depth test mode submit into the same batch, so using several has no
|
|
95
|
+
* additional rendering cost:
|
|
96
|
+
*
|
|
97
|
+
* ```javascript
|
|
98
|
+
* const xray = new WireRenderer(app);
|
|
99
|
+
* xray.depthTest = false;
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* These are thin lines, one pixel wide. For thick lines with caps, joins and dashes, intended as
|
|
103
|
+
* part of the rendered scene rather than as a debugging aid, see {@link WideLineRenderer} instead.
|
|
104
|
+
*
|
|
105
|
+
* @category Graphics
|
|
106
|
+
*/
|
|
107
|
+
class WireRenderer {
|
|
108
|
+
/**
|
|
109
|
+
* The color used by shapes, specified in sRGB color space. The alpha component is respected.
|
|
110
|
+
* Defaults to white.
|
|
111
|
+
*
|
|
112
|
+
* @type {Color}
|
|
113
|
+
*/
|
|
114
|
+
color = new Color(1, 1, 1, 1);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The layer shapes are rendered into, or null to use the {@link LAYERID_IMMEDIATE} layer.
|
|
118
|
+
* Defaults to null.
|
|
119
|
+
*
|
|
120
|
+
* @type {Layer|null}
|
|
121
|
+
*/
|
|
122
|
+
layer = null;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether shapes are depth tested against the depth buffer. Defaults to true.
|
|
126
|
+
*
|
|
127
|
+
* @type {boolean}
|
|
128
|
+
*/
|
|
129
|
+
depthTest = true;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The number of line segments used to approximate a full circle. Defaults to 20.
|
|
133
|
+
*
|
|
134
|
+
* @type {number}
|
|
135
|
+
*/
|
|
136
|
+
segments = 20;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A matrix applied to every point of every shape, or null for no transform. Assign this to
|
|
140
|
+
* draw a group of shapes in the local space of a node. Defaults to null.
|
|
141
|
+
*
|
|
142
|
+
* @type {Mat4|null}
|
|
143
|
+
*/
|
|
144
|
+
transform = null;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Creates a new WireRenderer instance.
|
|
148
|
+
*
|
|
149
|
+
* @param {AppBase} app - The application.
|
|
150
|
+
* @example
|
|
151
|
+
* const wire = new WireRenderer(app);
|
|
152
|
+
*/
|
|
153
|
+
constructor(app) {
|
|
154
|
+
Debug.assert(app, 'WireRenderer requires an application.');
|
|
155
|
+
this.app = app;
|
|
156
|
+
/** @private */
|
|
157
|
+
this._scene = app.scene;
|
|
158
|
+
/** @private */
|
|
159
|
+
this._immediate = app.scene.immediate;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Cursor for the shape currently being written, valid only for the duration of one call.
|
|
163
|
+
*
|
|
164
|
+
* @type {LineWriter|null}
|
|
165
|
+
* @private
|
|
166
|
+
*/
|
|
167
|
+
this._writer = null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Allocates room for a shape and points the cursor at it. The count must match exactly what
|
|
172
|
+
* the shape goes on to write.
|
|
173
|
+
*
|
|
174
|
+
* @param {number} vertexCount - The number of vertices the shape writes. Two per segment.
|
|
175
|
+
* @ignore
|
|
176
|
+
*/
|
|
177
|
+
_begin(vertexCount) {
|
|
178
|
+
this._writer = this._immediate.allocateLines(
|
|
179
|
+
vertexCount, this.color, this.depthTest, this.layer ?? this._scene.defaultDrawLayer);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @returns {number} The circle segment count, clamped to a usable range.
|
|
184
|
+
* @ignore
|
|
185
|
+
*/
|
|
186
|
+
_steps() {
|
|
187
|
+
return Math.max(3, Math.floor(this.segments));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Writes a single segment, applying {@link WireRenderer#transform}.
|
|
192
|
+
*
|
|
193
|
+
* @param {number} x0 - The start x coordinate.
|
|
194
|
+
* @param {number} y0 - The start y coordinate.
|
|
195
|
+
* @param {number} z0 - The start z coordinate.
|
|
196
|
+
* @param {number} x1 - The end x coordinate.
|
|
197
|
+
* @param {number} y1 - The end y coordinate.
|
|
198
|
+
* @param {number} z1 - The end z coordinate.
|
|
199
|
+
* @ignore
|
|
200
|
+
*/
|
|
201
|
+
_segment(x0, y0, z0, x1, y1, z1) {
|
|
202
|
+
const m = this.transform;
|
|
203
|
+
if (m) {
|
|
204
|
+
const d = m.data;
|
|
205
|
+
this._writer.segment(
|
|
206
|
+
d[0] * x0 + d[4] * y0 + d[8] * z0 + d[12],
|
|
207
|
+
d[1] * x0 + d[5] * y0 + d[9] * z0 + d[13],
|
|
208
|
+
d[2] * x0 + d[6] * y0 + d[10] * z0 + d[14],
|
|
209
|
+
d[0] * x1 + d[4] * y1 + d[8] * z1 + d[12],
|
|
210
|
+
d[1] * x1 + d[5] * y1 + d[9] * z1 + d[13],
|
|
211
|
+
d[2] * x1 + d[6] * y1 + d[10] * z1 + d[14]
|
|
212
|
+
);
|
|
213
|
+
} else {
|
|
214
|
+
this._writer.segment(x0, y0, z0, x1, y1, z1);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Writes a single vertex with its own color, applying {@link WireRenderer#transform}.
|
|
220
|
+
*
|
|
221
|
+
* @param {number} x - The x coordinate.
|
|
222
|
+
* @param {number} y - The y coordinate.
|
|
223
|
+
* @param {number} z - The z coordinate.
|
|
224
|
+
* @param {Color} color - The color of the vertex.
|
|
225
|
+
* @ignore
|
|
226
|
+
*/
|
|
227
|
+
_vertex(x, y, z, color) {
|
|
228
|
+
const m = this.transform;
|
|
229
|
+
if (m) {
|
|
230
|
+
const d = m.data;
|
|
231
|
+
this._writer.vertex(
|
|
232
|
+
d[0] * x + d[4] * y + d[8] * z + d[12],
|
|
233
|
+
d[1] * x + d[5] * y + d[9] * z + d[13],
|
|
234
|
+
d[2] * x + d[6] * y + d[10] * z + d[14],
|
|
235
|
+
color.r, color.g, color.b, color.a
|
|
236
|
+
);
|
|
237
|
+
} else {
|
|
238
|
+
this._writer.vertex(x, y, z, color.r, color.g, color.b, color.a);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Appends a circular arc lying in the plane spanned by the two supplied tangents.
|
|
244
|
+
*
|
|
245
|
+
* @param {Vec3} center - The center of the arc.
|
|
246
|
+
* @param {Vec3} u - The first tangent of the arc plane, normalized.
|
|
247
|
+
* @param {Vec3} v - The second tangent of the arc plane, normalized.
|
|
248
|
+
* @param {number} radius - The radius of the arc.
|
|
249
|
+
* @param {number} steps - The number of segments used by the arc.
|
|
250
|
+
* @param {number} [start] - The start angle in radians. Defaults to 0.
|
|
251
|
+
* @param {number} [sweep] - The swept angle in radians. Defaults to a full circle.
|
|
252
|
+
* @ignore
|
|
253
|
+
*/
|
|
254
|
+
_arc(center, u, v, radius, steps, start = 0, sweep = Math.PI * 2) {
|
|
255
|
+
|
|
256
|
+
// this is the hot path - it backs sphere, circle, cylinder, capsule, cone and arrow, so
|
|
257
|
+
// the overwhelming majority of segments come through here. It writes the batch storage
|
|
258
|
+
// itself rather than calling LineWriter#segment per segment, which measured about a
|
|
259
|
+
// quarter of the cost of generating a shape. The transform test is hoisted out of the
|
|
260
|
+
// loop for the same reason.
|
|
261
|
+
const writer = this._writer;
|
|
262
|
+
const positions = writer.positions;
|
|
263
|
+
const colors = writer.colors;
|
|
264
|
+
let k = writer.cursor;
|
|
265
|
+
|
|
266
|
+
Debug.assert(k + steps * 2 <= writer.end,
|
|
267
|
+
'WireRenderer#_arc would write past the end of its allocation.');
|
|
268
|
+
|
|
269
|
+
const { r, g, b, a } = this.color;
|
|
270
|
+
const m = this.transform;
|
|
271
|
+
const d = m ? m.data : null;
|
|
272
|
+
|
|
273
|
+
// the center and both tangents are hoisted into locals, as reading them off the Vec3
|
|
274
|
+
// instances inside the loop costs more than the write itself
|
|
275
|
+
const cx = center.x, cy = center.y, cz = center.z;
|
|
276
|
+
const ux = u.x, uy = u.y, uz = u.z;
|
|
277
|
+
const vx = v.x, vy = v.y, vz = v.z;
|
|
278
|
+
|
|
279
|
+
const step = sweep / steps;
|
|
280
|
+
let angle = start;
|
|
281
|
+
let c0 = Math.cos(angle) * radius;
|
|
282
|
+
let s0 = Math.sin(angle) * radius;
|
|
283
|
+
|
|
284
|
+
for (let i = 0; i < steps; i++) {
|
|
285
|
+
angle += step;
|
|
286
|
+
const c1 = Math.cos(angle) * radius;
|
|
287
|
+
const s1 = Math.sin(angle) * radius;
|
|
288
|
+
|
|
289
|
+
let x0 = cx + ux * c0 + vx * s0;
|
|
290
|
+
let y0 = cy + uy * c0 + vy * s0;
|
|
291
|
+
let z0 = cz + uz * c0 + vz * s0;
|
|
292
|
+
let x1 = cx + ux * c1 + vx * s1;
|
|
293
|
+
let y1 = cy + uy * c1 + vy * s1;
|
|
294
|
+
let z1 = cz + uz * c1 + vz * s1;
|
|
295
|
+
|
|
296
|
+
if (d) {
|
|
297
|
+
const ax = x0, ay = y0, az = z0;
|
|
298
|
+
const bx = x1, by = y1, bz = z1;
|
|
299
|
+
x0 = d[0] * ax + d[4] * ay + d[8] * az + d[12];
|
|
300
|
+
y0 = d[1] * ax + d[5] * ay + d[9] * az + d[13];
|
|
301
|
+
z0 = d[2] * ax + d[6] * ay + d[10] * az + d[14];
|
|
302
|
+
x1 = d[0] * bx + d[4] * by + d[8] * bz + d[12];
|
|
303
|
+
y1 = d[1] * bx + d[5] * by + d[9] * bz + d[13];
|
|
304
|
+
z1 = d[2] * bx + d[6] * by + d[10] * bz + d[14];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let p = k * 3;
|
|
308
|
+
positions[p++] = x0;
|
|
309
|
+
positions[p++] = y0;
|
|
310
|
+
positions[p++] = z0;
|
|
311
|
+
positions[p++] = x1;
|
|
312
|
+
positions[p++] = y1;
|
|
313
|
+
positions[p] = z1;
|
|
314
|
+
|
|
315
|
+
let c = k * 4;
|
|
316
|
+
colors[c++] = r;
|
|
317
|
+
colors[c++] = g;
|
|
318
|
+
colors[c++] = b;
|
|
319
|
+
colors[c++] = a;
|
|
320
|
+
colors[c++] = r;
|
|
321
|
+
colors[c++] = g;
|
|
322
|
+
colors[c++] = b;
|
|
323
|
+
colors[c] = a;
|
|
324
|
+
|
|
325
|
+
k += 2;
|
|
326
|
+
c0 = c1;
|
|
327
|
+
s0 = s1;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
writer.cursor = k;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Writes a list of points, expanding a strip into discrete segments when requested.
|
|
335
|
+
*
|
|
336
|
+
* @param {Vec3[]} positions - The points to write.
|
|
337
|
+
* @param {Color[]|undefined} colors - One color per point, or undefined to use
|
|
338
|
+
* {@link WireRenderer#color}.
|
|
339
|
+
* @param {boolean} strip - True to connect consecutive points, false to treat them as pairs.
|
|
340
|
+
* @param {boolean} closed - True to also connect the last point back to the first. Only used
|
|
341
|
+
* when `strip` is true.
|
|
342
|
+
* @ignore
|
|
343
|
+
*/
|
|
344
|
+
_writePoints(positions, colors, strip, closed) {
|
|
345
|
+
const count = positions.length;
|
|
346
|
+
|
|
347
|
+
Debug.assert(!colors || colors.length === count,
|
|
348
|
+
`WireRenderer: expected ${count} colors to match the supplied positions, got ${colors?.length}.`);
|
|
349
|
+
|
|
350
|
+
const segments = strip ? (closed ? count : count - 1) : count / 2;
|
|
351
|
+
this._begin(segments * 2);
|
|
352
|
+
|
|
353
|
+
if (colors) {
|
|
354
|
+
// one color per point, so each end of a segment is written on its own
|
|
355
|
+
if (strip) {
|
|
356
|
+
for (let i = 0; i < segments; i++) {
|
|
357
|
+
const next = (i + 1) % count;
|
|
358
|
+
this._vertex(positions[i].x, positions[i].y, positions[i].z, colors[i]);
|
|
359
|
+
this._vertex(positions[next].x, positions[next].y, positions[next].z, colors[next]);
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
for (let i = 0; i < count; i++) {
|
|
363
|
+
this._vertex(positions[i].x, positions[i].y, positions[i].z, colors[i]);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
} else if (strip) {
|
|
367
|
+
for (let i = 0; i < segments; i++) {
|
|
368
|
+
const a = positions[i];
|
|
369
|
+
const b = positions[(i + 1) % count];
|
|
370
|
+
this._segment(a.x, a.y, a.z, b.x, b.y, b.z);
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
for (let i = 0; i < count; i += 2) {
|
|
374
|
+
const a = positions[i];
|
|
375
|
+
const b = positions[i + 1];
|
|
376
|
+
this._segment(a.x, a.y, a.z, b.x, b.y, b.z);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Renders a single line segment.
|
|
383
|
+
*
|
|
384
|
+
* @param {Vec3} start - The start of the line, in world space.
|
|
385
|
+
* @param {Vec3} end - The end of the line, in world space.
|
|
386
|
+
* @example
|
|
387
|
+
* wire.line(new Vec3(0, 0, 0), new Vec3(0, 1, 0));
|
|
388
|
+
*/
|
|
389
|
+
line(start, end) {
|
|
390
|
+
this._begin(2);
|
|
391
|
+
this._segment(start.x, start.y, start.z, end.x, end.y, end.z);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Renders discrete line segments, formed by consecutive pairs of points.
|
|
396
|
+
*
|
|
397
|
+
* @param {Vec3[]} positions - The points to draw lines between. The length must be a multiple
|
|
398
|
+
* of two.
|
|
399
|
+
* @param {Color[]} [colors] - One color per point, or undefined to use
|
|
400
|
+
* {@link WireRenderer#color}. The color of each segment is interpolated between its ends.
|
|
401
|
+
* @example
|
|
402
|
+
* wire.lines([start, end], [Color.RED, Color.WHITE]);
|
|
403
|
+
*/
|
|
404
|
+
lines(positions, colors) {
|
|
405
|
+
Debug.assert(positions.length % 2 === 0,
|
|
406
|
+
'WireRenderer#lines requires an even number of positions.');
|
|
407
|
+
Debug.assert(colors === undefined || Array.isArray(colors),
|
|
408
|
+
'WireRenderer#lines colors must be an array of Color, or undefined to use the renderer color.');
|
|
409
|
+
|
|
410
|
+
// pass user data straight through when it needs no transform
|
|
411
|
+
if (!this.transform) {
|
|
412
|
+
this.app.drawLines(positions, colors ?? this.color, this.depthTest, this.layer ?? undefined);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
this._writePoints(positions, colors, false, false);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Renders discrete line segments from packed arrays of numbers. This is the fastest of the
|
|
421
|
+
* line functions, as it avoids reading individual {@link Vec3} and {@link Color} instances.
|
|
422
|
+
*
|
|
423
|
+
* @param {number[]|Float32Array} positions - Packed xyz coordinates, forming pairs of points.
|
|
424
|
+
* @param {number[]|Float32Array} [colors] - Packed rgba values, one color per point, or
|
|
425
|
+
* undefined to use {@link WireRenderer#color}.
|
|
426
|
+
* @example
|
|
427
|
+
* wire.linesPacked([0, 0, 0, 0, 1, 0]);
|
|
428
|
+
*/
|
|
429
|
+
linesPacked(positions, colors) {
|
|
430
|
+
Debug.assert(positions.length % 6 === 0,
|
|
431
|
+
'WireRenderer#linesPacked requires three values per position and two positions per segment, so the length must be a multiple of six.');
|
|
432
|
+
Debug.assert(colors === undefined || colors.length === (positions.length / 3) * 4,
|
|
433
|
+
'WireRenderer#linesPacked colors must hold four values per position, or be undefined.');
|
|
434
|
+
|
|
435
|
+
if (!this.transform) {
|
|
436
|
+
this.app.drawLineArrays(positions, colors ?? this.color, this.depthTest, this.layer ?? undefined);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const count = positions.length / 3;
|
|
441
|
+
this._begin(count);
|
|
442
|
+
|
|
443
|
+
if (colors) {
|
|
444
|
+
for (let i = 0, p = 0, c = 0; i < count; i++, p += 3, c += 4) {
|
|
445
|
+
_color.set(colors[c], colors[c + 1], colors[c + 2], colors[c + 3]);
|
|
446
|
+
this._vertex(positions[p], positions[p + 1], positions[p + 2], _color);
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
for (let i = 0, p = 0; i < count; i += 2, p += 6) {
|
|
450
|
+
this._segment(
|
|
451
|
+
positions[p], positions[p + 1], positions[p + 2],
|
|
452
|
+
positions[p + 3], positions[p + 4], positions[p + 5]
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Renders an open strip of connected line segments.
|
|
460
|
+
*
|
|
461
|
+
* @param {Vec3[]} positions - The points of the strip, in order.
|
|
462
|
+
* @param {Color[]} [colors] - One color per point, or undefined to use
|
|
463
|
+
* {@link WireRenderer#color}.
|
|
464
|
+
* @example
|
|
465
|
+
* wire.polyline(trajectory);
|
|
466
|
+
*/
|
|
467
|
+
polyline(positions, colors) {
|
|
468
|
+
Debug.assert(colors === undefined || Array.isArray(colors),
|
|
469
|
+
'WireRenderer#polyline colors must be an array of Color, or undefined to use the renderer color.');
|
|
470
|
+
|
|
471
|
+
if (positions.length < 2) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
this._writePoints(positions, colors, true, false);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Renders a closed strip of connected line segments, joining the last point back to the first.
|
|
480
|
+
*
|
|
481
|
+
* @param {Vec3[]} positions - The points of the loop, in order.
|
|
482
|
+
* @param {Color[]} [colors] - One color per point, or undefined to use
|
|
483
|
+
* {@link WireRenderer#color}.
|
|
484
|
+
* @example
|
|
485
|
+
* wire.loop(outline);
|
|
486
|
+
*/
|
|
487
|
+
loop(positions, colors) {
|
|
488
|
+
Debug.assert(colors === undefined || Array.isArray(colors),
|
|
489
|
+
'WireRenderer#loop colors must be an array of Color, or undefined to use the renderer color.');
|
|
490
|
+
|
|
491
|
+
if (positions.length < 2) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
this._writePoints(positions, colors, true, true);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Renders the edges of a box specified by its min and max corners.
|
|
500
|
+
*
|
|
501
|
+
* @param {Vec3} min - The min corner of the box.
|
|
502
|
+
* @param {Vec3} max - The max corner of the box.
|
|
503
|
+
* @example
|
|
504
|
+
* wire.boxMinMax(new Vec3(-1, -1, -1), new Vec3(1, 1, 1));
|
|
505
|
+
*/
|
|
506
|
+
boxMinMax(min, max) {
|
|
507
|
+
this._begin(12 * 2);
|
|
508
|
+
|
|
509
|
+
const { x: ax, y: ay, z: az } = min;
|
|
510
|
+
const { x: bx, y: by, z: bz } = max;
|
|
511
|
+
|
|
512
|
+
// near face
|
|
513
|
+
this._segment(ax, ay, az, bx, ay, az);
|
|
514
|
+
this._segment(bx, ay, az, bx, by, az);
|
|
515
|
+
this._segment(bx, by, az, ax, by, az);
|
|
516
|
+
this._segment(ax, by, az, ax, ay, az);
|
|
517
|
+
|
|
518
|
+
// far face
|
|
519
|
+
this._segment(ax, ay, bz, bx, ay, bz);
|
|
520
|
+
this._segment(bx, ay, bz, bx, by, bz);
|
|
521
|
+
this._segment(bx, by, bz, ax, by, bz);
|
|
522
|
+
this._segment(ax, by, bz, ax, ay, bz);
|
|
523
|
+
|
|
524
|
+
// connecting edges
|
|
525
|
+
this._segment(ax, ay, az, ax, ay, bz);
|
|
526
|
+
this._segment(bx, ay, az, bx, ay, bz);
|
|
527
|
+
this._segment(bx, by, az, bx, by, bz);
|
|
528
|
+
this._segment(ax, by, az, ax, by, bz);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Renders the edges of a bounding box. An {@link OrientedBox} is rendered in its own
|
|
533
|
+
* orientation, composed with {@link WireRenderer#transform}.
|
|
534
|
+
*
|
|
535
|
+
* @param {BoundingBox|OrientedBox} box - The box to render.
|
|
536
|
+
* @example
|
|
537
|
+
* wire.box(meshInstance.aabb);
|
|
538
|
+
*/
|
|
539
|
+
box(box) {
|
|
540
|
+
const orientation = /** @type {OrientedBox} */ (box).worldTransform;
|
|
541
|
+
|
|
542
|
+
if (orientation) {
|
|
543
|
+
// an oriented box is a unit-centered box in the space of its own transform
|
|
544
|
+
const outer = this.transform;
|
|
545
|
+
this.transform = outer ? _mat.mul2(outer, orientation) : orientation;
|
|
546
|
+
|
|
547
|
+
const he = box.halfExtents;
|
|
548
|
+
_u.set(-he.x, -he.y, -he.z);
|
|
549
|
+
_v.set(he.x, he.y, he.z);
|
|
550
|
+
this.boxMinMax(_u, _v);
|
|
551
|
+
|
|
552
|
+
this.transform = outer;
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
this.boxMinMax(box.getMin(), box.getMax());
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Renders a sphere as three great circles, one in each of the primary planes.
|
|
561
|
+
*
|
|
562
|
+
* @param {Vec3} center - The center of the sphere.
|
|
563
|
+
* @param {number} radius - The radius of the sphere.
|
|
564
|
+
* @example
|
|
565
|
+
* wire.sphere(new Vec3(0, 1, 0), 0.5);
|
|
566
|
+
*/
|
|
567
|
+
sphere(center, radius) {
|
|
568
|
+
const steps = this._steps();
|
|
569
|
+
this._begin(steps * 3 * 2);
|
|
570
|
+
|
|
571
|
+
_u.set(1, 0, 0);
|
|
572
|
+
_v.set(0, 1, 0);
|
|
573
|
+
this._arc(center, _u, _v, radius, steps);
|
|
574
|
+
|
|
575
|
+
_u.set(1, 0, 0);
|
|
576
|
+
_v.set(0, 0, 1);
|
|
577
|
+
this._arc(center, _u, _v, radius, steps);
|
|
578
|
+
|
|
579
|
+
_u.set(0, 1, 0);
|
|
580
|
+
_v.set(0, 0, 1);
|
|
581
|
+
this._arc(center, _u, _v, radius, steps);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Renders a circle lying in the plane described by a normal.
|
|
586
|
+
*
|
|
587
|
+
* @param {Vec3} center - The center of the circle.
|
|
588
|
+
* @param {Vec3} normal - The normal of the plane containing the circle. Need not be
|
|
589
|
+
* normalized.
|
|
590
|
+
* @param {number} radius - The radius of the circle.
|
|
591
|
+
* @example
|
|
592
|
+
* wire.circle(Vec3.ZERO, Vec3.UP, 5);
|
|
593
|
+
*/
|
|
594
|
+
circle(center, normal, radius) {
|
|
595
|
+
const steps = this._steps();
|
|
596
|
+
this._begin(steps * 2);
|
|
597
|
+
|
|
598
|
+
_dir.copy(normal).normalize();
|
|
599
|
+
buildBasis(_dir);
|
|
600
|
+
this._arc(center, _u, _v, radius, steps);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Renders a cylinder as a ring at each end joined by four side lines.
|
|
605
|
+
*
|
|
606
|
+
* @param {Vec3} start - The center of the start cap.
|
|
607
|
+
* @param {Vec3} end - The center of the end cap.
|
|
608
|
+
* @param {number} radius - The radius of the cylinder.
|
|
609
|
+
* @example
|
|
610
|
+
* wire.cylinder(base, tip, 0.5);
|
|
611
|
+
*/
|
|
612
|
+
cylinder(start, end, radius) {
|
|
613
|
+
_dir.sub2(end, start);
|
|
614
|
+
if (_dir.length() < 1e-6) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
_dir.normalize();
|
|
618
|
+
buildBasis(_dir);
|
|
619
|
+
|
|
620
|
+
const steps = this._steps();
|
|
621
|
+
this._begin((steps * 2 + 4) * 2);
|
|
622
|
+
|
|
623
|
+
this._arc(start, _u, _v, radius, steps);
|
|
624
|
+
this._arc(end, _u, _v, radius, steps);
|
|
625
|
+
this._sideLines(start, end, radius);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Appends the four longitudinal lines joining two rings, using the basis in `_u` and `_v`.
|
|
630
|
+
*
|
|
631
|
+
* @param {Vec3} start - The center of the start ring.
|
|
632
|
+
* @param {Vec3} end - The center of the end ring.
|
|
633
|
+
* @param {number} radius - The radius of both rings.
|
|
634
|
+
* @ignore
|
|
635
|
+
*/
|
|
636
|
+
_sideLines(start, end, radius) {
|
|
637
|
+
for (let i = 0; i < 4; i++) {
|
|
638
|
+
const t = i & 1 ? _v : _u;
|
|
639
|
+
const s = i < 2 ? radius : -radius;
|
|
640
|
+
this._segment(
|
|
641
|
+
start.x + t.x * s, start.y + t.y * s, start.z + t.z * s,
|
|
642
|
+
end.x + t.x * s, end.y + t.y * s, end.z + t.z * s
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Renders a capsule as a ring and hemispherical cap at each end, joined by four side lines.
|
|
649
|
+
*
|
|
650
|
+
* @param {Vec3} start - The center of the start cap sphere.
|
|
651
|
+
* @param {Vec3} end - The center of the end cap sphere.
|
|
652
|
+
* @param {number} radius - The radius of the capsule.
|
|
653
|
+
* @example
|
|
654
|
+
* wire.capsule(feet, head, 0.4);
|
|
655
|
+
*/
|
|
656
|
+
capsule(start, end, radius) {
|
|
657
|
+
_dir.sub2(end, start);
|
|
658
|
+
if (_dir.length() < 1e-6) {
|
|
659
|
+
this.sphere(start, radius);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
_dir.normalize();
|
|
663
|
+
buildBasis(_dir);
|
|
664
|
+
|
|
665
|
+
const steps = this._steps();
|
|
666
|
+
const half = Math.max(2, steps >> 1);
|
|
667
|
+
this._begin((steps * 2 + 4 + half * 4) * 2);
|
|
668
|
+
|
|
669
|
+
this._arc(start, _u, _v, radius, steps);
|
|
670
|
+
this._arc(end, _u, _v, radius, steps);
|
|
671
|
+
this._sideLines(start, end, radius);
|
|
672
|
+
|
|
673
|
+
// hemispherical caps, two half arcs bending away from the body at each end
|
|
674
|
+
this._arc(end, _u, _dir, radius, half, 0, Math.PI);
|
|
675
|
+
this._arc(end, _v, _dir, radius, half, 0, Math.PI);
|
|
676
|
+
this._arc(start, _u, _dir, radius, half, Math.PI, Math.PI);
|
|
677
|
+
this._arc(start, _v, _dir, radius, half, Math.PI, Math.PI);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Renders a cone as a base ring joined to its apex by four side lines. The parameters match
|
|
682
|
+
* those describing a spot light, so a light's cone can be visualized directly.
|
|
683
|
+
*
|
|
684
|
+
* @param {Vec3} apex - The tip of the cone.
|
|
685
|
+
* @param {Vec3} direction - The direction the cone opens along. Need not be normalized.
|
|
686
|
+
* @param {number} angle - The half-angle of the cone, in degrees, measured from `direction` to
|
|
687
|
+
* the cone edge.
|
|
688
|
+
* @param {number} length - The distance from the apex to the base.
|
|
689
|
+
* @example
|
|
690
|
+
* wire.cone(position, direction, 30, 10);
|
|
691
|
+
*/
|
|
692
|
+
cone(apex, direction, angle, length) {
|
|
693
|
+
_dir.copy(direction);
|
|
694
|
+
if (_dir.length() < 1e-6) {
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
_dir.normalize();
|
|
698
|
+
buildBasis(_dir);
|
|
699
|
+
|
|
700
|
+
const radius = length * Math.tan(Math.min(angle, 89.9) * DEG_TO_RAD);
|
|
701
|
+
_pos.copy(_dir).mulScalar(length).add(apex);
|
|
702
|
+
|
|
703
|
+
const steps = this._steps();
|
|
704
|
+
this._begin((steps + 4) * 2);
|
|
705
|
+
|
|
706
|
+
this._arc(_pos, _u, _v, radius, steps);
|
|
707
|
+
|
|
708
|
+
for (let i = 0; i < 4; i++) {
|
|
709
|
+
const t = i & 1 ? _v : _u;
|
|
710
|
+
const s = i < 2 ? radius : -radius;
|
|
711
|
+
this._segment(
|
|
712
|
+
apex.x, apex.y, apex.z,
|
|
713
|
+
_pos.x + t.x * s, _pos.y + t.y * s, _pos.z + t.z * s
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Renders a square section of a plane, with a short stub along its normal.
|
|
720
|
+
*
|
|
721
|
+
* The rotation of the square within its plane is derived from the normal, and no such
|
|
722
|
+
* derivation is continuous over all directions. An animated normal will therefore make the
|
|
723
|
+
* square appear to jump as it passes the direction where the derivation switches. To rotate a
|
|
724
|
+
* square smoothly, pass a fixed normal and drive {@link WireRenderer#transform} instead.
|
|
725
|
+
*
|
|
726
|
+
* @param {Vec3} center - The center of the square.
|
|
727
|
+
* @param {Vec3} normal - The normal of the plane. Need not be normalized.
|
|
728
|
+
* @param {number} size - The side length of the square.
|
|
729
|
+
* @example
|
|
730
|
+
* wire.plane(Vec3.ZERO, Vec3.UP, 10);
|
|
731
|
+
*/
|
|
732
|
+
plane(center, normal, size) {
|
|
733
|
+
this._begin(5 * 2);
|
|
734
|
+
|
|
735
|
+
_dir.copy(normal).normalize();
|
|
736
|
+
buildBasis(_dir);
|
|
737
|
+
|
|
738
|
+
const h = size * 0.5;
|
|
739
|
+
const ax = _u.x * h, ay = _u.y * h, az = _u.z * h;
|
|
740
|
+
const bx = _v.x * h, by = _v.y * h, bz = _v.z * h;
|
|
741
|
+
|
|
742
|
+
this._segment(
|
|
743
|
+
center.x - ax - bx, center.y - ay - by, center.z - az - bz,
|
|
744
|
+
center.x + ax - bx, center.y + ay - by, center.z + az - bz
|
|
745
|
+
);
|
|
746
|
+
this._segment(
|
|
747
|
+
center.x + ax - bx, center.y + ay - by, center.z + az - bz,
|
|
748
|
+
center.x + ax + bx, center.y + ay + by, center.z + az + bz
|
|
749
|
+
);
|
|
750
|
+
this._segment(
|
|
751
|
+
center.x + ax + bx, center.y + ay + by, center.z + az + bz,
|
|
752
|
+
center.x - ax + bx, center.y - ay + by, center.z - az + bz
|
|
753
|
+
);
|
|
754
|
+
this._segment(
|
|
755
|
+
center.x - ax + bx, center.y - ay + by, center.z - az + bz,
|
|
756
|
+
center.x - ax - bx, center.y - ay - by, center.z - az - bz
|
|
757
|
+
);
|
|
758
|
+
|
|
759
|
+
this._segment(
|
|
760
|
+
center.x, center.y, center.z,
|
|
761
|
+
center.x + _dir.x * h, center.y + _dir.y * h, center.z + _dir.z * h
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Renders a small axis-aligned cross marking a position.
|
|
767
|
+
*
|
|
768
|
+
* @param {Vec3} position - The position to mark.
|
|
769
|
+
* @param {number} size - The overall length of each arm of the cross.
|
|
770
|
+
* @example
|
|
771
|
+
* wire.point(hit.point, 0.2);
|
|
772
|
+
*/
|
|
773
|
+
point(position, size) {
|
|
774
|
+
this._begin(3 * 2);
|
|
775
|
+
|
|
776
|
+
const h = size * 0.5;
|
|
777
|
+
const { x, y, z } = position;
|
|
778
|
+
|
|
779
|
+
this._segment(x - h, y, z, x + h, y, z);
|
|
780
|
+
this._segment(x, y - h, z, x, y + h, z);
|
|
781
|
+
this._segment(x, y, z - h, x, y, z + h);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Renders an arrow, as a shaft with four barbs at its tip.
|
|
786
|
+
*
|
|
787
|
+
* @param {Vec3} from - The tail of the arrow.
|
|
788
|
+
* @param {Vec3} to - The tip of the arrow.
|
|
789
|
+
* @example
|
|
790
|
+
* wire.arrow(position, position.clone().add(velocity));
|
|
791
|
+
*/
|
|
792
|
+
arrow(from, to) {
|
|
793
|
+
_dir.sub2(to, from);
|
|
794
|
+
const length = _dir.length();
|
|
795
|
+
if (length < 1e-6) {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
_dir.mulScalar(1 / length);
|
|
799
|
+
buildBasis(_dir);
|
|
800
|
+
|
|
801
|
+
const steps = this._steps();
|
|
802
|
+
this._begin((steps + 5) * 2);
|
|
803
|
+
|
|
804
|
+
this._segment(from.x, from.y, from.z, to.x, to.y, to.z);
|
|
805
|
+
|
|
806
|
+
// the head is a small cone rather than a few barbs, so it still reads as an arrow at a
|
|
807
|
+
// distance where isolated lines do not. `to` may alias the _pos scratch, so use _head.
|
|
808
|
+
const headLength = length * 0.2;
|
|
809
|
+
const headRadius = headLength * 0.5;
|
|
810
|
+
_head.copy(_dir).mulScalar(-headLength).add(to);
|
|
811
|
+
|
|
812
|
+
this._arc(_head, _u, _v, headRadius, steps);
|
|
813
|
+
|
|
814
|
+
for (let i = 0; i < 4; i++) {
|
|
815
|
+
const t = i & 1 ? _v : _u;
|
|
816
|
+
const s = i < 2 ? headRadius : -headRadius;
|
|
817
|
+
this._segment(
|
|
818
|
+
to.x, to.y, to.z,
|
|
819
|
+
_head.x + t.x * s, _head.y + t.y * s, _head.z + t.z * s
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Renders the three axes of a matrix, colored red, green and blue for x, y and z respectively.
|
|
826
|
+
* This function ignores {@link WireRenderer#color}.
|
|
827
|
+
*
|
|
828
|
+
* @param {Mat4} matrix - The transform whose axes are rendered.
|
|
829
|
+
* @param {number} size - The length of each axis.
|
|
830
|
+
* @example
|
|
831
|
+
* wire.axes(entity.getWorldTransform(), 1);
|
|
832
|
+
*/
|
|
833
|
+
axes(matrix, size) {
|
|
834
|
+
this._begin(3 * 2);
|
|
835
|
+
|
|
836
|
+
const d = matrix.data;
|
|
837
|
+
const ox = d[12], oy = d[13], oz = d[14];
|
|
838
|
+
|
|
839
|
+
for (let i = 0; i < 3; i++) {
|
|
840
|
+
const c = i * 4;
|
|
841
|
+
|
|
842
|
+
// both ends of an axis share its color, so it reads as a solid line
|
|
843
|
+
_color.set(i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0, 1);
|
|
844
|
+
this._vertex(ox, oy, oz, _color);
|
|
845
|
+
this._vertex(
|
|
846
|
+
ox + d[c] * size, oy + d[c + 1] * size, oz + d[c + 2] * size, _color
|
|
847
|
+
);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Renders the edges of a view frustum. The camera does not need to be enabled or rendering,
|
|
853
|
+
* so the view volume of an inactive camera can be visualized.
|
|
854
|
+
*
|
|
855
|
+
* @param {CameraComponent|Mat4} source - A camera, or a view-projection matrix.
|
|
856
|
+
* @example
|
|
857
|
+
* wire.frustum(otherCamera.camera);
|
|
858
|
+
*/
|
|
859
|
+
frustum(source) {
|
|
860
|
+
// a Component also has a `data` property, so discriminate on the camera API rather than
|
|
861
|
+
// on the shape of a Mat4
|
|
862
|
+
const camera = /** @type {CameraComponent} */ (source);
|
|
863
|
+
if (camera.projectionMatrix) {
|
|
864
|
+
|
|
865
|
+
// both matrices are built the way the renderer builds them, so the shape drawn is the
|
|
866
|
+
// volume the camera would actually render. In particular the view transform is
|
|
867
|
+
// scale-free, and both calculateProjection and calculateTransform are honored, which
|
|
868
|
+
// matters for a camera under a scaled parent or an oblique / reflection camera.
|
|
869
|
+
// Reading camera.viewMatrix instead is not an option: it is only refreshed for a
|
|
870
|
+
// camera that is being rendered, and visualizing a camera you are not looking
|
|
871
|
+
// through is the point of this function.
|
|
872
|
+
const projection = _proj.copy(camera.projectionMatrix);
|
|
873
|
+
camera.calculateProjection?.(projection, VIEW_CENTER);
|
|
874
|
+
|
|
875
|
+
if (camera.calculateTransform) {
|
|
876
|
+
camera.calculateTransform(_view, VIEW_CENTER);
|
|
877
|
+
} else {
|
|
878
|
+
const node = camera.entity;
|
|
879
|
+
_view.setTRS(node.getPosition(), node.getRotation(), Vec3.ONE);
|
|
880
|
+
}
|
|
881
|
+
_view.invert();
|
|
882
|
+
|
|
883
|
+
_mat.mul2(projection, _view);
|
|
884
|
+
} else {
|
|
885
|
+
_mat.copy(/** @type {Mat4} */ (source));
|
|
886
|
+
}
|
|
887
|
+
_mat.invert();
|
|
888
|
+
|
|
889
|
+
this._begin(12 * 2);
|
|
890
|
+
|
|
891
|
+
const d = _mat.data;
|
|
892
|
+
for (let i = 0; i < 8; i++) {
|
|
893
|
+
const x = NDC_CORNERS[i * 3];
|
|
894
|
+
const y = NDC_CORNERS[i * 3 + 1];
|
|
895
|
+
const z = NDC_CORNERS[i * 3 + 2];
|
|
896
|
+
|
|
897
|
+
// a perspective matrix needs the w divide, so this cannot use transformPoint
|
|
898
|
+
const iw = 1 / (d[3] * x + d[7] * y + d[11] * z + d[15]);
|
|
899
|
+
_corners[i].set(
|
|
900
|
+
(d[0] * x + d[4] * y + d[8] * z + d[12]) * iw,
|
|
901
|
+
(d[1] * x + d[5] * y + d[9] * z + d[13]) * iw,
|
|
902
|
+
(d[2] * x + d[6] * y + d[10] * z + d[14]) * iw
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
for (let i = 0; i < FRUSTUM_EDGES.length; i += 2) {
|
|
907
|
+
const a = _corners[FRUSTUM_EDGES[i]];
|
|
908
|
+
const b = _corners[FRUSTUM_EDGES[i + 1]];
|
|
909
|
+
this._segment(a.x, a.y, a.z, b.x, b.y, b.z);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Renders the shape and extent of a light, using the light's own color. An omni light is drawn
|
|
915
|
+
* as a sphere of its range, a spot light as its cone, and a directional light as an arrow
|
|
916
|
+
* showing the direction it shines in. A light shines along the negative y-axis of its entity,
|
|
917
|
+
* so the shape follows that axis rather than the entity's forward direction.
|
|
918
|
+
*
|
|
919
|
+
* @param {LightComponent} light - The light to render.
|
|
920
|
+
* @param {number} [size] - The length of the arrow used for a directional light, which has no
|
|
921
|
+
* inherent extent. Defaults to 1.
|
|
922
|
+
* @example
|
|
923
|
+
* wire.light(entity.light);
|
|
924
|
+
*/
|
|
925
|
+
light(light, size = 1) {
|
|
926
|
+
const entity = light.entity;
|
|
927
|
+
const position = entity.getPosition();
|
|
928
|
+
|
|
929
|
+
// a light shines along the negative y-axis of its entity - see LightComponent. This is
|
|
930
|
+
// not the entity's forward (negative z) axis, which is what lookAt aims.
|
|
931
|
+
const direction = _lightDir.copy(entity.up).mulScalar(-1);
|
|
932
|
+
|
|
933
|
+
// the shapes below submit using the renderer color, so swap it for the light's own
|
|
934
|
+
const outer = this.color;
|
|
935
|
+
this.color = light.color;
|
|
936
|
+
|
|
937
|
+
switch (light.type) {
|
|
938
|
+
case 'omni':
|
|
939
|
+
this.sphere(position, light.range);
|
|
940
|
+
break;
|
|
941
|
+
case 'spot':
|
|
942
|
+
this.cone(position, direction, light.outerConeAngle, light.range);
|
|
943
|
+
break;
|
|
944
|
+
case 'directional':
|
|
945
|
+
_pos.copy(direction).mulScalar(size).add(position);
|
|
946
|
+
this.arrow(position, _pos);
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
this.color = outer;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
export { WireRenderer };
|