Skip to content

Commit

Permalink
Changed inheritance logic for lights.
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrank913 committed Jun 17, 2024
1 parent 661e99b commit 6343f5f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
21 changes: 14 additions & 7 deletions src/light/AmbientLight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AmbientLight, Color } from 'three';
import { AmbientLight, Color, Object3D } from 'three';
import { PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';

/**
Expand All @@ -9,21 +9,28 @@ import { PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';
* @module
*/

export default class DIVEAmbientLight extends AmbientLight {
export default class DIVEAmbientLight extends Object3D {
private _light: AmbientLight;

constructor() {
super(0xffffff, 1);
this.layers.mask = PRODUCT_LAYER_MASK;
super();

this.name = 'DIVEAmbientLight';

this._light = new AmbientLight(0xffffff, 1);
this._light.layers.mask = PRODUCT_LAYER_MASK;
this.add(this._light);
}

public SetColor(color: Color): void {
this.color = color;
this._light.color = color;
}

public SetIntensity(intensity: number): void {
this.intensity = intensity;
this._light.intensity = intensity;
}

public SetEnabled(enabled: boolean): void {
this.visible = enabled;
this._light.visible = enabled;
}
}
40 changes: 24 additions & 16 deletions src/light/PointLight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PointLight, Color, SphereGeometry, MeshBasicMaterial, Mesh, FrontSide } from 'three';
import { PointLight, Color, SphereGeometry, MeshBasicMaterial, Mesh, FrontSide, Object3D } from 'three';
import DIVECommunication from '../com/Communication';
import { HELPER_LAYER_MASK, PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';
import { DIVEMoveable } from '../interface/Moveable';
Expand All @@ -15,46 +15,54 @@ import type { TransformControls } from 'three/examples/jsm/Addons.js';
* @module
*/

export default class DIVEPointLight extends PointLight implements DIVESelectable, DIVEMoveable {
export default class DIVEPointLight extends Object3D implements DIVESelectable, DIVEMoveable {
public isMoveable: true = true;
public isSelectable: true = true;
public gizmo: TransformControls | null = null;

private light: PointLight;
private mesh: Mesh;

constructor() {
super(0xffffff, 1);
super();

this.name = 'DIVEPointLight';

this.light = new PointLight(0xffffff, 1);

this.layers.mask = PRODUCT_LAYER_MASK;
this.light.layers.mask = PRODUCT_LAYER_MASK;

this.castShadow = true;
this.shadow.mapSize.width = 512;
this.shadow.mapSize.height = 512;
this.light.castShadow = true;
this.light.shadow.mapSize.width = 512;
this.light.shadow.mapSize.height = 512;
this.add(this.light);

const geoSize = 0.1;

const geometry = new SphereGeometry(geoSize, geoSize * 320, geoSize * 320);

const material = new MeshBasicMaterial({ color: this.color, transparent: true, opacity: 0.8, side: FrontSide });
const material = new MeshBasicMaterial({ color: this.light.color, transparent: true, opacity: 0.8, side: FrontSide });

const mesh = new Mesh(geometry, material);
mesh.layers.mask = HELPER_LAYER_MASK;
this.mesh = new Mesh(geometry, material);
this.mesh.layers.mask = HELPER_LAYER_MASK;

this.add(mesh);
this.add(this.mesh);
}

public SetColor(color: Color): void {
this.color = color;
this.light.color = color;

((this.children[0] as Mesh).material as MeshBasicMaterial).color = color;
(this.mesh.material as MeshBasicMaterial).color = color;
}

public SetIntensity(intensity: number): void {
this.intensity = intensity;
this.light.intensity = intensity;

((this.children[0] as Mesh).material as MeshBasicMaterial).opacity = intensity > 0.8 ? 0.8 : intensity * 0.8;
(this.mesh.material as MeshBasicMaterial).opacity = intensity > 0.8 ? 0.8 : intensity * 0.8;
}

public SetEnabled(enabled: boolean): void {
this.visible = enabled;
this.light.visible = enabled;
}

public onMove(): void {
Expand Down
51 changes: 26 additions & 25 deletions src/light/SceneLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,51 @@ import { Color, DirectionalLight, HemisphereLight, Object3D } from "three";

export default class DIVESceneLight extends Object3D {

private hemiLight: HemisphereLight;
private dirLight: DirectionalLight;
private _hemiLight: HemisphereLight;
private _dirLight: DirectionalLight;

constructor() {
super();

this.name = "SceneLight";
this.name = 'DIVESceneLight';

this.hemiLight = new HemisphereLight(0xffffff, 0xffffff, 2);
this.hemiLight.layers.mask = PRODUCT_LAYER_MASK;
this.hemiLight.position.set(0, 50, 0);
this.add(this.hemiLight);
this._hemiLight = new HemisphereLight(0xffffff, 0xffffff, 2);
this._hemiLight.layers.mask = PRODUCT_LAYER_MASK;
this._hemiLight.position.set(0, 50, 0);
this.add(this._hemiLight);

this.dirLight = new DirectionalLight(0xffffff, 3);
this.dirLight.layers.mask = PRODUCT_LAYER_MASK;
this.dirLight.position.set(1, 1.75, 1);
this.dirLight.position.multiplyScalar(30);
this.dirLight.castShadow = true;
this._dirLight = new DirectionalLight(0xffffff, 3);
this._dirLight.layers.mask = PRODUCT_LAYER_MASK;
this._dirLight.position.set(1, 1.75, 1);
this._dirLight.position.multiplyScalar(30);
this._dirLight.castShadow = true;

this.dirLight.shadow.mapSize.width = 2048;
this.dirLight.shadow.mapSize.height = 2048;
this._dirLight.shadow.mapSize.width = 2048;
this._dirLight.shadow.mapSize.height = 2048;

const d = 5;

this.dirLight.shadow.camera.left = - d;
this.dirLight.shadow.camera.right = d;
this.dirLight.shadow.camera.top = d;
this.dirLight.shadow.camera.bottom = - d;
this._dirLight.shadow.camera.left = - d;
this._dirLight.shadow.camera.right = d;
this._dirLight.shadow.camera.top = d;
this._dirLight.shadow.camera.bottom = - d;

this.dirLight.shadow.camera.far = 3500;
this.add(this.dirLight);
this._dirLight.shadow.camera.far = 3500;
this.add(this._dirLight);
}

public SetColor(color: Color): void {
this.hemiLight.color = color;
this.dirLight.color = color;
this._hemiLight.color = color;
this._dirLight.color = color;
}

public SetIntensity(intensity: number): void {
this.hemiLight.intensity = intensity * 2;
this.dirLight.intensity = intensity * 3;
this._hemiLight.intensity = intensity * 2;
this._dirLight.intensity = intensity * 3;
}

public SetEnabled(enabled: boolean): void {
this.visible = enabled;
this._hemiLight.visible = enabled;
this._dirLight.visible = enabled;
}
}

0 comments on commit 6343f5f

Please sign in to comment.