# graphics3d/toon/glbmeshreader (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/graphics3d/toon/glbmeshreader/

Reading clay models (GLB) and skeleton poses. (vanilla.js graphics3d/toon/glbmeshreader.js)
- Reads models painted with material colors into toon 3D MeshBuilder vertex arrays. Texture coordinates and material textures are read too if present. (Came from webgame-rimminer)
  - One part is one node, and a node's mesh is one primitive per color. Vertices are position and normal, plus joint indices and weights if there is a skeleton.
    A primitive without normals gets flat normals (one per triangle) as the glTF spec says. (Vertices are split per triangle)
  - The material's baseColorFactor is a linear color, so it is converted to sRGB (pow 1 / 2.2 in the gamma color space, the standard sRGB curve in the linear color space, the inverse of the curve the shader decodes with), and emissiveFactor, if present, is stored as emission. (with emissiveTexture, the emission is passed only as a material range)
  - TEXCOORD_0, if present, goes into uv_data and TEXCOORD_1 into uv1_data (second UV, angel ring). A material range (ToonMaterialRange) is stored per primitive. (Material name, extras, texture image order)
    When painting split areas inside a part (accent color areas, display panels), pick them by material name or extras and draw only that area with index_start and index_count of ToonDrawOptions.
  - Images are read with read_glb_images, decoded with decode_glb_image and passed to ToonRenderer::create_texture.
  - outline in material extras is an outline shell (only back faces drawn), and basic is a part that does not receive light.
    If outline options are given, that shell is not stored, and a shell of uniform thickness per point is newly built from the body of each node that has a ".outline" pair.
  - If there is a skeleton (skin), the joint nodes and their inverse bind matrices, the animations (animations) and their extras are read together. Poses are computed by write_skin_pose.
    To modify bones before the blended pose is built into skin matrices, pass a hook (ToonPoseAdjust::Hook) or a modification table (ToonPoseAdjust::Bones, adjust_bones, BoneModifier) as pose_adjust.
  - Models whose parts are hidden or painted separately are read per node with read_glb_node_arrays. (Together with node extras and transparency)
  - For a skeletal model read as one lump, node extras, part positions and the joints they follow are read separately with read_glb_node_infos.
  - GLB files whose vertex attributes are stored as integers (KHR_mesh_quantization) are read too. Normalized integers are divided with the glTF spec formulas, and texture coordinates get the material texture KHR_texture_transform baked in.
    An unnamed mesh node belongs to the part of its named parent node. (the leaf node a quantizer adds for the dequantization transform) Err if extensionsRequired lists an extension that cannot be read (meshopt compression and others) or an accessor points to a buffer outside the binary chunk.
- Separate from the engine's SkinnedModel (textured PBR). Only the vertex arrays used for toon drawing are extracted.

## ToonOutlineOptions (struct) (Clone, Debug, Default)

Settings for building a new outline.
- width: shell width. (at 0 no shells are built at all, and the GLB's shells are not stored either)
- part_widths: width given separately per part (node name). added_parts: parts that get a shell even without a ".outline" pair.
- inner_parts: when inner_width is greater than 0, one more shell of that width is built and stored at the end of the outline shells, and that span is returned as inner_outline_ranges.
  The spans holding the bodies of inner_parts and of the inner_over_parts on top of them are returned as inner_body_ranges. (for drawing parts like glass once more with a thin line around them)

Example: Make one part thinner, and build outline shells for parts without a pair too

```rust
use std::collections::HashMap;
use vanilla::{read_glb_arrays, ToonMeshArrays, ToonOutlineOptions};

fn read_mannequin(bytes: &[u8]) -> Result<ToonMeshArrays, String> {
	let outline_options = ToonOutlineOptions {
		width: 1.5,
		part_widths: HashMap::from([(String::from("Mannequin_F"), 0.8)]),
		added_parts: vec![String::from("Mannequin_F")],
		..ToonOutlineOptions::default()
	};
	read_glb_arrays(bytes, Some(&outline_options), None)
}
```

- `pub width: f64` (field, since 0.4.0): Shell thickness. (Model coordinates, 0 builds no shells at all)
  - returns f64
- `pub part_widths: HashMap<String, f64>` (field, since 0.4.0): Thickness given separately per part (node name).
  - returns HashMap<String, f64>
- `pub added_parts: Vec<String>` (field, since 0.4.0): Parts to build a shell for even without a ".outline" pair. (Node names)
  - returns Vec<String>
- `pub inner_parts: Vec<String>` (field, since 0.4.0): Parts to build one more inner shell for. (Node names, when inner_width is greater than 0)
  - returns Vec<String>
- `pub inner_over_parts: Vec<String>` (field, since 0.4.0): Parts placed on top of inner_parts. (Node names, only the span holding their body goes into inner_body_ranges)
  - returns Vec<String>
- `pub inner_width: f64` (field, since 0.4.0): Inner shell thickness. (0 builds none)
  - returns f64

## ToonSkeletonNode (struct) (Clone, Debug)

One node of the skeleton. (rest pose)

Example: Compute joint matrices from two nodes in the rest pose

```rust
use std::collections::HashMap;
use vanilla::{write_skin_pose, ToonSkeleton, ToonSkeletonNode};

let root = ToonSkeletonNode { name: String::from("root"), parent_index: None, translation: [0.0, 1.0, 0.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let arm = ToonSkeletonNode { name: String::from("arm"), parent_index: Some(0), translation: [0.5, 0.0, 0.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [2.0; 3], matrix: None };
let skeleton = ToonSkeleton { joints: vec![0, 1], inverse_bind_matrices: Vec::new(), nodes: vec![root, arm], animations: HashMap::new() };
let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
write_skin_pose(&skeleton, "Idle", 0.0, &mut joint_matrices, None, None, None);
assert!((joint_matrices[16] - 2.0).abs() < 0.0001);
assert!((joint_matrices[28] - 0.5).abs() < 0.0001 && (joint_matrices[29] - 1.0).abs() < 0.0001);
```

- `pub name: String` (field, since 0.4.0): Node name. (empty string if none)
  - returns String
- `pub parent_index: Option<usize>` (field, since 0.4.0): Index of the parent node. (None for the root)
  - returns Option<usize>
- `pub translation: [f64; 3]` (field, since 0.4.0): Position. ([x, y, z])
  - returns [f64; 3]
- `pub rotation: [f64; 4]` (field, since 0.4.0): Rotation quaternion. ([x, y, z, w])
  - returns [f64; 4]
- `pub scale: [f64; 3]` (field, since 0.4.0): Scale. ([x, y, z])
  - returns [f64; 3]
- `pub matrix: Option<[f64; 16]>` (field, since 0.4.0): glTF matrix. (if present, used instead of position, rotation and scale, and not animated)
  - returns Option<[f64; 16]>

## ToonAnimationChannel (struct) (Clone, Debug)

One animation channel. (the animated node, what it changes, key times and values)

Example: Move one node with a STEP channel that keeps the previous key

```rust
use std::collections::HashMap;
use vanilla::{write_skin_pose, ToonAnimation, ToonAnimationChannel, ToonChannelPath, ToonSkeleton, ToonSkeletonNode};

let node = ToonSkeletonNode { name: String::from("body"), parent_index: None, translation: [0.0; 3], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let hop = ToonAnimationChannel { node_index: 0, path: ToonChannelPath::Translation, times: vec![0.0, 0.5], values: vec![0.0, 0.0, 0.0, 0.0, 1.0, 0.0], is_step: true };
let animation = ToonAnimation { duration: 1.0, channels: vec![hop], extras: serde_json::Value::Null };
let skeleton = ToonSkeleton { joints: vec![0], inverse_bind_matrices: Vec::new(), nodes: vec![node], animations: HashMap::from([(String::from("Hop"), animation)]) };
let mut joint_matrices = [0.0; 16];
write_skin_pose(&skeleton, "Hop", 0.25, &mut joint_matrices, None, None, None);
assert_eq!(joint_matrices[13], 0.0);
write_skin_pose(&skeleton, "Hop", 0.75, &mut joint_matrices, None, None, None);
assert_eq!(joint_matrices[13], 1.0);
```

- `pub node_index: usize` (field, since 0.4.0): Index of the animated node.
  - returns usize
- `pub path: ToonChannelPath` (field, since 0.4.0): What it changes. (One of position, rotation and scale)
  - returns ToonChannelPath
- `pub times: Vec<f64>` (field, since 0.4.0): Key times. (seconds)
  - returns Vec<f64>
- `pub values: Vec<f64>` (field, since 0.4.0): Values per key. (four for rotation, three each for position and scale)
  - returns Vec<f64>
- `pub is_step: bool` (field, since 0.4.0): Whether interpolation is STEP. (the previous key, without blending in between)
  - returns bool

## ToonAnimation (struct) (Clone, Debug)

One animation. (duration, channels, extras)

Example: Clamp a play-once animation to its end time

```rust
use std::collections::HashMap;
use vanilla::{write_skin_pose, ToonAnimation, ToonAnimationChannel, ToonChannelPath, ToonSkeleton, ToonSkeletonNode};

let node = ToonSkeletonNode { name: String::from("body"), parent_index: None, translation: [0.0; 3], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let rise = ToonAnimationChannel { node_index: 0, path: ToonChannelPath::Translation, times: vec![0.0, 2.0], values: vec![0.0, 0.0, 0.0, 0.0, 4.0, 0.0], is_step: false };
let animation = ToonAnimation { duration: 2.0, channels: vec![rise], extras: serde_json::json!({ "loop": "once" }) };
let skeleton = ToonSkeleton { joints: vec![0], inverse_bind_matrices: Vec::new(), nodes: vec![node], animations: HashMap::from([(String::from("Rise"), animation)]) };
let mut joint_matrices = [0.0; 16];
write_skin_pose(&skeleton, "Rise", 3.0, &mut joint_matrices, None, None, None);
assert_eq!(joint_matrices[13], 4.0);
```

- `pub duration: f64` (field, since 0.4.0): Duration. (the latest key time among the channels, seconds)
  - returns f64
- `pub channels: Vec<ToonAnimationChannel>` (field, since 0.4.0): Channels.
  - returns Vec<ToonAnimationChannel>
- `pub extras: Value` (field, since 0.4.0): Animation extras. (plays only once if loop is "once", empty if none)
  - returns Value

## ToonSkeleton (struct) (Clone, Debug)

Skeleton and animations. (first skin only)

Example: Compute the pose of two joints with one position animation

```rust
use std::collections::HashMap;
use vanilla::{write_skin_pose, ToonAnimation, ToonAnimationChannel, ToonChannelPath, ToonSkeleton, ToonSkeletonNode};

let root = ToonSkeletonNode { name: String::from("root"), parent_index: None, translation: [0.0; 3], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let arm = ToonSkeletonNode { name: String::from("arm"), parent_index: Some(0), translation: [1.0, 0.0, 0.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let lift = ToonAnimationChannel { node_index: 0, path: ToonChannelPath::Translation, times: vec![0.0, 1.0], values: vec![0.0, 0.0, 0.0, 0.0, 2.0, 0.0], is_step: false };
let animation = ToonAnimation { duration: 1.0, channels: vec![lift], extras: serde_json::Value::Null };
let skeleton = ToonSkeleton { joints: vec![0, 1], inverse_bind_matrices: Vec::new(), nodes: vec![root, arm], animations: HashMap::from([(String::from("Lift"), animation)]) };
let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
write_skin_pose(&skeleton, "Lift", 0.5, &mut joint_matrices, None, None, None);
assert!((joint_matrices[16 + 12] - 1.0).abs() < 0.0001);
assert!((joint_matrices[16 + 13] - 1.0).abs() < 0.0001);
```

Example: Compute the pose at a time from the skeleton of a GLB

```rust
use vanilla::{read_glb_arrays, write_skin_pose};

fn read_pose(bytes: &[u8], time: f64) -> Result<Vec<f32>, String> {
	let arrays = read_glb_arrays(bytes, None, None)?;
	let Some(skeleton) = arrays.skeleton.as_ref() else {
		return Ok(Vec::new());
	};
	let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
	write_skin_pose(skeleton, "Survey", time, &mut joint_matrices, None, None, None);
	Ok(joint_matrices)
}
```

- `pub joints: Vec<usize>` (field, since 0.4.0): Indices of the joint nodes. (the skin's joints, a vertex's joint number is an index into this list)
  - returns Vec<usize>
- `pub inverse_bind_matrices: Vec<f32>` (field, since 0.4.0): Inverse bind matrix per joint. (16 each, identity matrix if none)
  - returns Vec<f32>
- `pub nodes: Vec<ToonSkeletonNode>` (field, since 0.4.0): Rest pose of every node. (in node order)
  - returns Vec<ToonSkeletonNode>
- `pub animations: HashMap<String, ToonAnimation>` (field, since 0.4.0): Animations by name.
  - returns HashMap<String, ToonAnimation>

## ToonPartArrays (struct) (Clone, Debug)

A part read separately per node. (arrays, node extras, smallest opacity of the translucent materials)

Example: Upload a separate mesh per part and leave out only the visor

```rust
use vanilla::{read_glb_node_arrays, ToonMesh, ToonOutlineOptions, ToonRenderer};

fn create_part_meshes(gl: &vanilla::GlContext, renderer: &ToonRenderer, bytes: &[u8]) -> Result<Vec<(String, ToonMesh, f64)>, String> {
	let outline_options = ToonOutlineOptions { width: 1.5, added_parts: vec![String::from("vanguard_Mesh"), String::from("vanguard_visor")], ..ToonOutlineOptions::default() };
	let part_arrays = read_glb_node_arrays(bytes, Some(&outline_options), None)?;
	let mut meshes = Vec::new();
	for (part_name, part) in part_arrays.iter() {
		if part_name == "vanguard_visor" {
			continue;
		}
		if let Some(mesh) = renderer.create_mesh(gl, &part.arrays) {
			meshes.push((part_name.clone(), mesh, part.alpha));
		}
	}
	Ok(meshes)
}
```

- `pub arrays: ToonMeshArrays` (field, since 0.4.0): Vertex arrays of the part. (The skeleton is not read, so skeleton is None)
  - returns ToonMeshArrays
- `pub extras: Value` (field, since 0.4.0): Node extras. (empty if none)
  - returns Value
- `pub alpha: f64` (field, since 0.4.0): Smallest opacity of transparent materials (alphaMode BLEND). (1 if none)
  - returns f64

## ToonNodeInfo (struct) (Clone, Debug)

Info for one mesh node. (extras, center of the vertex bounds, index within the skin of the bone it is most heavily bound to)

Example: Read the visor position of a walking model

```rust
use vanilla::{read_glb_arrays, read_glb_node_infos, write_skin_pose, Matrix4};

fn read_visor_position(bytes: &[u8]) -> Result<Option<[f64; 3]>, String> {
	let arrays = read_glb_arrays(bytes, None, None)?;
	let node_infos = read_glb_node_infos(bytes)?;
	let (Some(skeleton), Some(visor_info)) = (arrays.skeleton.as_ref(), node_infos.get("vanguard_visor")) else {
		return Ok(None);
	};
	let Some(joint_index) = visor_info.joint_index else {
		return Ok(Some(visor_info.center));
	};
	let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
	write_skin_pose(skeleton, "Walk", 0.25, &mut joint_matrices, None, None, None);
	let mut joint_matrix = Matrix4::create_identity();
	joint_matrix.elements.copy_from_slice(&joint_matrices[joint_index * 16..joint_index * 16 + 16]);
	let visor_position = joint_matrix.transform_point(visor_info.center[0], visor_info.center[1], visor_info.center[2]);
	Ok(Some([visor_position.x, visor_position.y, visor_position.z]))
}
```

- `pub extras: Value` (field, since 0.4.0): Node extras. (empty if none)
  - returns Value
- `pub center: [f64; 3]` (field, since 0.4.0): Center of the vertex bounds. (model coordinates, the origin if there are no vertices)
  - returns [f64; 3]
- `pub joint_index: Option<usize>` (field, since 0.4.0): Index within the skin of the most heavily weighted joint. (None if not moved by a skeleton)
  - returns Option<usize>

## ToonPoseBlend (struct) (Clone, Copy, Debug)

Previous animation to blend the pose with. (briefly, when the animation changes)

Example: Briefly blend in the previous pose when the animation changes

```rust
use vanilla::{write_skin_pose, ToonPoseBlend};

let blend_duration = 0.2;
let time_since_change = 0.05;
let blend = ToonPoseBlend { animation_name: "Survey", time: 1.4, weight: 1.0 - time_since_change / blend_duration };
let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
write_skin_pose(&skeleton, "Run", time_since_change, &mut joint_matrices, Some(blend), None, None);
assert!((joint_matrices[13] - 0.5).abs() < 0.0001);
```

- `pub animation_name: &'a str` (field, since 0.4.0): Name of the previous animation.
  - returns &'a str
- `pub time: f64` (field, since 0.4.0): Time of the previous animation. (seconds)
  - returns f64
- `pub weight: f64` (field, since 0.4.0): Weight of the previous pose. (0 for no blending, 1 for the previous pose as is)
  - returns f64

## ToonSwingScales (type)

Swing scale per node. (animation name → list of (node index, scale), 1 keeps it as is, 0 is the rest pose)

```rust
pub type ToonSwingScales = HashMap<String, Vec<(usize, f64)>>
```

## BoneModifier (struct) (Clone, Copy, PartialEq, Debug, Default)

Modification of one bone. (one row of the adjust_bones and ToonPoseAdjust::Bones table, original { nodeIndex, name, translation, rotation, scale, mode, weight })
- Only the given fields (Some) are applied. When node_index is None the node is looked up by name. (the first node when several share the name, skipped when not found or when the node uses a matrix)
- weight is the ratio between before and after the modification. (0 keeps it, 1 applies it fully, clamped to 0 ~ 1, rotation is blended the short way and normalized)
- The default is all fields None, mode Add, weight 1.

Example: Rotate a tail joint 0.8 more radians around its own x axis and apply half of it

```rust
use vanilla::{BoneModifier, BoneModifyMode};

let half_angle: f64 = 0.4;
let modifier = BoneModifier { name: Some("b_Tail02_013"), rotation: Some([half_angle.sin(), 0.0, 0.0, half_angle.cos()]), weight: 0.5, ..Default::default() };
assert_eq!(modifier.mode, BoneModifyMode::Add);
assert_eq!(modifier.node_index, None);
```

- `pub node_index: Option<usize>` (field, since 0.4.0): Index in skeleton.nodes of the node to modify. (looked up by name when None)
  - returns Option<usize>
- `pub name: Option<&'a str>` (field, since 0.4.0): Name of the node to modify. (used when node_index is None)
  - returns Option<&'a str>
- `pub translation: Option<[f64; 3]>` (field, since 0.4.0): Translation. ([x, y, z], added with Add, set with Replace, unchanged when None)
  - returns Option<[f64; 3]>
- `pub rotation: Option<[f64; 4]>` (field, since 0.4.0): Rotation quaternion. ([x, y, z, w], multiplied after in the bone's own space with Add, the normalized value with Replace, unchanged when None)
  - returns Option<[f64; 4]>
- `pub scale: Option<[f64; 3]>` (field, since 0.4.0): Scale. ([x, y, z], multiplied with Add, set with Replace, unchanged when None)
  - returns Option<[f64; 3]>
- `pub mode: BoneModifyMode` (field, since 0.4.0): How it is applied. (default Add)
  - returns BoneModifyMode
- `pub weight: f64` (field, since 0.4.0): Ratio between before and after the modification. (0 ~ 1, default 1)
  - returns f64

## ToonGlbImage (struct) (Clone, Debug, Default)

One image in a GLB. (index in images, referenced by base_color_image and emissive_image of material ranges)

Example: Upload the images of a GLB as textures in images order

```rust
use vanilla::core::graphic::GlTexture;
use vanilla::{decode_glb_image, read_glb_images, ToonRenderer, ToonTextureOptions};

fn create_image_textures(gl: &vanilla::GlContext, renderer: &ToonRenderer, bytes: &[u8]) -> Result<Vec<Option<GlTexture>>, String> {
	let mut textures = Vec::new();
	for image in read_glb_images(bytes)?.iter() {
		if image.data.is_empty() {
			textures.push(None);
			continue;
		}
		let (width, height, pixels) = decode_glb_image(image)?;
		textures.push(Some(renderer.create_texture(gl, width, height, &pixels, ToonTextureOptions::default())?));
	}
	Ok(textures)
}
```

- `pub name: String` (field, since 0.4.0): Image name. (empty string if none)
  - returns String
- `pub mime_type: String` (field, since 0.4.0): Image type. (such as "image/png", empty string if none)
  - returns String
- `pub data: Vec<u8>` (field, since 0.4.0): Image file bytes. (empty for an external file uri)
  - returns Vec<u8>

## Functions and constants

- `pub fn read_glb_images(bytes: &[u8]) -> Result<Vec<ToonGlbImage>, String>` (method, since 0.4.0): Images in a GLB. (only images stored through a bufferView of the binary chunk are read, external file uri images have empty data)
  - `bytes` &[u8]: The whole GLB file.
  - returns Result<Vec<ToonGlbImage>, String>: Images in images order. (An image from an external file uri has empty data) Err(reason text) if it is not a GLB, a chunk is cut off or missing, or the JSON cannot be read.
- `pub fn decode_glb_image(image: &ToonGlbImage) -> Result<(u32, u32, Vec<u8>), String>` (method, since 0.4.0): Decodes one image from read_glb_images. ((width, height, RGBA bytes), alpha is not premultiplied, png, jpeg, webp, gif, bmp, ico through the image crate)
  - `image` &ToonGlbImage: One image from read_glb_images.
  - returns Result<(u32, u32, Vec<u8>), String>: (width, height, RGBA bytes) decoded image. (Passed to ToonRenderer::create_texture) Err(reason text) if it cannot be decoded.
- `pub fn read_glb_arrays(bytes: &[u8], outline_options: Option<&ToonOutlineOptions>, color_space: Option<ColorSpace>) -> Result<ToonMeshArrays, String>` (method, since 0.4.0): One GLB into vertex arrays. (the skeleton too if there is one, a new shell is built when outline settings are given)
  - `bytes` &[u8]: The whole GLB file.
  - `outline_options` Option<&ToonOutlineOptions>: If given, shells are newly built from the body of nodes that have a ".outline" pair instead of the GLB's outline shells. (None keeps the GLB's shells as is)
  - `color_space` Option<ColorSpace>: The color space of the vertex color (the curve that converts baseColorFactor to sRGB). (None means the engine's color space, the same as the original default, use the same value as the ToonRenderer that draws it)
  - returns Result<ToonMeshArrays, String>: The form of MeshBuilder::read_arrays plus skeleton (None if none), inner_outline_ranges, inner_body_ranges. (Passed to ToonRenderer::create_mesh) Err(reason text) if it is not a GLB, a chunk is cut off or missing, or the JSON cannot be read.
- `pub fn read_glb_node_arrays(bytes: &[u8], outline_options: Option<&ToonOutlineOptions>, color_space: Option<ColorSpace>) -> Result<HashMap<String, ToonPartArrays>, String>` (method, since 0.4.0): One GLB into vertex arrays per node name. (the skeleton is not read)
  - Nodes whose name ends in ".outline" (outline shells) are stored together with the node of the preceding name.
  - For each part, the node extras and the smallest opacity of its translucent materials (alphaMode BLEND) are stored too.
  - `bytes` &[u8]: The whole GLB file.
  - `outline_options` Option<&ToonOutlineOptions>: Same as read_glb_arrays.
  - `color_space` Option<ColorSpace>: Same as read_glb_arrays. (None means the engine's color space)
  - returns Result<HashMap<String, ToonPartArrays>, String>: Per node name, the part's arrays with extras and alpha. Err(reason text) if it is not a GLB, a chunk is cut off or missing, or the JSON cannot be read.
- `pub fn read_glb_node_infos(bytes: &[u8]) -> Result<HashMap<String, ToonNodeInfo>, String>` (method, since 0.4.0): Extras, center and following bone of each mesh node of the GLB. (nodes whose name ends in ".outline" and outline materials are excluded, an unnamed mesh node is gathered into its named parent part)
  - center is the center of the range the vertices occupy. (model coordinates, the rest pose position for nodes moved by the skeleton, the origin if there are no vertices)
  - joint_index is the index within the skin of the bone the node's vertices are most heavily bound to. (the order of the bone matrices written by write_skin_pose)
  - `bytes` &[u8]: The whole GLB file.
  - returns Result<HashMap<String, ToonNodeInfo>, String>: Per node name, the mesh node's extras, center and joint_index. Err(reason text) if it is not a GLB, a chunk is cut off or missing, or the JSON cannot be read.
- `pub fn read_animation_swing(skeleton: &ToonSkeleton, animation_name: &str, node_index: usize) -> f64` (method, since 0.4.0): The largest angle by which one node's rotation deviates from the rest pose in an animation. (radians, checked per keyframe, 0 if that node has no rotation)
  - `skeleton` &ToonSkeleton: skeleton of read_glb_arrays.
  - `animation_name` &str: Animation name.
  - `node_index` usize: Order in skeleton.nodes.
  - returns f64: The largest angle away from the rest pose. (Radians, 0 if none)
- `pub fn adjust_bones(locals: &mut [ToonSkeletonNode], skeleton: &ToonSkeleton, modifiers: &[BoneModifier])` (method, since 0.4.0): Modify the local transform of each bone in a blended pose. (the form write_skin_pose applies after sampling and blending the animation and before building skin matrices, nodes using a matrix are skipped)
  - Modifications are applied in order. (two on the same bone apply the later one on top of the earlier one)
  - Modifications with weight 0 or less and bones that are not found are skipped.
  - `locals` &mut [ToonSkeletonNode]: Local values per node. (what the ToonPoseAdjust::Hook hook receives, modified values are written here)
  - `skeleton` &ToonSkeleton: skeleton of read_glb_arrays. (bones are looked up by name)
  - `modifiers` &[BoneModifier]: Modifications per bone. (BoneModifier, in order)

  Example: Raise the hip by half and apply two modifications to the same bone

  ```rust
  use std::collections::HashMap;
  use vanilla::{adjust_bones, BoneModifier, ToonSkeleton, ToonSkeletonNode};
  
  let hip = ToonSkeletonNode { name: String::from("hip"), parent_index: None, translation: [0.0, 1.0, 0.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
  let skeleton = ToonSkeleton { joints: vec![0], inverse_bind_matrices: Vec::new(), nodes: vec![hip], animations: HashMap::new() };
  let mut locals = skeleton.nodes.clone();
  adjust_bones(&mut locals, &skeleton, &[
  	BoneModifier { name: Some("hip"), translation: Some([0.0, 4.0, 0.0]), weight: 0.5, ..Default::default() },
  	BoneModifier { node_index: Some(0), scale: Some([2.0, 1.0, 1.0]), ..Default::default() },
  ]);
  assert_eq!(locals[0].translation, [0.0, 3.0, 0.0]);
  assert_eq!(locals[0].scale, [2.0, 1.0, 1.0]);
  ```

- `pub fn write_skin_pose(skeleton: &ToonSkeleton, animation_name: &str, time: f64, target: &mut [f32], blend: Option<ToonPoseBlend>, swing_scales: Option<&ToonSwingScales>, pose_adjust: Option<ToonPoseAdjust>)` (method, since 0.4.0): Computes the skeleton pose. (samples the animation at that time and writes a matrix per bone into target, 16 each, rest pose if no animation)
  - If blend is given, blends in the previous animation's pose by that weight. (briefly, when the animation changes)
  - If swing_scales is given, that node in that animation has its rotation reduced toward the rest pose. (also applied to the blended previous pose)
  - With pose_adjust, each bone's local transform is modified after blending and before the skin matrices are built. (bone overrides such as poses not in the animation, head turning, crouching)
    Hook is called with (locals, skeleton) and that function modifies locals, Bones is applied as the per-bone modification table of adjust_bones. (None keeps the animation as is)
  - `skeleton` &ToonSkeleton: skeleton of read_glb_arrays.
  - `animation_name` &str: Animation name. (Rest pose if none)
  - `time` f64: Seconds. (wraps by the animation length, play-once animations are clamped to the end time)
  - `target` &mut [f32]: Where to write 16 per joint. (At least skeleton.joints count x 16, if short only the joints that fit are written, joint_matrices of ToonDrawOptions)
  - `blend` Option<ToonPoseBlend>: Previous pose to blend and its weight. (None means no blending)
  - `swing_scales` Option<&ToonSwingScales>: Swing scale as a list of (node index, scale) per animation name. (1 keeps as is, 0 gives the rest pose, None means none)
  - `pose_adjust` Option<ToonPoseAdjust>: Hook or modification table that modifies each bone's local transform after blending. (None keeps the animation as is, the locals a hook receives are a copy for this call only, so they can be modified in place)

## ToonChannelPath (enum) (Clone, Copy, PartialEq, Eq, Debug)

One track of an animation. (one of a node's translation, rotation or scale)

- `Translation`: Position. (Three values)
- `Rotation`: Rotation. (Four quaternion values)
- `Scale`: Scale. (Three values)
- `Other`: Anything else. (such as weights, read only and not applied to the pose)

## BoneModifyMode (enum) (Clone, Copy, PartialEq, Eq, Debug, Default)

Bone modification mode. (how one modification in the adjust_bones and write_skin_pose ToonPoseAdjust::Bones table is applied to the animation values, original BoneModifyMode)
- Add: layers on top of the animation. Translation is added in parent space, rotation is multiplied after in the bone's own space (q x given rotation), and scale is multiplied.
- Replace: sets the given values. (translation, rotation and scale are all local values in parent space)

Example: Lower the hip and make the head ignore the animation and face forward

```rust
use std::collections::HashMap;
use vanilla::{write_skin_pose, BoneModifier, BoneModifyMode, ToonPoseAdjust, ToonSkeleton, ToonSkeletonNode};

let hip = ToonSkeletonNode { name: String::from("hip"), parent_index: None, translation: [0.0, 5.0, 0.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let head = ToonSkeletonNode { name: String::from("head"), parent_index: Some(0), translation: [0.0, 1.0, 0.0], rotation: [0.0, 0.7071, 0.0, 0.7071], scale: [1.0; 3], matrix: None };
let skeleton = ToonSkeleton { joints: vec![0, 1], inverse_bind_matrices: Vec::new(), nodes: vec![hip, head], animations: HashMap::new() };
let modifiers = [
	BoneModifier { name: Some("hip"), translation: Some([0.0, -2.0, 0.0]), mode: BoneModifyMode::Add, ..Default::default() },
	BoneModifier { name: Some("head"), rotation: Some([0.0, 0.0, 0.0, 1.0]), mode: BoneModifyMode::Replace, ..Default::default() },
];
let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
write_skin_pose(&skeleton, "Idle", 0.0, &mut joint_matrices, None, None, Some(ToonPoseAdjust::Bones(&modifiers)));
assert_eq!(joint_matrices[13], 3.0);
assert_eq!(joint_matrices[16], 1.0);
```

- `Add` = `0`: Layer on top of the animation. (translation is added, rotation is multiplied after in the bone's own space, scale is multiplied)
- `Replace` = `1`: Set to the given values. (local values in parent space)

## ToonPoseAdjust (enum)

What modifies each bone's local transform in a blended pose. (pose_adjust of write_skin_pose, the function or table of the seventh argument of the original writeSkinPose)
- Hook: called with (local values per node, skeleton) and that function modifies the local values in place. (a copy for this call only, so the skeleton's rest pose does not change)
- Bones: applied as the per-bone modification table of adjust_bones.

Example: Use table modifications and direct modifications together inside a hook

```rust
use std::collections::HashMap;
use vanilla::{adjust_bones, write_skin_pose, BoneModifier, ToonPoseAdjust, ToonSkeleton, ToonSkeletonNode};

let spine = ToonSkeletonNode { name: String::from("spine"), parent_index: None, translation: [0.0; 3], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let tail = ToonSkeletonNode { name: String::from("tail"), parent_index: Some(0), translation: [0.0, 0.0, -1.0], rotation: [0.0, 0.0, 0.0, 1.0], scale: [1.0; 3], matrix: None };
let skeleton = ToonSkeleton { joints: vec![0, 1], inverse_bind_matrices: Vec::new(), nodes: vec![spine, tail], animations: HashMap::new() };
let half_angle: f64 = 0.3;
let mut crouch = |locals: &mut [ToonSkeletonNode], pose_skeleton: &ToonSkeleton| {
	adjust_bones(locals, pose_skeleton, &[BoneModifier { name: Some("spine"), rotation: Some([half_angle.sin(), 0.0, 0.0, half_angle.cos()]), ..Default::default() }]);
	locals[1].scale[1] *= 1.2;
};
let mut joint_matrices = vec![0.0f32; skeleton.joints.len() * 16];
write_skin_pose(&skeleton, "Idle", 0.0, &mut joint_matrices, None, None, Some(ToonPoseAdjust::Hook(&mut crouch)));
assert!((joint_matrices[5] - 0.6f64.cos() as f32).abs() < 0.0001);
assert_eq!(skeleton.nodes[1].scale, [1.0; 3]);
```

- `Hook` = `Hook(&'a mut dyn FnMut(&mut [ToonSkeletonNode], &ToonSkeleton))`: Hook that modifies the local values per node directly. (locals, skeleton)
- `Bones` = `Bones(&'a [BoneModifier<'a>])`: Per-bone modification table of adjust_bones.
