JSFiddle - React, Tailwind, and code Playground
by Matt
HTML
<base href="https://rawcdn.githack.com/mrdoob/three.js/r169/examples/" />
<script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/",
"@pixiv/three-vrm": "https://cdn.jsdelivr.net/npm/@pixiv/[email protected]/lib/three-vrm.module.min.js",
"lil-gui": "https://cdn.jsdelivr.net/npm/[email protected]/dist/lil-gui.esm.min.js"
}
}
</script>
CSS
body {
background-color: lightblue;
}
canvas {
position: fixed;
inset: 0;
}
JavaScript
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js'
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
import { VRMLoaderPlugin, VRMUtils } from '@pixiv/three-vrm';
import { GUI } from 'lil-gui'
const VRM_URL = "https://rawcdn.githack.com/pixiv/three-vrm/0f9fd31a0baeb16587365eb5597ebb4012f42e46/packages/three-vrm/examples/models/VRM1_Constraint_Twist_Sample.vrm"
let scene, camera, renderer
let orbitControls, clock, mixer
let currentVrm, vrmPose, pureMixer
let params
init().then(animate)
async function init() {
params = {
mixer: {
autoUpdate: false,
delta: 0.1,
update: () => updateMixerShared(params.mixer.delta),
pure: true,
resetPureState: () => pureMixer.reset()
}
}
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(75, undefined, 0.1, 1000)
camera.position.set(-0.2, 1.5, 1)
clock = new THREE.Clock()
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
renderer.toneMapping = THREE.ACESFilmicToneMapping
renderer.outputEncoding = THREE.sRGBEncoding
document.body.appendChild(renderer.domElement)
orbitControls = new OrbitControls(camera, renderer.domElement)
orbitControls.target.set(0, 1, 0)
const directionalLight = new THREE.DirectionalLight("white", Math.PI)
scene.add(directionalLight)
const gltfLoader = new GLTFLoader()
gltfLoader.register( ( parser ) => {
return new VRMLoaderPlugin( parser );
} );
const gltf = await gltfLoader.loadAsync(VRM_URL)
currentVrm = gltf.userData.vrm
VRMUtils.removeUnnecessaryVertices( gltf.scene );
VRMUtils.combineSkeletons( gltf.scene );
VRMUtils.rotateVRM0( gltf.scene )
scene.add(gltf.scene)
// Animation
mixer = new THREE.AnimationMixer(currentVrm.scene)
pureMixer = createPureMixer({
mixer,
saveState: () => currentVrm.humanoid.getNormalizedPose(),
loadState: (vrmPose) => {
...