JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="container"></div>
<div id="controls">
<a href="#" onclick="rotation(1)">Rotation 1</a><br>
<a href="#" onclick="rotation(2)">Rotation 2</a><br>
<a href="#" onclick="rotation(3)">Rotation 3</a><br>
</div>
</body>
CSS
body {
background-color: #000;
margin: 0;
overflow: hidden;
}
#controls {
position: absolute;
left: 8px;
top: 8px;
width: 160px;
height: 100px;
border-radius: 16px;
z-index: 2;
padding: 10px;
font-family: monospace;
font-size: 2em;
background-color: rgba(200,200,200,0.5);
}
a {
color: yellow;
text-decoration: none;
}
JavaScript
const THREE = await import('https://cdn.skypack.dev/three@0.136.0');
const { GLTFLoader } = await import('https://cdn.skypack.dev/three@0.136.0/examples/jsm/loaders/GLTFLoader.js');
import * as SkeletonUtils from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/utils/SkeletonUtils.js';
window.rotation = rotation;
let scene, renderer, camera;
let gGLTF = null;
init();
function init() {
const container = document.getElementById('container');
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 10);
scene = new THREE.Scene();
scene.background = new THREE.Color(0x66aa66);
renderer = new THREE.WebGLRenderer({
antialias: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
container.appendChild(renderer.domElement);
const light = new THREE.AmbientLight(0x999999);
scene.add(light);
const loader = new GLTFLoader();
loader.load(
'https://raw.githubusercontent.com/mrdoob/three.js/master/examples/models/gltf/Soldier.glb',
function (gltf) {
gGLTF = gltf;
const box = new THREE.Box3().setFromObject(gltf.scene);
const center = box.getCenter(new THREE.Vector3());
scene.add(gltf.scene);
rotation(1);
}
);
window.addEventListener(
'resize',
function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
},
false
);
renderer.setAnimationLoop(function () {
renderer.render(scene, camera);
});
}
function rotation(n) {
const box = new THREE.Box3().setFromObject(gGLTF.scene);
const center = box.getCenter(new THREE.Vector3());
if (n == 1) {
gGLTF.scene.rotation.y = 0.0;
...