ThreeJS 官方案例学习(webgl_animation_keyframes)
1.效果图

2.源码
<template>
<div>
<div id="container"></div>
</div>
</template>
<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
import gsap from 'gsap';
export default {
data() {
return {
container: null,
scene: null,
camera: null,
renderer: null,
controller: null,
stats: null,
mixer: null,
model: null,
gui: null,
clock: new THREE.Clock()
};
},
mounted() {
this.init()
window.addEventListener("resize", this.onWindowSize)
},
beforeUnmount() {
console.log('beforeUnmount===============');
this.renderer.forceContextLoss();
this.renderer.dispose();
this.renderer.content = null;
this.renderer = null
cancelAnimationFrame(this.animate)
this.scene.remove(this.camera)
this.scene.traverse((child) => {
if (child.material) {
if (child.material instanceof Array) {
child.material.forEach((item) => item.dispose());
} else {
child.material.dispose();
if (child.material.map) {
child.material.map.dispose();
}
}
}
if (child.geometry) {
child.geometry.dispose();
child.geometry.attributes = null;
}
child = null;
});
if (this.mixer) {
this.mixer.uncacheClip(this.mixer._actions[0]._clip);
this.mixer = null;
}
if (this.gui) {
this.gui.destroy()
this.gui = null
}
this.scene = null
this.container = null
this.camera = null
this.controller = null
this.stats = null
this.model = null
},
methods: {
init() {
this.container = document.getElementById('container')
this.setScene()
this.setCamera()
this.setRenderer()
this.setController()
this.addHelper()
this.setPMREMGenerator()
this.setLight()
this.setGltfLoader()
this.addStatus()
},
setScene() {
this.scene = new THREE.Scene()
this.scene.background = new THREE.Color(0xbfe3dd);
},
setCamera() {
this.camera = new THREE.PerspectiveCamera(60, this.container.clientWidth / this.container.clientHeight, 1, 100)
this.camera.position.set(5, 2, 8)
this.camera.aspect = this.container.clientWidth / this.container.clientHeight;
this.camera.updateProjectionMatrix();
this.camera.lookAt(new THREE.Vector3(0, 0, 0))
this.scene.add(this.camera)
},
setRenderer() {
this.renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true,
})
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.container.appendChild(this.renderer.domElement);
},
setController() {
this.controller = new OrbitControls(this.camera, this.renderer.domElement);
this.controller.enableDamping = true;
this.controller.dampingFactor = 0.04;
this.controller.target.set(0, 0.5, 0);
},
addHelper() {
let helper = new THREE.CameraHelper(this.camera);
let axisHelper = new THREE.AxesHelper(150);
this.scene.add(axisHelper);
let gridHelper = new THREE.GridHelper(100, 30, 0x2C2C2C, 0x888888);
this.scene.add(gridHelper);
},
setPMREMGenerator() {
const pmremGenerator = new THREE.PMREMGenerator(this.renderer);
this.scene.environment = pmremGenerator.fromScene(new RoomEnvironment(this.renderer), 0.04).texture;
},
setLight() {
const ambientLight = new THREE.AmbientLight(0x404040, 4);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.0);
const test = new THREE.PointLight("#ffffff", 10, 2);
const testHelperMap = new THREE.PointLightHelper(test);
},
addStatus() {
this.stats = new Stats();
this.container.appendChild(this.stats.dom);
},
onWindowSize() {
this.camera.aspect = this.container.clientWidth / this.container.clientHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.renderer.setPixelRatio(window.devicePixelRatio)
},
animate() {
const delta = this.clock.getDelta();
if (this.mixer) {
this.mixer.update(delta);
}
requestAnimationFrame(this.animate);
this.controller.update(delta);
this.stats.update();
this.renderer.render(this.scene, this.camera);
},
setGltfLoader() {
let that = this
const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("/draco/gltf/");
loader.setDRACOLoader(dracoLoader);
loader.load('/model/gltf/LittlestTokyo.glb', (gltf) => {
that.model = gltf.scene
that.model.position.set(1, 1, 0)
that.model.scale.set(0.01, 0.01, 0.01)
that.scene.add(that.model)
that.mixer = new THREE.AnimationMixer(that.model)
that.mixer.clipAction(gltf.animations[0]).play()
that.animate();
}, undefined, (err => {
console.error(err)
}))
}
},
};
</script>
<style>
#container {
position: absolute;
width: 100%;
height: 100%;
}
</style>