Path: blob/main/projects/HexGL/bkcore/hexgl/CameraChase.js
4627 views
/*1* HexGL2* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>3* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.4* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.5*/67var bkcore = bkcore || {};8bkcore.hexgl = bkcore.hexgl || {};910bkcore.hexgl.CameraChase = function(opts)11{12this.dir = new THREE.Vector3(0,0,1);13this.up = new THREE.Vector3(0,1,0);14this.target = new THREE.Vector3();15this.speedOffset = 0;16this.speedOffsetMax = 10;17this.speedOffsetStep = 0.05;1819this.modes = {20CHASE: 0,21ORBIT: 122}23this.mode = this.modes.CHASE;2425this.camera = opts.camera;26this.targetObject = opts.target;27this.cameraCube = opts.cameraCube == undefined ? null : opts.cameraCube;2829this.yoffset = opts.yoffest == undefined ? 8.0 : opts.yoffest;30this.zoffset = opts.zoffset == undefined ? 10.0 : opts.zoffset;31this.viewOffset = opts.viewOffset == undefined ? 10.0 : opts.viewOffset;32this.orbitOffset = 12;33this.lerp = opts.lerp == undefined ? 0.5 : opts.lerp;34this.time = 0.0;35}3637bkcore.hexgl.CameraChase.prototype.update = function(dt, ratio)38{39if(this.mode == this.modes.CHASE)40{41this.dir.set(0,0,1);42this.up.set(0,1,0);4344this.targetObject.matrix.rotateAxis(this.up);45this.targetObject.matrix.rotateAxis(this.dir);4647this.speedOffset += (this.speedOffsetMax*ratio - this.speedOffset) * Math.min(1, 0.3*dt);4849this.target.copy(this.targetObject.position);50this.target.subSelf(this.dir.multiplyScalar(this.zoffset + this.speedOffset));51this.target.addSelf(this.up.multiplyScalar(this.yoffset));52this.target.y += -this.up.y + this.yoffset;53this.camera.position.copy(this.target);5455this.camera.lookAt(this.dir.normalize().multiplyScalar(this.viewOffset).addSelf(this.targetObject.position));56}57else if(this.mode == this.modes.ORBIT)58{59this.time += dt*.008;60this.dir.set(61Math.cos(this.time)*this.orbitOffset,62this.yoffset/2,63Math.sin(this.time)*this.orbitOffset64);65this.target.copy(this.targetObject.position).addSelf(this.dir);66this.camera.position.copy(this.target);67this.camera.lookAt(this.targetObject.position);68}6970if(this.cameraCube != null)71this.cameraCube.rotation.copy(this.camera.rotation);72}7374