Skip to content

Commit

Permalink
Merge pull request #22 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
Realistic stellar distribution
  • Loading branch information
BarthPaleologue authored Jan 28, 2024
2 parents 777d76f + b8157e7 commit 97de552
Show file tree
Hide file tree
Showing 40 changed files with 468 additions and 140 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"import/no-cycle": "error",
"import/no-unresolved": "off",
"@typescript-eslint/switch-exhaustiveness-check": "error",
"@typescript-eslint/no-inferrable-types": "off"
"@typescript-eslint/no-inferrable-types": "off",
"eqeqeq": "error"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"test": "jest --coverage",
"docs": "typedoc --options typedoc.json",
"serve:docs": "http-server docs -p 8081",
"lint:check": "eslint src/ts/**/*.ts",
"lint:check": "eslint 'src/ts/**/**/*.ts'",
"lint:fix": "eslint src/ts/**/*.ts --fix"
}
}
4 changes: 2 additions & 2 deletions src/shaders/matterjet.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ float spiralDensity(vec3 pointOnCone, vec3 coneAxis, float coneMaxHeight) {

float density = 1.0;

// smoothstep fadeout when the height is too much (outside of cone) or too low (too close to the star)
density *= smoothstep(1.0, 0.0, heightFraction) * smoothstep(0.0, 0.05, heightFraction);
// smoothstep fadeout when the height is too much (outside of cone)
density *= smoothstep(1.0, 0.0, heightFraction);

float d = spiralSDF(theta + time, 0.2 + sqrt(heightFraction) / 2.0) / (0.3 + heightFraction * 2.0);
//d = pow(d, 4.0);
Expand Down
5 changes: 3 additions & 2 deletions src/ts/architecture/stellarObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { PointLight } from "@babylonjs/core/Lights/pointLight";
import { CelestialBody, CelestialBodyModel } from "./celestialBody";
import { STELLAR_TYPE } from "../stellarObjects/common";
import { Color3 } from "@babylonjs/core/Maths/math.color";

export interface StellarObject extends CelestialBody {
model: StellarObjectModel;
Expand All @@ -26,5 +26,6 @@ export interface StellarObject extends CelestialBody {
}

export interface StellarObjectModel extends CelestialBodyModel {
stellarType: STELLAR_TYPE;
readonly temperature: number;
readonly color: Color3;
}
2 changes: 1 addition & 1 deletion src/ts/defaultController/defaultControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class DefaultControls implements Controls {
displacement.addInPlace(upwardDisplacement);
displacement.addInPlace(rightDisplacement);

if (input.getAcceleration() != 0) this.speed *= 1 + input.getAcceleration() / 10;
if (input.getAcceleration() !== 0) this.speed *= 1 + input.getAcceleration() / 10;

return displacement;
}
Expand Down
1 change: 0 additions & 1 deletion src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ document.addEventListener("keydown", (e) => {

if (e.key === "y") {
if (starSystemView.scene.getActiveController() === shipControls) {

console.log("disembark");

characterController.getTransform().setEnabled(true);
Expand Down
1 change: 1 addition & 0 deletions src/ts/model/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum GENERATION_STEPS {
ACCENNT_COLOR = 400,

TEMPERATURE = 1100,
STELLAR_TYPE = 1900,

PRESSURE = 1100,
WATER_AMOUNT = 1200,
Expand Down
4 changes: 2 additions & 2 deletions src/ts/planets/gasPlanet/gasPlanetMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { Color3 } from "@babylonjs/core/Maths/math.color";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { TransformNode } from "@babylonjs/core/Meshes";
import { Star } from "../../stellarObjects/star/star";
import { flattenVector3Array } from "../../utils/algebra";
import { flattenColor3Array, flattenVector3Array } from "../../utils/algebra";
import { Camera } from "@babylonjs/core/Cameras/camera";
import { Transformable } from "../../architecture/transformable";

Expand Down Expand Up @@ -103,7 +103,7 @@ export class GasPlanetMaterial extends ShaderMaterial {
this.setVector3("playerPosition", player.globalPosition);

this.setArray3("star_positions", flattenVector3Array(stellarObjects.map((star) => star.getTransform().getAbsolutePosition())));
this.setArray3("star_colors", flattenVector3Array(stellarObjects.map((star) => (star instanceof Star ? star.model.surfaceColor : Vector3.One()))));
this.setArray3("star_colors", flattenColor3Array(stellarObjects.map((star) => (star instanceof Star ? star.model.color : Color3.White()))));
this.setInt("nbStars", stellarObjects.length);

this.setFloat("time", this.clock % 100000);
Expand Down
5 changes: 2 additions & 3 deletions src/ts/planets/telluricPlanet/telluricPlanetMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import { ShaderMaterial } from "@babylonjs/core/Materials/shaderMaterial";
import { Color3 } from "@babylonjs/core/Maths/math.color";
import { Vector3 } from "@babylonjs/core/Maths/math";
import { TransformNode } from "@babylonjs/core/Meshes";
import { getInverseRotationMatrix } from "../../uberCore/transforms/basicTransform";
import { Star } from "../../stellarObjects/star/star";
import { flattenVector3Array } from "../../utils/algebra";
import { flattenColor3Array, flattenVector3Array } from "../../utils/algebra";

import lutFragment from "../../../shaders/telluricPlanetMaterial/utils/lut.glsl";
import { ProceduralTexture } from "@babylonjs/core/Materials/Textures/Procedurals/proceduralTexture";
Expand Down Expand Up @@ -216,7 +215,7 @@ export class TelluricPlanetMaterial extends ShaderMaterial {
this.setVector3("playerPosition", cameraPosition);

this.setArray3("star_positions", flattenVector3Array(stellarObjects.map((star) => star.getTransform().getAbsolutePosition())));
this.setArray3("star_colors", flattenVector3Array(stellarObjects.map((star) => (star instanceof Star ? star.model.surfaceColor : Vector3.One()))));
this.setArray3("star_colors", flattenColor3Array(stellarObjects.map((star) => (star instanceof Star ? star.model.color : Color3.White()))));
this.setInt("nbStars", stellarObjects.length);

this.setVector3("planetPosition", this.planetTransform.getAbsolutePosition());
Expand Down
2 changes: 1 addition & 1 deletion src/ts/planets/telluricPlanet/terrain/chunks/chunkTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class ChunkTree {
* @returns The updated tree
*/
private updateLODRecursively(observerPositionW: Vector3, chunkForge: ChunkForge, tree: quadTree = this.tree, walked: number[] = []): quadTree {
if (walked.length == this.maxDepth) return tree;
if (walked.length === this.maxDepth) return tree;

const nodeRelativePosition = getChunkSphereSpacePositionFromPath(walked, this.direction, this.rootChunkLength / 2, getRotationQuaternion(this.parent));
const nodePositionW = nodeRelativePosition.add(this.parent.getAbsolutePosition());
Expand Down
7 changes: 4 additions & 3 deletions src/ts/postProcesses/lensFlarePostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { PhysicsRaycastResult } from "@babylonjs/core/Physics/physicsRaycastResu
import { PhysicsEngineV2 } from "@babylonjs/core/Physics/v2";
import { Matrix } from "@babylonjs/core/Maths/math";
import { StellarObject } from "../architecture/stellarObject";
import { Color3 } from "@babylonjs/core/Maths/math.color";

export type LensFlareSettings = {
visibility: number;
Expand Down Expand Up @@ -57,10 +58,10 @@ export class LensFlarePostProcess extends UberPostProcess implements ObjectPostP
...getActiveCameraUniforms(scene),
{
name: "flareColor",
type: UniformEnumType.Vector3,
type: UniformEnumType.Color3,
get: () => {
if (object instanceof Star) return object.model.surfaceColor;
else return new Vector3(1, 1, 1);
if (object instanceof Star) return object.model.color;
else return new Color3(1, 1, 1);
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/ts/postProcesses/matterJetPostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { UberScene } from "../uberCore/uberScene";
import { getActiveCameraUniforms, getObjectUniforms, getSamplers } from "./uniforms";
import { UberPostProcess } from "../uberCore/postProcesses/uberPostProcess";
import { Effect } from "@babylonjs/core/Materials/effect";
import { StellarObject } from "../stellarObjects/stellarObject";
import { ObjectPostProcess, UpdatablePostProcess } from "./objectPostProcess";
import { UniformEnumType, ShaderSamplers, ShaderUniforms } from "../uberCore/postProcesses/types";
import { StellarObject } from "../architecture/stellarObject";

export interface MatterJetUniforms {
// the rotation period in seconds of the matter jet
Expand Down
6 changes: 4 additions & 2 deletions src/ts/postProcesses/postProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,21 +281,22 @@ export class PostProcessManager {
* Creates a new VolumetricLight postprocess for the given star and adds it to the manager.
* @param star A star
*/
public addVolumetricLight(star: Star) {
public addVolumetricLight(star: Star | NeutronStar) {
this.volumetricLights.push(new VolumetricLight(star, this.scene));
}

/**
* Returns the volumetric light post process for the given star. Throws an error if no volumetric light is found.
* @param star A star
*/
public getVolumetricLight(star: Star): VolumetricLight | null {
public getVolumetricLight(star: Star | NeutronStar): VolumetricLight | null {
return this.volumetricLights.find((vl) => vl.object === star) ?? null;
}

/**
* Creates a new BlackHole postprocess for the given black hole and adds it to the manager.
* @param blackHole A black hole
* @param starfieldRotation
*/
public addBlackHole(blackHole: BlackHole, starfieldRotation: Quaternion) {
const blackhole = new BlackHolePostProcess(blackHole, this.scene, starfieldRotation);
Expand All @@ -307,6 +308,7 @@ export class PostProcessManager {
}

public addMatterJet(neutronStar: NeutronStar) {
console.log("add matter jet");
this.matterJets.push(new MatterJetPostProcess(neutronStar.name, neutronStar, this.scene));
}

Expand Down
5 changes: 3 additions & 2 deletions src/ts/postProcesses/uniforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { BoundingSphere } from "../architecture/boundingSphere";
import { Star } from "../stellarObjects/star/star";
import { Transformable } from "../architecture/transformable";
import { Scene } from "@babylonjs/core/scene";
import { Color3 } from "@babylonjs/core/Maths/math.color";

export function getActiveCameraUniforms(scene: Scene): ShaderUniforms {
return [
Expand Down Expand Up @@ -93,8 +94,8 @@ export function getStellarObjectsUniforms(stars: Transformable[]): ShaderUniform
},
{
name: "star_colors",
type: UniformEnumType.Vector3Array,
get: () => stars.map((star) => (star instanceof Star ? star.model.surfaceColor : Vector3.One()))
type: UniformEnumType.Color3Array,
get: () => stars.map((star) => (star instanceof Star ? star.model.color : Color3.White()))
},
{
name: "nbStars",
Expand Down
2 changes: 1 addition & 1 deletion src/ts/postProcesses/volumetricCloudsPostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { ObjectPostProcess } from "./objectPostProcess";
import { FlatCloudsPostProcess } from "./clouds/flatCloudsPostProcess";
import { Effect } from "@babylonjs/core/Materials/effect";
import { UniformEnumType, ShaderSamplers, ShaderUniforms } from "../uberCore/postProcesses/types";
import { StellarObject } from "../stellarObjects/stellarObject";
import { CloudsUniforms } from "./clouds/cloudsUniforms";

import { BoundingSphere } from "../architecture/boundingSphere";
import { Transformable } from "../architecture/transformable";
import { StellarObject } from "../architecture/stellarObject";

export type CloudsPostProcess = FlatCloudsPostProcess | VolumetricCloudsPostProcess;

Expand Down
5 changes: 3 additions & 2 deletions src/ts/postProcesses/volumetricLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import { UberScene } from "../uberCore/uberScene";
import { Texture } from "@babylonjs/core/Materials/Textures/texture";
import { ObjectPostProcess } from "./objectPostProcess";
import { Star } from "../stellarObjects/star/star";
import { NeutronStar } from "../stellarObjects/neutronStar/neutronStar";

export class VolumetricLight extends VolumetricLightScatteringPostProcess implements ObjectPostProcess {
readonly object: Star;
readonly object: Star | NeutronStar;

constructor(star: Star, scene: UberScene) {
constructor(star: Star | NeutronStar, scene: UberScene) {
if (scene.activeCamera === null) throw new Error("no camera");
super(`${star.name}VolumetricLight`, 1, scene.activeCamera, star.mesh, 100, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, scene);

Expand Down
4 changes: 2 additions & 2 deletions src/ts/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Settings = {
VERTEX_RESOLUTION: 64,
MIN_DISTANCE_BETWEEN_VERTICES: 1.5,

CLOUD_LAYER_HEIGHT: 15e3,
CLOUD_LAYER_HEIGHT: 7e3,
ATMOSPHERE_HEIGHT: 100e3,
OCEAN_DEPTH: 7e3,

Expand All @@ -41,7 +41,7 @@ export const Settings = {
export const CollisionMask = {
GROUND: 0b00000001,
SPACESHIP: 0b00000010,
LANDING_PADS: 0b00000100,
LANDING_PADS: 0b00000100
};

const seedableRNG = seededSquirrelNoise(Settings.UNIVERSE_SEED);
Expand Down
18 changes: 5 additions & 13 deletions src/ts/spaceship/shipControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { LOCAL_DIRECTION } from "../uberCore/localDirections";
import {
getForwardDirection,
getUpwardDirection,
pitch,
roll,
translate
} from "../uberCore/transforms/basicTransform";
import { getForwardDirection, getUpwardDirection, pitch, roll, translate } from "../uberCore/transforms/basicTransform";
import { TransformNode } from "@babylonjs/core/Meshes";
import { Controls } from "../uberCore/controls";
import { Input, InputType } from "../inputs/input";
Expand All @@ -35,12 +29,6 @@ import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera";
import { Spaceship } from "./spaceship";

enum ShipState {
FLYING,
LANDING,
LANDED
}

export class ShipControls implements Controls {
readonly spaceship: Spaceship;

Expand Down Expand Up @@ -160,6 +148,10 @@ export class ShipControls implements Controls {

for (const input of this.inputs) this.listenTo(input, deltaTime);

// camera shake
// this.thirdPersonCamera.alpha += (Math.random() - 0.5) / 500;
// this.thirdPersonCamera.beta += (Math.random() - 0.5) / 500;

this.getActiveCamera().getViewMatrix(true);
return this.getTransform().getAbsolutePosition();
}
Expand Down
23 changes: 20 additions & 3 deletions src/ts/spaceship/spaceship.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// This file is part of CosmosJourneyer
//
// Copyright (C) 2024 Barthélemy Paléologue <barth.paleologue@cosmosjourneyer.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh";
Expand Down Expand Up @@ -52,7 +69,7 @@ export class Spaceship implements Transformable {
radius: 1
};

private readonly warpTunnel: WarpTunnel;
readonly warpTunnel: WarpTunnel;

private readonly scene: Scene;

Expand Down Expand Up @@ -207,7 +224,7 @@ export class Spaceship implements Transformable {
this.warpDrive.update(currentForwardSpeed, this.closestObject.distance, this.closestObject.radius, deltaTime);

// the warp throttle goes from 0.1 to 1 smoothly using an inverse function
if(this.warpDrive.isEnabled()) this.warpTunnel.setThrottle(1 - 1 / (1.1 * (1 + 1e-6 * this.warpDrive.getWarpSpeed())));
if (this.warpDrive.isEnabled()) this.warpTunnel.setThrottle(1 - 1 / (1.1 * (1 + 1e-6 * this.warpDrive.getWarpSpeed())));
else this.warpTunnel.setThrottle(0);

for (const thruster of this.mainThrusters) thruster.update();
Expand All @@ -229,7 +246,7 @@ export class Spaceship implements Transformable {
this.aggregate.body.setAngularDamping(1);
}

if (this.state == ShipState.LANDING) {
if (this.state === ShipState.LANDING) {
if (this.landingTarget === null) {
throw new Error("Closest walkable object is null while landing");
}
Expand Down
4 changes: 2 additions & 2 deletions src/ts/spaceshipExtended/spaceship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ export class Spaceship {
const spacePressed = keyboard.isPressed(" ");
const forwardPressed = keyboard.isAnyPressed(["w", "z"]);

if (spacePressed != this.hoverThrustersRunning) {
if (spacePressed !== this.hoverThrustersRunning) {
if (spacePressed) Assets.EngineRunningSound.play();
else Assets.EngineRunningSound.stop();

this.hoverThrustersRunning = spacePressed;
}

if (forwardPressed != this.mainThrustersRunning) {
if (forwardPressed !== this.mainThrustersRunning) {
if (forwardPressed) Assets.EngineRunningSound.play();
else Assets.EngineRunningSound.stop();

Expand Down
4 changes: 2 additions & 2 deletions src/ts/spaceshipExtended/thrusterMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function buildThrusterMatrix(hoverThrusters: Thruster[]) {
}

export function getThrustAndTorque(thrusterConfiguration: number[], thrusterMatrix: Matrix): [Vector3, Vector3] {
if (thrusterMatrix.rows != 6) throw new Error("Thruster matrix must have 6 rows!");
if (thrusterMatrix.rows !== 6) throw new Error("Thruster matrix must have 6 rows!");
const thrustAndTorque: [number, number, number, number, number, number] = [0, 0, 0, 0, 0, 0];
for (let i = 0; i < thrusterMatrix.rows; i++) {
const row = thrusterMatrix.getRow(i);
Expand All @@ -57,7 +57,7 @@ export function getThrustAndTorque(thrusterConfiguration: number[], thrusterMatr
}

export function getThrusterConfiguration(targetThrust: Vector3, targetTorque: Vector3, inverseThrusterMatrix: Matrix): number[] {
if (inverseThrusterMatrix.columns != 6) throw new Error("Inverse thruster matrix must have 6 columns!");
if (inverseThrusterMatrix.columns !== 6) throw new Error("Inverse thruster matrix must have 6 columns!");
const targetThrustAndTorque = [targetThrust.x, targetThrust.y, targetThrust.z, targetTorque.x, targetTorque.y, targetTorque.z];
const nbThrusters = inverseThrusterMatrix.rows;
const thrusterConfiguration = new Array(nbThrusters).fill(0);
Expand Down
Loading

0 comments on commit 97de552

Please sign in to comment.