Skip to content

Commit

Permalink
Merge pull request #7 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
New Seed System
  • Loading branch information
BarthPaleologue authored Jan 9, 2024
2 parents 3267c8b + 850a357 commit 1c6d6c1
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 280 deletions.
4 changes: 3 additions & 1 deletion src/ts/blackHoleDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { StarSystemHelper } from "./starSystem/starSystemHelper";
import { Mouse } from "./inputs/mouse";
import { Keyboard } from "./inputs/keyboard";
import { Gamepad } from "./inputs/gamepad";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { SystemSeed } from "./utils/systemSeed";

const engine = new CosmosJourneyer();

Expand Down Expand Up @@ -62,7 +64,7 @@ engine.registerStarSystemUpdateCallback(() => {
(document.querySelector("#speedometer") as HTMLElement).innerHTML = `${throttleString} | ${parseSpeed(spaceshipController.getSpeed())}`;
});

const starSystemSeed = randRange(-1, 1, (step: number) => Math.random(), 0);
const starSystemSeed = new SystemSeed(Vector3.Zero(), 0);
const starSystem = new StarSystemController(starSystemSeed, scene);
starSystemView.setStarSystem(starSystem, false);

Expand Down
6 changes: 4 additions & 2 deletions src/ts/cosmosJourneyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Observable } from "@babylonjs/core/Misc/observable";
import { PauseMenu } from "./ui/pauseMenu";
import { StarSystemView } from "./starSystem/StarSystemView";
import { EngineFactory } from "@babylonjs/core/Engines/engineFactory";
import { SystemSeed } from "./utils/systemSeed";

enum EngineState {
RUNNING,
Expand Down Expand Up @@ -51,7 +52,8 @@ export class CosmosJourneyer {
this.pauseMenu.onScreenshot.add(() => this.takeScreenshot());
this.pauseMenu.onShare.add(() => {
const seed = this.getStarSystemView().getStarSystem().model.seed;
const url = new URL(`https://barthpaleologue.github.io/CosmosJourneyer/random.html?seed=${seed}`);
const payload = `starMapX=${seed.starSectorCoordinates.x}&starMapY=${seed.starSectorCoordinates.y}&starMapZ=${seed.starSectorCoordinates.z}&index=${seed.index}`;
const url = new URL(`https://barthpaleologue.github.io/CosmosJourneyer/random.html?${payload}`);
navigator.clipboard.writeText(url.toString()).then(() => console.log("Copied to clipboard"));
});

Expand Down Expand Up @@ -110,7 +112,7 @@ export class CosmosJourneyer {

// Init starmap view
this.starMap = new StarMap(this.engine);
this.starMap.onWarpObservable.add((seed: number) => {
this.starMap.onWarpObservable.add((seed: SystemSeed) => {
this.getStarSystemView().setStarSystem(new StarSystemController(seed, this.getStarSystemView().scene), true);
this.getStarSystemView().init();
this.toggleStarMap();
Expand Down
16 changes: 6 additions & 10 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import { ShipControls } from "./spaceship/shipControls";
import { PostProcessType } from "./postProcesses/postProcessTypes";
import { TelluricPlanemoModel } from "./planemos/telluricPlanemo/telluricPlanemoModel";
import { GasPlanetModel } from "./planemos/gasPlanet/gasPlanetModel";
import {
getForwardDirection,
getRotationQuaternion,
setRotationQuaternion,
translate
} from "./uberCore/transforms/basicTransform";
import { getForwardDirection, getRotationQuaternion, setRotationQuaternion, translate } from "./uberCore/transforms/basicTransform";
import { parsePercentageFrom01, parseSpeed } from "./utils/parseToStrings";

import { StarSystemHelper } from "./starSystem/starSystemHelper";
Expand All @@ -31,6 +26,7 @@ import { getMoonSeed } from "./planemos/common";

import { Gamepad } from "./inputs/gamepad";
import { CharacterControls } from "./spacelegs/characterControls";
import { SystemSeed } from "./utils/systemSeed";

const engine = new CosmosJourneyer();

Expand Down Expand Up @@ -104,7 +100,7 @@ engine.onToggleStarMapObservable.add((isStarMapOpen) => {

console.log(`Time is going ${Settings.TIME_MULTIPLIER} time${Settings.TIME_MULTIPLIER > 1 ? "s" : ""} faster than in reality`);

const starSystemSeed = 0;
const starSystemSeed = new SystemSeed(Vector3.Zero(), 0);
const starSystem = new StarSystemController(starSystemSeed, starSystemView.scene);
starSystem.model.setName("Alpha Testis");

Expand Down Expand Up @@ -218,8 +214,8 @@ if (aresAtmosphere) {
document.addEventListener("keydown", (e) => {
if (engine.isPaused()) return;

if(e.key === "y") {
if(starSystemView.scene.getActiveController() === spaceshipController) {
if (e.key === "y") {
if (starSystemView.scene.getActiveController() === spaceshipController) {
console.log("disembark");

characterController.getTransform().setEnabled(true);
Expand All @@ -230,7 +226,7 @@ document.addEventListener("keydown", (e) => {

starSystemView.scene.setActiveController(characterController);
starSystemView.getStarSystem().postProcessManager.rebuild();
} else if(starSystemView.scene.getActiveController() === characterController) {
} else if (starSystemView.scene.getActiveController() === characterController) {
console.log("embark");

characterController.getTransform().setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class ChunkTree {

const distanceThreshold = Settings.CHUNK_RENDER_DISTANCE_MULTIPLIER * (this.rootChunkLength / 2 ** walked.length);

if ((distanceToNodeSquared < distanceThreshold ** 2) || walked.length < this.minDepth) {
if (distanceToNodeSquared < distanceThreshold ** 2 || walked.length < this.minDepth) {
// if the node is near the camera or if we are loading minimal LOD
if (tree instanceof Array && tree.length === 4) {
return [
Expand Down
2 changes: 0 additions & 2 deletions src/ts/postProcesses/atmosphericScatteringPostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { TelluricPlanemo } from "../planemos/telluricPlanemo/telluricPlanemo";
import { GasPlanet } from "../planemos/gasPlanet/gasPlanet";
import { ObjectPostProcess } from "./objectPostProcess";
import { UniformEnumType, ShaderSamplers, ShaderUniforms, SamplerEnumType } from "../uberCore/postProcesses/types";
import { StellarObject } from "../stellarObjects/stellarObject";
import { BaseObject } from "../bodies/common";
import { Transformable } from "../uberCore/transforms/basicTransform";

export interface AtmosphereUniforms {
Expand Down
1 change: 0 additions & 1 deletion src/ts/postProcesses/objectPostProcess.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PostProcess } from "@babylonjs/core/PostProcesses/postProcess";
import { BaseObject } from "../bodies/common";
import { Transformable } from "../uberCore/transforms/basicTransform";

export interface UpdatablePostProcess extends PostProcess {
Expand Down
1 change: 0 additions & 1 deletion src/ts/postProcesses/shadowPostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Effect } from "@babylonjs/core/Materials/effect";
import { SamplerEnumType, ShaderSamplers, ShaderUniforms, UniformEnumType } from "../uberCore/postProcesses/types";
import { PostProcessType } from "./postProcessTypes";
import { RingsUniforms } from "./rings/ringsUniform";
import { RingsPostProcess } from "./rings/ringsPostProcess";
import { Assets } from "../assets";

export type ShadowUniforms = {
Expand Down
25 changes: 21 additions & 4 deletions src/ts/randomizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "../styles/index.scss";

import { StarSystemController } from "./starSystem/starSystemController";

import { randRange } from "extended-random";
import { centeredRand, randRange } from "extended-random";
import { Settings } from "./settings";
import { DefaultControls } from "./defaultController/defaultControls";
import { positionNearObject } from "./utils/positionNearObject";
Expand All @@ -15,6 +15,8 @@ import { parsePercentageFrom01, parseSpeed } from "./utils/parseToStrings";
import { Mouse } from "./inputs/mouse";
import { Keyboard } from "./inputs/keyboard";
import { Gamepad } from "./inputs/gamepad";
import { SystemSeed } from "./utils/systemSeed";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";

const engine = new CosmosJourneyer();

Expand Down Expand Up @@ -72,11 +74,25 @@ engine.onToggleStarMapObservable.add((isStarMapOpen) => {

//check if url contains a seed
const urlParams = new URLSearchParams(window.location.search);
const seed = urlParams.get("seed");
const urlStarMapX = urlParams.get("starMapX");
const urlStarMapY = urlParams.get("starMapY");
const urlStarMapZ = urlParams.get("starMapZ");
const urlIndex = urlParams.get("index");
const urlBodyIndex = urlParams.get("bodyIndex");

const starSystem = new StarSystemController(seed ? Number(seed) : randRange(-1, 1, (step: number) => Math.random(), 0) * Number.MAX_SAFE_INTEGER, scene);
const starMapX = urlStarMapX !== null ? Number(urlStarMapX) : Math.trunc((Math.random() * 2 - 1) * Number.MAX_SAFE_INTEGER * 0.1);
const starMapY = urlStarMapY !== null ? Number(urlStarMapY) : Math.trunc((Math.random() * 2 - 1) * Number.MAX_SAFE_INTEGER * 0.1);
const starMapZ = urlStarMapZ !== null ? Number(urlStarMapZ) : Math.trunc((Math.random() * 2 - 1) * Number.MAX_SAFE_INTEGER * 0.1);
const index = urlIndex !== null ? Number(urlIndex) : 0;
const bodyIndex = urlBodyIndex !== null ? Number(urlBodyIndex) : 0;

const seed = new SystemSeed(new Vector3(starMapX, starMapY, starMapZ), index);

const starSystem = new StarSystemController(seed, scene);
starSystemView.setStarSystem(starSystem, true);

engine.getStarMap().setCurrentStarSystem(seed);

document.addEventListener("keydown", (e) => {
if (engine.isPaused()) return;
if (e.key === "g") {
Expand All @@ -99,7 +115,8 @@ document.addEventListener("keydown", (e) => {
engine.init();

const nbRadius = starSystem.model.getBodyTypeOfStar(0) === BODY_TYPE.BLACK_HOLE ? 8 : 3;
positionNearObject(scene.getActiveController(), starSystem.planets.length > 0 ? starSystem.getBodies()[1] : starSystem.stellarObjects[0], starSystem, nbRadius);
if (bodyIndex >= starSystem.getBodies().length) throw new Error(`Body index (${bodyIndex}) out of bound (0 - ${starSystem.getBodies().length - 1})!`);
positionNearObject(scene.getActiveController(), starSystem.planets.length > 0 ? starSystem.getBodies()[bodyIndex] : starSystem.stellarObjects[0], starSystem, nbRadius);

engine.getStarSystemView().bodyEditor.setVisibility(EditorVisibility.NAVBAR);

Expand Down
14 changes: 12 additions & 2 deletions src/ts/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { seededSquirrelNoise } from "squirrel-noise";
import { makeNoise3D } from "fast-simplex-noise";

export const Settings = {
UNIVERSE_SEED: Math.PI,
EARTH_RADIUS: 1000e3, // target is 6000e3
Expand All @@ -20,5 +23,12 @@ export const Settings = {

export const CollisionMask = {
GROUND: 0b00000001,
SPACESHIP: 0b00000010,
}
SPACESHIP: 0b00000010
};

const seedableRNG = seededSquirrelNoise(Settings.UNIVERSE_SEED);
let step = 0;
const perlinRNG = makeNoise3D(() => {
return seedableRNG(step++);
});
export const UniverseDensity = (x: number, y: number, z: number) => (1.0 - Math.abs(perlinRNG(x * 0.2, y * 0.2, z * 0.2))) ** 8;
13 changes: 2 additions & 11 deletions src/ts/spaceship/shipControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ import { Observable } from "@babylonjs/core/Misc/observable";
import { Axis } from "@babylonjs/core/Maths/math.axis";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import { setEnabledBody } from "../utils/havok";
import {
getForwardDirection,
getUpwardDirection,
pitch,
roll,
rotate,
Transformable,
translate
} from "../uberCore/transforms/basicTransform";
import { getForwardDirection, getUpwardDirection, pitch, roll, rotate, Transformable, translate } from "../uberCore/transforms/basicTransform";
import { TransformNode } from "@babylonjs/core/Meshes";
import { Controls } from "../uberCore/controls";
import { Assets } from "../assets";
Expand Down Expand Up @@ -322,14 +314,13 @@ export class ShipControls implements Controls {
console.log(500 * deltaTime * Math.sign(distance), distance);
translate(this.getTransform(), gravityDir.scale(Math.min(500 * deltaTime * Math.sign(distance), distance)));


const currentUp = getUpwardDirection(this.getTransform());
const targetUp = landingSpotNormal;
const axis = Vector3.Cross(currentUp, targetUp);
const theta = Math.acos(Vector3.Dot(currentUp, targetUp));
rotate(this.getTransform(), axis, Math.min(0.1 * deltaTime, theta));

if(Math.abs(distance) < 0.3 && Math.abs(theta) < 0.01) {
if (Math.abs(distance) < 0.3 && Math.abs(theta) < 0.01) {
this.state = ShipState.LANDED;
this.aggregate.body.setMotionType(PhysicsMotionType.STATIC);
}
Expand Down
1 change: 0 additions & 1 deletion src/ts/spaceship/warpDrive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Vector3 } from "@babylonjs/havok";
import { Settings } from "../settings";
import { clamp } from "../utils/math";

Expand Down
3 changes: 2 additions & 1 deletion src/ts/starSystem/starSystemController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Star } from "../stellarObjects/star/star";
import { BlackHole } from "../stellarObjects/blackHole/blackHole";
import { NeutronStar } from "../stellarObjects/neutronStar/neutronStar";
import { ChunkForge } from "../planemos/telluricPlanemo/terrain/chunks/chunkForge";
import { SystemSeed } from "../utils/systemSeed";

export class StarSystemController {
readonly scene: UberScene;
Expand Down Expand Up @@ -73,7 +74,7 @@ export class StarSystemController {

private closestToScreenCenterOrbitalObject: AbstractObject | null = null;

constructor(model: StarSystemModel | number, scene: UberScene) {
constructor(model: StarSystemModel | SystemSeed, scene: UberScene) {
this.scene = scene;
this.postProcessManager = new PostProcessManager(this.scene);

Expand Down
7 changes: 4 additions & 3 deletions src/ts/starSystem/starSystemModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { centeredRand, randRangeInt, uniformRandBool } from "extended-random";
import { Settings } from "../settings";
import { BODY_TYPE } from "../model/common";
import { generateName } from "../utils/nameGenerator";
import { SystemSeed } from "../utils/systemSeed";

enum GENERATION_STEPS {
NAME,
Expand All @@ -14,14 +15,14 @@ enum GENERATION_STEPS {
}

export class StarSystemModel {
readonly seed: number;
readonly seed: SystemSeed;
readonly rng: (step: number) => number;

private name: string;

constructor(seed: number) {
constructor(seed: SystemSeed) {
this.seed = seed;
this.rng = seededSquirrelNoise(this.seed);
this.rng = seededSquirrelNoise(this.seed.hash);

this.name = generateName(this.rng, GENERATION_STEPS.NAME);
}
Expand Down
Loading

0 comments on commit 1c6d6c1

Please sign in to comment.