Debian Codegraph @

web/samples/skinning.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>Skinning</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 let buffer0 = "AAABAAMAAAADAAIAAgADAAUAAgAFAAQABAAFAAcABAAHAAYABgAHAAkABgAJAAgAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAD8AAAAAAACAPwAAAD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAwD8AAAAAAACAPwAAwD8AAAAAAAAAAAAAAEAAAAAAAACAPwAAAEAAAAAA";
20 let buffer1 = "AAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAABAPwAAgD4AAAAAAAAAAAAAQD8AAIA+AAAAAAAAAAAAAAA/AAAAPwAAAAAAAAAAAAAAPwAAAD8AAAAAAAAAAAAAgD4AAEA/AAAAAAAAAAAAAIA+AABAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAA=";
21 let buffer2 = "AACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAvwAAgL8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAL8AAIC/AAAAAAAAgD8=";
22 let buffer3 = "AAAAAAAAAD8AAIA/AADAPwAAAEAAACBAAABAQAAAYEAAAIBAAACQQAAAoEAAALBAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAPT9ND/0/TQ/AAAAAAAAAAD0/TQ/9P00PwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAPT9NL/0/TQ/AAAAAAAAAAD0/TS/9P00PwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAAAAAAAAAIA/";
23
24 buffer0 = Uint8Array.from(atob(buffer0), c => c.charCodeAt(0)).buffer;
25 buffer1 = Uint8Array.from(atob(buffer1), c => c.charCodeAt(0)).buffer;
26 buffer2 = Uint8Array.from(atob(buffer2), c => c.charCodeAt(0)).buffer;
27 buffer3 = Uint8Array.from(atob(buffer3), c => c.charCodeAt(0)).buffer;
28
29 const bufview0 = new Uint16Array(buffer0, 0, 24); // ushort indices
30 const bufview1 = new Float32Array(buffer0, 48); // vec3 positions
31 const bufview2 = new Uint8Array(buffer1, 0); // bone indices and weights
32 const bufview3 = new Float32Array(buffer2); // two bone matrices (inverseBindMatrices)
33 const bufview4 = new Float32Array(buffer3, 0, 12); // 12 floats (time in seconds)
34 const bufview5 = new Float32Array(buffer3, 48); // 12 rotation quaternions
35
36 /*
37
38 This demo is heavily inspired by gltfTutorial_019_SimpleSkin:
39
40 "nodes" : [
41 { "skin" : 0, "mesh" : 0, "children" : [ 1 ] },
42 { "children" : [ 2 ], "translation" : [ 0.0, 1.0, 0.0 ] },
43 { "rotation" : [ 0.0, 0.0, 0.0, 1.0 ] }
44 ],
45 "skins" : [ {
46 "inverseBindMatrices" : 4, // points to an accessor with two matrices
47 "joints" : [ 1, 2 ] // the 2nd and 3rd nodes (which have no geometry) are the joints
48 } ],
49 "animations" : [ {
50 "channels" : [ {
51 "sampler" : 0,
52 "target" : { "node" : 2, "path" : "rotation" } // the animation only applies to the 3rd node
53 } ],
54 "samplers" : [ { "input" : 5, "interpolation" : "LINEAR", "output" : 6 } ]
55 } ],
56 ...
57
58 */
59
60 Filament.init([ 'nonlit.filamat' ], () => {
61 window.AttributeType = Filament.VertexBuffer$AttributeType;
62 window.Fov = Filament.Camera$Fov;
63 window.Projection = Filament.Camera$Projection;
64 window.VertexAttribute = Filament.VertexAttribute;
65 window.app = new App(document.getElementsByTagName('canvas')[0]);
66 });
67
68 class App {
69 constructor(canvas) {
70 this.canvas = canvas;
71 const engine = this.engine = Filament.Engine.create(this.canvas);
72 this.scene = engine.createScene();
73 this.mesh = Filament.EntityManager.get().create();
74 this.scene.addEntity(this.mesh);
75
76 this.ib = Filament.IndexBuffer.Builder()
77 .indexCount(24)
78 .bufferType(Filament.IndexBuffer$IndexType.USHORT)
79 .build(engine);
80
81 this.ib.setBuffer(engine, bufview0);
82
83 this.vb = Filament.VertexBuffer.Builder()
84 .vertexCount(10)
85 .bufferCount(3)
86 .attribute(VertexAttribute.POSITION, 0, AttributeType.FLOAT3, 0, 12)
87 .attribute(VertexAttribute.BONE_INDICES, 1, AttributeType.USHORT4, 0, 16)
88 .attribute(VertexAttribute.BONE_WEIGHTS, 2, AttributeType.FLOAT4, 0, 16)
89 .build(engine);
90
91 this.vb.setBufferAt(engine, 0, bufview1);
92 this.vb.setBufferAt(engine, 1, bufview2.subarray(0, 160));
93 this.vb.setBufferAt(engine, 2, bufview2.subarray(160, 320));
94
95 const mat = engine.createMaterial('nonlit.filamat');
96 const matinst = mat.getDefaultInstance();
97 Filament.RenderableManager.Builder(1)
98 .boundingBox({ center: [-1, -1, -1], halfExtent: [1, 1, 1] })
99 .material(0, matinst)
100 .geometry(0, Filament.RenderableManager$PrimitiveType.TRIANGLES, this.vb, this.ib)
101 .skinning(2)
102 .build(engine, this.mesh);
103
104 this.swapChain = engine.createSwapChain();
105 this.renderer = engine.createRenderer();
106
107 this.camera = engine.createCamera(Filament.EntityManager.get().create());
108 const eye = [0, 0, 4], center = [0, 0, 0], up = [0, 1, 0];
109 this.camera.lookAt(eye, center, up);
110
111 this.view = engine.createView();
112 this.view.setCamera(this.camera);
113 this.view.setScene(this.scene);
114
115 this.renderer.setClearOptions({clearColor: [1.0, 1.0, 1.0, 1.0], clear: true});
116
117 this.resize();
118 this.render = this.render.bind(this);
119 this.resize = this.resize.bind(this);
120 window.addEventListener('resize', this.resize);
121 window.requestAnimationFrame(this.render);
122 }
123
124 render() {
125
126 const endTime = 5.5;
127 const timepoints = bufview4;
128 const quats = bufview5;
129 const inverseBindMatrices = bufview3;
130
131 const nframes = timepoints.length;
132 const seconds = (Date.now() / 1000) % endTime;
133 let t = -1;
134 let q = [0, 0, 0, 1];
135 for (let i = 0; i < nframes; i++) {
136 const j = (i + 1) % nframes;
137 const next = (i == nframes - 1) ? (endTime + timepoints[0]) : timepoints[i + 1];
138 const curr = timepoints[i];
139 if (seconds >= curr && seconds < next) {
140 t = (seconds - curr) / (next - curr);
141 const q0 = quats.subarray(4 * i, 4 * (i + 1));
142 const q1 = quats.subarray(4 * j, 4 * (j + 1));
143 quat.slerp(q, q0, q1, t);
144 break;
145 }
146 }
147
148 const transforms = [mat4.create(), mat4.create()];
149
150 mat4.multiply(transforms[0], transforms[0], inverseBindMatrices.subarray(0, 16));
151 mat4.multiply(transforms[1], transforms[1], inverseBindMatrices.subarray(16, 32));
152
153 const m = mat4.fromQuat(mat4.create(), q);
154 mat4.multiply(transforms[1], m, transforms[1]);
155
156 const rm = this.engine.getRenderableManager();
157 const renderable = rm.getInstance(this.mesh);
158 rm.setBonesFromMatrices(renderable, transforms, 0);
159 renderable.delete();
160
161 this.renderer.render(this.swapChain, this.view);
162 window.requestAnimationFrame(this.render);
163 }
164
165 resize() {
166 const dpr = window.devicePixelRatio;
167 const width = this.canvas.width = window.innerWidth * dpr;
168 const height = this.canvas.height = window.innerHeight * dpr;
169 this.view.setViewport([0, 0, width, height]);
170
171 const aspect = width / height;
172 const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
173 this.camera.setProjectionFov(45, aspect, 1.0, 10.0, fov);
174 }
175 }
176 </script>
177 </body>
178 </html>