Files
filament/web/samples/skinning.html
2020-06-29 18:35:24 -07:00

179 lines
7.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>Skinning</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1">
<link href="favicon.png" rel="icon" type="image/x-icon" />
<style>
body { margin: 0; overflow: hidden; }
canvas { touch-action: none; width: 100%; height: 100%; }
</style>
</head>
<body>
<canvas></canvas>
<script src="filament.js"></script>
<script src="gl-matrix-min.js"></script>
<script>
let buffer0 = "AAABAAMAAAADAAIAAgADAAUAAgAFAAQABAAFAAcABAAHAAYABgAHAAkABgAJAAgAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAD8AAAAAAACAPwAAAD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAwD8AAAAAAACAPwAAwD8AAAAAAAAAAAAAAEAAAAAAAACAPwAAAEAAAAAA";
let buffer1 = "AAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAABAPwAAgD4AAAAAAAAAAAAAQD8AAIA+AAAAAAAAAAAAAAA/AAAAPwAAAAAAAAAAAAAAPwAAAD8AAAAAAAAAAAAAgD4AAEA/AAAAAAAAAAAAAIA+AABAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAA=";
let buffer2 = "AACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAvwAAgL8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAL8AAIC/AAAAAAAAgD8=";
let buffer3 = "AAAAAAAAAD8AAIA/AADAPwAAAEAAACBAAABAQAAAYEAAAIBAAACQQAAAoEAAALBAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAPT9ND/0/TQ/AAAAAAAAAAD0/TQ/9P00PwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAPT9NL/0/TQ/AAAAAAAAAAD0/TS/9P00PwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAAAAAAAAAIA/";
buffer0 = Uint8Array.from(atob(buffer0), c => c.charCodeAt(0)).buffer;
buffer1 = Uint8Array.from(atob(buffer1), c => c.charCodeAt(0)).buffer;
buffer2 = Uint8Array.from(atob(buffer2), c => c.charCodeAt(0)).buffer;
buffer3 = Uint8Array.from(atob(buffer3), c => c.charCodeAt(0)).buffer;
const bufview0 = new Uint16Array(buffer0, 0, 24); // ushort indices
const bufview1 = new Float32Array(buffer0, 48); // vec3 positions
const bufview2 = new Uint8Array(buffer1, 0); // bone indices and weights
const bufview3 = new Float32Array(buffer2); // two bone matrices (inverseBindMatrices)
const bufview4 = new Float32Array(buffer3, 0, 12); // 12 floats (time in seconds)
const bufview5 = new Float32Array(buffer3, 48); // 12 rotation quaternions
/*
This demo is heavily inspired by gltfTutorial_019_SimpleSkin:
"nodes" : [
{ "skin" : 0, "mesh" : 0, "children" : [ 1 ] },
{ "children" : [ 2 ], "translation" : [ 0.0, 1.0, 0.0 ] },
{ "rotation" : [ 0.0, 0.0, 0.0, 1.0 ] }
],
"skins" : [ {
"inverseBindMatrices" : 4, // points to an accessor with two matrices
"joints" : [ 1, 2 ] // the 2nd and 3rd nodes (which have no geometry) are the joints
} ],
"animations" : [ {
"channels" : [ {
"sampler" : 0,
"target" : { "node" : 2, "path" : "rotation" } // the animation only applies to the 3rd node
} ],
"samplers" : [ { "input" : 5, "interpolation" : "LINEAR", "output" : 6 } ]
} ],
...
*/
Filament.init([ 'nonlit.filamat' ], () => {
window.AttributeType = Filament.VertexBuffer$AttributeType;
window.Fov = Filament.Camera$Fov;
window.Projection = Filament.Camera$Projection;
window.VertexAttribute = Filament.VertexAttribute;
window.app = new App(document.getElementsByTagName('canvas')[0]);
});
class App {
constructor(canvas) {
this.canvas = canvas;
const engine = this.engine = Filament.Engine.create(this.canvas);
this.scene = engine.createScene();
this.mesh = Filament.EntityManager.get().create();
this.scene.addEntity(this.mesh);
this.ib = Filament.IndexBuffer.Builder()
.indexCount(24)
.bufferType(Filament.IndexBuffer$IndexType.USHORT)
.build(engine);
this.ib.setBuffer(engine, bufview0);
this.vb = Filament.VertexBuffer.Builder()
.vertexCount(10)
.bufferCount(3)
.attribute(VertexAttribute.POSITION, 0, AttributeType.FLOAT3, 0, 12)
.attribute(VertexAttribute.BONE_INDICES, 1, AttributeType.USHORT4, 0, 16)
.attribute(VertexAttribute.BONE_WEIGHTS, 2, AttributeType.FLOAT4, 0, 16)
.build(engine);
this.vb.setBufferAt(engine, 0, bufview1);
this.vb.setBufferAt(engine, 1, bufview2.subarray(0, 160));
this.vb.setBufferAt(engine, 2, bufview2.subarray(160, 320));
const mat = engine.createMaterial('nonlit.filamat');
const matinst = mat.getDefaultInstance();
Filament.RenderableManager.Builder(1)
.boundingBox({ center: [-1, -1, -1], halfExtent: [1, 1, 1] })
.material(0, matinst)
.geometry(0, Filament.RenderableManager$PrimitiveType.TRIANGLES, this.vb, this.ib)
.skinning(2)
.build(engine, this.mesh);
this.swapChain = engine.createSwapChain();
this.renderer = engine.createRenderer();
this.camera = engine.createCamera(Filament.EntityManager.get().create());
const eye = [0, 0, 4], center = [0, 0, 0], up = [0, 1, 0];
this.camera.lookAt(eye, center, up);
this.view = engine.createView();
this.view.setCamera(this.camera);
this.view.setScene(this.scene);
this.renderer.setClearOptions({clearColor: [1.0, 1.0, 1.0, 1.0], clear: true});
this.resize();
this.render = this.render.bind(this);
this.resize = this.resize.bind(this);
window.addEventListener('resize', this.resize);
window.requestAnimationFrame(this.render);
}
render() {
const endTime = 5.5;
const timepoints = bufview4;
const quats = bufview5;
const inverseBindMatrices = bufview3;
const nframes = timepoints.length;
const seconds = (Date.now() / 1000) % endTime;
let t = -1;
let q = [0, 0, 0, 1];
for (let i = 0; i < nframes; i++) {
const j = (i + 1) % nframes;
const next = (i == nframes - 1) ? (endTime + timepoints[0]) : timepoints[i + 1];
const curr = timepoints[i];
if (seconds >= curr && seconds < next) {
t = (seconds - curr) / (next - curr);
const q0 = quats.subarray(4 * i, 4 * (i + 1));
const q1 = quats.subarray(4 * j, 4 * (j + 1));
quat.slerp(q, q0, q1, t);
break;
}
}
const transforms = [mat4.create(), mat4.create()];
mat4.multiply(transforms[0], transforms[0], inverseBindMatrices.subarray(0, 16));
mat4.multiply(transforms[1], transforms[1], inverseBindMatrices.subarray(16, 32));
const m = mat4.fromQuat(mat4.create(), q);
mat4.multiply(transforms[1], m, transforms[1]);
const rm = this.engine.getRenderableManager();
const renderable = rm.getInstance(this.mesh);
rm.setBonesFromMatrices(renderable, transforms, 0);
renderable.delete();
this.renderer.render(this.swapChain, this.view);
window.requestAnimationFrame(this.render);
}
resize() {
const dpr = window.devicePixelRatio;
const width = this.canvas.width = window.innerWidth * dpr;
const height = this.canvas.height = window.innerHeight * dpr;
this.view.setViewport([0, 0, width, height]);
const aspect = width / height;
const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL;
this.camera.setProjectionFov(45, aspect, 1.0, 10.0, fov);
}
}
</script>
</body>
</html>