Skip to content

Commit 2d6c001

Browse files
committed
Tidying up code and remove old files
1 parent 545f207 commit 2d6c001

6 files changed

Lines changed: 194 additions & 134 deletions

File tree

v3/src/camera3d/Camera3D.js

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var Class = require('../utils/Class');
2-
3-
var util = require('./vecutil');
4-
2+
var Matrix4 = require('../math/Matrix4');
3+
var RotateVec3 = require('../math/RotateVec3');
54
var Vector3 = require('../math/Vector3');
65
var Vector4 = require('../math/Vector4');
7-
var Matrix4 = require('../math/Matrix4');
6+
7+
// Local cache vars
88

99
var tmpVec3 = new Vector3();
1010
var tmpVec4 = new Vector4();
@@ -16,7 +16,8 @@ var tmpVec4 = new Vector4();
1616
*/
1717
var Camera3D = new Class({
1818

19-
initialize: function() {
19+
initialize: function ()
20+
{
2021
this.direction = new Vector3(0, 0, -1);
2122
this.up = new Vector3(0, 1, 0);
2223
this.position = new Vector3();
@@ -46,9 +47,12 @@ var Camera3D = new Class({
4647
* @param {Number} width the viewport width
4748
* @param {Number} height the viewport height
4849
*/
49-
setViewport: function(width, height) {
50+
setViewport: function (width, height)
51+
{
5052
this.viewportWidth = width;
5153
this.viewportHeight = height;
54+
55+
return this;
5256
},
5357

5458
/**
@@ -59,109 +63,133 @@ var Camera3D = new Class({
5963
* @param {[type]} vec [description]
6064
* @return {[type]} [description]
6165
*/
62-
translate: function(x, y, z) {
63-
if (typeof x === "object") {
66+
translate: function (x, y, z)
67+
{
68+
if (typeof x === 'object')
69+
{
6470
this.position.x += x.x || 0;
6571
this.position.y += x.y || 0;
6672
this.position.z += x.z || 0;
67-
} else {
73+
}
74+
else
75+
{
6876
this.position.x += x || 0;
6977
this.position.y += y || 0;
7078
this.position.z += z || 0;
7179
}
80+
81+
return this;
7282
},
7383

74-
lookAt: function(x, y, z) {
75-
var dir = this.direction,
76-
up = this.up;
84+
lookAt: function (x, y, z)
85+
{
86+
var dir = this.direction;
87+
var up = this.up;
7788

78-
if (typeof x === "object") {
89+
if (typeof x === 'object')
90+
{
7991
dir.copy(x);
80-
} else {
92+
}
93+
else
94+
{
8195
dir.set(x, y, z);
8296
}
8397

8498
dir.sub(this.position).normalize();
8599

86-
//calculate right vector
100+
// Calculate right vector
87101
tmpVec3.copy(dir).cross(up).normalize();
88102

89-
//calculate up vector
103+
// Calculate up vector
90104
up.copy(tmpVec3).cross(dir).normalize();
105+
106+
return this;
91107
},
92108

93-
rotate: function(radians, axis) {
94-
util.rotate(this.direction, axis, radians);
95-
util.rotate(this.up, axis, radians);
109+
rotate: function (radians, axis)
110+
{
111+
RotateVec3(this.direction, axis, radians);
112+
RotateVec3(this.up, axis, radians);
113+
114+
return this;
96115
},
97116

98-
rotateAround: function(point, radians, axis) {
117+
rotateAround: function (point, radians, axis)
118+
{
99119
tmpVec.copy(point).sub(this.position);
120+
100121
this.translate(tmpVec);
101122
this.rotate(radians, axis);
102123
this.translate(tmpVec.negate());
124+
125+
return this;
103126
},
104127

105-
project: function(vec, out) {
106-
if (!out)
107-
out = new Vector4();
128+
project: function (vec, out)
129+
{
130+
if (out === undefined) { out = new Vector4(); }
108131

109-
//TODO: support viewport XY
110-
var viewportWidth = this.viewportWidth,
111-
viewportHeight = this.viewportHeight,
112-
n = Camera3D.NEAR_RANGE,
113-
f = Camera3D.FAR_RANGE;
132+
// TODO: support viewport XY
133+
var viewportWidth = this.viewportWidth;
134+
var viewportHeight = this.viewportHeight;
135+
var n = Camera3D.NEAR_RANGE;
136+
var f = Camera3D.FAR_RANGE;
114137

115-
// for useful Z and W values we should do the usual steps...
116-
// clip space -> NDC -> window coords
138+
// For useful Z and W values we should do the usual steps: clip space -> NDC -> window coords
117139

118-
//implicit 1.0 for w component
140+
// Implicit 1.0 for w component
119141
tmpVec4.set(vec.x, vec.y, vec.z, 1.0);
120142

121-
//transform into clip space
143+
// Transform into clip space
122144
tmpVec4.transformMat4(this.combined);
123145

124-
//now into NDC
146+
// Now into NDC
125147
tmpVec4.x = tmpVec4.x / tmpVec4.w;
126148
tmpVec4.y = tmpVec4.y / tmpVec4.w;
127149
tmpVec4.z = tmpVec4.z / tmpVec4.w;
128150

129-
//and finally into window coordinates
151+
// And finally into window coordinates
130152
out.x = viewportWidth / 2 * tmpVec4.x + (0 + viewportWidth / 2);
131153
out.y = viewportHeight / 2 * tmpVec4.y + (0 + viewportHeight / 2);
132154
out.z = (f - n) / 2 * tmpVec4.z + (f + n) / 2;
133155

134-
//if the out vector has a fourth component, we also store (1/clip.w)
135-
//same idea as gl_FragCoord.w
156+
// If the out vector has a fourth component, we also store (1/clip.w), same idea as gl_FragCoord.w
136157
if (out.w === 0 || out.w)
158+
{
137159
out.w = 1 / tmpVec4.w;
160+
}
138161

139162
return out;
140163
},
141164

142-
unproject: function(vec, out) {
143-
if (!out)
144-
out = new Vector3();
165+
unproject: function (vec, out)
166+
{
167+
if (out === undefined) { out = new Vector3(); }
145168

146169
var viewport = tmpVec4.set(0, 0, this.viewportWidth, this.viewportHeight);
170+
147171
return out.copy(vec).unproject(viewport, this.invProjectionView);
148172
},
149173

150-
getPickRay: function(x, y) {
151-
var origin = this.ray.origin.set(x, y, 0),
152-
direction = this.ray.direction.set(x, y, 1),
153-
viewport = tmpVec4.set(0, 0, this.viewportWidth, this.viewportHeight),
154-
mtx = this.invProjectionView;
174+
getPickRay: function (x, y)
175+
{
176+
var origin = this.ray.origin.set(x, y, 0);
177+
var direction = this.ray.direction.set(x, y, 1);
178+
var viewport = tmpVec4.set(0, 0, this.viewportWidth, this.viewportHeight);
179+
var mtx = this.invProjectionView;
155180

156181
origin.unproject(viewport, mtx);
182+
157183
direction.unproject(viewport, mtx);
158184

159185
direction.sub(origin).normalize();
186+
160187
return this.ray;
161188
},
162189

163-
update: function() {
164-
//left empty for subclasses
190+
update: function ()
191+
{
192+
// Left empty for subclasses
165193
}
166194
});
167195

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,100 @@
1+
var Camera = require('./Camera3D');
12
var Class = require('../utils/Class');
2-
3+
var Matrix4 = require('../math/Matrix4');
34
var Vector3 = require('../math/Vector3');
45
var Vector4 = require('../math/Vector4');
5-
var Matrix4 = require('../math/Matrix4');
66

7-
var Camera = require('./Camera3D');
7+
// Local cache vars
88

99
var tmpVec3 = new Vector3();
1010

1111
var OrthographicCamera = new Class({
1212

1313
Extends: Camera,
1414

15-
zoom: {
16-
17-
set: function(v) {
18-
if (typeof v !== 'number')
19-
throw new Error("zoom must be a number");
20-
this._zoom = v;
21-
},
15+
initialize: function (viewportWidth, viewportHeight)
16+
{
17+
if (viewportWidth === undefined) { viewportWidth = 0; }
18+
if (viewportHeight === undefined) { viewportHeight = 0; }
2219

23-
get: function() {
24-
return this._zoom;
25-
}
26-
},
27-
28-
initialize: function(viewportWidth, viewportHeight) {
2920
Camera.call(this);
30-
this.viewportWidth = viewportWidth||0;
31-
this.viewportHeight = viewportHeight||0;
21+
22+
this.viewportWidth = viewportWidth;
23+
this.viewportHeight = viewportHeight;
3224

3325
this._zoom = 1.0;
26+
3427
this.near = 0;
28+
3529
this.update();
3630
},
3731

38-
setToOrtho: function(yDown, viewportWidth, viewportHeight) {
32+
setToOrtho: function (yDown, viewportWidth, viewportHeight)
33+
{
34+
if (viewportWidth === undefined) { viewportWidth = this.viewportWidth; }
35+
if (viewportHeight === undefined) { viewportHeight = this.viewportHeight; }
36+
3937
var zoom = this.zoom;
40-
viewportWidth = typeof viewportWidth === "number" ? viewportWidth : this.viewportWidth;
41-
viewportHeight = typeof viewportHeight === "number" ? viewportHeight : this.viewportHeight;
4238

43-
this.up.set(0, yDown ? -1 : 1, 0);
44-
this.direction.set(0, 0, yDown ? 1 : -1);
39+
this.up.set(0, (yDown) ? -1 : 1, 0);
40+
this.direction.set(0, 0, (yDown) ? 1 : -1);
4541
this.position.set(zoom * viewportWidth / 2, zoom * viewportHeight / 2, 0);
4642

4743
this.viewportWidth = viewportWidth;
4844
this.viewportHeight = viewportHeight;
49-
this.update();
45+
46+
return this.update();
5047
},
5148

52-
update: function() {
49+
update: function ()
50+
{
5351
//TODO: support x/y offset
54-
var w = this.viewportWidth,
55-
h = this.viewportHeight,
56-
near = Math.abs(this.near),
57-
far = Math.abs(this.far),
58-
zoom = this.zoom;
59-
60-
if (w===0||h===0) {
61-
//What to do here... hmm ?
62-
return;
52+
var w = this.viewportWidth;
53+
var h = this.viewportHeight;
54+
var near = Math.abs(this.near);
55+
var far = Math.abs(this.far);
56+
var zoom = this.zoom;
57+
58+
if (w===0 || h===0)
59+
{
60+
// What to do here... hmm?
61+
return this;
6362
}
6463

6564
this.projection.ortho(
6665
zoom * -w / 2, zoom * w / 2,
6766
zoom * -h / 2, zoom * h / 2,
68-
near, far);
67+
near,
68+
far
69+
);
6970

70-
//build the view matrix
71+
// Build the view matrix
7172
tmpVec3.copy(this.position).add(this.direction);
73+
7274
this.view.lookAt(this.position, tmpVec3, this.up);
7375

74-
//projection * view matrix
76+
// Projection * view matrix
7577
this.combined.copy(this.projection).mul(this.view);
7678

77-
//invert combined matrix, used for unproject
79+
// Invert combined matrix, used for unproject
7880
this.invProjectionView.copy(this.combined).invert();
81+
82+
return this;
83+
},
84+
85+
zoom: {
86+
87+
set: function (value)
88+
{
89+
this._zoom = value;
90+
},
91+
92+
get: function ()
93+
{
94+
return this._zoom;
95+
}
7996
}
97+
8098
});
8199

82-
module.exports = OrthographicCamera;
100+
module.exports = OrthographicCamera;

0 commit comments

Comments
 (0)