katachi-runtime 0.1.1 → 0.2.1
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 +16 -2
- package/index.d.ts +1 -1
- package/index.js +350 -58
- package/package.json +1 -1
- package/picker.d.ts +15 -0
- package/stage.d.ts +31 -1
- package/vehicle.d.ts +8 -0
package/README.md
CHANGED
|
@@ -106,11 +106,23 @@ class Assets {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
class Stage {
|
|
109
|
-
constructor(canvas: HTMLCanvasElement, options?:
|
|
110
|
-
show(assets: Assets, name: string, props?: VehicleProps): Promise<Model | undefined>; // undefined if another model was shown meanwhile
|
|
109
|
+
constructor(canvas: HTMLCanvasElement, options?: StageOptions); // drag to orbit, scroll to zoom; draws nothing while off screen
|
|
110
|
+
show(assets: Assets, name: string, props?: VehicleProps): Promise<Model | undefined>; // resolves once drawn; undefined if another model was shown meanwhile
|
|
111
|
+
view(direction?: [number, number, number], options?: { duration?: number }): Promise<boolean>; // flies around the model to look from there, or back to model.view; false if the user took over
|
|
112
|
+
setAutoRotate(on: boolean): void;
|
|
113
|
+
setLighting(lighting: "studio" | "night", options?: { duration?: number }): void; // fades the studio light to night and back
|
|
114
|
+
pick(clientX: number, clientY: number): THREE.Object3D | null; // the bone or mesh under a pointer, read from the GPU
|
|
115
|
+
photo(width: number, height: number): Promise<Blob>; // PNG of the current view, transparent background
|
|
111
116
|
dispose(): void;
|
|
112
117
|
}
|
|
113
118
|
|
|
119
|
+
interface StageOptions {
|
|
120
|
+
autoRotate?: boolean;
|
|
121
|
+
zoom?: boolean; // false leaves the wheel and vertical swipes to the page
|
|
122
|
+
entrance?: boolean; // the camera glides in when a model is shown
|
|
123
|
+
inset?: { top?: number; bottom?: number }; // pixels the model stays clear of, for controls over the canvas
|
|
124
|
+
}
|
|
125
|
+
|
|
114
126
|
class Model {
|
|
115
127
|
name: string;
|
|
116
128
|
root: THREE.Group; // add this to your scene
|
|
@@ -137,6 +149,8 @@ class Vehicle extends Model {
|
|
|
137
149
|
setLights(on: boolean): void;
|
|
138
150
|
openings(): Opening[];
|
|
139
151
|
setOpen(name: Opening, amount: number): void; // 0 to 1
|
|
152
|
+
openingOf(node: THREE.Object3D): Opening | null; // the door or panel a picked node belongs to
|
|
153
|
+
static views: { front; side; rear; top }; // directions for Stage.view
|
|
140
154
|
extras(): number[];
|
|
141
155
|
extraEnabled(id: number): boolean;
|
|
142
156
|
setExtra(id: number, enabled: boolean): void;
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Assets, type ModEntry, type ModelEntry, type ModelKind, type PedEntry, type PieceEntry, type VehicleEntry, type WheelEntry } from "./assets";
|
|
2
2
|
export { Character } from "./character";
|
|
3
3
|
export { Model } from "./model";
|
|
4
|
-
export { Stage, ThumbnailRenderer, type StageOptions, type ThumbnailFormat } from "./stage";
|
|
4
|
+
export { Stage, ThumbnailRenderer, type Direction, type StageInset, type StageLighting, type StageOptions, type ThumbnailFormat } from "./stage";
|
|
5
5
|
export { Vehicle, type Color, type Opening, type PaintName, type VehicleProps } from "./vehicle";
|
package/index.js
CHANGED
|
@@ -1338,6 +1338,7 @@ class Vehicle extends Model {
|
|
|
1338
1338
|
wheelTypes;
|
|
1339
1339
|
paints;
|
|
1340
1340
|
loader;
|
|
1341
|
+
static views = { front: [0, 0.12, -1], side: [-1, 0.1, 0], rear: [0, 0.16, 1], top: [0, 1, 0.04] };
|
|
1341
1342
|
view = [-0.6, 0.25, -0.75];
|
|
1342
1343
|
rest = new Map;
|
|
1343
1344
|
hidden;
|
|
@@ -1431,6 +1432,14 @@ class Vehicle extends Model {
|
|
|
1431
1432
|
bone.rotateZ(turn[2] * open);
|
|
1432
1433
|
bone.position.set(rest.position.x + slide[0] * open, rest.position.y + slide[1] * open, rest.position.z + slide[2] * open);
|
|
1433
1434
|
}
|
|
1435
|
+
openingOf(node) {
|
|
1436
|
+
for (let part = node;part; part = part.parent) {
|
|
1437
|
+
const opening = this.openings().find((name) => OPENINGS[name].bone === part?.name);
|
|
1438
|
+
if (opening)
|
|
1439
|
+
return opening;
|
|
1440
|
+
}
|
|
1441
|
+
return null;
|
|
1442
|
+
}
|
|
1434
1443
|
extras() {
|
|
1435
1444
|
return [...this.extraStates.keys()].sort((a, b) => a - b);
|
|
1436
1445
|
}
|
|
@@ -1561,6 +1570,7 @@ class Vehicle extends Model {
|
|
|
1561
1570
|
this.neon = new THREE4.Mesh(new THREE4.PlaneGeometry(size.x * 1.7, size.z * 1.35), new THREE4.MeshBasicMaterial({ map: glowTexture(), transparent: true, depthWrite: false }));
|
|
1562
1571
|
this.neon.rotation.x = -Math.PI / 2;
|
|
1563
1572
|
this.neon.position.set(center.x, box.min.y + size.y * 0.01, center.z);
|
|
1573
|
+
this.neon.userData.decoration = true;
|
|
1564
1574
|
this.root.add(this.neon);
|
|
1565
1575
|
}
|
|
1566
1576
|
setColor(this.neon.material.color, color);
|
|
@@ -1649,12 +1659,157 @@ class Assets {
|
|
|
1649
1659
|
}
|
|
1650
1660
|
}
|
|
1651
1661
|
// src/stage.ts
|
|
1652
|
-
import * as
|
|
1662
|
+
import * as THREE6 from "three";
|
|
1653
1663
|
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
|
1654
|
-
|
|
1664
|
+
|
|
1665
|
+
// src/picker.ts
|
|
1666
|
+
import * as THREE5 from "three";
|
|
1667
|
+
var CODE_BITS = 8;
|
|
1668
|
+
var CODE_MASK = 255;
|
|
1669
|
+
var VERTEX = `
|
|
1670
|
+
#include <common>
|
|
1671
|
+
#include <skinning_pars_vertex>
|
|
1672
|
+
attribute vec3 pickCode;
|
|
1673
|
+
uniform vec3 objectCode;
|
|
1674
|
+
uniform bool perVertex;
|
|
1675
|
+
varying vec3 vCode;
|
|
1676
|
+
void main() {
|
|
1677
|
+
vCode = perVertex ? pickCode : objectCode;
|
|
1678
|
+
#include <skinbase_vertex>
|
|
1679
|
+
#include <begin_vertex>
|
|
1680
|
+
#include <skinning_vertex>
|
|
1681
|
+
#include <project_vertex>
|
|
1682
|
+
}
|
|
1683
|
+
`;
|
|
1684
|
+
var FRAGMENT = `
|
|
1685
|
+
varying vec3 vCode;
|
|
1686
|
+
void main() {
|
|
1687
|
+
gl_FragColor = vec4(vCode, 1.0);
|
|
1688
|
+
}
|
|
1689
|
+
`;
|
|
1690
|
+
function encode(code) {
|
|
1691
|
+
return [code & CODE_MASK, code >> CODE_BITS];
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
class Picker {
|
|
1695
|
+
target = new THREE5.WebGLRenderTarget(1, 1);
|
|
1696
|
+
pixel = new Uint8Array(4);
|
|
1697
|
+
codes = new Map;
|
|
1698
|
+
nodes = [];
|
|
1699
|
+
clearColor = new THREE5.Color;
|
|
1700
|
+
material = new THREE5.ShaderMaterial({
|
|
1701
|
+
vertexShader: VERTEX,
|
|
1702
|
+
fragmentShader: FRAGMENT,
|
|
1703
|
+
uniforms: { objectCode: { value: new THREE5.Vector3 }, perVertex: { value: false } }
|
|
1704
|
+
});
|
|
1705
|
+
constructor() {
|
|
1706
|
+
this.material.onBeforeRender = (_renderer, _scene, _camera, geometry, object) => {
|
|
1707
|
+
const perVertex = geometry.hasAttribute("pickCode");
|
|
1708
|
+
const [low, high] = encode(perVertex ? 0 : this.codeOf(object));
|
|
1709
|
+
this.material.uniforms.perVertex.value = perVertex;
|
|
1710
|
+
this.material.uniforms.objectCode.value.set(low / CODE_MASK, high / CODE_MASK, 0);
|
|
1711
|
+
this.material.uniformsNeedUpdate = true;
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1714
|
+
pick(renderer, scene, root, camera, x, y, width, height) {
|
|
1715
|
+
this.prepare(root);
|
|
1716
|
+
const hidden = scene.children.filter((child) => child !== root && child.visible && child.isMesh);
|
|
1717
|
+
const previous = { target: renderer.getRenderTarget(), alpha: renderer.getClearAlpha(), shadows: renderer.shadowMap.autoUpdate };
|
|
1718
|
+
renderer.getClearColor(this.clearColor);
|
|
1719
|
+
for (const child of hidden)
|
|
1720
|
+
child.visible = false;
|
|
1721
|
+
scene.overrideMaterial = this.material;
|
|
1722
|
+
renderer.shadowMap.autoUpdate = false;
|
|
1723
|
+
const view = camera.view && { ...camera.view };
|
|
1724
|
+
camera.setViewOffset(view?.fullWidth ?? width, view?.fullHeight ?? height, x + (view?.offsetX ?? 0), y + (view?.offsetY ?? 0), 1, 1);
|
|
1725
|
+
renderer.setRenderTarget(this.target);
|
|
1726
|
+
renderer.setClearColor(0, 0);
|
|
1727
|
+
renderer.clear();
|
|
1728
|
+
renderer.render(scene, camera);
|
|
1729
|
+
renderer.readRenderTargetPixels(this.target, 0, 0, 1, 1, this.pixel);
|
|
1730
|
+
renderer.setRenderTarget(previous.target);
|
|
1731
|
+
renderer.setClearColor(this.clearColor, previous.alpha);
|
|
1732
|
+
renderer.shadowMap.autoUpdate = previous.shadows;
|
|
1733
|
+
if (view)
|
|
1734
|
+
camera.setViewOffset(view.fullWidth, view.fullHeight, view.offsetX, view.offsetY, view.width, view.height);
|
|
1735
|
+
else
|
|
1736
|
+
camera.clearViewOffset();
|
|
1737
|
+
scene.overrideMaterial = null;
|
|
1738
|
+
for (const child of hidden)
|
|
1739
|
+
child.visible = true;
|
|
1740
|
+
const code = this.pixel[0] + (this.pixel[1] << CODE_BITS);
|
|
1741
|
+
return this.nodes[code - 1] ?? null;
|
|
1742
|
+
}
|
|
1743
|
+
async warm(renderer, root, camera, scene) {
|
|
1744
|
+
this.prepare(root);
|
|
1745
|
+
const meshes = [];
|
|
1746
|
+
root.traverse((object) => {
|
|
1747
|
+
const mesh = object;
|
|
1748
|
+
if (mesh.isMesh)
|
|
1749
|
+
meshes.push([mesh, mesh.material]);
|
|
1750
|
+
});
|
|
1751
|
+
for (const [mesh] of meshes)
|
|
1752
|
+
mesh.material = this.material;
|
|
1753
|
+
const compiled = renderer.compileAsync(root, camera, scene);
|
|
1754
|
+
for (const [mesh, material] of meshes)
|
|
1755
|
+
mesh.material = material;
|
|
1756
|
+
await compiled;
|
|
1757
|
+
}
|
|
1758
|
+
dispose() {
|
|
1759
|
+
this.target.dispose();
|
|
1760
|
+
this.material.dispose();
|
|
1761
|
+
}
|
|
1762
|
+
prepare(root) {
|
|
1763
|
+
root.traverse((object) => {
|
|
1764
|
+
const mesh = object;
|
|
1765
|
+
if (!mesh.isSkinnedMesh || mesh.geometry.hasAttribute("pickCode"))
|
|
1766
|
+
return;
|
|
1767
|
+
const indices = mesh.geometry.getAttribute("skinIndex");
|
|
1768
|
+
const weights = mesh.geometry.getAttribute("skinWeight");
|
|
1769
|
+
const boneCodes = mesh.skeleton.bones.map((bone) => encode(this.codeOf(bone)));
|
|
1770
|
+
const meshCode = encode(this.codeOf(mesh));
|
|
1771
|
+
const codes = new Uint8Array(indices.count * 3);
|
|
1772
|
+
const influences = weights.itemSize;
|
|
1773
|
+
const read = (attribute, vertex, influence) => attribute instanceof THREE5.BufferAttribute ? attribute.array[vertex * influences + influence] : attribute.getComponent(vertex, influence);
|
|
1774
|
+
for (let vertex = 0;vertex < indices.count; vertex++) {
|
|
1775
|
+
let heaviest = 0;
|
|
1776
|
+
for (let influence = 1;influence < influences; influence++) {
|
|
1777
|
+
if (read(weights, vertex, influence) > read(weights, vertex, heaviest))
|
|
1778
|
+
heaviest = influence;
|
|
1779
|
+
}
|
|
1780
|
+
const [low, high] = boneCodes[read(indices, vertex, heaviest)] ?? meshCode;
|
|
1781
|
+
codes[vertex * 3] = low;
|
|
1782
|
+
codes[vertex * 3 + 1] = high;
|
|
1783
|
+
}
|
|
1784
|
+
mesh.geometry.setAttribute("pickCode", new THREE5.BufferAttribute(codes, 3, true));
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
codeOf(node) {
|
|
1788
|
+
let code = this.codes.get(node);
|
|
1789
|
+
if (code === undefined) {
|
|
1790
|
+
this.nodes.push(node);
|
|
1791
|
+
code = this.nodes.length;
|
|
1792
|
+
this.codes.set(node, code);
|
|
1793
|
+
}
|
|
1794
|
+
return code;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// src/stage.ts
|
|
1799
|
+
var KEY_DIRECTION = new THREE6.Vector3(-0.55, 1, 0.35).normalize();
|
|
1655
1800
|
var AUTO_ROTATE_SPEED = 0.8;
|
|
1656
1801
|
var STAGE_MARGIN = 1.12;
|
|
1657
1802
|
var TURN_SAMPLES = 24;
|
|
1803
|
+
var VIEW_DURATION = 900;
|
|
1804
|
+
var LIGHTING_DURATION = 1200;
|
|
1805
|
+
var ENTRANCE_DURATION = 1800;
|
|
1806
|
+
var ENTRANCE_TURN = 0.7;
|
|
1807
|
+
var ENTRANCE_DROP = 0.2;
|
|
1808
|
+
var ENTRANCE_PULL = 1.5;
|
|
1809
|
+
var LIGHTINGS = {
|
|
1810
|
+
studio: { environment: 1.15, key: 1.9, keyColor: new THREE6.Color(16774118), fill: 0.7, exposure: 0.7, shadow: 0.25 },
|
|
1811
|
+
night: { environment: 0.08, key: 0.55, keyColor: new THREE6.Color(9414911), fill: 0.12, exposure: 1, shadow: 0.12 }
|
|
1812
|
+
};
|
|
1658
1813
|
var SOFT_BOXES = [
|
|
1659
1814
|
[0.1, [30, 0.1, 30], [0, -1.3, 0]],
|
|
1660
1815
|
[5, [3.2, 0.1, 15], [-6.5, 6, 0]],
|
|
@@ -1663,12 +1818,17 @@ var SOFT_BOXES = [
|
|
|
1663
1818
|
[2.2, [0.1, 2.2, 18], [-12, 0.6, 0]],
|
|
1664
1819
|
[2.2, [0.1, 2.2, 18], [12, 0.6, 0]]
|
|
1665
1820
|
];
|
|
1821
|
+
var mix = (from, to, amount) => from + (to - from) * amount;
|
|
1822
|
+
var easeInOutCubic = (t) => t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2;
|
|
1823
|
+
var easeOutQuint = (t) => 1 - (1 - t) ** 5;
|
|
1824
|
+
var smoothstep = (t) => t * t * (3 - 2 * t);
|
|
1825
|
+
var shortestTurn = (angle) => Math.atan2(Math.sin(angle), Math.cos(angle));
|
|
1666
1826
|
function studio() {
|
|
1667
|
-
const scene = new
|
|
1668
|
-
scene.background = new
|
|
1669
|
-
const geometry = new
|
|
1827
|
+
const scene = new THREE6.Scene;
|
|
1828
|
+
scene.background = new THREE6.Color(0.16, 0.16, 0.16);
|
|
1829
|
+
const geometry = new THREE6.BoxGeometry;
|
|
1670
1830
|
for (const [brightness, size, position] of SOFT_BOXES) {
|
|
1671
|
-
const box = new
|
|
1831
|
+
const box = new THREE6.Mesh(geometry, new THREE6.MeshBasicMaterial({ color: new THREE6.Color(brightness, brightness, brightness) }));
|
|
1672
1832
|
box.scale.fromArray(size);
|
|
1673
1833
|
box.position.fromArray(position);
|
|
1674
1834
|
scene.add(box);
|
|
@@ -1677,22 +1837,34 @@ function studio() {
|
|
|
1677
1837
|
}
|
|
1678
1838
|
|
|
1679
1839
|
class Lighting {
|
|
1680
|
-
|
|
1840
|
+
renderer;
|
|
1841
|
+
scene;
|
|
1842
|
+
key = new THREE6.DirectionalLight;
|
|
1843
|
+
fill = new THREE6.DirectionalLight(14674431);
|
|
1681
1844
|
environment;
|
|
1682
1845
|
constructor(renderer, scene, camera) {
|
|
1683
|
-
renderer
|
|
1684
|
-
|
|
1846
|
+
this.renderer = renderer;
|
|
1847
|
+
this.scene = scene;
|
|
1848
|
+
renderer.toneMapping = THREE6.ACESFilmicToneMapping;
|
|
1685
1849
|
renderer.shadowMap.enabled = true;
|
|
1686
|
-
const pmrem = new
|
|
1850
|
+
const pmrem = new THREE6.PMREMGenerator(renderer);
|
|
1687
1851
|
this.environment = pmrem.fromScene(studio(), 0.008, 0.1, 80, { size: 512 });
|
|
1688
1852
|
pmrem.dispose();
|
|
1689
1853
|
scene.environment = this.environment.texture;
|
|
1690
|
-
scene.environmentIntensity = 1.15;
|
|
1691
1854
|
this.key.castShadow = true;
|
|
1692
1855
|
this.key.shadow.mapSize.setScalar(2048);
|
|
1693
1856
|
this.key.shadow.radius = 6;
|
|
1694
|
-
camera.add(
|
|
1857
|
+
camera.add(this.fill);
|
|
1695
1858
|
scene.add(this.key, camera);
|
|
1859
|
+
this.blend(0);
|
|
1860
|
+
}
|
|
1861
|
+
blend(night) {
|
|
1862
|
+
const { studio: day, night: dark } = LIGHTINGS;
|
|
1863
|
+
this.renderer.toneMappingExposure = mix(day.exposure, dark.exposure, night);
|
|
1864
|
+
this.scene.environmentIntensity = mix(day.environment, dark.environment, night);
|
|
1865
|
+
this.key.intensity = mix(day.key, dark.key, night);
|
|
1866
|
+
this.key.color.lerpColors(day.keyColor, dark.keyColor, night);
|
|
1867
|
+
this.fill.intensity = mix(day.fill, dark.fill, night);
|
|
1696
1868
|
}
|
|
1697
1869
|
fit(radius) {
|
|
1698
1870
|
this.key.position.copy(KEY_DIRECTION).multiplyScalar(radius * 3);
|
|
@@ -1703,30 +1875,40 @@ class Lighting {
|
|
|
1703
1875
|
dispose() {
|
|
1704
1876
|
this.environment.dispose();
|
|
1705
1877
|
this.key.dispose();
|
|
1878
|
+
this.fill.dispose();
|
|
1706
1879
|
}
|
|
1707
1880
|
}
|
|
1708
1881
|
|
|
1709
1882
|
class Stage {
|
|
1710
1883
|
canvas;
|
|
1711
1884
|
renderer;
|
|
1712
|
-
scene = new
|
|
1713
|
-
camera = new
|
|
1885
|
+
scene = new THREE6.Scene;
|
|
1886
|
+
camera = new THREE6.PerspectiveCamera(30, 1, 0.05, 300);
|
|
1714
1887
|
controls;
|
|
1715
1888
|
lighting;
|
|
1716
|
-
ground = new
|
|
1889
|
+
ground = new THREE6.Mesh(new THREE6.PlaneGeometry(4, 4), new THREE6.ShadowMaterial({ opacity: LIGHTINGS.studio.shadow }));
|
|
1717
1890
|
resizer;
|
|
1718
1891
|
watcher;
|
|
1892
|
+
entrance;
|
|
1893
|
+
inset;
|
|
1719
1894
|
subject = null;
|
|
1895
|
+
picker = null;
|
|
1896
|
+
size = new THREE6.Vector3;
|
|
1720
1897
|
showing = 0;
|
|
1721
1898
|
visible = true;
|
|
1899
|
+
night = 0;
|
|
1900
|
+
flight = null;
|
|
1901
|
+
fade = null;
|
|
1722
1902
|
constructor(canvas, options = {}) {
|
|
1723
1903
|
this.canvas = canvas;
|
|
1724
|
-
this.renderer = new
|
|
1904
|
+
this.renderer = new THREE6.WebGLRenderer({ canvas, antialias: true, alpha: true });
|
|
1725
1905
|
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
|
1726
1906
|
this.lighting = new Lighting(this.renderer, this.scene, this.camera);
|
|
1727
1907
|
this.ground.rotation.x = -Math.PI / 2;
|
|
1728
1908
|
this.ground.receiveShadow = true;
|
|
1729
1909
|
this.scene.add(this.ground);
|
|
1910
|
+
this.entrance = options.entrance ?? false;
|
|
1911
|
+
this.inset = { top: options.inset?.top ?? 0, bottom: options.inset?.bottom ?? 0 };
|
|
1730
1912
|
this.controls = new OrbitControls(this.camera, canvas);
|
|
1731
1913
|
this.controls.enableDamping = true;
|
|
1732
1914
|
this.controls.maxPolarAngle = Math.PI / 2 - 0.02;
|
|
@@ -1735,7 +1917,7 @@ class Stage {
|
|
|
1735
1917
|
if (!this.controls.enableZoom)
|
|
1736
1918
|
canvas.style.touchAction = "pan-y";
|
|
1737
1919
|
this.controls.autoRotateSpeed = AUTO_ROTATE_SPEED;
|
|
1738
|
-
this.controls.addEventListener("start", this.
|
|
1920
|
+
this.controls.addEventListener("start", this.yieldToUser);
|
|
1739
1921
|
this.watcher = new IntersectionObserver(([entry]) => {
|
|
1740
1922
|
this.visible = entry.isIntersecting;
|
|
1741
1923
|
});
|
|
@@ -1743,9 +1925,10 @@ class Stage {
|
|
|
1743
1925
|
this.resizer = new ResizeObserver(() => this.resize());
|
|
1744
1926
|
this.resizer.observe(canvas);
|
|
1745
1927
|
this.resize();
|
|
1746
|
-
this.renderer.setAnimationLoop(() => {
|
|
1928
|
+
this.renderer.setAnimationLoop((time) => {
|
|
1747
1929
|
if (!this.visible || canvas.clientWidth === 0)
|
|
1748
1930
|
return;
|
|
1931
|
+
this.advance(time);
|
|
1749
1932
|
this.controls.update();
|
|
1750
1933
|
this.renderer.render(this.scene, this.camera);
|
|
1751
1934
|
});
|
|
@@ -1756,48 +1939,131 @@ class Stage {
|
|
|
1756
1939
|
this.scene.remove(this.subject.root);
|
|
1757
1940
|
this.subject.dispose();
|
|
1758
1941
|
this.subject = null;
|
|
1942
|
+
this.picker?.dispose();
|
|
1943
|
+
this.picker = null;
|
|
1759
1944
|
assets.clear();
|
|
1760
1945
|
}
|
|
1761
1946
|
const subject = await assets.load(name, props);
|
|
1947
|
+
const picker = new Picker;
|
|
1762
1948
|
if (showing === this.showing) {
|
|
1763
|
-
await Promise.all([
|
|
1949
|
+
await Promise.all([
|
|
1950
|
+
this.renderer.compileAsync(subject.root, this.camera, this.scene),
|
|
1951
|
+
this.renderer.compileAsync(this.scene, this.camera),
|
|
1952
|
+
picker.warm(this.renderer, subject.root, this.camera, this.scene)
|
|
1953
|
+
]);
|
|
1764
1954
|
}
|
|
1765
1955
|
if (showing !== this.showing) {
|
|
1766
1956
|
subject.dispose();
|
|
1957
|
+
picker.dispose();
|
|
1767
1958
|
return;
|
|
1768
1959
|
}
|
|
1769
1960
|
this.subject = subject;
|
|
1961
|
+
this.picker = picker;
|
|
1770
1962
|
this.frame(subject);
|
|
1963
|
+
if (this.entrance)
|
|
1964
|
+
this.enter();
|
|
1771
1965
|
this.renderer.render(this.scene, this.camera);
|
|
1772
1966
|
return subject;
|
|
1773
1967
|
}
|
|
1968
|
+
view(direction, { duration = VIEW_DURATION } = {}) {
|
|
1969
|
+
if (!this.subject)
|
|
1970
|
+
return Promise.resolve(false);
|
|
1971
|
+
this.controls.autoRotate = false;
|
|
1972
|
+
return this.fly(this.offset(direction ?? this.subject.view), duration, easeInOutCubic);
|
|
1973
|
+
}
|
|
1974
|
+
setAutoRotate(on) {
|
|
1975
|
+
this.controls.autoRotate = on;
|
|
1976
|
+
}
|
|
1977
|
+
setLighting(lighting, { duration = LIGHTING_DURATION } = {}) {
|
|
1978
|
+
this.fade = { from: this.night, to: lighting === "night" ? 1 : 0, start: performance.now(), duration: Math.max(duration, 1) };
|
|
1979
|
+
}
|
|
1980
|
+
pick(clientX, clientY) {
|
|
1981
|
+
if (!this.subject || !this.picker)
|
|
1982
|
+
return null;
|
|
1983
|
+
const bounds = this.canvas.getBoundingClientRect();
|
|
1984
|
+
return this.picker.pick(this.renderer, this.scene, this.subject.root, this.camera, clientX - bounds.left, clientY - bounds.top, bounds.width, bounds.height);
|
|
1985
|
+
}
|
|
1986
|
+
photo(width, height) {
|
|
1987
|
+
const ratio = this.renderer.getPixelRatio();
|
|
1988
|
+
this.renderer.setPixelRatio(1);
|
|
1989
|
+
this.renderer.setSize(width, height, false);
|
|
1990
|
+
this.camera.aspect = width / height;
|
|
1991
|
+
this.camera.clearViewOffset();
|
|
1992
|
+
this.renderer.render(this.scene, this.camera);
|
|
1993
|
+
const photo = new Promise((resolve, reject) => this.canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("Could not encode the photo")), "image/png"));
|
|
1994
|
+
this.renderer.setPixelRatio(ratio);
|
|
1995
|
+
this.resize();
|
|
1996
|
+
this.renderer.render(this.scene, this.camera);
|
|
1997
|
+
return photo;
|
|
1998
|
+
}
|
|
1774
1999
|
dispose() {
|
|
1775
2000
|
this.showing++;
|
|
2001
|
+
this.flight?.done(false);
|
|
1776
2002
|
this.subject?.dispose();
|
|
2003
|
+
this.picker?.dispose();
|
|
1777
2004
|
this.resizer.disconnect();
|
|
1778
2005
|
this.watcher.disconnect();
|
|
1779
2006
|
this.renderer.setAnimationLoop(null);
|
|
1780
|
-
this.controls.removeEventListener("start", this.
|
|
2007
|
+
this.controls.removeEventListener("start", this.yieldToUser);
|
|
1781
2008
|
this.controls.dispose();
|
|
1782
2009
|
this.lighting.dispose();
|
|
1783
2010
|
this.renderer.dispose();
|
|
1784
2011
|
this.renderer.forceContextLoss();
|
|
1785
2012
|
}
|
|
1786
|
-
|
|
2013
|
+
yieldToUser = () => {
|
|
1787
2014
|
this.controls.autoRotate = false;
|
|
2015
|
+
this.flight?.done(false);
|
|
2016
|
+
this.flight = null;
|
|
1788
2017
|
};
|
|
1789
|
-
|
|
1790
|
-
const
|
|
1791
|
-
|
|
1792
|
-
|
|
2018
|
+
advance(now) {
|
|
2019
|
+
const { flight, fade } = this;
|
|
2020
|
+
if (flight) {
|
|
2021
|
+
const progress = THREE6.MathUtils.clamp((now - flight.start) / flight.duration, 0, 1);
|
|
2022
|
+
const eased = flight.ease(progress);
|
|
2023
|
+
const { from, to } = flight;
|
|
2024
|
+
this.camera.position.setFromSphericalCoords(mix(from.radius, to.radius, eased), mix(from.phi, to.phi, eased), mix(from.theta, to.theta, eased)).add(this.controls.target);
|
|
2025
|
+
if (progress === 1) {
|
|
2026
|
+
this.flight = null;
|
|
2027
|
+
flight.done(true);
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
if (fade) {
|
|
2031
|
+
const progress = THREE6.MathUtils.clamp((now - fade.start) / fade.duration, 0, 1);
|
|
2032
|
+
this.night = mix(fade.from, fade.to, smoothstep(progress));
|
|
2033
|
+
this.lighting.blend(this.night);
|
|
2034
|
+
this.ground.material.opacity = mix(LIGHTINGS.studio.shadow, LIGHTINGS.night.shadow, this.night);
|
|
2035
|
+
if (progress === 1)
|
|
2036
|
+
this.fade = null;
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
fly(offset, duration, ease) {
|
|
2040
|
+
this.flight?.done(false);
|
|
2041
|
+
const from = new THREE6.Spherical().setFromVector3(this.camera.position.clone().sub(this.controls.target));
|
|
2042
|
+
const to = new THREE6.Spherical().setFromVector3(offset);
|
|
2043
|
+
to.theta = from.theta + shortestTurn(to.theta - from.theta);
|
|
2044
|
+
return new Promise((done) => {
|
|
2045
|
+
this.flight = { from, to, start: performance.now(), duration: Math.max(duration, 1), ease, done };
|
|
2046
|
+
});
|
|
2047
|
+
}
|
|
2048
|
+
enter() {
|
|
2049
|
+
const hero = this.camera.position.clone().sub(this.controls.target);
|
|
2050
|
+
const start = new THREE6.Spherical().setFromVector3(hero);
|
|
2051
|
+
start.theta += ENTRANCE_TURN;
|
|
2052
|
+
start.phi = Math.min(start.phi + ENTRANCE_DROP, this.controls.maxPolarAngle);
|
|
2053
|
+
start.radius *= ENTRANCE_PULL;
|
|
2054
|
+
this.camera.position.setFromSpherical(start).add(this.controls.target);
|
|
2055
|
+
this.controls.update();
|
|
2056
|
+
this.fly(hero, ENTRANCE_DURATION, easeOutQuint);
|
|
2057
|
+
}
|
|
2058
|
+
offset(direction) {
|
|
2059
|
+
const { size } = this;
|
|
2060
|
+
const slopeY = Math.tan(THREE6.MathUtils.degToRad(this.camera.fov / 2));
|
|
1793
2061
|
const slopeX = slopeY * this.camera.aspect;
|
|
1794
|
-
const radius = size.length() / 2;
|
|
1795
2062
|
const reach = Math.hypot(size.x, size.z) / 2;
|
|
1796
|
-
const
|
|
1797
|
-
const
|
|
1798
|
-
const
|
|
1799
|
-
const
|
|
1800
|
-
const point = new THREE5.Vector3;
|
|
2063
|
+
const toCamera = new THREE6.Vector3(...direction).normalize();
|
|
2064
|
+
const right = new THREE6.Vector3().crossVectors(new THREE6.Vector3(0, 1, 0), toCamera).normalize();
|
|
2065
|
+
const up = new THREE6.Vector3().crossVectors(toCamera, right);
|
|
2066
|
+
const point = new THREE6.Vector3;
|
|
1801
2067
|
let distance = 0;
|
|
1802
2068
|
for (let turn = 0;turn < TURN_SAMPLES; turn++) {
|
|
1803
2069
|
const angle = turn / TURN_SAMPLES * Math.PI * 2;
|
|
@@ -1807,22 +2073,34 @@ class Stage {
|
|
|
1807
2073
|
distance = Math.max(distance, depth + Math.abs(point.dot(right)) * STAGE_MARGIN / slopeX, depth + Math.abs(point.dot(up)) * STAGE_MARGIN / slopeY);
|
|
1808
2074
|
}
|
|
1809
2075
|
}
|
|
2076
|
+
return toCamera.multiplyScalar(distance);
|
|
2077
|
+
}
|
|
2078
|
+
frame(subject) {
|
|
2079
|
+
this.size = subject.placeAtOrigin().getSize(new THREE6.Vector3);
|
|
2080
|
+
this.scene.add(subject.root);
|
|
2081
|
+
const radius = this.size.length() / 2;
|
|
2082
|
+
const target = new THREE6.Vector3(0, this.size.y / 2, 0);
|
|
1810
2083
|
this.lighting.fit(radius);
|
|
1811
2084
|
this.ground.scale.setScalar(radius);
|
|
1812
2085
|
Object.assign(this.camera, { near: radius * 0.02, far: radius * 20 }).updateProjectionMatrix();
|
|
1813
2086
|
this.controls.target.copy(target);
|
|
1814
2087
|
this.controls.minDistance = radius * 0.2;
|
|
1815
2088
|
this.controls.maxDistance = radius * 8;
|
|
1816
|
-
this.camera.position.copy(target).
|
|
2089
|
+
this.camera.position.copy(target).add(this.offset(subject.view));
|
|
1817
2090
|
this.controls.update();
|
|
1818
2091
|
}
|
|
1819
2092
|
resize() {
|
|
1820
2093
|
const { clientWidth: width, clientHeight: height } = this.canvas;
|
|
1821
2094
|
if (width === 0 || height === 0)
|
|
1822
2095
|
return;
|
|
2096
|
+
const { top, bottom } = this.inset;
|
|
2097
|
+
const framed = Math.max(height - top - bottom, 1);
|
|
1823
2098
|
this.renderer.setSize(width, height, false);
|
|
1824
|
-
this.camera.aspect = width /
|
|
1825
|
-
|
|
2099
|
+
this.camera.aspect = width / framed;
|
|
2100
|
+
if (top || bottom)
|
|
2101
|
+
this.camera.setViewOffset(width, framed, 0, -top, width, height);
|
|
2102
|
+
else
|
|
2103
|
+
this.camera.updateProjectionMatrix();
|
|
1826
2104
|
}
|
|
1827
2105
|
}
|
|
1828
2106
|
var FIELD_OF_VIEW = 20;
|
|
@@ -1835,14 +2113,14 @@ class ThumbnailRenderer {
|
|
|
1835
2113
|
size;
|
|
1836
2114
|
format;
|
|
1837
2115
|
renderer;
|
|
1838
|
-
scene = new
|
|
1839
|
-
camera = new
|
|
2116
|
+
scene = new THREE6.Scene;
|
|
2117
|
+
camera = new THREE6.PerspectiveCamera(FIELD_OF_VIEW, 1);
|
|
1840
2118
|
lighting;
|
|
1841
2119
|
pixels;
|
|
1842
2120
|
constructor(size = 512, format = "png") {
|
|
1843
2121
|
this.size = size;
|
|
1844
2122
|
this.format = format;
|
|
1845
|
-
this.renderer = new
|
|
2123
|
+
this.renderer = new THREE6.WebGLRenderer({ antialias: true, alpha: true, preserveDrawingBuffer: true });
|
|
1846
2124
|
this.renderer.setSize(size, size, false);
|
|
1847
2125
|
this.renderer.setClearColor(0, 0);
|
|
1848
2126
|
this.lighting = new Lighting(this.renderer, this.scene, this.camera);
|
|
@@ -1850,15 +2128,15 @@ class ThumbnailRenderer {
|
|
|
1850
2128
|
}
|
|
1851
2129
|
async render(model, view = model.view) {
|
|
1852
2130
|
const box = model.placeAtOrigin();
|
|
1853
|
-
const center = box.getCenter(new
|
|
1854
|
-
const radius = box.getSize(new
|
|
1855
|
-
const toCamera = new
|
|
1856
|
-
const right = new
|
|
1857
|
-
const up = new
|
|
1858
|
-
const slope = Math.tan(
|
|
2131
|
+
const center = box.getCenter(new THREE6.Vector3);
|
|
2132
|
+
const radius = box.getSize(new THREE6.Vector3).length() / 2;
|
|
2133
|
+
const toCamera = new THREE6.Vector3(...view).normalize();
|
|
2134
|
+
const right = new THREE6.Vector3().crossVectors(new THREE6.Vector3(0, 1, 0), toCamera).normalize();
|
|
2135
|
+
const up = new THREE6.Vector3().crossVectors(toCamera, right);
|
|
2136
|
+
const slope = Math.tan(THREE6.MathUtils.degToRad(FIELD_OF_VIEW / 2));
|
|
1859
2137
|
let distance = 0;
|
|
1860
2138
|
for (let corner = 0;corner < 8; corner++) {
|
|
1861
|
-
const point = new
|
|
2139
|
+
const point = new THREE6.Vector3(corner & 1 ? box.max.x : box.min.x, corner & 2 ? box.max.y : box.min.y, corner & 4 ? box.max.z : box.min.z).sub(center);
|
|
1862
2140
|
const extent = Math.max(Math.abs(point.dot(right)), Math.abs(point.dot(up))) * MARGIN;
|
|
1863
2141
|
distance = Math.max(distance, point.dot(toCamera) + extent / slope);
|
|
1864
2142
|
}
|
|
@@ -1868,28 +2146,42 @@ class ThumbnailRenderer {
|
|
|
1868
2146
|
this.lighting.fit(radius);
|
|
1869
2147
|
this.scene.add(model.root);
|
|
1870
2148
|
try {
|
|
1871
|
-
this.draw(model
|
|
2149
|
+
this.draw(model);
|
|
1872
2150
|
} finally {
|
|
1873
2151
|
this.scene.remove(model.root);
|
|
1874
2152
|
this.camera.clearViewOffset();
|
|
1875
2153
|
}
|
|
1876
2154
|
return new Promise((resolve, reject) => this.renderer.domElement.toBlob((blob) => blob ? resolve(blob) : reject(new Error(`Could not encode ${model.name}`)), `image/${this.format}`, WEBP_QUALITY));
|
|
1877
2155
|
}
|
|
1878
|
-
draw(
|
|
2156
|
+
draw(model) {
|
|
2157
|
+
const decorations = [];
|
|
2158
|
+
model.root.traverse((object) => {
|
|
2159
|
+
if (object.visible && object.userData.decoration)
|
|
2160
|
+
decorations.push(object);
|
|
2161
|
+
});
|
|
1879
2162
|
let frame = { x: 0, y: 0, side: this.size };
|
|
1880
|
-
for (
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
2163
|
+
for (const object of decorations)
|
|
2164
|
+
object.visible = false;
|
|
2165
|
+
try {
|
|
2166
|
+
for (let pass = 1;pass < FRAMING_PASSES; pass++) {
|
|
2167
|
+
this.shoot(frame);
|
|
2168
|
+
const fitted = this.fit(frame);
|
|
2169
|
+
if (!fitted)
|
|
2170
|
+
throw new Error(`${model.name} has nothing to show`);
|
|
2171
|
+
const pixel = frame.side / this.size;
|
|
2172
|
+
if (Math.max(Math.abs(fitted.x - frame.x), Math.abs(fitted.y - frame.y), Math.abs(fitted.side - frame.side)) < 2 * pixel)
|
|
2173
|
+
break;
|
|
2174
|
+
frame = fitted;
|
|
2175
|
+
}
|
|
2176
|
+
} finally {
|
|
2177
|
+
for (const object of decorations)
|
|
2178
|
+
object.visible = true;
|
|
1892
2179
|
}
|
|
2180
|
+
this.shoot(frame);
|
|
2181
|
+
}
|
|
2182
|
+
shoot({ x, y, side }) {
|
|
2183
|
+
this.camera.setViewOffset(this.size, this.size, x, y, side, side);
|
|
2184
|
+
this.renderer.render(this.scene, this.camera);
|
|
1893
2185
|
}
|
|
1894
2186
|
fit(frame) {
|
|
1895
2187
|
const { size, pixels } = this;
|
package/package.json
CHANGED
package/picker.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
export declare class Picker {
|
|
3
|
+
private readonly target;
|
|
4
|
+
private readonly pixel;
|
|
5
|
+
private readonly codes;
|
|
6
|
+
private readonly nodes;
|
|
7
|
+
private readonly clearColor;
|
|
8
|
+
private readonly material;
|
|
9
|
+
constructor();
|
|
10
|
+
pick(renderer: THREE.WebGLRenderer, scene: THREE.Scene, root: THREE.Object3D, camera: THREE.PerspectiveCamera, x: number, y: number, width: number, height: number): THREE.Object3D | null;
|
|
11
|
+
warm(renderer: THREE.WebGLRenderer, root: THREE.Object3D, camera: THREE.Camera, scene: THREE.Scene): Promise<void>;
|
|
12
|
+
dispose(): void;
|
|
13
|
+
private prepare;
|
|
14
|
+
private codeOf;
|
|
15
|
+
}
|
package/stage.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
1
2
|
import type { Assets } from "./assets";
|
|
2
3
|
import type { Model } from "./model";
|
|
3
4
|
import type { VehicleProps } from "./vehicle";
|
|
5
|
+
export type Direction = readonly [number, number, number];
|
|
6
|
+
export type StageLighting = "studio" | "night";
|
|
7
|
+
export interface StageInset {
|
|
8
|
+
top?: number;
|
|
9
|
+
bottom?: number;
|
|
10
|
+
}
|
|
4
11
|
export interface StageOptions {
|
|
5
12
|
autoRotate?: boolean;
|
|
6
13
|
zoom?: boolean;
|
|
14
|
+
entrance?: boolean;
|
|
15
|
+
inset?: StageInset;
|
|
7
16
|
}
|
|
8
17
|
/** An interactive 3D view of one model, drawn on a canvas. Drag to orbit, scroll to zoom. */
|
|
9
18
|
export declare class Stage {
|
|
@@ -16,15 +25,35 @@ export declare class Stage {
|
|
|
16
25
|
private readonly ground;
|
|
17
26
|
private readonly resizer;
|
|
18
27
|
private readonly watcher;
|
|
28
|
+
private readonly entrance;
|
|
29
|
+
private readonly inset;
|
|
19
30
|
private subject;
|
|
31
|
+
private picker;
|
|
32
|
+
private size;
|
|
20
33
|
private showing;
|
|
21
34
|
private visible;
|
|
35
|
+
private night;
|
|
36
|
+
private flight;
|
|
37
|
+
private fade;
|
|
22
38
|
constructor(canvas: HTMLCanvasElement, options?: StageOptions);
|
|
23
39
|
/** Loads a model and shows it in place of the current one. Resolves to `undefined` if another model was shown meanwhile. */
|
|
24
40
|
show(assets: Assets, name: string, props?: VehicleProps): Promise<Model | undefined>;
|
|
41
|
+
view(direction?: Direction, { duration }?: {
|
|
42
|
+
duration?: number;
|
|
43
|
+
}): Promise<boolean>;
|
|
44
|
+
setAutoRotate(on: boolean): void;
|
|
45
|
+
setLighting(lighting: StageLighting, { duration }?: {
|
|
46
|
+
duration?: number;
|
|
47
|
+
}): void;
|
|
48
|
+
pick(clientX: number, clientY: number): THREE.Object3D | null;
|
|
49
|
+
photo(width: number, height: number): Promise<Blob>;
|
|
25
50
|
/** Frees the renderer and the model on show. */
|
|
26
51
|
dispose(): void;
|
|
27
|
-
private readonly
|
|
52
|
+
private readonly yieldToUser;
|
|
53
|
+
private advance;
|
|
54
|
+
private fly;
|
|
55
|
+
private enter;
|
|
56
|
+
private offset;
|
|
28
57
|
private frame;
|
|
29
58
|
private resize;
|
|
30
59
|
}
|
|
@@ -43,6 +72,7 @@ export declare class ThumbnailRenderer {
|
|
|
43
72
|
/** Renders a model from `view`, filling the image. */
|
|
44
73
|
render(model: Model, view?: readonly [number, number, number]): Promise<Blob>;
|
|
45
74
|
private draw;
|
|
75
|
+
private shoot;
|
|
46
76
|
private fit;
|
|
47
77
|
/** Frees the renderer and its lighting. */
|
|
48
78
|
dispose(): void;
|
package/vehicle.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
1
2
|
import type { ModEntry, VehicleEntry, WheelEntry } from "./assets";
|
|
2
3
|
import type { ModelLoader } from "./loader";
|
|
3
4
|
import { Model } from "./model";
|
|
@@ -64,6 +65,12 @@ export declare class Vehicle extends Model {
|
|
|
64
65
|
private readonly wheelTypes;
|
|
65
66
|
private readonly paints;
|
|
66
67
|
private readonly loader;
|
|
68
|
+
static readonly views: {
|
|
69
|
+
readonly front: readonly [0, 0.12, -1];
|
|
70
|
+
readonly side: readonly [-1, 0.1, 0];
|
|
71
|
+
readonly rear: readonly [0, 0.16, 1];
|
|
72
|
+
readonly top: readonly [0, 1, 0.04];
|
|
73
|
+
};
|
|
67
74
|
readonly view: readonly [-0.6, 0.25, -0.75];
|
|
68
75
|
private readonly rest;
|
|
69
76
|
private readonly hidden;
|
|
@@ -89,6 +96,7 @@ export declare class Vehicle extends Model {
|
|
|
89
96
|
openings(): Opening[];
|
|
90
97
|
/** Opens a door or panel, from 0 (closed) to 1 (fully open). */
|
|
91
98
|
setOpen(name: Opening, amount: number): void;
|
|
99
|
+
openingOf(node: THREE.Object3D): Opening | null;
|
|
92
100
|
/** Lists this vehicle's extra IDs. */
|
|
93
101
|
extras(): number[];
|
|
94
102
|
/** Returns whether an extra is shown. */
|