npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@davidcks/r3f-vrm

v0.0.6

Published

A React package to easily integrate and interact with VRM avatars using @react-three/fiber and Three.js.

Downloads

247

Readme

r3f-vrm

This package provides a set of tools and components to easily integrate and interact with VRM avatars using @react-three/fiber and Three.js. It includes several managers for handling expressions, positions, and focus, as well as utilities for loading and managing VRM models and their animations.

It currently provides utilities for converting fbx, vrma, and bvh files to expressions.

Table of Contents

Installation

This package is hosted on npm:

npm install @DavidCks/r3f-vrm

Animations

You can use FBX, BVH, and VRMA files for animations. The MotionExpressionManager will convert these files to expressions that can be applied to the VRM model.

Supported Animation Formats

  • FBX:
    • mixamo
    • mootion
  • BVH:
    • MoMask
  • VRMA:
    • all

Usage

VRMAvatar

VRMAvatar is a r3f component that loads a VRM model and triggers callbacks when the model is loaded and during the loading process. It integrates with the VRMManager to allow for controlling expressions and other interactions.

Example

import React from "react";
import { VRMAvatar } from "r3f-vrm";
import { Vector3 } from "three";
import { useThree } from "@react-three/fiber";

const MyVRMAvatar: React.FC = () => {
  const managerRef = React.useRef<VRMManager>(null);
  const {scene, camera} = useThree();

  useFrame((_,delta) => {
    scene.update(delta);
  });

  return (
    <VRMAvatar
      scene={scene}
      camera={camera}
      vrmUrl="path_to_vrm.vrm"
      onLoad={
        (manager) => {
          managerRef.current = manager;
          manager.focusManager.focus();
        }
      }
    />
  );
};

export default MyVRMAvatar;

Managers

VRMManager

VRMManager is the central class that manages the VRM model, including its focus, expressions, and position. It integrates with the VRM object and the Three.js Camera and Scene, and provides methods to control the avatar.

Methods

  • update(delta: number): Updates the VRM model and its managers. Call this method each frame.
  • focusManager: Handles camera focus on the avatar.
  • expressionManager: Manages facial, mouth, and motion expressions.
  • positionManager: Controls the position of the VRM model.

FocusManager

The FocusManager controls the camera's focus on the VRM model's head with smooth transitions and offsets.

Methods

  • focus(focusProps): Focuses the camera on the VRM model with optional properties.
  • unfocus(): Removes the focus from the VRM model.
  • update(delta: number): Updates the focus each frame. The VRMManager handles focus updates in its' update method.

ExpressionManager

ExpressionManager handles the expressions of the VRM model, including face, mouth, and motion expressions. It delegates these responsibilities to specific sub-managers.

Methods

  • express(expressionInput): Applies expressions to the VRM model.
  • update(delta: number): Processes expressions each frame. The VRMManager handles expression updates in its' update method.

Animation Conversion Worker

The conversion of animation files (fbx, vrma, bvh) to expressions can be offloaded to a Web Worker. This worker is created and managed by the MotionExpressionWorkerClient class, which is used internally by the MotionExpressionManager.

This is useful for offloading the conversion process to a separate thread, preventing the main thread from being blocked during the conversion to avoid performance issues such as stuttering.

To use the worker, you need to copy the motion-expression-worker.bundle.js file from the build directory to your public directory and pass the url to the VRMAvatar components motionExpressionWorkerUr prop:

<VRMAvatar
  ...
  motionExpressionWorkerUrl={"motion-expression-worker.bundle.js"}
  ...
/>

To copy it to the public directory, you should use your build tool (e.g., webpack, parcel, etc.) to copy the file to the public directory. For example, in webpack, you can use the CopyWebpackPlugin:

const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  ...
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        { from: "node_modules/@DavidCks/r3f-vrm/build/motion-expression-worker.bundle.js", to: "public" },
      ],
    }),
  ],
  ...
};

Prefetching Animations

The VRMAvatar has a prefetchFiles prop that will prefetch the animations in the background. You can use this if you know you'll need the animations later on and want to avoid loading times.

<VRMAvatar
  ...
  prefetchFiles={["idle.fbx", "walk.bvh", "run.vrma", ...]}
  ...
/>

PositionManager

PositionManager controls the position of the VRM model in the 3D space.

Methods

  • get position(): Returns the current position of the VRM model.
  • set position(newPosition: Vector3): Sets a new position for the VRM model.
  • updatePosition(newPosition: Vector3): Updates the position.
  • move(delta: Vector3): Moves the VRM model by a delta value.

Development

To modify or extend this package, fork or clone the repository and install dependencies:

git clone https://github.com/DavidCks/r3f-vrm.git
cd r3f-vrm
npm install

The src directory has an index.tsx file that acts as a testing playground. You can use storybook to test and develop through that component (or configure your own if necessary):

npm run dev

License

This project is licensed under the MIT License. See the LICENSE file for more details.


This package simplifies the integration of VRM avatars into React applications, providing powerful tools for interaction and animation within a 3D scene.