Преглед изворни кода

Merge pull request #21266 from DefinitelyMaybe/objects

Convert some objects to es6 classes
Mr.doob пре 4 година
родитељ
комит
c4dc0ab1d5
5 измењених фајлова са 117 додато и 128 уклоњено
  1. 7 11
      src/objects/Group.js
  2. 34 35
      src/objects/LOD.js
  3. 7 9
      src/objects/LineLoop.js
  4. 31 32
      src/objects/Skeleton.js
  5. 38 41
      src/objects/Sprite.js

+ 7 - 11
src/objects/Group.js

@@ -1,20 +1,16 @@
 import { Object3D } from '../core/Object3D.js';
 
-function Group() {
+class Group extends Object3D {
 
-	Object3D.call( this );
+	constructor() {
 
-	this.type = 'Group';
+		super();
+		this.type = 'Group';
+		Object.defineProperty( this, 'isGroup', { value: true } );
 
-}
-
-Group.prototype = Object.assign( Object.create( Object3D.prototype ), {
-
-	constructor: Group,
+	}
 
-	isGroup: true
-
-} );
+}
 
 
 export { Group };

+ 34 - 35
src/objects/LOD.js

@@ -1,37 +1,36 @@
 import { Vector3 } from '../math/Vector3.js';
 import { Object3D } from '../core/Object3D.js';
 
-const _v1 = new Vector3();
-const _v2 = new Vector3();
+const _v1 = /*@__PURE__*/ new Vector3();
+const _v2 = /*@__PURE__*/ new Vector3();
 
-function LOD() {
+class LOD extends Object3D {
 
-	Object3D.call( this );
+	constructor() {
 
-	this._currentLevel = 0;
+		super();
 
-	this.type = 'LOD';
+		this._currentLevel = 0;
 
-	Object.defineProperties( this, {
-		levels: {
-			enumerable: true,
-			value: []
-		}
-	} );
-
-	this.autoUpdate = true;
-
-}
+		this.type = 'LOD';
 
-LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
+		Object.defineProperties( this, {
+			levels: {
+				enumerable: true,
+				value: []
+			},
+			isLOD: {
+				value: true,
+			}
+		} );
 
-	constructor: LOD,
+		this.autoUpdate = true;
 
-	isLOD: true,
+	}
 
-	copy: function ( source ) {
+	copy( source ) {
 
-		Object3D.prototype.copy.call( this, source, false );
+		super.copy( source, false );
 
 		const levels = source.levels;
 
@@ -47,9 +46,9 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		return this;
 
-	},
+	}
 
-	addLevel: function ( object, distance = 0 ) {
+	addLevel( object, distance = 0 ) {
 
 		distance = Math.abs( distance );
 
@@ -73,15 +72,15 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		return this;
 
-	},
+	}
 
-	getCurrentLevel: function () {
+	getCurrentLevel() {
 
 		return this._currentLevel;
 
-	},
+	}
 
-	getObjectForDistance: function ( distance ) {
+	getObjectForDistance( distance ) {
 
 		const levels = this.levels;
 
@@ -105,9 +104,9 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		return null;
 
-	},
+	}
 
-	raycast: function ( raycaster, intersects ) {
+	raycast( raycaster, intersects ) {
 
 		const levels = this.levels;
 
@@ -121,9 +120,9 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		}
 
-	},
+	}
 
-	update: function ( camera ) {
+	update( camera ) {
 
 		const levels = this.levels;
 
@@ -163,11 +162,11 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		}
 
-	},
+	}
 
-	toJSON: function ( meta ) {
+	toJSON( meta ) {
 
-		const data = Object3D.prototype.toJSON.call( this, meta );
+		const data = super.toJSON( meta );
 
 		if ( this.autoUpdate === false ) data.object.autoUpdate = false;
 
@@ -190,7 +189,7 @@ LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	}
 
-} );
+}
 
 
 export { LOD };

+ 7 - 9
src/objects/LineLoop.js

@@ -1,20 +1,18 @@
 import { Line } from './Line.js';
 
-function LineLoop( geometry, material ) {
+class LineLoop extends Line {
 
-	Line.call( this, geometry, material );
+	constructor( geometry, material ) {
 
-	this.type = 'LineLoop';
+		super( geometry, material );
+		this.type = 'LineLoop';
+		Object.defineProperty( this, 'isLineLoop', { value: true } );
 
-}
-
-LineLoop.prototype = Object.assign( Object.create( Line.prototype ), {
+	}
 
-	constructor: LineLoop,
 
-	isLineLoop: true,
 
-} );
+}
 
 
 export { LineLoop };

+ 31 - 32
src/objects/Skeleton.js

@@ -2,29 +2,29 @@ import { Bone } from './Bone.js';
 import { Matrix4 } from '../math/Matrix4.js';
 import { MathUtils } from '../math/MathUtils.js';
 
-const _offsetMatrix = new Matrix4();
-const _identityMatrix = new Matrix4();
+const _offsetMatrix = /*@__PURE__*/ new Matrix4();
+const _identityMatrix = /*@__PURE__*/ new Matrix4();
 
-function Skeleton( bones = [], boneInverses = [] ) {
+class Skeleton {
 
-	this.uuid = MathUtils.generateUUID();
+	constructor( bones = [], boneInverses = [] ) {
 
-	this.bones = bones.slice( 0 );
-	this.boneInverses = boneInverses;
-	this.boneMatrices = null;
+		this.uuid = MathUtils.generateUUID();
 
-	this.boneTexture = null;
-	this.boneTextureSize = 0;
+		this.bones = bones.slice( 0 );
+		this.boneInverses = boneInverses;
+		this.boneMatrices = null;
 
-	this.frame = - 1;
+		this.boneTexture = null;
+		this.boneTextureSize = 0;
 
-	this.init();
+		this.frame = - 1;
 
-}
+		this.init();
 
-Object.assign( Skeleton.prototype, {
+	}
 
-	init: function () {
+	init() {
 
 		const bones = this.bones;
 		const boneInverses = this.boneInverses;
@@ -57,9 +57,9 @@ Object.assign( Skeleton.prototype, {
 
 		}
 
-	},
+	}
 
-	calculateInverses: function () {
+	calculateInverses() {
 
 		this.boneInverses.length = 0;
 
@@ -77,9 +77,9 @@ Object.assign( Skeleton.prototype, {
 
 		}
 
-	},
+	}
 
-	pose: function () {
+	pose() {
 
 		// recover the bind-time world matrices
 
@@ -120,9 +120,9 @@ Object.assign( Skeleton.prototype, {
 
 		}
 
-	},
+	}
 
-	update: function () {
+	update() {
 
 		const bones = this.bones;
 		const boneInverses = this.boneInverses;
@@ -148,15 +148,15 @@ Object.assign( Skeleton.prototype, {
 
 		}
 
-	},
+	}
 
-	clone: function () {
+	clone() {
 
 		return new Skeleton( this.bones, this.boneInverses );
 
-	},
+	}
 
-	getBoneByName: function ( name ) {
+	getBoneByName( name ) {
 
 		for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
 
@@ -172,9 +172,9 @@ Object.assign( Skeleton.prototype, {
 
 		return undefined;
 
-	},
+	}
 
-	dispose: function ( ) {
+	dispose( ) {
 
 		if ( this.boneTexture !== null ) {
 
@@ -184,9 +184,9 @@ Object.assign( Skeleton.prototype, {
 
 		}
 
-	},
+	}
 
-	fromJSON: function ( json, bones ) {
+	fromJSON( json, bones ) {
 
 		this.uuid = json.uuid;
 
@@ -211,9 +211,9 @@ Object.assign( Skeleton.prototype, {
 
 		return this;
 
-	},
+	}
 
-	toJSON: function () {
+	toJSON() {
 
 		const data = {
 			metadata: {
@@ -244,7 +244,6 @@ Object.assign( Skeleton.prototype, {
 
 	}
 
-} );
-
+}
 
 export { Skeleton };

+ 38 - 41
src/objects/Sprite.js

@@ -10,61 +10,58 @@ import { SpriteMaterial } from '../materials/SpriteMaterial.js';
 
 let _geometry;
 
-const _intersectPoint = new Vector3();
-const _worldScale = new Vector3();
-const _mvPosition = new Vector3();
+const _intersectPoint = /*@__PURE__*/ new Vector3();
+const _worldScale = /*@__PURE__*/ new Vector3();
+const _mvPosition = /*@__PURE__*/ new Vector3();
 
-const _alignedPosition = new Vector2();
-const _rotatedPosition = new Vector2();
-const _viewWorldMatrix = new Matrix4();
+const _alignedPosition = /*@__PURE__*/ new Vector2();
+const _rotatedPosition = /*@__PURE__*/ new Vector2();
+const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
 
-const _vA = new Vector3();
-const _vB = new Vector3();
-const _vC = new Vector3();
+const _vA = /*@__PURE__*/ new Vector3();
+const _vB = /*@__PURE__*/ new Vector3();
+const _vC = /*@__PURE__*/ new Vector3();
 
-const _uvA = new Vector2();
-const _uvB = new Vector2();
-const _uvC = new Vector2();
+const _uvA = /*@__PURE__*/ new Vector2();
+const _uvB = /*@__PURE__*/ new Vector2();
+const _uvC = /*@__PURE__*/ new Vector2();
 
-function Sprite( material ) {
+class Sprite extends Object3D {
 
-	Object3D.call( this );
+	constructor( material ) {
 
-	this.type = 'Sprite';
+		super();
+		this.type = 'Sprite';
 
-	if ( _geometry === undefined ) {
+		if ( _geometry === undefined ) {
 
-		_geometry = new BufferGeometry();
+			_geometry = new BufferGeometry();
 
-		const float32Array = new Float32Array( [
-			- 0.5, - 0.5, 0, 0, 0,
-			0.5, - 0.5, 0, 1, 0,
-			0.5, 0.5, 0, 1, 1,
-			- 0.5, 0.5, 0, 0, 1
-		] );
+			const float32Array = new Float32Array( [
+				- 0.5, - 0.5, 0, 0, 0,
+				0.5, - 0.5, 0, 1, 0,
+				0.5, 0.5, 0, 1, 1,
+				- 0.5, 0.5, 0, 0, 1
+			] );
 
-		const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
+			const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
 
-		_geometry.setIndex( [ 0, 1, 2,	0, 2, 3 ] );
-		_geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
-		_geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
+			_geometry.setIndex( [ 0, 1, 2,	0, 2, 3 ] );
+			_geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
+			_geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
 
-	}
-
-	this.geometry = _geometry;
-	this.material = ( material !== undefined ) ? material : new SpriteMaterial();
-
-	this.center = new Vector2( 0.5, 0.5 );
+		}
 
-}
+		this.geometry = _geometry;
+		this.material = ( material !== undefined ) ? material : new SpriteMaterial();
 
-Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
+		this.center = new Vector2( 0.5, 0.5 );
 
-	constructor: Sprite,
+		Object.defineProperty( this, 'isSprite', { value: true } );
 
-	isSprite: true,
+	}
 
-	raycast: function ( raycaster, intersects ) {
+	raycast( raycaster, intersects ) {
 
 		if ( raycaster.camera === null ) {
 
@@ -137,9 +134,9 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		} );
 
-	},
+	}
 
-	copy: function ( source ) {
+	copy( source ) {
 
 		Object3D.prototype.copy.call( this, source );
 
@@ -151,7 +148,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	}
 
-} );
+}
 
 function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {