Skip to content

Commit

Permalink
Added some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleGough committed Jul 31, 2023
1 parent 35cf788 commit 9f12f67
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
39 changes: 30 additions & 9 deletions src/setup/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ export interface PointOfInterest {

export class Label {
parent: THREE.Object3D;
parentRadius: number;
radius: number;
elements: CSS2DObject[];

constructor(parent: THREE.Object3D, parentRadius: number) {
/**
* Represents a collection of labels for a celestial body.
* @constructor
* @param parent - Parent object for the labels.
* @param radius - Distance between parent centre and label positions.
*/
constructor(parent: THREE.Object3D, radius: number) {
this.parent = parent;
this.parentRadius = parentRadius;
this.radius = radius;
this.elements = [];
}

Expand Down Expand Up @@ -46,18 +52,28 @@ export class Label {
this.elements.push(label);
};

/**
* Show all point-of-interest labels.
*/
showPOI = () => {
this.elements.forEach((label) => {
label.layers.enable(LAYERS.POILabel);
});
};

/**
* Hides all point-of-interest labels.
*/
hidePOI = () => {
this.elements.forEach((label) => {
label.layers.disable(LAYERS.POILabel);
});
};

/**
* Update label opacities depending on camera position and direction.
* @param camera - Camera used to calculate distance and direction to labels.
*/
update = (camera: THREE.Camera) => {
this.elements.forEach((label) => {
const rotationOpacity = this.getRotationOpacity(camera, label);
Expand All @@ -67,16 +83,21 @@ export class Label {
});
};

rotateLabel = (y: number, z: number) => {
const vector = new THREE.Vector3(this.parentRadius, 0, 0);
private rotateLabel = (y: number, z: number) => {
const vector = new THREE.Vector3(this.radius, 0, 0);
vector.applyAxisAngle(new THREE.Vector3(0, 1, 0), y);
vector.applyAxisAngle(new THREE.Vector3(0, 0, 1), z);
return vector;
};

getRotationOpacity = (camera: THREE.Camera, label: CSS2DObject): number => {
private getRotationOpacity = (
camera: THREE.Camera,
label: CSS2DObject
): number => {
const hideThreshold = 1;
const fadeThreshold = 0.75;

// Calculates the great-circle distance between the camera and label with normalised vectors.
const cameraVector = camera.position.clone().normalize();
const labelVector = label.position.clone().normalize();
const delta = Math.acos(cameraVector.dot(labelVector));
Expand All @@ -90,9 +111,9 @@ export class Label {
}
};

getDistanceOpacity = (camera: THREE.Camera): number => {
const hideThreshold = this.parentRadius * 12;
const fadeThreshold = this.parentRadius * 8;
private getDistanceOpacity = (camera: THREE.Camera): number => {
const hideThreshold = this.radius * 12;
const fadeThreshold = this.radius * 8;
const distance = camera.position.length();

if (distance > hideThreshold) {
Expand Down
9 changes: 7 additions & 2 deletions src/setup/loading.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// List of prompts to display whilst loading textures.
const loadingPrompts = [
"Detecting neutrinos",
"Forming event horizons",
Expand All @@ -10,14 +11,18 @@ const loadingPrompts = [
"Increasing entropy",
];

const loadingTextInterval = setInterval(() => {
// Switch loading screen text every 2 seconds.
const switchLoadText = setInterval(() => {
const index = Math.floor(Math.random() * loadingPrompts.length);
const loadText = document.getElementById("loader-text") as HTMLDivElement;
loadText.textContent = `${loadingPrompts[index]}...`;
}, 2000);

/**
* Updates the loading screen once textures are loaded.
*/
export const onLoaded = () => {
clearInterval(loadingTextInterval);
clearInterval(switchLoadText);
const loadText = document.getElementById("loader-text") as HTMLDivElement;
loadText.textContent = "Click to continue...";

Expand Down
41 changes: 32 additions & 9 deletions src/setup/planetary-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,28 @@ export class PlanetaryObject {
this.mesh.add(this.createAtmosphereMesh());
}

this.initLabels(body);
this.initLabels(body.labels);
}

initLabels = (body: Body) => {
/**
* Creates label objects for each point-of-interest.
* @param labels - List of labels to display.
*/
private initLabels = (labels?: PointOfInterest[]) => {
this.labels = new Label(this.mesh, this.radius);

if (body.labels) {
body.labels.forEach((poi) => {
if (labels) {
labels.forEach((poi) => {
this.labels.createPOILabel(poi);
});
}
};

loadTextures(textures: TexturePaths) {
/**
* Prepare and load textures.
* @param textures - Object of texture paths to load.
*/
private loadTextures(textures: TexturePaths) {
this.map = loadTexture(textures.map);
if (textures.bump) {
this.bumpMap = loadTexture(textures.bump);
Expand All @@ -117,7 +125,11 @@ export class PlanetaryObject {
}
}

createMesh = () => {
/**
* Creates the main mesh object with textures.
* @returns celestial body mesh.
*/
private createMesh = () => {
if (this.type === "ring") {
return createRingMesh(this.map);
}
Expand Down Expand Up @@ -156,7 +168,11 @@ export class PlanetaryObject {
return sphere;
};

createAtmosphereMesh = () => {
/**
* Creates the atmosphere mesh object with textures.
* @returns atmosphere mesh.
*/
private createAtmosphereMesh = () => {
const geometry = new THREE.SphereGeometry(this.radius + 0.0005, 64, 64);

const material = new THREE.MeshPhongMaterial({
Expand All @@ -174,14 +190,18 @@ export class PlanetaryObject {
return sphere;
};

getRotation = (elapsedTime: number) => {
private getRotation = (elapsedTime: number) => {
return this.daylength ? (elapsedTime * timeFactor) / this.daylength : 0;
};

getOrbitRotation = (elapsedTime: number) => {
private getOrbitRotation = (elapsedTime: number) => {
return this.daylength ? (elapsedTime * timeFactor) / (this.period * 24) : 0;
};

/**
* Updates orbital position and rotation.
* @param elapsedTime - number of seconds elapsed.
*/
tick = (elapsedTime: number) => {
// Convert real-time seconds to rotation.
const rotation = this.getRotation(elapsedTime);
Expand All @@ -199,6 +219,9 @@ export class PlanetaryObject {
}
};

/**
* @returns the minimum orbital control camera distance allowed.
*/
getMinDistance = (): number => {
return this.radius * 3.5;
};
Expand Down

0 comments on commit 9f12f67

Please sign in to comment.