Debian Codegraph @

web/samples/animation.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>glTF Animation</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>
18
19 const mesh_url = "AnimatedTriangle.gltf";
20
21 Filament.init([mesh_url], () => {
22 window.gltfio = Filament.gltfio;
23 window.Fov = Filament.Camera$Fov;
24 window.LightType = Filament.LightManager$Type;
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
34 const loader = engine.createAssetLoader();
35 const asset = this.asset = loader.createAssetFromJson(mesh_url);
36
37 const sunlight = Filament.EntityManager.get().create();
38 Filament.LightManager.Builder(LightType.SUN).direction([0, 0, -1]).build(engine, sunlight);
39 this.scene.addEntity(sunlight);
40
41 const onDone = () => {
42 loader.delete();
43
44 // Dynamically enable two-sided lighting for testing purposes.
45 const entities = asset.getEntities();
46 const rm = engine.getRenderableManager();
47 const instance = rm.getInstance(entities[0]);
48 rm.getMaterialInstanceAt(instance, 0).setDoubleSided(true)
49 instance.delete();
50
51 scene.addEntities(entities);
52 this.animator = asset.getAnimator();
53 this.animationStartTime = Date.now();
54 };
55 asset.loadResources(onDone);
56
57 this.swapChain = engine.createSwapChain();
58 this.renderer = engine.createRenderer();
59 this.camera = engine.createCamera(Filament.EntityManager.get().create());
60
61 this.view = engine.createView();
62 this.view.setCamera(this.camera);
63 this.view.setScene(this.scene);
64
65 this.renderer.setClearOptions({clearColor: [0.2, 0.3, 0.4, 1.0], clear: true});
66
67 this.resize();
68 this.render = this.render.bind(this);
69 this.resize = this.resize.bind(this);
70 window.addEventListener("resize", this.resize);
71 window.requestAnimationFrame(this.render);
72 }
73
74 render() {
75 if (this.animator) {
76 const ms = Date.now() - this.animationStartTime;
77 this.animator.applyAnimation(0, (ms / 1000) % 1.0);
78 this.animator.updateBoneMatrices();
79 }
80
81 this.renderer.render(this.swapChain, this.view);
82 window.requestAnimationFrame(this.render);
83 }
84
85 resize() {
86 const dpr = window.devicePixelRatio;
87 const width = this.canvas.width = window.innerWidth * dpr;
88 const height = this.canvas.height = window.innerHeight * dpr;
89 this.view.setViewport([0, 0, width, height]);
90 const eye = [0, 0, 5], center = [0, 0, 0], up = [0, 1, 0];
91 this.camera.lookAt(eye, center, up);
92 const aspect = width / height;
93 const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
94 this.camera.setProjectionFov(30, aspect, 1.0, 20.0, fov);
95 }
96 }
97 </script>
98 </body>
99 </html>