Debian Codegraph @

web/samples/suzanne.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>Filament Suzanne</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 albedo_suffix = Filament.getSupportedFormatSuffix('astc s3tc');
20 const texture_suffix = Filament.getSupportedFormatSuffix('etc');
21
22 const env = 'default_env'
23 const ibl_url = `${env}/${env}_ibl.ktx`;
24 const sky_url = `${env}/${env}_skybox.ktx`;
25 const albedo_url = `albedo${albedo_suffix}.ktx`;
26 const ao_url = `ao${texture_suffix}.ktx`;
27 const metallic_url = `metallic${texture_suffix}.ktx`;
28 const normal_url = `normal${texture_suffix}.ktx`;
29 const roughness_url = `roughness${texture_suffix}.ktx`;
30 const filamat_url = 'textured.filamat';
31 const filamesh_url = 'suzanne.filamesh';
32
33 Filament.init([
34 filamat_url,
35 filamesh_url,
36 albedo_url,
37 ao_url,
38 metallic_url,
39 normal_url,
40 roughness_url,
41 ibl_url,
42 sky_url
43 ], () => {
44 window.VertexAttribute = Filament.VertexAttribute;
45 window.AttributeType = Filament.VertexBuffer$AttributeType;
46 window.PrimitiveType = Filament.RenderableManager$PrimitiveType;
47 window.IndexType = Filament.IndexBuffer$IndexType;
48 window.Fov = Filament.Camera$Fov;
49 window.LightType = Filament.LightManager$Type;
50 window.app = new App(document.getElementsByTagName('canvas')[0]);
51 });
52
53 class App {
54 constructor(canvas) {
55 this.canvas = canvas;
56 const engine = this.engine = Filament.Engine.create(this.canvas);
57 this.scene = engine.createScene();
58
59 const sunlight = Filament.EntityManager.get().create();
60 Filament.LightManager.Builder(LightType.SUN)
61 .color([0.98, 0.92, 0.89])
62 .intensity(80000.0)
63 .direction([0.6, -1.0, -0.8])
64 .castShadows(true)
65 .sunAngularRadius(1.9)
66 .sunHaloSize(10.0)
67 .sunHaloFalloff(80.0)
68 .build(engine, sunlight);
69 this.scene.addEntity(sunlight);
70
71 const indirectLight = this.ibl = engine.createIblFromKtx(ibl_url);
72 this.scene.setIndirectLight(indirectLight);
73 indirectLight.setIntensity(80000);
74
75 const skybox = engine.createSkyFromKtx(sky_url);
76 this.scene.setSkybox(skybox);
77
78 const material = engine.createMaterial(filamat_url);
79 const matinstance = material.createInstance();
80
81 const sampler = new Filament.TextureSampler(
82 Filament.MinFilter.LINEAR_MIPMAP_LINEAR,
83 Filament.MagFilter.LINEAR,
84 Filament.WrapMode.CLAMP_TO_EDGE);
85
86 const albedo = engine.createTextureFromKtx(albedo_url, {srgb: true});
87 const roughness = engine.createTextureFromKtx(roughness_url);
88 const metallic = engine.createTextureFromKtx(metallic_url);
89 const normal = engine.createTextureFromKtx(normal_url);
90 const ao = engine.createTextureFromKtx(ao_url);
91
92 matinstance.setTextureParameter('albedo', albedo, sampler)
93 matinstance.setTextureParameter('roughness', roughness, sampler)
94 matinstance.setTextureParameter('metallic', metallic, sampler)
95 matinstance.setTextureParameter('normal', normal, sampler)
96 matinstance.setTextureParameter('ao', ao, sampler)
97
98 const mesh = engine.loadFilamesh(filamesh_url, matinstance);
99 this.suzanne = mesh.renderable;
100 this.scene.addEntity(mesh.renderable);
101
102 this.swapChain = engine.createSwapChain();
103 this.renderer = engine.createRenderer();
104 this.camera = engine.createCamera(Filament.EntityManager.get().create());
105 this.view = engine.createView();
106 this.view.setCamera(this.camera);
107 this.view.setScene(this.scene);
108 this.resize();
109 this.render = this.render.bind(this);
110 this.resize = this.resize.bind(this);
111 window.addEventListener('resize', this.resize);
112 window.requestAnimationFrame(this.render);
113 }
114
115 render() {
116 const radians = Date.now() / 1000;
117 const transform = mat4.fromRotation(mat4.create(), radians, [0, 1, 0]);
118 const tcm = this.engine.getTransformManager();
119 const inst = tcm.getInstance(this.suzanne);
120 tcm.setTransform(inst, transform);
121 inst.delete();
122 this.renderer.render(this.swapChain, this.view);
123 window.requestAnimationFrame(this.render);
124 }
125
126 resize() {
127 const dpr = window.devicePixelRatio;
128 const width = this.canvas.width = window.innerWidth * dpr;
129 const height = this.canvas.height = window.innerHeight * dpr;
130 this.view.setViewport([0, 0, width, height]);
131 const eye = [0, 0, 4], center = [0, 0, 0], up = [0, 1, 0];
132 this.camera.lookAt(eye, center, up);
133 const aspect = width / height;
134 const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
135 this.camera.setProjectionFov(45, aspect, 1.0, 10.0, fov);
136 }
137 }
138 </script>
139 </body>
140 </html>