@automovie/viewer 0.0.0 → 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/README.md +1 -1
- package/lib/AutoMoviePlayer.d.ts +2 -2
- package/lib/AutoMoviePlayer.js.map +1 -1
- package/lib/applyPose.d.ts +2 -2
- package/lib/applyPose.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/raster/AutoMovieRasterLighting.d.ts +43 -0
- package/lib/raster/AutoMovieRasterLighting.js +179 -0
- package/lib/raster/AutoMovieRasterLighting.js.map +1 -0
- package/lib/raster/AutoMovieRasterSheet.d.ts +68 -0
- package/lib/raster/AutoMovieRasterSheet.js +141 -0
- package/lib/raster/AutoMovieRasterSheet.js.map +1 -0
- package/lib/raster/AutoMovieRasterTarget.d.ts +87 -0
- package/lib/raster/AutoMovieRasterTarget.js +227 -0
- package/lib/raster/AutoMovieRasterTarget.js.map +1 -0
- package/lib/raster/IAutoMovieSceneRaster.d.ts +52 -0
- package/lib/raster/IAutoMovieSceneRaster.js +2 -0
- package/lib/raster/IAutoMovieSceneRaster.js.map +1 -0
- package/lib/raster/IRasterizeAutoMovieSceneProps.d.ts +28 -0
- package/lib/raster/IRasterizeAutoMovieSceneProps.js +2 -0
- package/lib/raster/IRasterizeAutoMovieSceneProps.js.map +1 -0
- package/lib/raster/encodeAutoMovieRasterPng.d.ts +17 -0
- package/lib/raster/encodeAutoMovieRasterPng.js +71 -0
- package/lib/raster/encodeAutoMovieRasterPng.js.map +1 -0
- package/lib/raster/index.d.ts +8 -0
- package/lib/raster/index.js +9 -0
- package/lib/raster/index.js.map +1 -0
- package/lib/raster/rasterizeAutoMovieScene.d.ts +33 -0
- package/lib/raster/rasterizeAutoMovieScene.js +409 -0
- package/lib/raster/rasterizeAutoMovieScene.js.map +1 -0
- package/lib/raster/stampAutoMovieRasterLabel.d.ts +19 -0
- package/lib/raster/stampAutoMovieRasterLabel.js +88 -0
- package/lib/raster/stampAutoMovieRasterLabel.js.map +1 -0
- package/package.json +3 -3
- package/src/AutoMoviePlayer.ts +1 -1
- package/src/applyPose.ts +1 -1
- package/src/index.ts +1 -0
- package/src/raster/AutoMovieRasterLighting.ts +221 -0
- package/src/raster/AutoMovieRasterSheet.ts +175 -0
- package/src/raster/AutoMovieRasterTarget.ts +282 -0
- package/src/raster/IAutoMovieSceneRaster.ts +62 -0
- package/src/raster/IRasterizeAutoMovieSceneProps.ts +34 -0
- package/src/raster/encodeAutoMovieRasterPng.ts +77 -0
- package/src/raster/index.ts +8 -0
- package/src/raster/rasterizeAutoMovieScene.ts +594 -0
- package/src/raster/stampAutoMovieRasterLabel.ts +120 -0
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
|
|
3
|
+
import { AutoMovieRasterLighting } from "./AutoMovieRasterLighting.js";
|
|
4
|
+
import { AutoMovieRasterTarget } from "./AutoMovieRasterTarget.js";
|
|
5
|
+
import type { IAutoMovieSceneRaster } from "./IAutoMovieSceneRaster.js";
|
|
6
|
+
import type { IRasterizeAutoMovieSceneProps } from "./IRasterizeAutoMovieSceneProps.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Draw a three.js scene through its camera on the CPU, in Node, with no
|
|
10
|
+
* browser or GPU: the inspection path for reading a film's poses, contacts,
|
|
11
|
+
* staging and framing frame by frame.
|
|
12
|
+
*
|
|
13
|
+
* Visible meshes (including skinned, morphed and instanced meshes) are drawn
|
|
14
|
+
* as flat-shaded triangles with a per-pixel depth buffer, honouring each
|
|
15
|
+
* material's side, visibility, opacity, vertex or instance colours and
|
|
16
|
+
* emissive colour; lit materials take the scene's lights through three.js's
|
|
17
|
+
* Lambert convention and `MeshBasicMaterial` is unlit. Lines are drawn one
|
|
18
|
+
* pixel wide; points are squares of their `PointsMaterial` size in output
|
|
19
|
+
* pixels, attenuated by distance as three.js does when `sizeAttenuation` is
|
|
20
|
+
* on; transparent surfaces are blended back to front after
|
|
21
|
+
* opaque ones, and the result is converted to sRGB. Textures, shadows, fog,
|
|
22
|
+
* sprites and post-processing are not drawn; the statistics count them.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* production.seek(12.5);
|
|
26
|
+
* const frame = rasterizeAutoMovieScene({ scene, camera, width: 720, height: 1280 });
|
|
27
|
+
* await writeFile("f.png", encodeAutoMovieRasterPng(frame));
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @evidence contracts/development/common.md#meaningful-documentation The comment states the purpose, every drawn object kind and material property honoured, the shading convention, the transparency order and what is left out and where it is counted.
|
|
31
|
+
* @evidence contracts/development/implementation.md#principled-implementation Vertices go through the camera's own projection and view matrices and are clipped against the near plane in clip space before division, so geometry behind the camera never wraps; skinned and morphed vertices come from three.js's own getVertexPosition, so poses match the Viewer's bones.
|
|
32
|
+
* @evidence contracts/development/implementation.md#clear-and-simple-structure Traversal submits primitives; the raster target owns buffers and fills, the lighting owns irradiance, and transparent triangles are queued and drawn after the opaque pass.
|
|
33
|
+
* @evidence contracts/development/implementation.md#prohibited-implementation-shortcuts Invalid raster sizes and supersampling factors throw; unsupported objects and materials are counted rather than silently dropped or guessed.
|
|
34
|
+
* @evidence contracts/development/implementation.md#efficient-algorithms Each mesh's vertices are transformed once per instance and shared by its triangles; triangle fill cost is bounded by each triangle's clipped bounding box.
|
|
35
|
+
* @evidence contracts/development/implementation.md#reuse-equivalent-work Lights are flattened once per frame and scratch vectors are reused across triangles.
|
|
36
|
+
* @evidence contracts/development/implementation.md#bound-retention-and-release-resources The frame allocates its buffers and per-mesh vertex arrays and keeps only the returned bytes; the scene and camera are read, never retained or modified beyond three.js's own world-matrix update.
|
|
37
|
+
*/
|
|
38
|
+
export function rasterizeAutoMovieScene(
|
|
39
|
+
props: IRasterizeAutoMovieSceneProps,
|
|
40
|
+
): IAutoMovieSceneRaster {
|
|
41
|
+
const factor: number = props.supersample ?? 1;
|
|
42
|
+
if (
|
|
43
|
+
![props.width, props.height].every(
|
|
44
|
+
(size: number): boolean => Number.isSafeInteger(size) && size > 0,
|
|
45
|
+
) ||
|
|
46
|
+
props.width * props.height > 33_177_600
|
|
47
|
+
)
|
|
48
|
+
throw new Error("Raster size must be positive integers within 8K UHD");
|
|
49
|
+
if (!Number.isSafeInteger(factor) || factor < 1 || factor > 4)
|
|
50
|
+
throw new Error("Supersampling must be an integer from 1 to 4");
|
|
51
|
+
|
|
52
|
+
const { scene, camera } = props;
|
|
53
|
+
scene.updateMatrixWorld();
|
|
54
|
+
if (camera.parent === null) camera.updateMatrixWorld();
|
|
55
|
+
const width: number = props.width * factor;
|
|
56
|
+
const height: number = props.height * factor;
|
|
57
|
+
const background: THREE.Color =
|
|
58
|
+
scene instanceof THREE.Scene && scene.background instanceof THREE.Color
|
|
59
|
+
? scene.background
|
|
60
|
+
: new THREE.Color(props.background ?? 0x202020);
|
|
61
|
+
const target: AutoMovieRasterTarget = new AutoMovieRasterTarget(
|
|
62
|
+
width,
|
|
63
|
+
height,
|
|
64
|
+
[background.r, background.g, background.b],
|
|
65
|
+
);
|
|
66
|
+
const statistics: IAutoMovieSceneRaster.IStatistics = {
|
|
67
|
+
meshes: 0,
|
|
68
|
+
triangles: 0,
|
|
69
|
+
lines: 0,
|
|
70
|
+
points: 0,
|
|
71
|
+
skipped: {},
|
|
72
|
+
untextured: 0,
|
|
73
|
+
approximated: 0,
|
|
74
|
+
};
|
|
75
|
+
const frame: IFrame = {
|
|
76
|
+
target,
|
|
77
|
+
statistics,
|
|
78
|
+
lighting: new AutoMovieRasterLighting(scene, camera),
|
|
79
|
+
viewProjection: new THREE.Matrix4().multiplyMatrices(
|
|
80
|
+
camera.projectionMatrix,
|
|
81
|
+
camera.matrixWorldInverse,
|
|
82
|
+
),
|
|
83
|
+
eye: new THREE.Vector3().setFromMatrixPosition(camera.matrixWorld),
|
|
84
|
+
forward: new THREE.Vector3(0, 0, -1).transformDirection(camera.matrixWorld),
|
|
85
|
+
orthographic: camera instanceof THREE.OrthographicCamera,
|
|
86
|
+
transparent: [],
|
|
87
|
+
factor,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// Opaque primitives first, in traversal order; the depth buffer resolves visibility.
|
|
91
|
+
scene.traverseVisible((object: THREE.Object3D): void => {
|
|
92
|
+
if (object instanceof THREE.Mesh) {
|
|
93
|
+
if ((object as { isBatchedMesh?: boolean }).isBatchedMesh === true)
|
|
94
|
+
skip(statistics, object.type);
|
|
95
|
+
else submitMesh(frame, object);
|
|
96
|
+
} else if (object instanceof THREE.Line) submitLine(frame, object);
|
|
97
|
+
else if (object instanceof THREE.Points) submitPoints(frame, object);
|
|
98
|
+
else if (object instanceof THREE.Sprite) skip(statistics, object.type);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Transparent surfaces blend far to near without writing depth, as the Viewer sorts them.
|
|
102
|
+
frame.transparent.sort(
|
|
103
|
+
(a: ITransparent, b: ITransparent): number => b.depth - a.depth,
|
|
104
|
+
);
|
|
105
|
+
for (const t of frame.transparent)
|
|
106
|
+
target.triangle(...t.vertices, t.rgb, t.alpha, false);
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
width: props.width,
|
|
110
|
+
height: props.height,
|
|
111
|
+
pixels: target.toSrgb(factor),
|
|
112
|
+
statistics,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Everything one frame's submissions share. */
|
|
117
|
+
interface IFrame {
|
|
118
|
+
target: AutoMovieRasterTarget;
|
|
119
|
+
statistics: IAutoMovieSceneRaster.IStatistics;
|
|
120
|
+
lighting: AutoMovieRasterLighting;
|
|
121
|
+
viewProjection: THREE.Matrix4;
|
|
122
|
+
eye: THREE.Vector3;
|
|
123
|
+
forward: THREE.Vector3;
|
|
124
|
+
orthographic: boolean;
|
|
125
|
+
transparent: ITransparent[];
|
|
126
|
+
/** Supersampling factor: target pixels per output pixel along each axis. */
|
|
127
|
+
factor: number;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One queued transparent triangle in screen coordinates. */
|
|
131
|
+
interface ITransparent {
|
|
132
|
+
vertices: [
|
|
133
|
+
number,
|
|
134
|
+
number,
|
|
135
|
+
number,
|
|
136
|
+
number,
|
|
137
|
+
number,
|
|
138
|
+
number,
|
|
139
|
+
number,
|
|
140
|
+
number,
|
|
141
|
+
number,
|
|
142
|
+
];
|
|
143
|
+
rgb: [number, number, number];
|
|
144
|
+
alpha: number;
|
|
145
|
+
depth: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Count a visible object this rasterizer does not draw. */
|
|
149
|
+
function skip(
|
|
150
|
+
statistics: IAutoMovieSceneRaster.IStatistics,
|
|
151
|
+
type: string,
|
|
152
|
+
): void {
|
|
153
|
+
statistics.skipped[type] = (statistics.skipped[type] ?? 0) + 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Submit every triangle of a mesh, once per instance. */
|
|
157
|
+
function submitMesh(frame: IFrame, mesh: THREE.Mesh): void {
|
|
158
|
+
const geometry: THREE.BufferGeometry = mesh.geometry;
|
|
159
|
+
const position:
|
|
160
|
+
| THREE.BufferAttribute
|
|
161
|
+
| THREE.InterleavedBufferAttribute
|
|
162
|
+
| undefined = geometry.getAttribute("position");
|
|
163
|
+
if (position === undefined) return;
|
|
164
|
+
const materials: THREE.Material[] = Array.isArray(mesh.material)
|
|
165
|
+
? mesh.material
|
|
166
|
+
: [mesh.material];
|
|
167
|
+
for (const material of materials) {
|
|
168
|
+
if ((material as { map?: unknown }).map) frame.statistics.untextured++;
|
|
169
|
+
if (material instanceof THREE.ShaderMaterial)
|
|
170
|
+
frame.statistics.approximated++;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Local vertex positions with morph targets and skinning applied once.
|
|
174
|
+
const count: number = position.count;
|
|
175
|
+
const local: Float32Array = new Float32Array(count * 3);
|
|
176
|
+
const vertex: THREE.Vector3 = new THREE.Vector3();
|
|
177
|
+
const deformed: boolean =
|
|
178
|
+
mesh instanceof THREE.SkinnedMesh ||
|
|
179
|
+
(geometry.morphAttributes.position !== undefined &&
|
|
180
|
+
(mesh.morphTargetInfluences ?? []).some((w: number): boolean => w !== 0));
|
|
181
|
+
for (let i: number = 0; i < count; i++) {
|
|
182
|
+
if (deformed) mesh.getVertexPosition(i, vertex);
|
|
183
|
+
else vertex.fromBufferAttribute(position, i);
|
|
184
|
+
local[i * 3] = vertex.x;
|
|
185
|
+
local[i * 3 + 1] = vertex.y;
|
|
186
|
+
local[i * 3 + 2] = vertex.z;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const index: THREE.BufferAttribute | null = geometry.getIndex();
|
|
190
|
+
const total: number = index === null ? count : index.count;
|
|
191
|
+
const ranges: { start: number; count: number; materialIndex: number }[] =
|
|
192
|
+
Array.isArray(mesh.material) && geometry.groups.length > 0
|
|
193
|
+
? geometry.groups.map(
|
|
194
|
+
(g: {
|
|
195
|
+
start: number;
|
|
196
|
+
count: number;
|
|
197
|
+
materialIndex?: number;
|
|
198
|
+
}): {
|
|
199
|
+
start: number;
|
|
200
|
+
count: number;
|
|
201
|
+
materialIndex: number;
|
|
202
|
+
} => ({
|
|
203
|
+
start: g.start,
|
|
204
|
+
count: g.count,
|
|
205
|
+
materialIndex: g.materialIndex ?? 0,
|
|
206
|
+
}),
|
|
207
|
+
)
|
|
208
|
+
: [{ start: 0, count: total, materialIndex: 0 }];
|
|
209
|
+
const drawStart: number = geometry.drawRange.start;
|
|
210
|
+
const drawEnd: number = Math.min(total, drawStart + geometry.drawRange.count);
|
|
211
|
+
const colors:
|
|
212
|
+
| THREE.BufferAttribute
|
|
213
|
+
| THREE.InterleavedBufferAttribute
|
|
214
|
+
| undefined = geometry.getAttribute("color");
|
|
215
|
+
const instances: number =
|
|
216
|
+
mesh instanceof THREE.InstancedMesh ? mesh.count : 1;
|
|
217
|
+
const world: THREE.Matrix4 = new THREE.Matrix4();
|
|
218
|
+
const instance: THREE.Matrix4 = new THREE.Matrix4();
|
|
219
|
+
const tint: THREE.Color = new THREE.Color();
|
|
220
|
+
const worlds: Float32Array = new Float32Array(count * 3);
|
|
221
|
+
const clips: Float32Array = new Float32Array(count * 4);
|
|
222
|
+
const p: THREE.Vector4 = new THREE.Vector4();
|
|
223
|
+
|
|
224
|
+
for (let n: number = 0; n < instances; n++) {
|
|
225
|
+
world.copy(mesh.matrixWorld);
|
|
226
|
+
tint.setRGB(1, 1, 1);
|
|
227
|
+
if (mesh instanceof THREE.InstancedMesh) {
|
|
228
|
+
mesh.getMatrixAt(n, instance);
|
|
229
|
+
world.multiply(instance);
|
|
230
|
+
if (mesh.instanceColor !== null) mesh.getColorAt(n, tint);
|
|
231
|
+
}
|
|
232
|
+
frame.statistics.meshes++;
|
|
233
|
+
const mirrored: boolean = world.determinant() < 0;
|
|
234
|
+
|
|
235
|
+
// World and clip coordinates of every vertex of this instance.
|
|
236
|
+
const e: number[] = world.elements;
|
|
237
|
+
const v: number[] = frame.viewProjection.elements;
|
|
238
|
+
for (let i: number = 0; i < count; i++) {
|
|
239
|
+
const x: number = local[i * 3]!;
|
|
240
|
+
const y: number = local[i * 3 + 1]!;
|
|
241
|
+
const z: number = local[i * 3 + 2]!;
|
|
242
|
+
const wx: number = e[0]! * x + e[4]! * y + e[8]! * z + e[12]!;
|
|
243
|
+
const wy: number = e[1]! * x + e[5]! * y + e[9]! * z + e[13]!;
|
|
244
|
+
const wz: number = e[2]! * x + e[6]! * y + e[10]! * z + e[14]!;
|
|
245
|
+
worlds[i * 3] = wx;
|
|
246
|
+
worlds[i * 3 + 1] = wy;
|
|
247
|
+
worlds[i * 3 + 2] = wz;
|
|
248
|
+
p.set(
|
|
249
|
+
v[0]! * wx + v[4]! * wy + v[8]! * wz + v[12]!,
|
|
250
|
+
v[1]! * wx + v[5]! * wy + v[9]! * wz + v[13]!,
|
|
251
|
+
v[2]! * wx + v[6]! * wy + v[10]! * wz + v[14]!,
|
|
252
|
+
v[3]! * wx + v[7]! * wy + v[11]! * wz + v[15]!,
|
|
253
|
+
);
|
|
254
|
+
clips[i * 4] = p.x;
|
|
255
|
+
clips[i * 4 + 1] = p.y;
|
|
256
|
+
clips[i * 4 + 2] = p.z;
|
|
257
|
+
clips[i * 4 + 3] = p.w;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
for (const range of ranges) {
|
|
261
|
+
const material: THREE.Material | undefined =
|
|
262
|
+
materials[range.materialIndex];
|
|
263
|
+
if (material === undefined || !material.visible || material.opacity <= 0)
|
|
264
|
+
continue;
|
|
265
|
+
const start: number = Math.max(range.start, drawStart);
|
|
266
|
+
const end: number = Math.min(range.start + range.count, drawEnd);
|
|
267
|
+
for (let t: number = start; t + 2 < end; t += 3) {
|
|
268
|
+
const a: number = index === null ? t : index.getX(t);
|
|
269
|
+
const b: number = index === null ? t + 1 : index.getX(t + 1);
|
|
270
|
+
const c: number = index === null ? t + 2 : index.getX(t + 2);
|
|
271
|
+
submitTriangle(
|
|
272
|
+
frame,
|
|
273
|
+
material,
|
|
274
|
+
worlds,
|
|
275
|
+
clips,
|
|
276
|
+
a,
|
|
277
|
+
b,
|
|
278
|
+
c,
|
|
279
|
+
mirrored,
|
|
280
|
+
colors,
|
|
281
|
+
tint,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Scratch vectors shared by triangle submissions. */
|
|
289
|
+
const scratch: {
|
|
290
|
+
ab: THREE.Vector3;
|
|
291
|
+
ac: THREE.Vector3;
|
|
292
|
+
normal: THREE.Vector3;
|
|
293
|
+
centroid: THREE.Vector3;
|
|
294
|
+
view: THREE.Vector3;
|
|
295
|
+
} = {
|
|
296
|
+
ab: new THREE.Vector3(),
|
|
297
|
+
ac: new THREE.Vector3(),
|
|
298
|
+
normal: new THREE.Vector3(),
|
|
299
|
+
centroid: new THREE.Vector3(),
|
|
300
|
+
view: new THREE.Vector3(),
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/** Cull, shade, clip and fill one triangle of a mesh. */
|
|
304
|
+
function submitTriangle(
|
|
305
|
+
frame: IFrame,
|
|
306
|
+
material: THREE.Material,
|
|
307
|
+
worlds: Float32Array,
|
|
308
|
+
clips: Float32Array,
|
|
309
|
+
a: number,
|
|
310
|
+
b: number,
|
|
311
|
+
c: number,
|
|
312
|
+
mirrored: boolean,
|
|
313
|
+
colors: THREE.BufferAttribute | THREE.InterleavedBufferAttribute | undefined,
|
|
314
|
+
tint: THREE.Color,
|
|
315
|
+
): void {
|
|
316
|
+
const { ab, ac, normal, centroid, view } = scratch;
|
|
317
|
+
ab.set(
|
|
318
|
+
worlds[b * 3]! - worlds[a * 3]!,
|
|
319
|
+
worlds[b * 3 + 1]! - worlds[a * 3 + 1]!,
|
|
320
|
+
worlds[b * 3 + 2]! - worlds[a * 3 + 2]!,
|
|
321
|
+
);
|
|
322
|
+
ac.set(
|
|
323
|
+
worlds[c * 3]! - worlds[a * 3]!,
|
|
324
|
+
worlds[c * 3 + 1]! - worlds[a * 3 + 1]!,
|
|
325
|
+
worlds[c * 3 + 2]! - worlds[a * 3 + 2]!,
|
|
326
|
+
);
|
|
327
|
+
normal.crossVectors(ab, ac);
|
|
328
|
+
const length: number = normal.length();
|
|
329
|
+
if (!(length > 0)) return;
|
|
330
|
+
normal.divideScalar(mirrored ? -length : length);
|
|
331
|
+
centroid.set(
|
|
332
|
+
(worlds[a * 3]! + worlds[b * 3]! + worlds[c * 3]!) / 3,
|
|
333
|
+
(worlds[a * 3 + 1]! + worlds[b * 3 + 1]! + worlds[c * 3 + 1]!) / 3,
|
|
334
|
+
(worlds[a * 3 + 2]! + worlds[b * 3 + 2]! + worlds[c * 3 + 2]!) / 3,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
// Facing follows the material's side, with the normal turned toward the viewer when both sides draw.
|
|
338
|
+
if (frame.orthographic) view.copy(frame.forward).negate();
|
|
339
|
+
else view.subVectors(frame.eye, centroid);
|
|
340
|
+
const front: boolean = normal.dot(view) > 0;
|
|
341
|
+
if (material.side === THREE.FrontSide && !front) return;
|
|
342
|
+
if (material.side === THREE.BackSide && front) return;
|
|
343
|
+
if (!front) normal.negate();
|
|
344
|
+
|
|
345
|
+
// Base colour: material colour, vertex colours and instance colour, linear RGB.
|
|
346
|
+
const base: [number, number, number] = baseColor(material);
|
|
347
|
+
if (material.vertexColors && colors !== undefined)
|
|
348
|
+
for (let k: number = 0; k < 3; k++)
|
|
349
|
+
base[k] =
|
|
350
|
+
base[k]! *
|
|
351
|
+
((colors.getComponent(a, k) +
|
|
352
|
+
colors.getComponent(b, k) +
|
|
353
|
+
colors.getComponent(c, k)) /
|
|
354
|
+
3);
|
|
355
|
+
base[0] *= tint.r;
|
|
356
|
+
base[1] *= tint.g;
|
|
357
|
+
base[2] *= tint.b;
|
|
358
|
+
const rgb: [number, number, number] = shade(
|
|
359
|
+
frame,
|
|
360
|
+
material,
|
|
361
|
+
base,
|
|
362
|
+
centroid,
|
|
363
|
+
normal,
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
const polygon: number[][] = clipNear(
|
|
367
|
+
[a, b, c].map((i: number): number[] => [
|
|
368
|
+
clips[i * 4]!,
|
|
369
|
+
clips[i * 4 + 1]!,
|
|
370
|
+
clips[i * 4 + 2]!,
|
|
371
|
+
clips[i * 4 + 3]!,
|
|
372
|
+
]),
|
|
373
|
+
);
|
|
374
|
+
if (polygon.length < 3) return;
|
|
375
|
+
const screen: number[][] = polygon.map((q: number[]): number[] =>
|
|
376
|
+
toScreen(frame.target, q),
|
|
377
|
+
);
|
|
378
|
+
const transparent: boolean = material.transparent && material.opacity < 1;
|
|
379
|
+
for (let k: number = 1; k + 1 < screen.length; k++) {
|
|
380
|
+
const vertices: ITransparent["vertices"] = [
|
|
381
|
+
screen[0]![0]!,
|
|
382
|
+
screen[0]![1]!,
|
|
383
|
+
screen[0]![2]!,
|
|
384
|
+
screen[k]![0]!,
|
|
385
|
+
screen[k]![1]!,
|
|
386
|
+
screen[k]![2]!,
|
|
387
|
+
screen[k + 1]![0]!,
|
|
388
|
+
screen[k + 1]![1]!,
|
|
389
|
+
screen[k + 1]![2]!,
|
|
390
|
+
];
|
|
391
|
+
frame.statistics.triangles++;
|
|
392
|
+
if (transparent)
|
|
393
|
+
frame.transparent.push({
|
|
394
|
+
vertices,
|
|
395
|
+
rgb,
|
|
396
|
+
alpha: material.opacity,
|
|
397
|
+
depth: (vertices[2] + vertices[5] + vertices[8]) / 3,
|
|
398
|
+
});
|
|
399
|
+
else frame.target.triangle(...vertices, rgb, 1, material.depthWrite);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** A material's linear base colour, or neutral clay for shader materials. */
|
|
404
|
+
function baseColor(material: THREE.Material): [number, number, number] {
|
|
405
|
+
const color: unknown = (material as { color?: unknown }).color;
|
|
406
|
+
if (
|
|
407
|
+
material instanceof THREE.ShaderMaterial ||
|
|
408
|
+
!(color instanceof THREE.Color)
|
|
409
|
+
)
|
|
410
|
+
return [0.6, 0.6, 0.6];
|
|
411
|
+
return [color.r, color.g, color.b];
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Lit or unlit colour of a surface point, including emission. */
|
|
415
|
+
function shade(
|
|
416
|
+
frame: IFrame,
|
|
417
|
+
material: THREE.Material,
|
|
418
|
+
base: [number, number, number],
|
|
419
|
+
point: THREE.Vector3,
|
|
420
|
+
normal: THREE.Vector3,
|
|
421
|
+
): [number, number, number] {
|
|
422
|
+
const unlit: boolean =
|
|
423
|
+
material instanceof THREE.MeshBasicMaterial ||
|
|
424
|
+
material instanceof THREE.MeshNormalMaterial ||
|
|
425
|
+
material instanceof THREE.ShaderMaterial;
|
|
426
|
+
let rgb: [number, number, number] = base;
|
|
427
|
+
if (!unlit) {
|
|
428
|
+
const metalness: number =
|
|
429
|
+
(material as { metalness?: number }).metalness ?? 0;
|
|
430
|
+
const irradiance: [number, number, number] = frame.lighting.irradiance(
|
|
431
|
+
point,
|
|
432
|
+
normal,
|
|
433
|
+
);
|
|
434
|
+
const diffuse: number = (1 - metalness) / Math.PI;
|
|
435
|
+
rgb = [
|
|
436
|
+
base[0] * irradiance[0] * diffuse,
|
|
437
|
+
base[1] * irradiance[1] * diffuse,
|
|
438
|
+
base[2] * irradiance[2] * diffuse,
|
|
439
|
+
];
|
|
440
|
+
}
|
|
441
|
+
const emissive: unknown = (material as { emissive?: unknown }).emissive;
|
|
442
|
+
if (emissive instanceof THREE.Color) {
|
|
443
|
+
const intensity: number =
|
|
444
|
+
(material as { emissiveIntensity?: number }).emissiveIntensity ?? 1;
|
|
445
|
+
rgb = [
|
|
446
|
+
rgb[0] + emissive.r * intensity,
|
|
447
|
+
rgb[1] + emissive.g * intensity,
|
|
448
|
+
rgb[2] + emissive.b * intensity,
|
|
449
|
+
];
|
|
450
|
+
}
|
|
451
|
+
return rgb;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Clip a clip-space polygon against the near plane `z >= -w`. */
|
|
455
|
+
function clipNear(polygon: number[][]): number[][] {
|
|
456
|
+
const out: number[][] = [];
|
|
457
|
+
for (let i: number = 0; i < polygon.length; i++) {
|
|
458
|
+
const current: number[] = polygon[i]!;
|
|
459
|
+
const next: number[] = polygon[(i + 1) % polygon.length]!;
|
|
460
|
+
const dc: number = current[2]! + current[3]!;
|
|
461
|
+
const dn: number = next[2]! + next[3]!;
|
|
462
|
+
if (dc >= 0) out.push(current);
|
|
463
|
+
if (dc >= 0 !== dn >= 0) {
|
|
464
|
+
const t: number = dc / (dc - dn);
|
|
465
|
+
out.push(
|
|
466
|
+
current.map(
|
|
467
|
+
(value: number, k: number): number => value + (next[k]! - value) * t,
|
|
468
|
+
),
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return out;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** Perspective-divide a clip-space point into target pixels and device depth. */
|
|
476
|
+
function toScreen(target: AutoMovieRasterTarget, q: number[]): number[] {
|
|
477
|
+
const clipW: number = q[3]!;
|
|
478
|
+
const w: number = clipW === 0 ? 1e-9 : clipW;
|
|
479
|
+
return [
|
|
480
|
+
((q[0]! / w) * 0.5 + 0.5) * target.width,
|
|
481
|
+
(1 - ((q[1]! / w) * 0.5 + 0.5)) * target.height,
|
|
482
|
+
q[2]! / w,
|
|
483
|
+
];
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Draw a line object's segments, depth-tested unless its material disables it. */
|
|
487
|
+
function submitLine(frame: IFrame, line: THREE.Line): void {
|
|
488
|
+
const position:
|
|
489
|
+
| THREE.BufferAttribute
|
|
490
|
+
| THREE.InterleavedBufferAttribute
|
|
491
|
+
| undefined = line.geometry.getAttribute("position");
|
|
492
|
+
const material: THREE.Material = Array.isArray(line.material)
|
|
493
|
+
? line.material[0]!
|
|
494
|
+
: line.material;
|
|
495
|
+
if (position === undefined || material === undefined || !material.visible)
|
|
496
|
+
return;
|
|
497
|
+
const rgb: [number, number, number] = baseColor(material);
|
|
498
|
+
const stride: number = line instanceof THREE.LineSegments ? 2 : 1;
|
|
499
|
+
const index: THREE.BufferAttribute | null = line.geometry.getIndex();
|
|
500
|
+
const count: number = index === null ? position.count : index.count;
|
|
501
|
+
const at: (k: number) => number = (k: number): number =>
|
|
502
|
+
index === null ? k : index.getX(k);
|
|
503
|
+
const loop: boolean = line instanceof THREE.LineLoop;
|
|
504
|
+
const matrix: THREE.Matrix4 = new THREE.Matrix4().multiplyMatrices(
|
|
505
|
+
frame.viewProjection,
|
|
506
|
+
line.matrixWorld,
|
|
507
|
+
);
|
|
508
|
+
const a: THREE.Vector4 = new THREE.Vector4();
|
|
509
|
+
const b: THREE.Vector4 = new THREE.Vector4();
|
|
510
|
+
for (let k: number = 0; k + 1 < count + (loop ? 1 : 0); k += stride) {
|
|
511
|
+
a.set(
|
|
512
|
+
position.getX(at(k)),
|
|
513
|
+
position.getY(at(k)),
|
|
514
|
+
position.getZ(at(k)),
|
|
515
|
+
1,
|
|
516
|
+
).applyMatrix4(matrix);
|
|
517
|
+
const j: number = at((k + 1) % count);
|
|
518
|
+
b.set(position.getX(j), position.getY(j), position.getZ(j), 1).applyMatrix4(
|
|
519
|
+
matrix,
|
|
520
|
+
);
|
|
521
|
+
const clipped: number[][] = clipSegment(
|
|
522
|
+
[a.x, a.y, a.z, a.w],
|
|
523
|
+
[b.x, b.y, b.z, b.w],
|
|
524
|
+
);
|
|
525
|
+
if (clipped.length < 2) continue;
|
|
526
|
+
const p: number[] = toScreen(frame.target, clipped[0]!);
|
|
527
|
+
const q: number[] = toScreen(frame.target, clipped[1]!);
|
|
528
|
+
frame.target.line(
|
|
529
|
+
p[0]!,
|
|
530
|
+
p[1]!,
|
|
531
|
+
p[2]!,
|
|
532
|
+
q[0]!,
|
|
533
|
+
q[1]!,
|
|
534
|
+
q[2]!,
|
|
535
|
+
rgb,
|
|
536
|
+
material.depthTest,
|
|
537
|
+
);
|
|
538
|
+
frame.statistics.lines++;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Clip a clip-space segment against the near plane. */
|
|
543
|
+
function clipSegment(a: number[], b: number[]): number[][] {
|
|
544
|
+
const da: number = a[2]! + a[3]!;
|
|
545
|
+
const db: number = b[2]! + b[3]!;
|
|
546
|
+
if (da < 0 && db < 0) return [];
|
|
547
|
+
if (da >= 0 && db >= 0) return [a, b];
|
|
548
|
+
const t: number = da / (da - db);
|
|
549
|
+
const cut: number[] = a.map(
|
|
550
|
+
(value: number, k: number): number => value + (b[k]! - value) * t,
|
|
551
|
+
);
|
|
552
|
+
return da >= 0 ? [a, cut] : [cut, b];
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** Draw a points object's vertices as small depth-tested squares. */
|
|
556
|
+
function submitPoints(frame: IFrame, points: THREE.Points): void {
|
|
557
|
+
const position:
|
|
558
|
+
| THREE.BufferAttribute
|
|
559
|
+
| THREE.InterleavedBufferAttribute
|
|
560
|
+
| undefined = points.geometry.getAttribute("position");
|
|
561
|
+
const material: THREE.Material = Array.isArray(points.material)
|
|
562
|
+
? points.material[0]!
|
|
563
|
+
: points.material;
|
|
564
|
+
if (position === undefined || material === undefined || !material.visible)
|
|
565
|
+
return;
|
|
566
|
+
const rgb: [number, number, number] = baseColor(material);
|
|
567
|
+
const matrix: THREE.Matrix4 = new THREE.Matrix4().multiplyMatrices(
|
|
568
|
+
frame.viewProjection,
|
|
569
|
+
points.matrixWorld,
|
|
570
|
+
);
|
|
571
|
+
// three.js draws a point `size` drawing-buffer pixels wide, times half the
|
|
572
|
+
// buffer height over view depth when attenuated in perspective.
|
|
573
|
+
const size: number =
|
|
574
|
+
material instanceof THREE.PointsMaterial ? material.size : 1;
|
|
575
|
+
const attenuate: boolean =
|
|
576
|
+
material instanceof THREE.PointsMaterial &&
|
|
577
|
+
material.sizeAttenuation &&
|
|
578
|
+
!frame.orthographic;
|
|
579
|
+
const p: THREE.Vector4 = new THREE.Vector4();
|
|
580
|
+
for (let k: number = 0; k < position.count; k++) {
|
|
581
|
+
p.set(position.getX(k), position.getY(k), position.getZ(k), 1).applyMatrix4(
|
|
582
|
+
matrix,
|
|
583
|
+
);
|
|
584
|
+
if (p.w <= 0 || p.z < -p.w) continue;
|
|
585
|
+
const s: number[] = toScreen(frame.target, [p.x, p.y, p.z, p.w]);
|
|
586
|
+
const width: number =
|
|
587
|
+
size *
|
|
588
|
+
frame.factor *
|
|
589
|
+
(attenuate ? frame.target.height / (2 * frame.factor) / p.w : 1);
|
|
590
|
+
const radius: number = Math.max(0, Math.round((width - 1) / 2));
|
|
591
|
+
frame.target.point(s[0]!, s[1]!, s[2]!, radius, rgb, material.depthTest);
|
|
592
|
+
frame.statistics.points++;
|
|
593
|
+
}
|
|
594
|
+
}
|