<template>
<div id="container" />
</div>
</template>
<script>
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
export default {
data() {
return {
clock: new THREE.Clock(),
mixer: null,
camera: null,
scene: null,
renderer: null
mounted() {
this.init()
methods: {
formatTooltip(val) {
return val
init() {
this.createScene()
this.createModels()
this.createLight()
this.createCamera()
this.createRender()
this.render()
createScene() {
this.scene = new THREE.Scene()
createModels() {
const THIS = this
const publicPath = process.env.BASE_URL
const loader = new GLTFLoader()
loader.load(`${publicPath}models/gltf/hand.gltf`, model => {
THIS.scene.add(model.scene)
this.mixer = new THREE.AnimationMixer(model.scene)
const clip = model.animations[0]
const action = this.mixer.clipAction(clip)
this.mixer.timeScale = 0.5
action.setDuration(2).play()
createLight() {
const ambientLight = new THREE.AmbientLight(0x111111)
this.scene.add(ambientLight)
const directionLight = new THREE.DirectionalLight(0xffffff)
directionLight.position.set(300, 200, 300)
directionLight.intensity = 1.5
this.scene.add(directionLight)
createCamera() {
const element = document.getElementById('container')
const width = element.clientWidth
const height = element.clientHeight
const k = width / height
this.camera = new THREE.PerspectiveCamera(45, k, 0.1, 1000)
this.camera.position.set(5, 5, 5)
this.camera.lookAt(new THREE.Vector3(0, 0, 0))
this.scene.add(this.camera)
createRender() {
const element = document.getElementById('container')
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
this.renderer.setSize(element.clientWidth, element.clientHeight)
this.renderer.setClearColor(0xeeeeee, 1)
element.appendChild(this.renderer.domElement)
render() {
if (this.mixer) {
const delta = this.clock.getDelta()
this.mixer.update(delta)
this.renderer.render(this.scene, this.camera)
requestAnimationFrame(this.render)
</script>
<style>
#container {
position: absolute;
width: 100%;
height: 100%;
</style>
var renderer = initRenderer();
renderer.setClearColor(0x262626);
var camera = initCamera();
var scene = new THREE.Sce
简单动画1.demo效果2.知识要点2.1 three.js动画分类2.1.1基础动画2.1.2移动相机2.1.3变形和蒙皮2.1.4加载外部动画2.2 基础动画实现方式2.2.1 利用requestAnimationFrame()函数实现动画2.2.2 利用setInterval()或setTimeout()实现动画3.实现要点3.1 创建底板、方块、球体、圆柱并添加到场景3.1 实现方块、球体、圆柱动画4.demo代码
1.demo效果
如上图,该demo实现方块旋转、球体上下跳动、圆柱放大缩小简单动
Threejs中有很多支持外部模型导入的库。在这里我将使用GLTF模型作为展示。
首先,先要引入官方给的引入库。
<script src="js/GLTFLoader.js"></script>
然后就是普普通通的搭建一个实验场地,一个控制器,方便观察
var Scene,camera,renderer;
init();
initcamera();
function init() {
Scene=new THREE.Scene();
第九章 创建动画和移动相机基础动画:render();function render(){
renderer.render(scene,camera); requestAnimationFrame(render);//通常保持60/s的帧率渲染
}一、简单动画复杂动画的基础function render(){
cube.rotation.x += controls.rotati
属性accessors是一个访问buffer数据的中间件
bufferView:指明了在bufferViews中的索引
componentType:指明了每一个数据有几个字节组成,常见的类型如下
‘5120’: Int8Array, // gl.BYTE
‘5121’: Uint8Array, // gl.UNSIGNED_BYTE
‘5122’: Int16Array, // gl.SHORT