Tween.js
是一个轻量级的JavaScript
库,中文官网是:http://www.createjs.cc/tweenjs/。通过这个库可以很容易地实现某个属性在两个值之间的进行过渡,而且起始值和结束值之间的所有中间值都会自动计算出来,这个过程叫作tweening
(补间)。基础请查看:Tween.js补间动画插件入门
案例查看地址:http://www.wjceo.com/blog/threejs/2018-03-27/141.html
Tween.js
插件文件<script src="/lib/js/libs/tween.min.js">script>
tween
和tweenBack
。tween
补间定义了position
的属性如何从1过渡到0,tweenBack
刚好相反。通过使用chain()
方法可以将这两个补间衔接起来,这样当动画启动之后,程序就会在这两个补间循环。代码最后定义的是onUpdate
方法,这个方法通过获取当前的position
的值重新生成了所有顶点的y
轴的位置。var position = {
y: 1};
tween = new TWEEN.Tween(position).to({
y: 0}, 5000);
tween.easing(TWEEN.Easing.Sinusoidal.InOut);
var tweenBack = new TWEEN.Tween(position).to({
y: 1}, 5000);
tweenBack.easing(TWEEN.Easing.Sinusoidal.InOut);
tween.chain(tweenBack);
tweenBack.chain(tween);
var count = geometry.getAttribute("position").count;
//获取一下模型的最矮处
geometry.computeBoundingBox();
var minY = geometry.boundingBox.min.y;
var onUpdate = function () {
var y = this.y;
var arr = [];
for (var i = 0; i < count; i++) {
arr.push(geometry.localPosition[i * 3]);
arr.push((geometry.localPosition[i * 3 + 1]-minY) * y);
arr.push(geometry.localPosition[i * 3 + 2]);
}
geometry.getAttribute("position").array = new Float32Array(arr);
geometry.getAttribute("position").needsUpdate = true;
};
tween.onUpdate(onUpdate);
tweenBack.onUpdate(onUpdate);
tween.start()
。tween.start();
Tween.js
库什么时候来更新所有的补间,所以我们在每次刷新时,调用TWEEN.update()
方法:function render() {
TWEEN.update();
renderer.render(scene, camera);
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
canvas {
display: block;
}
style>
head>
<body onload="draw();">
body>
<script src="/lib/three.js">script>
<script src="/lib/js/loaders/PLYLoader.js">script>
<script src="/lib/js/controls/OrbitControls.js">script>
<script src="/lib/js/libs/stats.min.js">script>
<script src="/lib/js/libs/tween.min.js">script>
<script src="/lib/js/libs/dat.gui.min.js">script>
<script>
var renderer;
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
//告诉渲染器需要阴影效果
renderer.setClearColor(0xffffff);
document.body.appendChild(renderer.domElement);
}
var camera;
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 50);
camera.lookAt(new THREE.Vector3(0, 0, 0));
}
var scene;
function initScene() {
scene = new THREE.Scene();
}
//初始化dat.GUI简化试验流程
var gui;
function initGui() {
//声明一个保存需求修改的相关数据的对象
gui = {};
var datGui = new dat.GUI();
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
}
var light;
function initLight() {
scene.add(new THREE.AmbientLight(0x444444));
light = new THREE.PointLight(0xffffff);
light.position.set(0, 50, 50);
//告诉平行光需要开启阴影投射
light.castShadow = true;
scene.add(light);
}
function initModel() {
//辅助工具
var helper = new THREE.AxesHelper(50);
scene.add(helper);
var loader = new THREE.PLYLoader();
loader.load("/lib/models/ply/binary/Lucy100k.ply", function (geometry) {
//更新顶点的法向量
geometry.computeVertexNormals();
//创建纹理,并将模型添加到场景道中
var material = new THREE.MeshStandardMaterial({color: 0x0055ff});
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.y = Math.PI;
mesh.scale.set(0.02, 0.02, 0.02);
scene.add(mesh);
//保存一份默认的顶点信息
var position = geometry.getAttribute("position").array;
geometry.localPosition = [];
for (var i = 0; i < position.length; i++) {
geometry.localPosition.push(position[i]);
}
//初始化tween动画
initTween(geometry);
});
}
//添加tween动画
var tween;
function initTween(geometry) {
var position = {y: 1};
tween = new TWEEN.Tween(position).to({y: 0}, 5000);
tween.easing(TWEEN.Easing.Sinusoidal.InOut);
var tweenBack = new TWEEN.Tween(position).to({y: 1}, 5000);
tweenBack.easing(TWEEN.Easing.Sinusoidal.InOut);
tween.chain(tweenBack);
tweenBack.chain(tween);
var count = geometry.getAttribute("position").count;
//获取一下模型的最矮处
geometry.computeBoundingBox();
var minY = geometry.boundingBox.min.y;
var onUpdate = function () {
var y = this.y;
var arr = [];
for (var i = 0; i < count; i++) {
arr.push(geometry.localPosition[i * 3]);
arr.push((geometry.localPosition[i * 3 + 1]-minY) * y);
arr.push(geometry.localPosition[i * 3 + 2]);
}
geometry.getAttribute("position").array = new Float32Array(arr);
geometry.getAttribute("position").needsUpdate = true;
};
tween.onUpdate(onUpdate);
tweenBack.onUpdate(onUpdate);
tween.start();
}
//初始化性能插件
var stats;
function initStats() {
stats = new Stats();
document.body.appendChild(stats.dom);
}
//用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放
var controls;
function initControls() {
controls = new THREE.OrbitControls(camera, renderer.domElement);
// 如果使用animate方法时,将此函数删除
//controls.addEventListener( 'change', render );
// 使动画循环使用时阻尼或自转 意思是否有惯性
controls.enableDamping = true;
//动态阻尼系数 就是鼠标拖拽旋转灵敏度
//controls.dampingFactor = 0.25;
//是否可以缩放
controls.enableZoom = true;
//是否自动旋转
controls.autoRotate = false;
controls.autoRotateSpeed = 0.5;
//设置相机距离原点的最远距离
controls.minDistance = 1;
//设置相机距离原点的最远距离
controls.maxDistance = 200;
//是否开启右键拖拽
controls.enablePan = true;
}
function render() {
TWEEN.update();
renderer.render(scene, camera);
}
//窗口变动触发的函数
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
//更新控制器
render();
//更新性能插件
stats.update();
controls.update();
requestAnimationFrame(animate);
}
function draw() {
initGui();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
animate();
window.onresize = onWindowResize;
}
script>
html>