简介
模型导入到上一节也就先告一段落了,接下来,我将学习动画方面的知识。在这之前,我先复习了一下基础变化的相关知识。
基础动画就是缩放、位置和旋转,也就是配置模型的scale
、position
和rotation
。我直接写了一个案例,来实现三种动画。
实现案例
案例查看地址:http://www.wjceo.com/blog/threejs/2018-03-25/140.html
- 首先,我们创建了三个添加动画的模型:立方体、球和圆柱。
sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial);
- 然后在render里面分别修改它们的
scale
、position
和rotation
。
//处理位置变化的球的移动
positionStep += gui.positionStep;
sphere.position.x = 10*Math.cos(positionStep) - 10;
sphere.position.y = 5 + Math.abs(Math.sin(positionStep))*10;
//处理立方体的旋转
cube.rotation.x += gui.rotationStep;
cube.rotation.y += gui.rotationStep;
cube.rotation.z += gui.rotationStep;
//处理圆柱体的缩放
scaleStep += gui.scaleStep;
cylinder.scale.x = Math.abs(Math.sin(scaleStep));
cylinder.scale.y = Math.abs(Math.cos(scaleStep));
cylinder.scale.z = Math.abs(Math.sin(scaleStep));
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<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/controls/OrbitControls.js"></script>
<script src="/lib/js/libs/stats.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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是,没有设置的这个清晰 THREE.PCFShadowMap
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, 40, 50);
camera.lookAt(new THREE.Vector3(0,0,0));
}
var scene;
function initScene() {
scene = new THREE.Scene();
}
//初始化dat.GUI简化试验流程
var gui;
function initGui() {
//声明一个保存需求修改的相关数据的对象
gui = {
scaleStep:0.01,
rotationStep:0.03,
positionStep:0.05
};
var datGui = new dat.GUI();
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
datGui.add(gui, "scaleStep", 0, 1);
datGui.add(gui, "rotationStep", 0, 1);
datGui.add(gui, "positionStep", 0, 1);
}
var light;
function initLight() {
scene.add(new THREE.AmbientLight(0x444444));
light = new THREE.PointLight(0xffffff);
light.position.set(15,50,10);
//告诉平行光需要开启阴影投射
light.castShadow = true;
scene.add(light);
}
var sphere,cube,cylinder;
function initModel() {
//球
var sphereGeometry = new THREE.SphereGeometry(5,200,200);
var sphereMaterial = new THREE.MeshLambertMaterial({color:0xaaaaaa});
sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.x = -10;
sphere.position.y = 5;
sphere.position.z = 10;
//告诉球需要投射阴影
sphere.castShadow = true;
scene.add(sphere);
//辅助工具
var helper = new THREE.AxesHelper(50);
scene.add(helper);
//立方体
var cubeGeometry = new THREE.CubeGeometry(10,10,8);
var cubeMaterial = new THREE.MeshLambertMaterial({color:0x00ffff});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 15;
cube.position.y = 5;
cube.position.z = -5;
//告诉立方体需要投射阴影
cube.castShadow = true;
scene.add(cube);
//创建圆柱体
var cylinderGeometry = new THREE.CylinderGeometry(5, 5, 10, 40, 20);
var cylinderMaterial = new THREE.MeshPhongMaterial({color:0xff5f4d});
cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial);
cylinder.position.set(-15, 5, -10);
cylinder.castShadow = true;
scene.add(cylinder);
//底部平面
var planeGeometry = new THREE.PlaneGeometry(100,100);
var planeMaterial = new THREE.MeshStandardMaterial({color:0xaaaaaa});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = - 0.5 * Math.PI;
plane.position.y = 0;
//告诉底部平面需要接收阴影
plane.receiveShadow = true;
scene.add(plane);
}
//初始化性能插件
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.minDistance = 100;
//设置相机距离原点的最远距离
controls.maxDistance = 200;
//是否开启右键拖拽
controls.enablePan = true;
}
var positionStep = 0,scaleStep = 0; //模型旋转的速度
function render() {
//处理简单动画
//处理位置变化的球的移动
positionStep += gui.positionStep;
sphere.position.x = 10*Math.cos(positionStep) - 10;
sphere.position.y = 5 + Math.abs(Math.sin(positionStep))*10;
//处理立方体的旋转
cube.rotation.x += gui.rotationStep;
cube.rotation.y += gui.rotationStep;
cube.rotation.z += gui.rotationStep;
//处理圆柱体的缩放
scaleStep += gui.scaleStep;
cylinder.scale.x = Math.abs(Math.sin(scaleStep));
cylinder.scale.y = Math.abs(Math.cos(scaleStep));
cylinder.scale.z = Math.abs(Math.sin(scaleStep));
}
//窗口变动触发的函数
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
//更新控制器
render();
//更新性能插件
stats.update();
controls.update();
renderer.render( scene, camera );
requestAnimationFrame(animate);
}
function draw() {
initGui();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
animate();
window.onresize = onWindowResize;
}
</script>
</html>