Debian Codegraph @

web/samples/morphing.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>glTF Morphing</title>
5 <meta charset="utf-8">
6 <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1">
7 <link href="favicon.png" rel="icon" type="image/x-icon" />
8 <style>
9 body { margin: 0; overflow: hidden; }
10 canvas { touch-action: none; width: 100%; height: 100%; }
11 </style>
12 </head>
13 <body>
14 <canvas></canvas>
15 <script src="filament.js"></script>
16 <script src="gl-matrix-min.js"></script>
17 <script src="https://unpkg.com/gltumble"></script>
18 <script>
19
20 const mesh_url = "AnimatedMorphCube.glb";
21
22 Filament.init([mesh_url], () => {
23 window.gltfio = Filament.gltfio;
24 window.Fov = Filament.Camera$Fov;
25 window.app = new App(document.querySelector("canvas"));
26 });
27
28 class App {
29 constructor(canvas) {
30 this.canvas = canvas;
31 const engine = this.engine = Filament.Engine.create(this.canvas);
32 const scene = this.scene = engine.createScene();
33 this.trackball = new Trackball(canvas, {startSpin: 0.035});
34
35 const loader = engine.createAssetLoader();
36 const asset = this.asset = loader.createAssetFromBinary(mesh_url);
37
38 const onDone = () => {
39 loader.delete();
40 scene.addEntities(asset.getEntities());
41 this.animator = asset.getAnimator();
42 this.animationStartTime = Date.now();
43 };
44 asset.loadResources(onDone);
45
46 this.swapChain = engine.createSwapChain();
47 this.renderer = engine.createRenderer();
48 this.camera = engine.createCamera(Filament.EntityManager.get().create());
49
50 this.view = engine.createView();
51 this.view.setCamera(this.camera);
52 this.view.setScene(this.scene);
53
54 this.renderer.setClearOptions({clearColor: [0.6, 0.6, 0.6, 1.0], clear: true});
55
56 this.resize();
57 this.render = this.render.bind(this);
58 this.resize = this.resize.bind(this);
59 window.addEventListener("resize", this.resize);
60 window.requestAnimationFrame(this.render);
61 }
62
63 render() {
64 const tcm = this.engine.getTransformManager();
65 const inst = tcm.getInstance(this.asset.getRoot());
66 tcm.setTransform(inst, this.trackball.getMatrix());
67 inst.delete();
68
69 if (this.animator) {
70 const ms = Date.now() - this.animationStartTime;
71 this.animator.applyAnimation(0, (ms / 1000) % 1.0);
72 this.animator.updateBoneMatrices();
73 }
74
75 this.renderer.render(this.swapChain, this.view);
76 window.requestAnimationFrame(this.render);
77 }
78
79 resize() {
80 const dpr = window.devicePixelRatio;
81 const width = this.canvas.width = window.innerWidth * dpr;
82 const height = this.canvas.height = window.innerHeight * dpr;
83 this.view.setViewport([0, 0, width, height]);
84 const eye = [0, 0, 5], center = [0, 0, 0], up = [0, 1, 0];
85 this.camera.lookAt(eye, center, up);
86 const aspect = width / height;
87 const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
88 this.camera.setProjectionFov(30, aspect, 1.0, 20.0, fov);
89 }
90 }
91 </script>
92 </body>
93 </html>