Files
filament/docs/samples/web/skinning.html
Filament Bot 8705a627db [automated] Updating /docs due to commit 4b040fb
Full commit hash is 4b040fbae6

DOCS_ALLOW_DIRECT_EDITS
2026-03-27 06:08:44 +00:00

358 lines
18 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>skinning - Filament</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../../highlight.css">
<link rel="stylesheet" href="../../tomorrow-night.css">
<link rel="stylesheet" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- MathJax -->
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "light" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div style="display:flex;align-items:center;justify-content:center">
<img class="flogo" src="../../images/filament_logo_small.png"></img>
</div>
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<!-- Filament: disable themes because the markdeep part does not look good for dark themes -->
<!--
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
-->
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Filament</h1>
<div class="right-buttons">
<a href="https://github.com/google/filament" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<style>
body { margin: 0; overflow: hidden; }
canvas { touch-action: none; width: 100%; height: 400px; border: 1px solid black; }
</style>
<p><canvas></canvas></p>
<script src="../../web/lib/filament.js"></script>
<script src="../../web/lib/gl-matrix-min.js"></script>
<script>
let buffer0 = "AAABAAMAAAADAAIAAgADAAUAAgAFAAQABAAFAAcABAAHAAYABgAHAAkABgAJAAgAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAD8AAAAAAACAPwAAAD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAwD8AAAAAAACAPwAAwD8AAAAAAAAAAAAAAEAAAAAAAACA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(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 } ]
} ],
...
*/
const ibl_url = '../../web/assets/skinning/default_env/default_env_ibl.ktx';
const sky_url = '../../web/assets/skinning/default_env/default_env_skybox.ktx';
Filament.init([ '../../web/assets/skinning/skinning.filamat', ibl_url, sky_url ], () => {
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();
const indirectLight = engine.createIblFromKtx1(ibl_url);
indirectLight.setIntensity(50000);
this.scene.setIndirectLight(indirectLight);
const skybox = engine.createSkyFromKtx1(sky_url);
this.scene.setSkybox(skybox);
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('../../web/assets/skinning/skinning.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 = this.canvas.clientWidth * dpr;
const height = this.canvas.height = this.canvas.clientHeight * 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>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../samples/web/parquet.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../samples/web/sky.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../samples/web/parquet.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../samples/web/sky.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>