Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/hexgl/CameraChase.js
4627 views
1
/*
2
* HexGL
3
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
4
* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.
5
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
6
*/
7
8
var bkcore = bkcore || {};
9
bkcore.hexgl = bkcore.hexgl || {};
10
11
bkcore.hexgl.CameraChase = function(opts)
12
{
13
this.dir = new THREE.Vector3(0,0,1);
14
this.up = new THREE.Vector3(0,1,0);
15
this.target = new THREE.Vector3();
16
this.speedOffset = 0;
17
this.speedOffsetMax = 10;
18
this.speedOffsetStep = 0.05;
19
20
this.modes = {
21
CHASE: 0,
22
ORBIT: 1
23
}
24
this.mode = this.modes.CHASE;
25
26
this.camera = opts.camera;
27
this.targetObject = opts.target;
28
this.cameraCube = opts.cameraCube == undefined ? null : opts.cameraCube;
29
30
this.yoffset = opts.yoffest == undefined ? 8.0 : opts.yoffest;
31
this.zoffset = opts.zoffset == undefined ? 10.0 : opts.zoffset;
32
this.viewOffset = opts.viewOffset == undefined ? 10.0 : opts.viewOffset;
33
this.orbitOffset = 12;
34
this.lerp = opts.lerp == undefined ? 0.5 : opts.lerp;
35
this.time = 0.0;
36
}
37
38
bkcore.hexgl.CameraChase.prototype.update = function(dt, ratio)
39
{
40
if(this.mode == this.modes.CHASE)
41
{
42
this.dir.set(0,0,1);
43
this.up.set(0,1,0);
44
45
this.targetObject.matrix.rotateAxis(this.up);
46
this.targetObject.matrix.rotateAxis(this.dir);
47
48
this.speedOffset += (this.speedOffsetMax*ratio - this.speedOffset) * Math.min(1, 0.3*dt);
49
50
this.target.copy(this.targetObject.position);
51
this.target.subSelf(this.dir.multiplyScalar(this.zoffset + this.speedOffset));
52
this.target.addSelf(this.up.multiplyScalar(this.yoffset));
53
this.target.y += -this.up.y + this.yoffset;
54
this.camera.position.copy(this.target);
55
56
this.camera.lookAt(this.dir.normalize().multiplyScalar(this.viewOffset).addSelf(this.targetObject.position));
57
}
58
else if(this.mode == this.modes.ORBIT)
59
{
60
this.time += dt*.008;
61
this.dir.set(
62
Math.cos(this.time)*this.orbitOffset,
63
this.yoffset/2,
64
Math.sin(this.time)*this.orbitOffset
65
);
66
this.target.copy(this.targetObject.position).addSelf(this.dir);
67
this.camera.position.copy(this.target);
68
this.camera.lookAt(this.targetObject.position);
69
}
70
71
if(this.cameraCube != null)
72
this.cameraCube.rotation.copy(this.camera.rotation);
73
}
74