From d08729a75155be73ff69030c92e4b231c2d66db7 Mon Sep 17 00:00:00 2001 From: linbingquan <695601626@qq.com> Date: Fri, 12 Feb 2021 19:57:38 +0800 Subject: [PATCH 1/4] core: Covert to es6 class. --- src/core/BufferAttribute.js | 238 +++++++++++++------------ src/core/EventDispatcher.js | 21 +-- src/core/InstancedBufferAttribute.js | 34 ++-- src/core/InstancedBufferGeometry.js | 28 ++- src/core/InstancedInterleavedBuffer.js | 26 ++- src/core/InterleavedBuffer.js | 60 +++---- src/core/InterleavedBufferAttribute.js | 106 +++++------ src/core/Raycaster.js | 85 ++++----- 8 files changed, 286 insertions(+), 312 deletions(-) diff --git a/src/core/BufferAttribute.js b/src/core/BufferAttribute.js index 3e0b5153fe95e4..9fcdb3d5e56c78 100644 --- a/src/core/BufferAttribute.js +++ b/src/core/BufferAttribute.js @@ -7,53 +7,49 @@ import { StaticDrawUsage } from '../constants.js'; const _vector = new Vector3(); const _vector2 = new Vector2(); -function BufferAttribute( array, itemSize, normalized ) { +class BufferAttribute { - if ( Array.isArray( array ) ) { + constructor( array, itemSize, normalized ) { - throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' ); + if ( Array.isArray( array ) ) { - } - - this.name = ''; + throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' ); - this.array = array; - this.itemSize = itemSize; - this.count = array !== undefined ? array.length / itemSize : 0; - this.normalized = normalized === true; + } - this.usage = StaticDrawUsage; - this.updateRange = { offset: 0, count: - 1 }; + Object.defineProperty( this, 'isBufferAttribute', { value: true } ); - this.version = 0; + this.name = ''; -} + this.array = array; + this.itemSize = itemSize; + this.count = array !== undefined ? array.length / itemSize : 0; + this.normalized = normalized === true; -Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', { + this.usage = StaticDrawUsage; + this.updateRange = { offset: 0, count: - 1 }; - set: function ( value ) { - - if ( value === true ) this.version ++; + this.version = 0; } -} ); + set needsUpdate( value ) { -Object.assign( BufferAttribute.prototype, { + if ( value === true ) this.version ++; - isBufferAttribute: true, + } - onUploadCallback: function () {}, + onUploadCallback() {} - setUsage: function ( value ) { + setUsage( value ) { this.usage = value; return this; - }, + } - copy: function ( source ) { + copy( source ) { this.name = source.name; this.array = new source.array.constructor( source.array ); @@ -65,9 +61,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - copyAt: function ( index1, attribute, index2 ) { + copyAt( index1, attribute, index2 ) { index1 *= this.itemSize; index2 *= attribute.itemSize; @@ -80,17 +76,17 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - copyArray: function ( array ) { + copyArray( array ) { this.array.set( array ); return this; - }, + } - copyColorsArray: function ( colors ) { + copyColorsArray( colors ) { const array = this.array; let offset = 0; @@ -114,9 +110,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - copyVector2sArray: function ( vectors ) { + copyVector2sArray( vectors ) { const array = this.array; let offset = 0; @@ -139,9 +135,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - copyVector3sArray: function ( vectors ) { + copyVector3sArray( vectors ) { const array = this.array; let offset = 0; @@ -165,9 +161,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - copyVector4sArray: function ( vectors ) { + copyVector4sArray( vectors ) { const array = this.array; let offset = 0; @@ -192,9 +188,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - applyMatrix3: function ( m ) { + applyMatrix3( m ) { if ( this.itemSize === 2 ) { @@ -222,9 +218,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - applyMatrix4: function ( m ) { + applyMatrix4( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { @@ -240,9 +236,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - applyNormalMatrix: function ( m ) { + applyNormalMatrix( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { @@ -258,9 +254,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - transformDirection: function ( m ) { + transformDirection( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { @@ -276,73 +272,75 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - set: function ( value, offset = 0 ) { + set( value, offset = 0 ) { + + if ( offset === undefined ) offset = 0; this.array.set( value, offset ); return this; - }, + } - getX: function ( index ) { + getX( index ) { return this.array[ index * this.itemSize ]; - }, + } - setX: function ( index, x ) { + setX( index, x ) { this.array[ index * this.itemSize ] = x; return this; - }, + } - getY: function ( index ) { + getY( index ) { return this.array[ index * this.itemSize + 1 ]; - }, + } - setY: function ( index, y ) { + setY( index, y ) { this.array[ index * this.itemSize + 1 ] = y; return this; - }, + } - getZ: function ( index ) { + getZ( index ) { return this.array[ index * this.itemSize + 2 ]; - }, + } - setZ: function ( index, z ) { + setZ( index, z ) { this.array[ index * this.itemSize + 2 ] = z; return this; - }, + } - getW: function ( index ) { + getW( index ) { return this.array[ index * this.itemSize + 3 ]; - }, + } - setW: function ( index, w ) { + setW( index, w ) { this.array[ index * this.itemSize + 3 ] = w; return this; - }, + } - setXY: function ( index, x, y ) { + setXY( index, x, y ) { index *= this.itemSize; @@ -351,9 +349,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - setXYZ: function ( index, x, y, z ) { + setXYZ( index, x, y, z ) { index *= this.itemSize; @@ -363,9 +361,9 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - setXYZW: function ( index, x, y, z, w ) { + setXYZW( index, x, y, z, w ) { index *= this.itemSize; @@ -376,23 +374,23 @@ Object.assign( BufferAttribute.prototype, { return this; - }, + } - onUpload: function ( callback ) { + onUpload( callback ) { this.onUploadCallback = callback; return this; - }, + } - clone: function () { + clone() { return new this.constructor( this.array, this.itemSize ).copy( this ); - }, + } - toJSON: function () { + toJSON() { return { itemSize: this.itemSize, @@ -403,108 +401,112 @@ Object.assign( BufferAttribute.prototype, { } -} ); +} // -function Int8BufferAttribute( array, itemSize, normalized ) { +class Int8BufferAttribute extends BufferAttribute { - BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized ); + constructor( array, itemSize, normalized ) { + + super( new Int8Array( array ), itemSize, normalized ); + + } } -Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Int8BufferAttribute.prototype.constructor = Int8BufferAttribute; +class Uint8BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Uint8BufferAttribute( array, itemSize, normalized ) { + super( new Uint8Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized ); + } } -Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute; +class Uint8ClampedBufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Uint8ClampedBufferAttribute( array, itemSize, normalized ) { + super( new Uint8ClampedArray( array ), itemSize, normalized ); - BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized ); + } } -Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute; +class Int16BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Int16BufferAttribute( array, itemSize, normalized ) { + super( new Int16Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized ); + } } -Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Int16BufferAttribute.prototype.constructor = Int16BufferAttribute; +class Uint16BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Uint16BufferAttribute( array, itemSize, normalized ) { + super( new Uint16Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized ); + } } -Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute; +class Int32BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Int32BufferAttribute( array, itemSize, normalized ) { + super( new Int32Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized ); + } } -Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Int32BufferAttribute.prototype.constructor = Int32BufferAttribute; +class Uint32BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Uint32BufferAttribute( array, itemSize, normalized ) { + super( new Uint32Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized ); + } } -Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute; +class Float16BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { -function Float16BufferAttribute( array, itemSize, normalized ) { + super( new Uint16Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized ); + Object.defineProperty( this, 'isFloat16BufferAttribute', { value: true } ); + + } } -Float16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Float16BufferAttribute.prototype.constructor = Float16BufferAttribute; -Float16BufferAttribute.prototype.isFloat16BufferAttribute = true; +class Float32BufferAttribute extends BufferAttribute { + + constructor( array, itemSize, normalized ) { -function Float32BufferAttribute( array, itemSize, normalized ) { + super( new Float32Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized ); + } } -Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Float32BufferAttribute.prototype.constructor = Float32BufferAttribute; +class Float64BufferAttribute extends BufferAttribute { + constructor( array, itemSize, normalized ) { -function Float64BufferAttribute( array, itemSize, normalized ) { + super( new Float64Array( array ), itemSize, normalized ); - BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized ); + } } -Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); -Float64BufferAttribute.prototype.constructor = Float64BufferAttribute; - // export { diff --git a/src/core/EventDispatcher.js b/src/core/EventDispatcher.js index 77988ca004e745..91c7907bdf7a31 100644 --- a/src/core/EventDispatcher.js +++ b/src/core/EventDispatcher.js @@ -2,11 +2,9 @@ * https://github.com/mrdoob/eventdispatcher.js/ */ -function EventDispatcher() {} +class EventDispatcher { -Object.assign( EventDispatcher.prototype, { - - addEventListener: function ( type, listener ) { + addEventListener( type, listener ) { if ( this._listeners === undefined ) this._listeners = {}; @@ -24,9 +22,9 @@ Object.assign( EventDispatcher.prototype, { } - }, + } - hasEventListener: function ( type, listener ) { + hasEventListener( type, listener ) { if ( this._listeners === undefined ) return false; @@ -34,9 +32,9 @@ Object.assign( EventDispatcher.prototype, { return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1; - }, + } - removeEventListener: function ( type, listener ) { + removeEventListener( type, listener ) { if ( this._listeners === undefined ) return; @@ -55,9 +53,9 @@ Object.assign( EventDispatcher.prototype, { } - }, + } - dispatchEvent: function ( event ) { + dispatchEvent( event ) { if ( this._listeners === undefined ) return; @@ -81,7 +79,6 @@ Object.assign( EventDispatcher.prototype, { } -} ); - +} export { EventDispatcher }; diff --git a/src/core/InstancedBufferAttribute.js b/src/core/InstancedBufferAttribute.js index d9a56c7eae69c1..0f33f1463d75db 100644 --- a/src/core/InstancedBufferAttribute.js +++ b/src/core/InstancedBufferAttribute.js @@ -1,30 +1,28 @@ import { BufferAttribute } from './BufferAttribute.js'; -function InstancedBufferAttribute( array, itemSize, normalized, meshPerAttribute ) { +class InstancedBufferAttribute extends BufferAttribute { - if ( typeof ( normalized ) === 'number' ) { + constructor( array, itemSize, normalized, meshPerAttribute ) { - meshPerAttribute = normalized; + super( array, itemSize, normalized ); - normalized = false; + Object.defineProperty( this, 'isInstancedBufferAttribute', { value: true } ); - console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' ); + if ( typeof ( normalized ) === 'number' ) { - } - - BufferAttribute.call( this, array, itemSize, normalized ); + meshPerAttribute = normalized; - this.meshPerAttribute = meshPerAttribute || 1; + normalized = false; -} + console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' ); -InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribute.prototype ), { + } - constructor: InstancedBufferAttribute, + this.meshPerAttribute = meshPerAttribute || 1; - isInstancedBufferAttribute: true, + } - copy: function ( source ) { + copy( source ) { BufferAttribute.prototype.copy.call( this, source ); @@ -32,9 +30,9 @@ InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribu return this; - }, + } - toJSON: function () { + toJSON() { const data = BufferAttribute.prototype.toJSON.call( this ); @@ -46,8 +44,6 @@ InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribu } -} ); - - +} export { InstancedBufferAttribute }; diff --git a/src/core/InstancedBufferGeometry.js b/src/core/InstancedBufferGeometry.js index b762e286df1d2d..9b4e8982a55662 100644 --- a/src/core/InstancedBufferGeometry.js +++ b/src/core/InstancedBufferGeometry.js @@ -1,21 +1,19 @@ import { BufferGeometry } from './BufferGeometry.js'; -function InstancedBufferGeometry() { +class InstancedBufferGeometry extends BufferGeometry { - BufferGeometry.call( this ); + constructor() { - this.type = 'InstancedBufferGeometry'; - this.instanceCount = Infinity; + super(); -} - -InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry.prototype ), { + Object.defineProperty( this, 'isInstancedBufferGeometry', { value: true } ); - constructor: InstancedBufferGeometry, + this.type = 'InstancedBufferGeometry'; + this.instanceCount = Infinity; - isInstancedBufferGeometry: true, + } - copy: function ( source ) { + copy( source ) { BufferGeometry.prototype.copy.call( this, source ); @@ -23,15 +21,15 @@ InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry return this; - }, + } - clone: function () { + clone() { return new this.constructor().copy( this ); - }, + } - toJSON: function () { + toJSON() { const data = BufferGeometry.prototype.toJSON.call( this ); @@ -43,6 +41,6 @@ InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry } -} ); +} export { InstancedBufferGeometry }; diff --git a/src/core/InstancedInterleavedBuffer.js b/src/core/InstancedInterleavedBuffer.js index caae9bd0c0046e..555760e89d2227 100644 --- a/src/core/InstancedInterleavedBuffer.js +++ b/src/core/InstancedInterleavedBuffer.js @@ -1,20 +1,18 @@ import { InterleavedBuffer } from './InterleavedBuffer.js'; -function InstancedInterleavedBuffer( array, stride, meshPerAttribute ) { +class InstancedInterleavedBuffer extends InterleavedBuffer { - InterleavedBuffer.call( this, array, stride ); + constructor( array, stride, meshPerAttribute ) { - this.meshPerAttribute = meshPerAttribute || 1; + super( array, stride ); -} - -InstancedInterleavedBuffer.prototype = Object.assign( Object.create( InterleavedBuffer.prototype ), { + Object.defineProperty( this, 'isInstancedInterleavedBuffer', { value: true } ); - constructor: InstancedInterleavedBuffer, + this.meshPerAttribute = meshPerAttribute || 1; - isInstancedInterleavedBuffer: true, + } - copy: function ( source ) { + copy( source ) { InterleavedBuffer.prototype.copy.call( this, source ); @@ -22,9 +20,9 @@ InstancedInterleavedBuffer.prototype = Object.assign( Object.create( Interleaved return this; - }, + } - clone: function ( data ) { + clone( data ) { const ib = InterleavedBuffer.prototype.clone.call( this, data ); @@ -32,9 +30,9 @@ InstancedInterleavedBuffer.prototype = Object.assign( Object.create( Interleaved return ib; - }, + } - toJSON: function ( data ) { + toJSON( data ) { const json = InterleavedBuffer.prototype.toJSON.call( this, data ); @@ -45,6 +43,6 @@ InstancedInterleavedBuffer.prototype = Object.assign( Object.create( Interleaved } -} ); +} export { InstancedInterleavedBuffer }; diff --git a/src/core/InterleavedBuffer.js b/src/core/InterleavedBuffer.js index e00fbec34285fe..c5a45cc9ede6dc 100644 --- a/src/core/InterleavedBuffer.js +++ b/src/core/InterleavedBuffer.js @@ -1,46 +1,42 @@ import { MathUtils } from '../math/MathUtils.js'; import { StaticDrawUsage } from '../constants.js'; -function InterleavedBuffer( array, stride ) { +class InterleavedBuffer { - this.array = array; - this.stride = stride; - this.count = array !== undefined ? array.length / stride : 0; + constructor( array, stride ) { - this.usage = StaticDrawUsage; - this.updateRange = { offset: 0, count: - 1 }; + this.array = array; + this.stride = stride; + this.count = array !== undefined ? array.length / stride : 0; - this.version = 0; + this.isInterleavedBuffer = true; - this.uuid = MathUtils.generateUUID(); + this.usage = StaticDrawUsage; + this.updateRange = { offset: 0, count: - 1 }; -} - -Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', { + this.version = 0; - set: function ( value ) { - - if ( value === true ) this.version ++; + this.uuid = MathUtils.generateUUID(); } -} ); + set needsUpdate( value ) { -Object.assign( InterleavedBuffer.prototype, { + if ( value === true ) this.version ++; - isInterleavedBuffer: true, + } - onUploadCallback: function () {}, + onUploadCallback() {} - setUsage: function ( value ) { + setUsage( value ) { this.usage = value; return this; - }, + } - copy: function ( source ) { + copy( source ) { this.array = new source.array.constructor( source.array ); this.count = source.count; @@ -49,9 +45,9 @@ Object.assign( InterleavedBuffer.prototype, { return this; - }, + } - copyAt: function ( index1, attribute, index2 ) { + copyAt( index1, attribute, index2 ) { index1 *= this.stride; index2 *= attribute.stride; @@ -64,17 +60,17 @@ Object.assign( InterleavedBuffer.prototype, { return this; - }, + } - set: function ( value, offset = 0 ) { + set( value, offset = 0 ) { this.array.set( value, offset ); return this; - }, + } - clone: function ( data ) { + clone( data ) { if ( data.arrayBuffers === undefined ) { @@ -101,17 +97,17 @@ Object.assign( InterleavedBuffer.prototype, { return ib; - }, + } - onUpload: function ( callback ) { + onUpload( callback ) { this.onUploadCallback = callback; return this; - }, + } - toJSON: function ( data ) { + toJSON( data ) { if ( data.arrayBuffers === undefined ) { @@ -144,6 +140,6 @@ Object.assign( InterleavedBuffer.prototype, { } -} ); +} export { InterleavedBuffer }; diff --git a/src/core/InterleavedBufferAttribute.js b/src/core/InterleavedBufferAttribute.js index e4a956d3355571..c62cbfdaeade9e 100644 --- a/src/core/InterleavedBufferAttribute.js +++ b/src/core/InterleavedBufferAttribute.js @@ -3,57 +3,42 @@ import { BufferAttribute } from './BufferAttribute.js'; const _vector = new Vector3(); -function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) { +class InterleavedBufferAttribute { - this.name = ''; + constructor( interleavedBuffer, itemSize, offset, normalized ) { - this.data = interleavedBuffer; - this.itemSize = itemSize; - this.offset = offset; + Object.defineProperty( this, 'isInterleavedBufferAttribute', { value: true } ); - this.normalized = normalized === true; + this.name = ''; -} - -Object.defineProperties( InterleavedBufferAttribute.prototype, { - - count: { - - get: function () { + this.data = interleavedBuffer; + this.itemSize = itemSize; + this.offset = offset; - return this.data.count; + this.normalized = normalized === true; - } + } - }, + get count() { - array: { + return this.data.count; - get: function () { + } - return this.data.array; + get array() { - } + return this.data.array; - }, - - needsUpdate: { + } - set: function ( value ) { + set needsUpdate( value ) { - this.data.needsUpdate = value; - } + this.data.needsUpdate = value; } -} ); - -Object.assign( InterleavedBufferAttribute.prototype, { - - isInterleavedBufferAttribute: true, - - applyMatrix4: function ( m ) { + applyMatrix4( m ) { for ( let i = 0, l = this.data.count; i < l; i ++ ) { @@ -69,65 +54,65 @@ Object.assign( InterleavedBufferAttribute.prototype, { return this; - }, + } - setX: function ( index, x ) { + setX( index, x ) { this.data.array[ index * this.data.stride + this.offset ] = x; return this; - }, + } - setY: function ( index, y ) { + setY( index, y ) { this.data.array[ index * this.data.stride + this.offset + 1 ] = y; return this; - }, + } - setZ: function ( index, z ) { + setZ( index, z ) { this.data.array[ index * this.data.stride + this.offset + 2 ] = z; return this; - }, + } - setW: function ( index, w ) { + setW( index, w ) { this.data.array[ index * this.data.stride + this.offset + 3 ] = w; return this; - }, + } - getX: function ( index ) { + getX( index ) { return this.data.array[ index * this.data.stride + this.offset ]; - }, + } - getY: function ( index ) { + getY( index ) { return this.data.array[ index * this.data.stride + this.offset + 1 ]; - }, + } - getZ: function ( index ) { + getZ( index ) { return this.data.array[ index * this.data.stride + this.offset + 2 ]; - }, + } - getW: function ( index ) { + getW( index ) { return this.data.array[ index * this.data.stride + this.offset + 3 ]; - }, + } - setXY: function ( index, x, y ) { + setXY( index, x, y ) { index = index * this.data.stride + this.offset; @@ -136,9 +121,9 @@ Object.assign( InterleavedBufferAttribute.prototype, { return this; - }, + } - setXYZ: function ( index, x, y, z ) { + setXYZ( index, x, y, z ) { index = index * this.data.stride + this.offset; @@ -148,9 +133,9 @@ Object.assign( InterleavedBufferAttribute.prototype, { return this; - }, + } - setXYZW: function ( index, x, y, z, w ) { + setXYZW( index, x, y, z, w ) { index = index * this.data.stride + this.offset; @@ -161,9 +146,9 @@ Object.assign( InterleavedBufferAttribute.prototype, { return this; - }, + } - clone: function ( data ) { + clone( data ) { if ( data === undefined ) { @@ -203,9 +188,9 @@ Object.assign( InterleavedBufferAttribute.prototype, { } - }, + } - toJSON: function ( data ) { + toJSON( data ) { if ( data === undefined ) { @@ -262,7 +247,6 @@ Object.assign( InterleavedBufferAttribute.prototype, { } -} ); - +} export { InterleavedBufferAttribute }; diff --git a/src/core/Raycaster.js b/src/core/Raycaster.js index e39e58aa33b976..f64545851fb7d6 100644 --- a/src/core/Raycaster.js +++ b/src/core/Raycaster.js @@ -1,37 +1,6 @@ import { Ray } from '../math/Ray.js'; import { Layers } from './Layers.js'; -function Raycaster( origin, direction, near, far ) { - - this.ray = new Ray( origin, direction ); - // direction is assumed to be normalized (for accurate distance calculations) - - this.near = near || 0; - this.far = far || Infinity; - this.camera = null; - this.layers = new Layers(); - - this.params = { - Mesh: {}, - Line: { threshold: 1 }, - LOD: {}, - Points: { threshold: 1 }, - Sprite: {} - }; - - Object.defineProperties( this.params, { - PointCloud: { - get: function () { - - console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' ); - return this.Points; - - } - } - } ); - -} - function ascSort( a, b ) { return a.distance - b.distance; @@ -60,17 +29,40 @@ function intersectObject( object, raycaster, intersects, recursive ) { } -Object.assign( Raycaster.prototype, { +class Raycaster { - set: function ( origin, direction ) { + constructor( origin, direction, near, far ) { + this.ray = new Ray( origin, direction ); // direction is assumed to be normalized (for accurate distance calculations) - this.ray.set( origin, direction ); + this.near = near || 0; + this.far = far || Infinity; + this.camera = null; + this.layers = new Layers(); - }, + this.params = { + Mesh: {}, + Line: { threshold: 1 }, + LOD: {}, + Points: { threshold: 1 }, + Sprite: {} + }; - setFromCamera: function ( coords, camera ) { + Object.defineProperties( this.params, { + PointCloud: { + get: function () { + + console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' ); + return this.Points; + + } + } + } ); + + } + + setFromCamera( coords, camera ) { if ( camera && camera.isPerspectiveCamera ) { @@ -90,9 +82,9 @@ Object.assign( Raycaster.prototype, { } - }, + } - intersectObject: function ( object, recursive, optionalTarget ) { + intersectObject( object, recursive, optionalTarget ) { const intersects = optionalTarget || []; @@ -102,9 +94,9 @@ Object.assign( Raycaster.prototype, { return intersects; - }, + } - intersectObjects: function ( objects, recursive, optionalTarget ) { + intersectObjects( objects, recursive, optionalTarget ) { const intersects = optionalTarget || []; @@ -127,7 +119,18 @@ Object.assign( Raycaster.prototype, { } -} ); +} + +Object.assign( Raycaster.prototype, { + set: function ( origin, direction ) { + + // direction is assumed to be normalized (for accurate distance calculations) + + this.ray.set( origin, direction ); + + } + +} ); export { Raycaster }; From c61db863f1b27993805af3d60768d94611e4973d Mon Sep 17 00:00:00 2001 From: linbingquan <695601626@qq.com> Date: Fri, 12 Feb 2021 20:24:21 +0800 Subject: [PATCH 2/4] fix(Raycaster): Covert to ES6 class. --- src/core/Raycaster.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/core/Raycaster.js b/src/core/Raycaster.js index f64545851fb7d6..e50d95934e3a64 100644 --- a/src/core/Raycaster.js +++ b/src/core/Raycaster.js @@ -62,6 +62,14 @@ class Raycaster { } + set( origin, direction ) { + + // direction is assumed to be normalized (for accurate distance calculations) + + this.ray.set( origin, direction ); + + } + setFromCamera( coords, camera ) { if ( camera && camera.isPerspectiveCamera ) { @@ -121,16 +129,4 @@ class Raycaster { } -Object.assign( Raycaster.prototype, { - - set: function ( origin, direction ) { - - // direction is assumed to be normalized (for accurate distance calculations) - - this.ray.set( origin, direction ); - - } - -} ); - export { Raycaster }; From 39c8932440b32432875f8d0e9a96b21a28ecde26 Mon Sep 17 00:00:00 2001 From: linbingquan <695601626@qq.com> Date: Fri, 12 Feb 2021 20:39:00 +0800 Subject: [PATCH 3/4] Clean up. --- src/core/BufferAttribute.js | 2 -- src/core/InstancedInterleavedBuffer.js | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/BufferAttribute.js b/src/core/BufferAttribute.js index 9fcdb3d5e56c78..ded0681359e6af 100644 --- a/src/core/BufferAttribute.js +++ b/src/core/BufferAttribute.js @@ -276,8 +276,6 @@ class BufferAttribute { set( value, offset = 0 ) { - if ( offset === undefined ) offset = 0; - this.array.set( value, offset ); return this; diff --git a/src/core/InstancedInterleavedBuffer.js b/src/core/InstancedInterleavedBuffer.js index 555760e89d2227..7ef7293c662f1b 100644 --- a/src/core/InstancedInterleavedBuffer.js +++ b/src/core/InstancedInterleavedBuffer.js @@ -2,13 +2,13 @@ import { InterleavedBuffer } from './InterleavedBuffer.js'; class InstancedInterleavedBuffer extends InterleavedBuffer { - constructor( array, stride, meshPerAttribute ) { + constructor( array, stride, meshPerAttribute = 1 ) { super( array, stride ); Object.defineProperty( this, 'isInstancedInterleavedBuffer', { value: true } ); - this.meshPerAttribute = meshPerAttribute || 1; + this.meshPerAttribute = meshPerAttribute; } From b4049d1c6960967de958dac96d4985d59ecb2552 Mon Sep 17 00:00:00 2001 From: linbingquan <695601626@qq.com> Date: Wed, 17 Feb 2021 18:25:08 +0800 Subject: [PATCH 4/4] Clean up for ES6 default parameter. --- src/core/InstancedBufferAttribute.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/InstancedBufferAttribute.js b/src/core/InstancedBufferAttribute.js index 0f33f1463d75db..e571b62f1d2283 100644 --- a/src/core/InstancedBufferAttribute.js +++ b/src/core/InstancedBufferAttribute.js @@ -2,7 +2,7 @@ import { BufferAttribute } from './BufferAttribute.js'; class InstancedBufferAttribute extends BufferAttribute { - constructor( array, itemSize, normalized, meshPerAttribute ) { + constructor( array, itemSize, normalized, meshPerAttribute = 1 ) { super( array, itemSize, normalized ); @@ -18,7 +18,7 @@ class InstancedBufferAttribute extends BufferAttribute { } - this.meshPerAttribute = meshPerAttribute || 1; + this.meshPerAttribute = meshPerAttribute; }