Skip to content

Commit 5de98bc

Browse files
committed
add Camera methods from previous version, for compatibility
1 parent 2827ea2 commit 5de98bc

File tree

2 files changed

+228
-3
lines changed

2 files changed

+228
-3
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ Examples
7979
History
8080
---------
8181

82+
Version 1.2:
83+
84+
- Support TGA textures
85+
- Camera class rewritten and support added for the mouse wheel
86+
and middle mouse button
87+
- Lines and points supported in OBJ files
88+
- Support loading custom textures from byte arrays
89+
- Add method to create capsule shapes in Meshes class
90+
- Mesh builder (vector3 method) avoids adding degenerate triangles
91+
- Optimizations and bug fixes
92+
8293
Version 1.1:
8394

8495
- Add frame.js, a frame counter helper used in some of the demos

camera.js

+217-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,154 @@
1+
/** @private */
2+
function OldCamera(scene, fov, nearZ, farZ){
3+
this.target=[0,0,0]; // target position
4+
this.distance=Math.max(nearZ,10); // distance from the target in units
5+
this.position=[0,0,this.distance];
6+
this.angleQuat=GLMath.quatIdentity();
7+
this.angleX=0;
8+
this.angleY=0;
9+
this.scene=scene;
10+
this.fov=fov;
11+
this.near=nearZ;
12+
this.far=farZ;
13+
if(typeof InputTracker!="undefined"){
14+
this.input=new InputTracker(scene.getContext().canvas);
15+
}
16+
this.currentAspect=this.scene.getAspect();
17+
this.scene.setPerspective(this.fov,this.currentAspect,this.near,this.far);
18+
this._updateLookAt();
19+
}
20+
OldCamera._vec3diff=function(a,b){
21+
return [a[0]-b[0],a[1]-b[1],a[2]-b[2]];
22+
}
23+
/** @deprecated */
24+
OldCamera.prototype.moveAngleHorizontal=function(angleDegrees){
25+
if(angleDegrees!=0){
26+
this._angleHorizontal(-angleDegrees);
27+
this._resetPosition();
28+
}
29+
return this;
30+
}
31+
/** @private */
32+
OldCamera.prototype._angleHorizontal=function(angleDegrees){
33+
var change=((angleDegrees>=0 && angleDegrees<360) ? angleDegrees : ((angleDegrees%360)+(angleDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy180;
34+
this.angleY+=change;
35+
this.angleY=((this.angleY>=0 && this.angleY<GLMath.PiTimes2) ? this.angleY : ((this.angleY%GLMath.PiTimes2)+(this.angleY<0 ? GLMath.PiTimes2 : 0)))
36+
this.angleQuat=GLMath.quatMultiply(this.angleQuat,
37+
GLMath.quatFromAxisAngle(change*GLMath.Num180DividedByPi,0,1,0));
38+
GLMath.quatNormInPlace(this.angleQuat);
39+
}
40+
/** @private */
41+
OldCamera.prototype._angleVertical=function(angleDegrees){
42+
var change=((angleDegrees>=0 && angleDegrees<360) ? angleDegrees : ((angleDegrees%360)+(angleDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy180;
43+
this.angleX+=change;
44+
this.angleX=((this.angleX>=0 && this.angleX<GLMath.PiTimes2) ? this.angleX : ((this.angleX%GLMath.PiTimes2)+(this.angleX<0 ? GLMath.PiTimes2 : 0)))
45+
this.angleQuat=GLMath.quatMultiply(this.angleQuat,
46+
GLMath.quatFromAxisAngle(change*GLMath.Num180DividedByPi,1,0,0));
47+
GLMath.quatNormInPlace(this.angleQuat);
48+
}
49+
/** @deprecated */
50+
OldCamera.prototype.moveAngleVertical=function(angleDegrees){
51+
if(angleDegrees!=0){
52+
this._angleVertical(-angleDegrees);
53+
this._resetPosition();
54+
}
55+
return this;
56+
}
57+
OldCamera.prototype.turnVertical=function(angleDegrees){
58+
if(angleDegrees!=0){
59+
var pos=this.getPosition();
60+
this._angleVertical(angleDegrees);
61+
this._resetTarget();
62+
}
63+
return this;
64+
}
65+
/** @private */
66+
OldCamera.prototype._resetPosition=function(){
67+
var diff=OldCamera._vec3diff(this.target,this.position);
68+
var dist=GLMath.vec3length(diff);
69+
var newVector=GLMath.quatTransform(this.angleQuat,[0,0,-dist]);
70+
this.position[0]=this.target[0]-newVector[0];
71+
this.position[1]=this.target[1]-newVector[1];
72+
this.position[2]=this.target[2]-newVector[2];
73+
this._updateLookAt();
74+
this.setDistance(this.distance);
75+
}
76+
/** @private */
77+
OldCamera.prototype._resetTarget=function(){
78+
var diff=OldCamera._vec3diff(this.target,this.position);
79+
var dist=GLMath.vec3length(diff);
80+
var newVector=GLMath.quatTransform(this.angleQuat,[0,0,-dist]);
81+
this.target[0]=this.position[0]+newVector[0];
82+
this.target[1]=this.position[1]+newVector[1];
83+
this.target[2]=this.position[2]+newVector[2];
84+
this._updateLookAt();
85+
this.setDistance(this.distance);
86+
}
87+
OldCamera.prototype.turnHorizontal=function(angleDegrees){
88+
if(angleDegrees!=0){
89+
var pos=this.getPosition();
90+
this._angleHorizontal(-angleDegrees);
91+
this._resetTarget();
92+
}
93+
return this;
94+
}
95+
96+
97+
/**
98+
* Not documented yet.
99+
* @param {*} dist
100+
*/
101+
OldCamera.prototype.setDistance=function(dist){
102+
if(dist<0)throw new Error("invalid distance")
103+
if(dist<this.near)dist=this.near;
104+
var diff=OldCamera._vec3diff(this.target,this.position);
105+
var curdist=GLMath.vec3length(diff);
106+
var distdiff=curdist-diff;
107+
this.distance=dist;
108+
if(distdiff!=0){
109+
var factor=dist/curdist;
110+
this.position[0]=this.target[0]-diff[0]*factor;
111+
this.position[1]=this.target[1]-diff[1]*factor;
112+
this.position[2]=this.target[2]-diff[2]*factor;
113+
this._updateLookAt();
114+
}
115+
return this;
116+
}
117+
OldCamera.prototype.movePosition=function(cx, cy, cz){
118+
if(cx!=0 && cy!=0 && cz!=0){
119+
var pos=this.getPosition();
120+
this.target[0]+=(cx-pos[0]);
121+
this.target[1]+=(cy-pos[1]);
122+
this.target[2]+=(cz-pos[2]);
123+
this.position=[cx,cy,cz];
124+
}
125+
return this;
126+
}
127+
/** @private */
128+
OldCamera.prototype.moveForward=function(dist){
129+
if(dist!=0){
130+
var diff=OldCamera._vec3diff(this.target,this.position);
131+
var realDist=GLMath.vec3length(diff);
132+
var factor=dist/realDist;
133+
this.position[0]+=diff[0]*factor;
134+
this.position[1]+=diff[1]*factor;
135+
this.position[2]+=diff[2]*factor;
136+
this.target[0]+=diff[0]*factor;
137+
this.target[1]+=diff[1]*factor;
138+
this.target[2]+=diff[2]*factor;
139+
this._updateLookAt();
140+
}
141+
return this;
142+
}
143+
/** @private */
144+
OldCamera.prototype._updateLookAt=function(){
145+
this.scene.setLookAt(this.getPosition(),this.target,
146+
GLMath.quatTransform(this.angleQuat,[0,1,0]));
147+
}
148+
/** @private */
149+
OldCamera.prototype.getPosition=function(){
150+
return this.position.slice(0,3);
151+
}
1152
/**
2153
* A class for controlling the projection and
3154
* view of a 3D scene, in the nature of an abstract "camera".
@@ -26,6 +177,12 @@ function Camera(scene, fov, nearZ, farZ){
26177
this.fov=fov;
27178
this.near=nearZ;
28179
this.far=farZ;
180+
// for backward compatibility only; will be removed
181+
this.oldcam=new OldCamera(scene,fov,nearZ,farZ);
182+
// for backward compatibility only; will be removed
183+
this.oldcam.input=null;
184+
// for backward compatibility only; will be removed
185+
this.calledOldcam=false;
29186
if(typeof InputTracker!="undefined"){
30187
this.input=new InputTracker(scene.getContext().canvas);
31188
}
@@ -50,9 +207,14 @@ Camera.prototype._updateView=function(){
50207
*/
51208
Camera.prototype.setDistance=function(dist){
52209
// don't move closer than the near plane
53-
this.distance=Math.max(this.near,dist);
54-
this.position=[0,0,this.distance]
55-
return this._updateView();
210+
if(this.calledOldcam){
211+
this.oldcam.setDistance(dist);
212+
return this;
213+
} else {
214+
this.distance=Math.max(this.near,dist);
215+
this.position=[0,0,this.distance]
216+
return this._updateView();
217+
}
56218
}
57219
/** @private */
58220
Camera.prototype._orbit=function(deltaMouseX,deltaMouseY,angleMultiplier){
@@ -82,6 +244,58 @@ Camera.prototype._trackball=function(deltaMouseX,deltaMouseY,angleMultiplier){
82244
GLMath.quatNormInPlace(this.inverseQuat);
83245
}
84246
}
247+
/** @deprecated */
248+
Camera.prototype.moveAngleVertical=function(angleDegrees){
249+
this.calledOldcam=true;
250+
this.oldcam.moveAngleVertical(angleDegrees)
251+
this.position=this.oldcam.getPosition();
252+
return this;
253+
}
254+
/** @deprecated */
255+
Camera.prototype.moveAngleHorizontal=function(angleDegrees){
256+
this.calledOldcam=true;
257+
this.oldcam.moveAngleHorizontal(angleDegrees)
258+
this.position=this.oldcam.getPosition();
259+
return this;
260+
}
261+
/** @deprecated */
262+
Camera.prototype.turnVertical=function(angleDegrees){
263+
this.calledOldcam=true;
264+
this.oldcam.turnVertical(angleDegrees)
265+
this.position=this.oldcam.getPosition();
266+
return this;
267+
}
268+
/** @deprecated */
269+
Camera.prototype.turnHorizontal=function(angleDegrees){
270+
this.calledOldcam=true;
271+
this.oldcam.turnHorizontal(angleDegrees)
272+
this.position=this.oldcam.getPosition();
273+
return this;
274+
}
275+
/** @deprecated */
276+
Camera.prototype.movePosition=function(cx,cy,cz){
277+
this.calledOldcam=true;
278+
this.oldcam.movePosition(cx,cy,cz)
279+
this.position=this.oldcam.getPosition();
280+
return this;
281+
}
282+
Camera.prototype.moveClose=function(dist){
283+
return this.setDistance(this.distance-dist);
284+
}
285+
/** @deprecated */
286+
Camera.prototype.moveForward=function(dist){
287+
this.calledOldcam=true;
288+
this.oldcam.moveForward(dist)
289+
this.position=this.oldcam.getPosition();
290+
return this;
291+
}
292+
/**
293+
* Not documented yet.
294+
* @param {*} e
295+
*/
296+
Camera.prototype.getPosition=function(){
297+
return this.position.slice(0,3);
298+
}
85299
/**
86300
* Not documented yet.
87301
* @param {*} e

0 commit comments

Comments
 (0)