pub struct AvatarAsset { /* private fields */ }Expand description
A loaded VRM 1.0 avatar: the parsed VRM data (meta, humanoid, expressions, look-at, first-person, spring bones, constraints) plus the renderable glTF assets (nodes, primitives, materials, skins, images).
Load it with AvatarAsset::from_bytes / AvatarAsset::load, or compose
it programmatically with crate::AvatarAssetBuilder. Drive a frame with
crate::AvatarRuntime.
Implementations§
Source§impl AvatarAsset
impl AvatarAsset
Sourcepub fn meta(&self) -> &AvatarMeta
pub fn meta(&self) -> &AvatarMeta
Returns a reference to the AvatarMeta.
Examples found in repository?
9fn main() {
10 let path = std::env::args().nth(1).expect("usage: basic <avatar.vrm>");
11 let avatar = AvatarAsset::load(&path).expect("failed to load avatar");
12
13 println!("avatar: {}", avatar.meta().name());
14 println!("primitives: {}", avatar.primitives().len());
15
16 let mut runtime = AvatarRuntime::new(avatar);
17
18 // One frame with spring bones and constraints enabled by default.
19 let frame = runtime.update(1.0 / 60.0).expect("valid delta time");
20
21 // Bake fully transformed, renderer-ready CPU meshes on demand.
22 let meshes = frame.bake_cpu();
23 for mesh in &meshes {
24 println!(
25 "mesh(node {}): vertices={} indices={} topology={:?} material={:?}",
26 mesh.node_id,
27 mesh.positions.len(),
28 mesh.indices.len(),
29 mesh.topology,
30 mesh.material_id,
31 );
32 }
33}Sourcepub fn expressions(&self) -> &Expressions
pub fn expressions(&self) -> &Expressions
Returns a reference to the Expressions.
Sourcepub fn primitives(&self) -> &[RenderPrimitive]
pub fn primitives(&self) -> &[RenderPrimitive]
Returns the list of RenderPrimitives parsed from the model.
Examples found in repository?
9fn main() {
10 let path = std::env::args().nth(1).expect("usage: basic <avatar.vrm>");
11 let avatar = AvatarAsset::load(&path).expect("failed to load avatar");
12
13 println!("avatar: {}", avatar.meta().name());
14 println!("primitives: {}", avatar.primitives().len());
15
16 let mut runtime = AvatarRuntime::new(avatar);
17
18 // One frame with spring bones and constraints enabled by default.
19 let frame = runtime.update(1.0 / 60.0).expect("valid delta time");
20
21 // Bake fully transformed, renderer-ready CPU meshes on demand.
22 let meshes = frame.bake_cpu();
23 for mesh in &meshes {
24 println!(
25 "mesh(node {}): vertices={} indices={} topology={:?} material={:?}",
26 mesh.node_id,
27 mesh.positions.len(),
28 mesh.indices.len(),
29 mesh.topology,
30 mesh.material_id,
31 );
32 }
33}Sourcepub fn primitive(&self, id: PrimitiveId) -> Option<&RenderPrimitive>
pub fn primitive(&self, id: PrimitiveId) -> Option<&RenderPrimitive>
Resolves a type-safe primitive identifier.
Sourcepub fn images(&self) -> &[AvatarImage]
pub fn images(&self) -> &[AvatarImage]
Returns the list of AvatarImages parsed from the model.
Sourcepub fn image(&self, id: ImageId) -> Option<&AvatarImage>
pub fn image(&self, id: ImageId) -> Option<&AvatarImage>
Resolves a type-safe image identifier.
Sourcepub fn materials(&self) -> &[Material]
pub fn materials(&self) -> &[Material]
Returns common glTF materials with optional MToon parameters.
Sourcepub fn material(&self, id: MaterialId) -> Option<&Material>
pub fn material(&self, id: MaterialId) -> Option<&Material>
Resolves a type-safe material identifier.
Sourcepub fn textures(&self) -> &[AvatarTexture]
pub fn textures(&self) -> &[AvatarTexture]
Returns the glTF texture objects, kept separate from image data.
Sourcepub fn texture(&self, id: TextureId) -> Option<&AvatarTexture>
pub fn texture(&self, id: TextureId) -> Option<&AvatarTexture>
Resolves a type-safe texture identifier.
Sourcepub fn base_color(
&self,
material_index: u32,
color_type: MaterialColorType,
) -> [f32; 4]
pub fn base_color( &self, material_index: u32, color_type: MaterialColorType, ) -> [f32; 4]
Returns the initial (base) color for the given material attribute.
Returns white [1, 1, 1, 1] when the material index is out of range or
the material does not support color_type.
Sourcepub fn instantiate(&self) -> AvatarInstance
pub fn instantiate(&self) -> AvatarInstance
Creates a mutable runtime state (expression weights, bone rotations, look-at target) detached from this avatar.
Sourcepub fn node_constraints(&self) -> &[NodeConstraint]
pub fn node_constraints(&self) -> &[NodeConstraint]
Returns the list of NodeConstraints.
Sourcepub fn spring_bone(&self) -> &SpringBone
pub fn spring_bone(&self) -> &SpringBone
Returns the SpringBone configuration.
Sourcepub fn first_person(&self) -> &FirstPerson
pub fn first_person(&self) -> &FirstPerson
Returns the FirstPerson configuration.
Sourcepub fn nodes(&self) -> &[AvatarNode]
pub fn nodes(&self) -> &[AvatarNode]
Returns the parsed node tree.
Sourcepub fn node(&self, id: NodeId) -> Option<&AvatarNode>
pub fn node(&self, id: NodeId) -> Option<&AvatarNode>
Resolves a type-safe node identifier.
Sourcepub fn compute_world_matrices(&self, bone_updates: &[BoneUpdate]) -> Vec<Mat4>
pub fn compute_world_matrices(&self, bone_updates: &[BoneUpdate]) -> Vec<Mat4>
Computes the world transform matrix of every node, applying the given bone rotations.
Sourcepub fn compute_joint_matrices(
&self,
skin: &Skin,
bone_updates: &[BoneUpdate],
) -> Vec<Mat4>
pub fn compute_joint_matrices( &self, skin: &Skin, bone_updates: &[BoneUpdate], ) -> Vec<Mat4>
Computes the joint matrices for skin, applying the given bone rotations.
Sourcepub fn vrmc_vrm(&self) -> Option<&VRMCVrmSchema>
pub fn vrmc_vrm(&self) -> Option<&VRMCVrmSchema>
Returns the raw VRMCVrmSchema this avatar was parsed from.
Sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, LoadError>
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, LoadError>
Loads and parses a VRM avatar from a file path.
Relative buffer and image URIs are resolved against the file’s parent directory, including percent-encoded image paths.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, LoadError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, LoadError>
Parses a VRM avatar from an in-memory byte slice.
Images must be embedded in buffer views or PNG/JPEG data URIs. Use
Self::from_file when the document contains external references.
Sourcepub fn from_file_with_options<P: AsRef<Path>>(
path: P,
options: LoadOptions,
) -> Result<LoadOutcome, LoadError>
pub fn from_file_with_options<P: AsRef<Path>>( path: P, options: LoadOptions, ) -> Result<LoadOutcome, LoadError>
Loads an avatar and returns any diagnostics produced by the selected validation mode. Relative buffer and image URIs are resolved against the file’s parent directory.
Sourcepub fn from_bytes_with_options(
bytes: &[u8],
options: LoadOptions,
) -> Result<LoadOutcome, LoadError>
pub fn from_bytes_with_options( bytes: &[u8], options: LoadOptions, ) -> Result<LoadOutcome, LoadError>
Parses an avatar and returns diagnostics for recoverable violations. Unresolved external buffer and image references are rejected.
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Self, LoadError>
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, LoadError>
Loads a VRM 1.0 asset from a file using strict validation.
Relative external buffers and images are resolved against the file’s
parent directory. Use Self::load_with_options to collect diagnostics
from permissive validation.
§Errors
Returns crate::LoadError when the file cannot be read, its glTF or
VRM data is invalid, an image cannot be decoded, or it requires an
unsupported extension.
§Examples
use vrm_runtime::AvatarAsset;
let avatar = AvatarAsset::load("avatar.vrm")?;
println!("{}", avatar.meta().name());Examples found in repository?
9fn main() {
10 let path = std::env::args().nth(1).expect("usage: basic <avatar.vrm>");
11 let avatar = AvatarAsset::load(&path).expect("failed to load avatar");
12
13 println!("avatar: {}", avatar.meta().name());
14 println!("primitives: {}", avatar.primitives().len());
15
16 let mut runtime = AvatarRuntime::new(avatar);
17
18 // One frame with spring bones and constraints enabled by default.
19 let frame = runtime.update(1.0 / 60.0).expect("valid delta time");
20
21 // Bake fully transformed, renderer-ready CPU meshes on demand.
22 let meshes = frame.bake_cpu();
23 for mesh in &meshes {
24 println!(
25 "mesh(node {}): vertices={} indices={} topology={:?} material={:?}",
26 mesh.node_id,
27 mesh.positions.len(),
28 mesh.indices.len(),
29 mesh.topology,
30 mesh.material_id,
31 );
32 }
33}More examples
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 let mut arguments = std::env::args().skip(1);
10 let avatar_path = arguments
11 .next()
12 .expect("usage: vrma <avatar.vrm> <motion.vrma>");
13 let animation_path = arguments
14 .next()
15 .expect("usage: vrma <avatar.vrm> <motion.vrma>");
16
17 let avatar = AvatarAsset::load(avatar_path)?;
18 let animation = VrmAnimation::load(animation_path)?;
19 let duration = animation.duration();
20 let mut runtime = AvatarRuntime::new(avatar);
21
22 runtime.play(
23 animation,
24 PlaybackOptions {
25 mode: PlaybackMode::Once,
26 ..PlaybackOptions::default()
27 },
28 )?;
29
30 let delta_seconds = 1.0 / 60.0;
31 let frame_count = ((duration / delta_seconds).ceil() as usize).max(1) + 1;
32 for frame_index in 0..frame_count {
33 let (draw_count, bone_update_count) = {
34 let frame = runtime.update(delta_seconds)?;
35 (frame.gpu().draws().len(), frame.bone_updates().len())
36 };
37
38 if frame_index % 60 == 0 || !runtime.is_playing() {
39 println!(
40 "time={:.3}s draws={} bone_updates={}",
41 runtime.playback_time().unwrap_or(0.0),
42 draw_count,
43 bone_update_count,
44 );
45 }
46 if !runtime.is_playing() {
47 break;
48 }
49 }
50
51 Ok(())
52}Sourcepub fn load_with_options<P: AsRef<Path>>(
path: P,
options: LoadOptions,
) -> Result<LoadOutcome, LoadError>
pub fn load_with_options<P: AsRef<Path>>( path: P, options: LoadOptions, ) -> Result<LoadOutcome, LoadError>
Loads a VRM asset from a file with explicit validation options.
Sourcepub const fn capabilities() -> Capabilities
pub const fn capabilities() -> Capabilities
Returns the feature set implemented by this crate version.