Skip to content

Commit

Permalink
Merge pull request #13 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
Architecture Revamp
  • Loading branch information
BarthPaleologue authored Jan 19, 2024
2 parents 5a0e45e + 4afbb09 commit aa1adad
Show file tree
Hide file tree
Showing 106 changed files with 1,111 additions and 858 deletions.
1 change: 0 additions & 1 deletion src/html/helmetOverlay.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<section id="helmetOverlay">
<div id="bodyData">
<p id="bodyName"></p>
<p id="bodySeed"></p>
</div>
<div id="speedometer"></div>
</section>
12 changes: 6 additions & 6 deletions src/shaders/telluricPlanetMaterial/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ uniform mat4 normalMatrix;

uniform vec3 planetPosition;

uniform mat4 planetInverseRotationMatrix;
uniform mat4 inversePlanetWorldMatrix;

varying vec3 vPositionW;
varying vec3 vNormalW;
Expand All @@ -32,13 +32,13 @@ void main() {
vPositionW = vec3(world * vec4(position, 1.0));
vNormalW = normalize(mat3(normalMatrix) * normal);

vPosition = vPositionW - planetPosition;
vPosition = vec3(inversePlanetWorldMatrix * vec4(vPositionW, 1.0));
vLocalPosition = position;

vUnitSamplePoint = mat3(planetInverseRotationMatrix) * normalize(vPosition);
vSamplePointScaled = mat3(planetInverseRotationMatrix) * vPosition / 1000e3;
vSphereNormalW = mat3(normalMatrix) * vUnitSamplePoint;
vSamplePoint = mat3(planetInverseRotationMatrix) * vPosition;
vUnitSamplePoint = normalize(vPosition);
vSamplePointScaled = vPosition / 1000e3;
vSphereNormalW = normalize(vPosition);
vSamplePoint = vPosition;

vNormal = normal;
}
28 changes: 28 additions & 0 deletions src/ts/architecture/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Architecture of interface spanning from simple Transforms to complex Celestial bodies and Space stations:

![img.png](img.png)

This folder contains all the abstraction necessary to the architecture of the image.

This means all the node except the leaves (upper nodes) which are the actual concrete implementation of celestial bodies and space stations.

It all starts with simple `Transformable` and `BoundingSphere`:
those are simple objects that possess a BaylonJS `TransformNode` for their position, rotation, scaling and a bounding volume to make simple calculations for distances.

An `OrbitalObject` builds on top of this by adding the notion of orbit. They possess `OrbitProperties` that describes their motion around their `parent` which can be null in the case of some objects (think like stars).

`OrbitalObject` also possess `PhysicalProperties` that complement the `OrbitProperties` with information such as mass, axial tilting and rotation period.

To sum up, an `OrbitalObject` is an object that can rotate around another `OrbitalObject` and rotate on itself.

`CelestialBody` builds up on top of `OrbitalObject` by adding the notion of `BODY_TYPE` and `radius` that is expected from spherical objects.

`CelestialBody` are spherical orbital objects that encompasses both planets and stellar objects.
They can have specific post-processes applied to them (like atmosphere, clouds, rings...), which is why they also implement `HasPostProcesses`.

`StellarObject` builds on top of `CelestialBody` by adding a `PointLight` that is used to light up the scene. They also have a `STELLAR_TYPE` that describes their type (star, black hole, neutron star).

`Planet` builds on top of `CelestialBody` by adding new `PlanetPhysicalProperties` that describe their temperature and pressure. They also keep track of their number of moons.

The other nodes are the concrete implementations of all these abstractions.
They can be found in their respective folders (`planets` for `TelluricPlanet` and `GasPlanet`, `stellarObjects` for `Star`, `BlackHole` and `NeutronStar`, and spaceStations for `SpaceStation`).
3 changes: 3 additions & 0 deletions src/ts/architecture/boundingSphere.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface BoundingSphere {
getBoundingRadius(): number;
}
5 changes: 5 additions & 0 deletions src/ts/architecture/canHaveRings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RingsUniforms } from "../postProcesses/rings/ringsUniform";

export interface CanHaveRings {
getRingsUniforms(): RingsUniforms | null;
}
18 changes: 18 additions & 0 deletions src/ts/architecture/celestialBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { OrbitalObject, OrbitalObjectModel } from "./orbitalObject";
import { HasPostProcesses } from "./hasPostProcesses";
import { CanHaveRings } from "./canHaveRings";
import { BODY_TYPE } from "../model/common";

export interface CelestialBody extends OrbitalObject, CanHaveRings, HasPostProcesses {
model: CelestialBodyModel;

/**
* Returns the radius of the celestial body
*/
getRadius(): number;
}

export interface CelestialBodyModel extends OrbitalObjectModel {
readonly bodyType: BODY_TYPE;
readonly radius: number;
}
5 changes: 5 additions & 0 deletions src/ts/architecture/hasPostProcesses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PostProcessType } from "../postProcesses/postProcessTypes";

export interface HasPostProcesses {
postProcesses: PostProcessType[];
}
Binary file added src/ts/architecture/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions src/ts/architecture/orbitalObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Transformable } from "./transformable";
import { BoundingSphere } from "./boundingSphere";
import { OrbitProperties } from "../orbit/orbitProperties";
import { rotateVector3AroundInPlace } from "../utils/algebra";
import { Quaternion, Vector3 } from "@babylonjs/core/Maths/math";
import { getRotationQuaternion, setRotationQuaternion, translate } from "../uberCore/transforms/basicTransform";
import { OrbitalObjectPhysicalProperties } from "./physicalProperties";

export interface OrbitalObject extends Transformable, BoundingSphere {
name: string;

model: OrbitalObjectModel;

getRotationAxis(): Vector3;

getOrbitProperties(): OrbitProperties;

getPhysicalProperties(): OrbitalObjectPhysicalProperties;

getTypeName(): string;

parent: OrbitalObject | null;
}

export class OrbitalObject {
static getRotationAxis(object: OrbitalObject) {
return object.getTransform().up;
}

static computeNextOrbitalPosition(object: OrbitalObject, deltaTime: number) {
const orbit = object.getOrbitProperties();
if (orbit.period === 0 || object.parent === null) return object.getTransform().getAbsolutePosition();

const barycenter = object.parent.getTransform().getAbsolutePosition();

// enforce distance to orbit center
const oldPosition = object.getTransform().getAbsolutePosition().subtract(barycenter);
const newPosition = oldPosition.clone();

// rotate the object around the barycenter of the orbit, around the normal to the orbital plane
const dtheta = (2 * Math.PI * deltaTime) / orbit.period;
rotateVector3AroundInPlace(newPosition, barycenter, orbit.normalToPlane, dtheta);

newPosition.normalize().scaleInPlace(orbit.radius);

// enforce orbital plane
const correctionAxis = Vector3.Cross(orbit.normalToPlane, newPosition.normalizeToNew());
const correctionAngle = 0.5 * Math.PI - Vector3.GetAngleBetweenVectors(orbit.normalToPlane, newPosition.normalizeToNew(), correctionAxis);
newPosition.applyRotationQuaternionInPlace(Quaternion.RotationAxis(correctionAxis, correctionAngle));

return newPosition.addInPlace(barycenter);
}

static updateOrbitalPosition(object: OrbitalObject, deltaTime: number) {
const orbit = object.getOrbitProperties();
if (orbit.period === 0 || object.parent === null) return;

const oldPosition = object.getTransform().getAbsolutePosition();
const newPosition = OrbitalObject.computeNextOrbitalPosition(object, deltaTime);
translate(object.getTransform(), newPosition.subtractInPlace(oldPosition));
}

static getDeltaTheta(object: OrbitalObject, deltaTime: number) {
if (object.getPhysicalProperties().rotationPeriod === 0) return 0;
return (2 * Math.PI * deltaTime) / object.getPhysicalProperties().rotationPeriod;
}

/**
* Updates the rotation of the body around its axis
* @param object
* @param deltaTime The time elapsed since the last update
* @returns The elapsed angle of rotation around the axis
*/
static updateRotation(object: OrbitalObject, deltaTime: number) {
const dtheta = OrbitalObject.getDeltaTheta(object, deltaTime);
if (dtheta === 0) return;

const elementaryRotationQuaternion = Quaternion.RotationAxis(OrbitalObject.getRotationAxis(object), dtheta);
const newQuaternion = elementaryRotationQuaternion.multiply(getRotationQuaternion(object.getTransform()));

setRotationQuaternion(object.getTransform(), newQuaternion);
}
}

export interface OrbitalObjectModel {
rng: (step: number) => number;
seed: number;

orbit: OrbitProperties;
physicalProperties: OrbitalObjectPhysicalProperties;

readonly parentBody: OrbitalObjectModel | null;
readonly childrenBodies: OrbitalObjectModel[];
}
24 changes: 24 additions & 0 deletions src/ts/architecture/physicalProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type OrbitalObjectPhysicalProperties = {
mass: number;
rotationPeriod: number;
axialTilt: number;
};

export type StarPhysicalProperties = OrbitalObjectPhysicalProperties & {
temperature: number;
};

export type BlackHolePhysicalProperties = OrbitalObjectPhysicalProperties & {
accretionDiskRadius: number;
};

export type PlanetPhysicalProperties = OrbitalObjectPhysicalProperties & {
minTemperature: number;
maxTemperature: number;
pressure: number;
};

export type TelluricPlanetPhysicalProperties = PlanetPhysicalProperties & {
waterAmount: number;
oceanLevel: number;
};
18 changes: 18 additions & 0 deletions src/ts/architecture/planet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Camera } from "@babylonjs/core/Cameras/camera";
import { CelestialBody, CelestialBodyModel } from "./celestialBody";
import { PlanetPhysicalProperties } from "./physicalProperties";
import { Transformable } from "./transformable";

export interface Planet extends CelestialBody {
model: PlanetModel;

updateMaterial(controller: Camera, stellarObjects: Transformable[], deltaTime: number): void;
}

export interface PlanetModel extends CelestialBodyModel {
physicalProperties: PlanetPhysicalProperties;

nbMoons: number;

getApparentRadius(): number;
}
13 changes: 13 additions & 0 deletions src/ts/architecture/stellarObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PointLight } from "@babylonjs/core/Lights/pointLight";
import { CelestialBody, CelestialBodyModel } from "./celestialBody";
import { STELLAR_TYPE } from "../stellarObjects/common";

export interface StellarObject extends CelestialBody {
model: StellarObjectModel;

getLight(): PointLight;
}

export interface StellarObjectModel extends CelestialBodyModel {
stellarType: STELLAR_TYPE;
}
7 changes: 7 additions & 0 deletions src/ts/architecture/transformable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TransformNode } from "@babylonjs/core/Meshes";

export interface Transformable {
getTransform(): TransformNode;

dispose(): void;
}
3 changes: 3 additions & 0 deletions src/ts/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import plumeParticle from "../asset/textures/plume.png";

import atmosphereLUT from "../shaders/textures/atmosphereLUT.glsl";

import seamlessPerlin from "../asset/perlin.png";
import spaceship from "../asset/spaceship/spaceship2.glb";
import shipCarrier from "../asset/spacestation/shipcarrier.glb";
import banana from "../asset/banana/banana.glb";
Expand Down Expand Up @@ -70,6 +71,7 @@ export class Assets {

static AtmosphereLUT: ProceduralTexture;

static SeamlessPerlin: Texture;
private static Spaceship: Mesh;
private static EndeavorSpaceship: Mesh;
private static Spacestation: Mesh;
Expand Down Expand Up @@ -107,6 +109,7 @@ export class Assets {

Assets.manager.addTextureTask("PlumeParticle", plumeParticle).onSuccess = (task) => (Assets.PlumeParticle = task.texture);

Assets.manager.addTextureTask("SeamlessPerlin", seamlessPerlin).onSuccess = (task) => (Assets.SeamlessPerlin = task.texture);
Assets.AtmosphereLUT = new ProceduralTexture("atmosphereLUT", 100, { fragmentSource: atmosphereLUT }, scene, undefined, false, false);
Assets.AtmosphereLUT.refreshRate = 0;

Expand Down
38 changes: 0 additions & 38 deletions src/ts/bodies/abstractBody.ts

This file was deleted.

Loading

0 comments on commit aa1adad

Please sign in to comment.