Browse Source

Extras: Convert to classes. (#21624)

* Extras: Convert to classes.

* Extras: Clean up.
Michael Herzog 4 years ago
parent
commit
2f14e629a7

+ 3 - 3
src/extras/DataUtils.js

@@ -1,11 +1,11 @@
 const _floatView = new Float32Array( 1 );
 const _floatView = new Float32Array( 1 );
 const _int32View = new Int32Array( _floatView.buffer );
 const _int32View = new Int32Array( _floatView.buffer );
 
 
-const DataUtils = {
+class DataUtils {
 
 
 	// Converts float32 to float16 (stored as uint16 value).
 	// Converts float32 to float16 (stored as uint16 value).
 
 
-	toHalfFloat: function ( val ) {
+	static toHalfFloat( val ) {
 
 
 		// Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
 		// Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
 
 
@@ -54,6 +54,6 @@ const DataUtils = {
 
 
 	}
 	}
 
 
-};
+}
 
 
 export { DataUtils };
 export { DataUtils };

+ 3 - 3
src/extras/ImageUtils.js

@@ -1,8 +1,8 @@
 let _canvas;
 let _canvas;
 
 
-const ImageUtils = {
+class ImageUtils {
 
 
-	getDataURL: function ( image ) {
+	static getDataURL( image ) {
 
 
 		if ( /^data:/i.test( image.src ) ) {
 		if ( /^data:/i.test( image.src ) ) {
 
 
@@ -59,6 +59,6 @@ const ImageUtils = {
 
 
 	}
 	}
 
 
-};
+}
 
 
 export { ImageUtils };
 export { ImageUtils };

+ 7 - 7
src/extras/ShapeUtils.js

@@ -1,10 +1,10 @@
 import { Earcut } from './Earcut.js';
 import { Earcut } from './Earcut.js';
 
 
-const ShapeUtils = {
+class ShapeUtils {
 
 
 	// calculate area of the contour polygon
 	// calculate area of the contour polygon
 
 
-	area: function ( contour ) {
+	static area( contour ) {
 
 
 		const n = contour.length;
 		const n = contour.length;
 		let a = 0.0;
 		let a = 0.0;
@@ -17,15 +17,15 @@ const ShapeUtils = {
 
 
 		return a * 0.5;
 		return a * 0.5;
 
 
-	},
+	}
 
 
-	isClockWise: function ( pts ) {
+	static isClockWise( pts ) {
 
 
 		return ShapeUtils.area( pts ) < 0;
 		return ShapeUtils.area( pts ) < 0;
 
 
-	},
+	}
 
 
-	triangulateShape: function ( contour, holes ) {
+	static triangulateShape( contour, holes ) {
 
 
 		const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
 		const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
 		const holeIndices = []; // array of hole indices
 		const holeIndices = []; // array of hole indices
@@ -64,7 +64,7 @@ const ShapeUtils = {
 
 
 	}
 	}
 
 
-};
+}
 
 
 function removeDupEndPts( points ) {
 function removeDupEndPts( points ) {
 
 

+ 35 - 35
src/extras/core/Curve.js

@@ -33,39 +33,39 @@ import { Matrix4 } from '../../math/Matrix4.js';
  *
  *
  **/
  **/
 
 
-function Curve() {
+class Curve {
 
 
-	this.type = 'Curve';
+	constructor() {
 
 
-	this.arcLengthDivisions = 200;
+		this.type = 'Curve';
 
 
-}
+		this.arcLengthDivisions = 200;
 
 
-Object.assign( Curve.prototype, {
+	}
 
 
 	// Virtual base class method to overwrite and implement in subclasses
 	// Virtual base class method to overwrite and implement in subclasses
 	//	- t [0 .. 1]
 	//	- t [0 .. 1]
 
 
-	getPoint: function ( /* t, optionalTarget */ ) {
+	getPoint( /* t, optionalTarget */ ) {
 
 
 		console.warn( 'THREE.Curve: .getPoint() not implemented.' );
 		console.warn( 'THREE.Curve: .getPoint() not implemented.' );
 		return null;
 		return null;
 
 
-	},
+	}
 
 
 	// Get point at relative position in curve according to arc length
 	// Get point at relative position in curve according to arc length
 	// - u [0 .. 1]
 	// - u [0 .. 1]
 
 
-	getPointAt: function ( u, optionalTarget ) {
+	getPointAt( u, optionalTarget ) {
 
 
 		const t = this.getUtoTmapping( u );
 		const t = this.getUtoTmapping( u );
 		return this.getPoint( t, optionalTarget );
 		return this.getPoint( t, optionalTarget );
 
 
-	},
+	}
 
 
 	// Get sequence of points using getPoint( t )
 	// Get sequence of points using getPoint( t )
 
 
-	getPoints: function ( divisions = 5 ) {
+	getPoints( divisions = 5 ) {
 
 
 		const points = [];
 		const points = [];
 
 
@@ -77,11 +77,11 @@ Object.assign( Curve.prototype, {
 
 
 		return points;
 		return points;
 
 
-	},
+	}
 
 
 	// Get sequence of points using getPointAt( u )
 	// Get sequence of points using getPointAt( u )
 
 
-	getSpacedPoints: function ( divisions = 5 ) {
+	getSpacedPoints( divisions = 5 ) {
 
 
 		const points = [];
 		const points = [];
 
 
@@ -93,20 +93,20 @@ Object.assign( Curve.prototype, {
 
 
 		return points;
 		return points;
 
 
-	},
+	}
 
 
 	// Get total curve arc length
 	// Get total curve arc length
 
 
-	getLength: function () {
+	getLength() {
 
 
 		const lengths = this.getLengths();
 		const lengths = this.getLengths();
 		return lengths[ lengths.length - 1 ];
 		return lengths[ lengths.length - 1 ];
 
 
-	},
+	}
 
 
 	// Get list of cumulative segment lengths
 	// Get list of cumulative segment lengths
 
 
-	getLengths: function ( divisions ) {
+	getLengths( divisions ) {
 
 
 		if ( divisions === undefined ) divisions = this.arcLengthDivisions;
 		if ( divisions === undefined ) divisions = this.arcLengthDivisions;
 
 
@@ -139,18 +139,18 @@ Object.assign( Curve.prototype, {
 
 
 		return cache; // { sums: cache, sum: sum }; Sum is in the last element.
 		return cache; // { sums: cache, sum: sum }; Sum is in the last element.
 
 
-	},
+	}
 
 
-	updateArcLengths: function () {
+	updateArcLengths() {
 
 
 		this.needsUpdate = true;
 		this.needsUpdate = true;
 		this.getLengths();
 		this.getLengths();
 
 
-	},
+	}
 
 
 	// Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
 	// Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
 
 
-	getUtoTmapping: function ( u, distance ) {
+	getUtoTmapping( u, distance ) {
 
 
 		const arcLengths = this.getLengths();
 		const arcLengths = this.getLengths();
 
 
@@ -223,14 +223,14 @@ Object.assign( Curve.prototype, {
 
 
 		return t;
 		return t;
 
 
-	},
+	}
 
 
 	// Returns a unit vector tangent at t
 	// Returns a unit vector tangent at t
 	// In case any sub curve does not implement its tangent derivation,
 	// In case any sub curve does not implement its tangent derivation,
 	// 2 points a small delta apart will be used to find its gradient
 	// 2 points a small delta apart will be used to find its gradient
 	// which seems to give a reasonable approximation
 	// which seems to give a reasonable approximation
 
 
-	getTangent: function ( t, optionalTarget ) {
+	getTangent( t, optionalTarget ) {
 
 
 		const delta = 0.0001;
 		const delta = 0.0001;
 		let t1 = t - delta;
 		let t1 = t - delta;
@@ -250,16 +250,16 @@ Object.assign( Curve.prototype, {
 
 
 		return tangent;
 		return tangent;
 
 
-	},
+	}
 
 
-	getTangentAt: function ( u, optionalTarget ) {
+	getTangentAt( u, optionalTarget ) {
 
 
 		const t = this.getUtoTmapping( u );
 		const t = this.getUtoTmapping( u );
 		return this.getTangent( t, optionalTarget );
 		return this.getTangent( t, optionalTarget );
 
 
-	},
+	}
 
 
-	computeFrenetFrames: function ( segments, closed ) {
+	computeFrenetFrames( segments, closed ) {
 
 
 		// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
 		// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
 
 
@@ -372,23 +372,23 @@ Object.assign( Curve.prototype, {
 			binormals: binormals
 			binormals: binormals
 		};
 		};
 
 
-	},
+	}
 
 
-	clone: function () {
+	clone() {
 
 
 		return new this.constructor().copy( this );
 		return new this.constructor().copy( this );
 
 
-	},
+	}
 
 
-	copy: function ( source ) {
+	copy( source ) {
 
 
 		this.arcLengthDivisions = source.arcLengthDivisions;
 		this.arcLengthDivisions = source.arcLengthDivisions;
 
 
 		return this;
 		return this;
 
 
-	},
+	}
 
 
-	toJSON: function () {
+	toJSON() {
 
 
 		const data = {
 		const data = {
 			metadata: {
 			metadata: {
@@ -403,9 +403,9 @@ Object.assign( Curve.prototype, {
 
 
 		return data;
 		return data;
 
 
-	},
+	}
 
 
-	fromJSON: function ( json ) {
+	fromJSON( json ) {
 
 
 		this.arcLengthDivisions = json.arcLengthDivisions;
 		this.arcLengthDivisions = json.arcLengthDivisions;
 
 
@@ -413,7 +413,7 @@ Object.assign( Curve.prototype, {
 
 
 	}
 	}
 
 
-} );
+}
 
 
 
 
 export { Curve };
 export { Curve };

+ 17 - 16
src/extras/objects/ImmediateRenderObject.js

@@ -1,28 +1,29 @@
 import { Object3D } from '../../core/Object3D.js';
 import { Object3D } from '../../core/Object3D.js';
 
 
-function ImmediateRenderObject( material ) {
+class ImmediateRenderObject extends Object3D {
 
 
-	Object3D.call( this );
+	constructor( material ) {
 
 
-	this.material = material;
-	this.render = function ( /* renderCallback */ ) {};
+		super();
 
 
-	this.hasPositions = false;
-	this.hasNormals = false;
-	this.hasColors = false;
-	this.hasUvs = false;
+		this.material = material;
+		this.render = function ( /* renderCallback */ ) {};
 
 
-	this.positionArray = null;
-	this.normalArray = null;
-	this.colorArray = null;
-	this.uvArray = null;
+		this.hasPositions = false;
+		this.hasNormals = false;
+		this.hasColors = false;
+		this.hasUvs = false;
 
 
-	this.count = 0;
+		this.positionArray = null;
+		this.normalArray = null;
+		this.colorArray = null;
+		this.uvArray = null;
 
 
-}
+		this.count = 0;
+
+	}
 
 
-ImmediateRenderObject.prototype = Object.create( Object3D.prototype );
-ImmediateRenderObject.prototype.constructor = ImmediateRenderObject;
+}
 
 
 ImmediateRenderObject.prototype.isImmediateRenderObject = true;
 ImmediateRenderObject.prototype.isImmediateRenderObject = true;