Debian Codegraph @

web/samples/helmet.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>FlightHelmet</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 html, body { height: 100%; }
10 body { margin: 0; overflow: hidden; }
11 #container { position: relative; height: 100%; }
12 canvas { position: absolute; width: 100%; height: 100%; }
13 #messages { position: absolute; width: 100%; height: 100%; padding-left: 10px; color:white; }
14 </style>
15 </head>
16 <body>
17 <div id="container">
18 <canvas></canvas>
19 <pre id="messages"></pre>
20 </div>
21 <script src="filament.js"></script>
22 <script src="gl-matrix-min.js"></script>
23 <script src="https://unpkg.com/gltumble"></script>
24 <script>
25
26 const env = 'default_env';
27 const ibl_url = `${env}/${env}_ibl.ktx`;
28 const sky_url = `${env}/${env}_skybox.ktx`;
29 const mesh_url = 'FlightHelmet.gltf';
30
31 Filament.init([mesh_url, ibl_url, sky_url], () => {
32 window.gltfio = Filament.gltfio;
33 window.Fov = Filament.Camera$Fov;
34 window.LightType = Filament.LightManager$Type;
35 window.IndirectLight = Filament.IndirectLight;
36 window.app = new App(document.getElementsByTagName('canvas')[0]);
37 });
38
39 class App {
40 constructor(canvas) {
41 this.canvas = canvas;
42 const engine = this.engine = Filament.Engine.create(this.canvas);
43 const scene = this.scene = engine.createScene();
44 this.trackball = new Trackball(canvas, {startSpin: 0.035});
45
46 const indirectLight = this.ibl = engine.createIblFromKtx(ibl_url);
47 this.scene.setIndirectLight(indirectLight);
48
49 const iblDirection = IndirectLight.getDirectionEstimate(indirectLight.shfloats);
50 const iblColor = IndirectLight.getColorEstimate(indirectLight.shfloats, iblDirection);
51 const iblIntensity = 20000;
52
53 indirectLight.setIntensity(iblIntensity);
54
55 // Rotate the IBL so that a bright light is behind the helmet, to show off bloom.
56 const mat = [];
57 const radians = 3.14;
58 mat3.fromRotation(mat, radians, [0, 1, 0]);
59 indirectLight.setRotation(mat);
60
61 const skybox = engine.createSkyFromKtx(sky_url);
62 this.scene.setSkybox(skybox);
63
64 const sunlight = Filament.EntityManager.get().create();
65 Filament.LightManager.Builder(LightType.SUN)
66 .color(iblColor.slice(0, 3))
67 .intensity(iblColor[3] * iblIntensity)
68 .direction(iblDirection)
69 .sunAngularRadius(1.9)
70 .castShadows(true)
71 .sunHaloSize(10.0)
72 .sunHaloFalloff(80.0)
73 .build(engine, sunlight);
74 this.scene.addEntity(sunlight);
75
76 const loader = this.loader = engine.createAssetLoader();
77
78 this.allowRefresh = false;
79 const asset = this.asset = loader.createAssetFromJson(mesh_url);
80 this.assetRoot = this.asset.getRoot();
81
82 const messages = document.getElementById('messages');
83
84 // Crudely indicate progress by printing the URI of each resource as it is loaded.
85 const onFetched = (uri) => messages.innerText += `Downloaded ${uri}\n`;
86
87 const onDone = () => {
88 this.allowRefresh = true;
89
90 // Enable shadows on every renderable.
91 const entities = asset.getEntities();
92 const rm = engine.getRenderableManager();
93 for (const entity of entities) {
94 const instance = rm.getInstance(entity);
95 rm.setCastShadows(instance, true);
96 instance.delete();
97 }
98
99 // Hide the HUD.
100 messages.remove();
101 };
102 asset.loadResources(onDone, onFetched);
103
104 const cameraEntity = Filament.EntityManager.get().create();
105 this.camera = engine.createCamera(cameraEntity);
106
107 const colorGrading = Filament.ColorGrading.Builder()
108 .toneMapping(Filament.ColorGrading$ToneMapping.ACES_LEGACY)
109 .build(engine);
110
111 this.swapChain = engine.createSwapChain();
112 this.renderer = engine.createRenderer();
113 this.view = engine.createView();
114 this.view.setVignetteOptions({ midPoint: 0.8, enabled: true });
115
116 this.view.setBloomOptions({ stength: 0.2, enabled: true });
117
118 this.view.setCamera(this.camera);
119 this.view.setScene(this.scene);
120 this.view.setColorGrading(colorGrading);
121 this.resize();
122 this.render = this.render.bind(this);
123 this.resize = this.resize.bind(this);
124 this.refresh = this.refresh.bind(this);
125 window.addEventListener('resize', this.resize);
126 window.addEventListener('dblclick', this.refresh);
127 window.requestAnimationFrame(this.render);
128 }
129
130 // Test for memory leaks by destroying and recreating the asset.
131 refresh() {
132 if (!this.allowRefresh) {
133 console.warn('Refresh not allowed while the model is still loading.');
134 return;
135 }
136 console.info('Refreshing...');
137 this.allowRefresh = false;
138 this.scene.removeEntities(this.asset.getEntities());
139 this.loader.destroyAsset(this.asset);
140 this.asset = this.loader.createAssetFromJson(mesh_url);
141 const onDone = () => { this.allowRefresh = true; }
142 this.asset.loadResources(onDone);
143 }
144
145 render() {
146 // Spin the model according to the trackball controller.
147 const tcm = this.engine.getTransformManager();
148 const inst = tcm.getInstance(this.assetRoot);
149 tcm.setTransform(inst, this.trackball.getMatrix());
150 inst.delete();
151
152 // Add renderable entities to the scene as they become ready.
153 let entity;
154 const popRenderable = () => {
155 entity = this.asset.popRenderable();
156 return entity.getId() != 0;
157 }
158 while (popRenderable()) {
159 this.scene.addEntity(entity);
160 entity.delete();
161 }
162 entity.delete();
163
164 // Render the scene and request the next animation frame.
165 if (this.renderer.beginFrame(this.swapChain)) {
166 this.renderer.renderView(this.view);
167 this.renderer.endFrame();
168 }
169 this.engine.execute();
170
171 window.requestAnimationFrame(this.render);
172 }
173
174 resize() {
175 const dpr = window.devicePixelRatio;
176 const width = this.canvas.width = window.innerWidth * dpr;
177 const height = this.canvas.height = window.innerHeight * dpr;
178 this.view.setViewport([0, 0, width, height]);
179 const y = -0.125, eye = [0, y, 2], center = [0, y, 0], up = [0, 1, 0];
180 this.camera.lookAt(eye, center, up);
181 const aspect = width / height;
182 const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
183 this.camera.setProjectionFov(30, aspect, 1.0, 10.0, fov);
184 }
185 }
186 </script>
187 </body>
188 </html>