Skip to content

Commit 92e531c

Browse files
authored
Cameras: Convert to classes. (#21623)
1 parent 2a2a8a5 commit 92e531c

File tree

3 files changed

+62
-69
lines changed

3 files changed

+62
-69
lines changed

src/cameras/Camera.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@ import { Matrix4 } from '../math/Matrix4.js';
22
import { Object3D } from '../core/Object3D.js';
33
import { Vector3 } from '../math/Vector3.js';
44

5-
function Camera() {
5+
class Camera extends Object3D {
66

7-
Object3D.call( this );
7+
constructor() {
88

9-
this.type = 'Camera';
9+
super();
1010

11-
this.matrixWorldInverse = new Matrix4();
11+
this.type = 'Camera';
1212

13-
this.projectionMatrix = new Matrix4();
14-
this.projectionMatrixInverse = new Matrix4();
13+
this.matrixWorldInverse = new Matrix4();
1514

16-
}
17-
18-
Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
19-
20-
constructor: Camera,
15+
this.projectionMatrix = new Matrix4();
16+
this.projectionMatrixInverse = new Matrix4();
2117

22-
isCamera: true,
18+
}
2319

24-
copy: function ( source, recursive ) {
20+
copy( source, recursive ) {
2521

26-
Object3D.prototype.copy.call( this, source, recursive );
22+
super.copy( source, recursive );
2723

2824
this.matrixWorldInverse.copy( source.matrixWorldInverse );
2925

@@ -32,9 +28,9 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
3228

3329
return this;
3430

35-
},
31+
}
3632

37-
getWorldDirection: function ( target ) {
33+
getWorldDirection( target ) {
3834

3935
if ( target === undefined ) {
4036

@@ -49,30 +45,32 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
4945

5046
return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
5147

52-
},
48+
}
5349

54-
updateMatrixWorld: function ( force ) {
50+
updateMatrixWorld( force ) {
5551

56-
Object3D.prototype.updateMatrixWorld.call( this, force );
52+
super.updateMatrixWorld( force );
5753

5854
this.matrixWorldInverse.copy( this.matrixWorld ).invert();
5955

60-
},
56+
}
6157

62-
updateWorldMatrix: function ( updateParents, updateChildren ) {
58+
updateWorldMatrix( updateParents, updateChildren ) {
6359

64-
Object3D.prototype.updateWorldMatrix.call( this, updateParents, updateChildren );
60+
super.updateWorldMatrix( updateParents, updateChildren );
6561

6662
this.matrixWorldInverse.copy( this.matrixWorld ).invert();
6763

68-
},
64+
}
6965

70-
clone: function () {
66+
clone() {
7167

7268
return new this.constructor().copy( this );
7369

7470
}
7571

76-
} );
72+
}
73+
74+
Camera.prototype.isCamera = true;
7775

7876
export { Camera };

src/cameras/OrthographicCamera.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Camera } from './Camera.js';
2-
import { Object3D } from '../core/Object3D.js';
32

43
class OrthographicCamera extends Camera {
54

@@ -114,7 +113,7 @@ class OrthographicCamera extends Camera {
114113

115114
toJSON( meta ) {
116115

117-
const data = Object3D.prototype.toJSON.call( this, meta );
116+
const data = super.toJSON( meta );
118117

119118
data.object.zoom = this.zoom;
120119
data.object.left = this.left;

src/cameras/PerspectiveCamera.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
import { Camera } from './Camera.js';
2-
import { Object3D } from '../core/Object3D.js';
32
import { MathUtils } from '../math/MathUtils.js';
43

5-
function PerspectiveCamera( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
4+
class PerspectiveCamera extends Camera {
65

7-
Camera.call( this );
6+
constructor( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
87

9-
this.type = 'PerspectiveCamera';
8+
super();
109

11-
this.fov = fov;
12-
this.zoom = 1;
10+
this.type = 'PerspectiveCamera';
1311

14-
this.near = near;
15-
this.far = far;
16-
this.focus = 10;
12+
this.fov = fov;
13+
this.zoom = 1;
1714

18-
this.aspect = aspect;
19-
this.view = null;
15+
this.near = near;
16+
this.far = far;
17+
this.focus = 10;
2018

21-
this.filmGauge = 35; // width of the film (default in millimeters)
22-
this.filmOffset = 0; // horizontal film offset (same unit as gauge)
19+
this.aspect = aspect;
20+
this.view = null;
2321

24-
this.updateProjectionMatrix();
22+
this.filmGauge = 35; // width of the film (default in millimeters)
23+
this.filmOffset = 0; // horizontal film offset (same unit as gauge)
2524

26-
}
27-
28-
PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
29-
30-
constructor: PerspectiveCamera,
25+
this.updateProjectionMatrix();
3126

32-
isPerspectiveCamera: true,
27+
}
3328

34-
copy: function ( source, recursive ) {
29+
copy( source, recursive ) {
3530

36-
Camera.prototype.copy.call( this, source, recursive );
31+
super.copy( source, recursive );
3732

3833
this.fov = source.fov;
3934
this.zoom = source.zoom;
@@ -50,7 +45,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
5045

5146
return this;
5247

53-
},
48+
}
5449

5550
/**
5651
* Sets the FOV by focal length in respect to the current .filmGauge.
@@ -60,47 +55,47 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
6055
*
6156
* Values for focal length and film gauge must have the same unit.
6257
*/
63-
setFocalLength: function ( focalLength ) {
58+
setFocalLength( focalLength ) {
6459

6560
/** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */
6661
const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
6762

6863
this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope );
6964
this.updateProjectionMatrix();
7065

71-
},
66+
}
7267

7368
/**
7469
* Calculates the focal length from the current .fov and .filmGauge.
7570
*/
76-
getFocalLength: function () {
71+
getFocalLength() {
7772

7873
const vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
7974

8075
return 0.5 * this.getFilmHeight() / vExtentSlope;
8176

82-
},
77+
}
8378

84-
getEffectiveFOV: function () {
79+
getEffectiveFOV() {
8580

8681
return MathUtils.RAD2DEG * 2 * Math.atan(
8782
Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom );
8883

89-
},
84+
}
9085

91-
getFilmWidth: function () {
86+
getFilmWidth() {
9287

9388
// film not completely covered in portrait format (aspect < 1)
9489
return this.filmGauge * Math.min( this.aspect, 1 );
9590

96-
},
91+
}
9792

98-
getFilmHeight: function () {
93+
getFilmHeight() {
9994

10095
// film not completely covered in landscape format (aspect > 1)
10196
return this.filmGauge / Math.max( this.aspect, 1 );
10297

103-
},
98+
}
10499

105100
/**
106101
* Sets an offset in a larger frustum. This is useful for multi-window or
@@ -137,7 +132,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
137132
*
138133
* Note there is no reason monitors have to be the same size or in a grid.
139134
*/
140-
setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
135+
setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
141136

142137
this.aspect = fullWidth / fullHeight;
143138

@@ -165,9 +160,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
165160

166161
this.updateProjectionMatrix();
167162

168-
},
163+
}
169164

170-
clearViewOffset: function () {
165+
clearViewOffset() {
171166

172167
if ( this.view !== null ) {
173168

@@ -177,9 +172,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
177172

178173
this.updateProjectionMatrix();
179174

180-
},
175+
}
181176

182-
updateProjectionMatrix: function () {
177+
updateProjectionMatrix() {
183178

184179
const near = this.near;
185180
let top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom;
@@ -207,11 +202,11 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
207202

208203
this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
209204

210-
},
205+
}
211206

212-
toJSON: function ( meta ) {
207+
toJSON( meta ) {
213208

214-
const data = Object3D.prototype.toJSON.call( this, meta );
209+
const data = super.toJSON( meta );
215210

216211
data.object.fov = this.fov;
217212
data.object.zoom = this.zoom;
@@ -231,7 +226,8 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
231226

232227
}
233228

234-
} );
229+
}
235230

231+
PerspectiveCamera.prototype.isPerspectiveCamera = true;
236232

237233
export { PerspectiveCamera };

0 commit comments

Comments
 (0)