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

feat: Make the camera manager marker better #4768

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import { Scene, Object3D, Vector3, Sprite, SpriteMaterial, CanvasTexture } from 'three';
import { FlexibleCameraManager } from './FlexibleCameraManager';
import { FlexibleControlsType } from './FlexibleControlsType';
import { FlexibleControlsOptions } from './FlexibleControlsOptions';

const TEXTURE_SIZE = 25;
const LINE_WIDTH = 3;
const NO_DEPTH_TEST_OPACITY = 0.5;

export class FlexibleCameraMarkers {
private readonly _scene: Scene;
private _targetMarker: Sprite | undefined;
private _targetMarker1: Sprite | undefined;
private _targetMarker2: Sprite | undefined;

//================================================
// CONSTRUCTOR
Expand All @@ -24,29 +30,38 @@ export class FlexibleCameraMarkers {

public update(manager: FlexibleCameraManager): void {
if (this.isVisible(manager)) {
if (!this._targetMarker) {
this._targetMarker = createSprite(manager.options.outerMarkerColor, manager.options.innerMarkerColor);
this._scene.add(this._targetMarker);
this._targetMarker.visible = true;
} else if (!this._targetMarker.visible) {
this._targetMarker.visible = true;
if (this._targetMarker1 === undefined) {
this._targetMarker1 = createSprite(manager.options, true);
this._scene.add(this._targetMarker1);
}
if (this._targetMarker2 === undefined) {
this._targetMarker2 = createSprite(manager.options, false);
this._scene.add(this._targetMarker2);
}
for (const marker of this.getMarkers()) {
setPosition(marker, manager.controls.getTarget(), manager);
if (!marker.visible) {
marker.visible = true;
}
}
setPosition(this._targetMarker, manager.controls.getTarget(), manager);
} else {
if (this._targetMarker && this._targetMarker.visible) {
this._targetMarker.visible = false;
for (const marker of this.getMarkers()) {
if (marker.visible) {
marker.visible = false;
}
}
}
}

public dispose(): void {
if (this._targetMarker) {
this._scene.remove(this._targetMarker);
this._targetMarker.material.map?.dispose();
this._targetMarker.material.dispose();
this._targetMarker.geometry.dispose();
this._targetMarker = undefined;
for (const marker of this.getMarkers()) {
this._scene.remove(marker);
marker.material.map?.dispose();
marker.material.dispose();
marker.geometry.dispose();
}
this._targetMarker1 = undefined;
this._targetMarker2 = undefined;
}

private isVisible(manager: FlexibleCameraManager): boolean {
Expand All @@ -64,6 +79,11 @@ export class FlexibleCameraMarkers {
}
return true;
}

private *getMarkers(): Generator<Sprite> {
if (this._targetMarker1 !== undefined) yield this._targetMarker1;
if (this._targetMarker2 !== undefined) yield this._targetMarker2;
}
}

function setPosition(object3D: Object3D, position: Vector3, manager: FlexibleCameraManager): void {
Expand All @@ -75,9 +95,13 @@ function setPosition(object3D: Object3D, position: Vector3, manager: FlexibleCam
object3D.updateMatrixWorld();
}

function createSprite(outerColor: string, innerColor: string): Sprite {
const texture = createTexture(25, 3, outerColor, innerColor);
const material = new SpriteMaterial({ map: texture, depthTest: false });
function createSprite(options: FlexibleControlsOptions, depthTest: boolean): Sprite {
const texture = createTexture(TEXTURE_SIZE, LINE_WIDTH, options.outerMarkerColor, options.innerMarkerColor);
const material = new SpriteMaterial({ map: texture, depthTest });
if (!depthTest) {
material.transparent = true;
material.opacity = NO_DEPTH_TEST_OPACITY;
}
const sprite = new Sprite(material);
sprite.updateMatrixWorld();
sprite.visible = true;
Expand Down
Loading