Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/lights: move to es6 classes #20018

Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/lights/AmbientLight.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { Light } from './Light.js';

function AmbientLight( color, intensity ) {
class AmbientLight extends Light {

Light.call( this, color, intensity );
constructor( color, intensity ) {

this.type = 'AmbientLight';
super( color, intensity );

this.castShadow = undefined;
this.type = 'AmbientLight';
Object.defineProperty( this, 'isAmbientLight', { value: true } );

}

AmbientLight.prototype = Object.assign( Object.create( Light.prototype ), {

constructor: AmbientLight,
this.castShadow = undefined;

isAmbientLight: true

} );
}

}

export { AmbientLight };
30 changes: 14 additions & 16 deletions src/lights/AmbientLightProbe.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import { Color } from '../math/Color.js';
import { LightProbe } from './LightProbe.js';

function AmbientLightProbe( color, intensity ) {
class AmbientLightProbe extends LightProbe {

LightProbe.call( this, undefined, intensity );
constructor( color, intensity ) {

const color1 = new Color().set( color );
super( undefined, intensity );

// without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI );
this.sh.coefficients[ 0 ].set( color1.r, color1.g, color1.b ).multiplyScalar( 2 * Math.sqrt( Math.PI ) );
Object.defineProperty( this, 'isAmbientLightProbe', { value: true } );

}

AmbientLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
const color1 = new Color().set( color );

constructor: AmbientLightProbe,
// without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI );
this.sh.coefficients[ 0 ].set( color1.r, color1.g, color1.b ).multiplyScalar( 2 * Math.sqrt( Math.PI ) );

isAmbientLightProbe: true,
}

copy: function ( source ) { // modifying color not currently supported
copy( source ) { // modifying color not currently supported

LightProbe.prototype.copy.call( this, source );
super.copy( source );

return this;

},
}

toJSON: function ( meta ) {
toJSON( meta ) {

const data = LightProbe.prototype.toJSON.call( this, meta );
const data = super.toJSON( meta );

// data.sh = this.sh.toArray(); // todo

return data;

}

} );
}

export { AmbientLightProbe };
30 changes: 13 additions & 17 deletions src/lights/DirectionalLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ import { Light } from './Light.js';
import { DirectionalLightShadow } from './DirectionalLightShadow.js';
import { Object3D } from '../core/Object3D.js';

function DirectionalLight( color, intensity ) {
class DirectionalLight extends Light {

Light.call( this, color, intensity );
constructor( color, intensity ) {

this.type = 'DirectionalLight';
super( color, intensity );

this.position.copy( Object3D.DefaultUp );
this.updateMatrix();
this.type = 'DirectionalLight';
Object.defineProperty( this, 'isDirectionalLight', { value: true } );

this.target = new Object3D();
this.position.copy( Object3D.DefaultUp );
this.updateMatrix();

this.shadow = new DirectionalLightShadow();
this.target = new Object3D();

}

DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {

constructor: DirectionalLight,
this.shadow = new DirectionalLightShadow();

isDirectionalLight: true,
}

copy: function ( source ) {
copy( source ) {

Light.prototype.copy.call( this, source );
super.copy( source );

this.target = source.target.clone();

Expand All @@ -35,7 +32,6 @@ DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {

}

} );

}

export { DirectionalLight };
19 changes: 8 additions & 11 deletions src/lights/DirectionalLightShadow.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { LightShadow } from './LightShadow.js';
import { OrthographicCamera } from '../cameras/OrthographicCamera.js';

function DirectionalLightShadow() {
class DirectionalLightShadow extends LightShadow {

LightShadow.call( this, new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
constructor() {

}

DirectionalLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
super( new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );

constructor: DirectionalLightShadow,
Object.defineProperty( this, 'isDirectionalLightShadow', { value: true } );

isDirectionalLightShadow: true,
}

updateMatrices: function ( light ) {
updateMatrices( light ) {

LightShadow.prototype.updateMatrices.call( this, light );
super.updateMatrices( light );

}

} );

}

export { DirectionalLightShadow };
30 changes: 13 additions & 17 deletions src/lights/HemisphereLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,34 @@ import { Light } from './Light.js';
import { Color } from '../math/Color.js';
import { Object3D } from '../core/Object3D.js';

function HemisphereLight( skyColor, groundColor, intensity ) {
class HemisphereLight extends Light {

Light.call( this, skyColor, intensity );
constructor( skyColor, groundColor, intensity ) {

this.type = 'HemisphereLight';
super( skyColor, intensity );

this.castShadow = undefined;
this.type = 'HemisphereLight';
Object.defineProperty( this, 'isHemisphereLight', { value: true } );

this.position.copy( Object3D.DefaultUp );
this.updateMatrix();
this.castShadow = undefined;

this.groundColor = new Color( groundColor );
this.position.copy( Object3D.DefaultUp );
this.updateMatrix();

}

HemisphereLight.prototype = Object.assign( Object.create( Light.prototype ), {

constructor: HemisphereLight,
this.groundColor = new Color( groundColor );

isHemisphereLight: true,
}

copy: function ( source ) {
copy( source ) {

Light.prototype.copy.call( this, source );
super.copy( source );

this.groundColor.copy( source.groundColor );

return this;

}

} );

}

export { HemisphereLight };
42 changes: 20 additions & 22 deletions src/lights/HemisphereLightProbe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,47 @@ import { Color } from '../math/Color.js';
import { Vector3 } from '../math/Vector3.js';
import { LightProbe } from './LightProbe.js';

function HemisphereLightProbe( skyColor, groundColor, intensity ) {
class HemisphereLightProbe extends LightProbe {

LightProbe.call( this, undefined, intensity );
constructor( skyColor, groundColor, intensity ) {

const color1 = new Color().set( skyColor );
const color2 = new Color().set( groundColor );
super( undefined, intensity );

const sky = new Vector3( color1.r, color1.g, color1.b );
const ground = new Vector3( color2.r, color2.g, color2.b );
Object.defineProperty( this, 'isHemisphereLightProbe', { value: true } );

// without extra factor of PI in the shader, should = 1 / Math.sqrt( Math.PI );
const c0 = Math.sqrt( Math.PI );
const c1 = c0 * Math.sqrt( 0.75 );
const color1 = new Color().set( skyColor );
const color2 = new Color().set( groundColor );

this.sh.coefficients[ 0 ].copy( sky ).add( ground ).multiplyScalar( c0 );
this.sh.coefficients[ 1 ].copy( sky ).sub( ground ).multiplyScalar( c1 );
const sky = new Vector3( color1.r, color1.g, color1.b );
const ground = new Vector3( color2.r, color2.g, color2.b );

}

HemisphereLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
// without extra factor of PI in the shader, should = 1 / Math.sqrt( Math.PI );
const c0 = Math.sqrt( Math.PI );
const c1 = c0 * Math.sqrt( 0.75 );

constructor: HemisphereLightProbe,
this.sh.coefficients[ 0 ].copy( sky ).add( ground ).multiplyScalar( c0 );
this.sh.coefficients[ 1 ].copy( sky ).sub( ground ).multiplyScalar( c1 );

isHemisphereLightProbe: true,
}

copy: function ( source ) { // modifying colors not currently supported
copy( source ) { // modifying colors not currently supported

LightProbe.prototype.copy.call( this, source );
super.copy( source );

return this;

},
}

toJSON: function ( meta ) {
toJSON( meta ) {

const data = LightProbe.prototype.toJSON.call( this, meta );
const data = super.toJSON( meta );

// data.sh = this.sh.toArray(); // todo

return data;

}

} );
}

export { HemisphereLightProbe };
34 changes: 15 additions & 19 deletions src/lights/Light.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import { Object3D } from '../core/Object3D.js';
import { Color } from '../math/Color.js';

function Light( color, intensity ) {
class Light extends Object3D {

Object3D.call( this );
constructor( color, intensity ) {

this.type = 'Light';
super();

this.color = new Color( color );
this.intensity = intensity !== undefined ? intensity : 1;
this.type = 'Light';
Object.defineProperty( this, 'isLight', { value: true } );

this.receiveShadow = undefined;
this.color = new Color( color );
this.intensity = intensity !== undefined ? intensity : 1;

}

Light.prototype = Object.assign( Object.create( Object3D.prototype ), {
this.receiveShadow = undefined;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did this come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are from old code that was there when I originally did the change.
This commit was the change.
I can take then out.


constructor: Light,

isLight: true,
}

copy: function ( source ) {
copy( source ) {

Object3D.prototype.copy.call( this, source );
super.copy( source );

this.color.copy( source.color );
this.intensity = source.intensity;

return this;

},
}

toJSON: function ( meta ) {
toJSON( meta ) {

const data = Object3D.prototype.toJSON.call( this, meta );
const data = super.toJSON( meta );

data.object.color = this.color.getHex();
data.object.intensity = this.intensity;
Expand All @@ -51,7 +48,6 @@ Light.prototype = Object.assign( Object.create( Object3D.prototype ), {

}

} );

}

export { Light };
Loading