-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv3dViewer.ts
134 lines (116 loc) · 4.39 KB
/
v3dViewer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import '@babylonjs/inspector';
import { CubicEase } from '@babylonjs/core/Animations/easing';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { DracoCompression } from '@babylonjs/core/Meshes/Compression/dracoCompression';
import { Tools } from '@babylonjs/core/Misc/tools';
import { CameraConfig } from '../config/cameraConfig';
import { Config } from '../config/config';
import { V3D_CONFIG } from '../config/preset/v3dConfig';
import { AbstractScene } from '../scene/abstractScene';
import { V3DScene } from '../scene/v3dScene';
import './viewer.css';
function createCanvas(): HTMLCanvasElement {
const canvas = document.createElement('canvas');
canvas.id = 'renderCanvas';
return canvas;
}
/**
* This file is only for testing/demo purposes
*/
(async function () {
const canvas = createCanvas();
document.body.appendChild(canvas);
// --> Step 1: create an instance of V3DScene
const v3dScene = new V3DScene(canvas, V3D_CONFIG, {
enableDragAndDrop: true,
sceneConfig: {
useLoadingUI: true,
},
});
// --> Step 2: register any observers using V3DScene.observableManager
// Set camera first interaction callback
v3dScene.setOnCameraFirstInteraction(() => {
console.log('CameraFirstInteraction');
});
v3dScene.observableManager.onViewerInitDoneObservable.add((viewer: AbstractScene) => {
if (!viewer) {
return;
}
console.log(`HardwareScalingLevel: ${viewer.engine.getHardwareScalingLevel()}`);
console.log(DracoCompression.Configuration);
});
v3dScene.observableManager.onConfigChangedObservable.add((config: Config) => {
console.log(config);
});
v3dScene.observableManager.onModelLoadedObservable.add((model) => {
console.log(`Bounding box dimensions: ${model.getOverallBoundingBoxDimensions()}`);
model.showShadowOnGroundDepthMap();
model.moveCenterToTargetCoordinate();
const radius = 2 * Math.max(...model.getOverallBoundingBoxDimensions().asArray());
v3dScene.updateConfig({
cameraConfig: {
ArcRotateCamera: {
type: 'arcRotateCamera',
radius: radius,
lowerRadiusLimit: radius * 0.05,
upperRadiusLimit: radius * 5,
minZ: radius * 0.02,
maxZ: radius * 40,
},
},
});
});
// --> Step 3: call V3DScene.init() function
await v3dScene.init();
// --> Step 4: load glTF/glb model
const model = await v3dScene.loadGltf('public/model/mannequin.glb', true);
// --> Step 5: call V3DScene.updateConfig() to update scene setup and/or handle user interactions
const radius = 2 * Math.max(...model.getOverallBoundingBoxDimensions().asArray());
const alphaInDegrees = Tools.ToDegrees(2.5);
const cameraConfigPre: CameraConfig = {
ArcRotateCamera: {
type: 'arcRotateCamera',
enable: true,
attachControl: true,
target: Vector3.Zero(),
alphaInDegrees: alphaInDegrees + 360,
betaInDegrees: Tools.ToDegrees(0.975),
lowerBetaLimitInDegrees: Tools.ToDegrees(0.01),
upperBetaLimitInDegrees: Tools.ToDegrees(3.132),
radius: radius,
lowerRadiusLimit: radius * 0.05,
upperRadiusLimit: radius * 5,
minZ: radius * 0.02,
maxZ: radius * 40,
animationDuration: 0,
minimizeRotation: false,
},
};
const cameraConfig: CameraConfig = {
ArcRotateCamera: {
type: 'arcRotateCamera',
alphaInDegrees: alphaInDegrees,
animationDuration: 2,
cameraAnimationConfig: {
easingFunction: new CubicEase(),
easingMode: 'EASINGMODE_EASEINOUT',
loopMode: 'ANIMATIONLOOPMODE_CONSTANT',
maxFPS: 60,
},
},
};
await v3dScene.updateConfig({
cameraConfig: cameraConfigPre,
});
await v3dScene.updateConfig({
cameraConfig: cameraConfig,
});
// Create keystroke tests
document.addEventListener('keydown', async (event) => {
const key = event.key;
// Pressing '?' should show/hide the debug layer
if (key === '?') {
v3dScene.toggleDebugMode();
}
});
})();