Skip to content

Commit

Permalink
Merge branch 'WIP'
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Oct 24, 2023
2 parents 9eb6d2b + 5b7a0c1 commit 5e61f05
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/ts/blackHoleDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StarSystem } from "./controller/starSystem";

import { randRange } from "extended-random";
import { Settings } from "./settings";
import { PlayerController } from "./spacelegs/playerController";
import { DefaultController } from "./spacelegs/defaultController";
import { positionNearObject } from "./utils/positionNearObject";
import { SpaceEngine } from "./controller/spaceEngine";
import { ShipController } from "./spaceship/shipController";
Expand All @@ -23,7 +23,7 @@ const mouse = new Mouse(engine.canvas, 100);
const keyboard = new Keyboard();
const gamepad = new Gamepad();

const player = new PlayerController(scene);
const player = new DefaultController(scene);
player.speed = 0.2 * Settings.EARTH_RADIUS;
player.getActiveCamera().maxZ = Settings.EARTH_RADIUS * 100000;
player.addInput(keyboard);
Expand Down
19 changes: 8 additions & 11 deletions src/ts/controller/chunks/chunkTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { PhysicsAggregate } from "@babylonjs/core/Physics/v2/physicsAggregate";
import { TransformNode } from "@babylonjs/core/Meshes";
import { getRotationQuaternion } from "../uberCore/transforms/basicTransform";
import { Camera } from "@babylonjs/core/Cameras/camera";
import { getAngularSize, isSizeOnScreenEnough } from "../../utils/isObjectVisibleOnScreen";

Check warning on line 17 in src/ts/controller/chunks/chunkTree.ts

View workflow job for this annotation

GitHub Actions / build (16)

'getAngularSize' is defined but never used

Check warning on line 17 in src/ts/controller/chunks/chunkTree.ts

View workflow job for this annotation

GitHub Actions / build (18)

'getAngularSize' is defined but never used

/**
* A quadTree is defined recursively
Expand Down Expand Up @@ -136,10 +138,10 @@ export class ChunkTree {

// if view ray goes through planet then we don't need to load more chunks
/*const direction = tree.mesh.getAbsolutePosition().subtract(observerPositionW);
const rayDir = direction.normalizeToNew();
const rayDir = direction.normalizeToNew();
const [intersect, t0, t1] = rayIntersectSphere(observerPositionW, rayDir, this.parent.getAbsolutePosition(), this.rootChunkLength / 2);
if (intersect && t0 ** 2 > direction.lengthSquared()) return tree;*/
const [intersect, t0, t1] = rayIntersectSphere(observerPositionW, rayDir, this.parent.getAbsolutePosition(), this.rootChunkLength / 2);
if (intersect && t0 ** 2 > direction.lengthSquared()) return tree;*/

const newTree = [
this.createChunk(walked.concat([0]), true),
Expand Down Expand Up @@ -196,19 +198,14 @@ export class ChunkTree {
return chunk;
}

public computeCulling(cameraPosition: Vector3): void {
public computeCulling(camera: Camera): void {
this.executeOnEveryChunk((chunk: PlanetChunk) => {
if (!chunk.isReady()) return;

chunk.mesh.setEnabled(true);
chunk.mesh.setEnabled(true); // this is needed to update the world matrix
chunk.transform.computeWorldMatrix(true);

const distance = Vector3.Distance(cameraPosition, chunk.transform.getAbsolutePosition());
const angularSize = (chunk.getBoundingRadius() * 2) / distance;

const chunkIsTooSmall = angularSize / Settings.FOV < 0.002;

chunk.mesh.setEnabled(!chunkIsTooSmall);
chunk.mesh.setEnabled(isSizeOnScreenEnough(chunk, camera));
});
}

Expand Down
43 changes: 35 additions & 8 deletions src/ts/controller/inputs/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,49 @@ export class Mouse implements Input {
deadAreaRadius = 100;
private canvas: HTMLCanvasElement;

private isLeftButtonDown = false;

readonly onMouseEnterObservable: Observable<void> = new Observable();
readonly onMouseLeaveObservable: Observable<void> = new Observable();

constructor(canvas: HTMLCanvasElement, deadAreaRadius = 50) {
this.deadAreaRadius = deadAreaRadius;
this.canvas = canvas;

document.addEventListener("mousemove", (e) => {
this.dx = (e.x - this.x) / this.canvas.width;
this.dy = (e.y - this.y) / this.canvas.height;

this.x = e.x;
this.y = e.y;
document.addEventListener("pointermove", (e) => {
this.dx = e.x - this.x;
this.dy = e.y - this.y;

this.dxToCenter = e.x - this.canvas.width / 2;
this.dyToCenter = e.y - this.canvas.height / 2;
});

document.addEventListener("mouseenter", () => {
window.addEventListener("mouseenter", () => {
this.onMouseEnterObservable.notifyObservers();
});
document.addEventListener("mouseleave", () => {
window.addEventListener("mouseleave", () => {
this.onMouseLeaveObservable.notifyObservers();
});

window.addEventListener("pointerdown", (e) => {
if (e.button === 0) this.isLeftButtonDown = true;
});

window.addEventListener("pointerup", (e) => {
if (e.button === 0) this.isLeftButtonDown = false;
});
}

public isLeftButtonPressed(): boolean {
return this.isLeftButtonDown;
}

public reset() {
this.x += this.dx;
this.y += this.dy;

this.dx = 0;
this.dy = 0;
}

getRoll() {
Expand Down Expand Up @@ -81,7 +100,15 @@ export class Mouse implements Input {
return this.dx;
}

getDxNormalized(): number {
return this.dx / Math.max(this.canvas.width, this.canvas.height);
}

getDy(): number {
return this.dy;
}

getDyNormalized(): number {
return this.dy / Math.max(this.canvas.height, this.canvas.width);
}
}
13 changes: 7 additions & 6 deletions src/ts/controller/starSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export class StarSystem {
}

public makeStar(model: number | StarModel = this.model.getStarSeed(this.stellarObjects.length)): Star {
const star = new Star(`${this.model.getName()} ${this.stellarObjects.length + 1}`, this.scene, model, this.stellarObjects[0]);
const name = `${this.model.getName()} ${this.stellarObjects.length + 1}`;
const star = new Star(name, this.scene, model, this.stellarObjects[0]);
this.addStellarObject(star);
return star;
}
Expand Down Expand Up @@ -384,8 +385,8 @@ export class StarSystem {
const controller = this.scene.getActiveController();

/*const displacementTranslation = controller.aggregate.transformNode.getAbsolutePosition().negate();
this.translateEverythingNow(displacementTranslation);
translate(controller.aggregate.transformNode, displacementTranslation);*/
this.translateEverythingNow(displacementTranslation);
translate(controller.aggregate.transformNode, displacementTranslation);*/

for (const object of this.orbitalObjects) {
object.updateInternalClock(deltaTime);
Expand All @@ -399,8 +400,8 @@ export class StarSystem {
translate(controller.getTransform(), newPosition.subtract(initialPosition));

/*const direction = controller.aggregate.transformNode.getAbsolutePosition().subtract(object.nextState.position).normalize();
const gravity = 9.81;
controller.aggregate.body.applyForce(direction.scale(gravity), controller.aggregate.body.getObjectCenterWorld());*/
const gravity = 9.81;
controller.aggregate.body.applyForce(direction.scale(gravity), controller.aggregate.body.getObjectCenterWorld());*/
}

const dtheta = object.updateRotation(deltaTime);
Expand All @@ -421,7 +422,7 @@ export class StarSystem {

for (const body of this.telluricPlanets.concat(this.satellites)) body.updateLOD(controller.getTransform().getAbsolutePosition());

for (const object of this.orbitalObjects) object.computeCulling(controller.getActiveCamera().getAbsolutePosition());
for (const object of this.orbitalObjects) object.computeCulling(controller.getActiveCamera());

for (const planet of this.planemosWithMaterial) planet.updateMaterial(controller, this.stellarObjects, deltaTime);

Expand Down
4 changes: 2 additions & 2 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { StarSystem } from "./controller/starSystem";

import { Settings } from "./settings";
import { Assets } from "./controller/assets";
import { PlayerController } from "./spacelegs/playerController";
import { DefaultController } from "./spacelegs/defaultController";
import { positionNearObject } from "./utils/positionNearObject";
import { SpaceEngine } from "./controller/spaceEngine";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
Expand Down Expand Up @@ -37,7 +37,7 @@ const mouse = new Mouse(engine.canvas, 100);
const keyboard = new Keyboard();
const gamepad = new Gamepad();

const player = new PlayerController(scene);
const player = new DefaultController(scene);
player.speed = 0.2 * Settings.EARTH_RADIUS;
player.getActiveCamera().maxZ = Settings.EARTH_RADIUS * 100000;
player.addInput(keyboard);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/model/starSystemModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class StarSystemModel {
}

public getBodyTypeOfPlanet(index: number) {
if (uniformRandBool(0.05, this.rng, GENERATION_STEPS.CHOOSE_PLANET_TYPE + (index + 20) * 50)) return BODY_TYPE.MANDELBULB;
if (uniformRandBool(0.01, this.rng, GENERATION_STEPS.CHOOSE_PLANET_TYPE + (index + 20) * 500)) return BODY_TYPE.MANDELBULB;
if (uniformRandBool(0.5, this.rng, GENERATION_STEPS.CHOOSE_PLANET_TYPE + index)) return BODY_TYPE.TELLURIC;
return BODY_TYPE.GAS;
}
Expand Down
13 changes: 10 additions & 3 deletions src/ts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { TelluricPlanemoModel } from "./model/planemos/telluricPlanemoModel";
import { TelluricPlanemo } from "./view/bodies/planemos/telluricPlanemo";
import { UberScene } from "./controller/uberCore/uberScene";
import { Settings } from "./settings";
import { StarModel } from "./model/stellarObjects/starModel";
import { Star } from "./view/bodies/stellarObjects/star";

const canvas = document.getElementById("renderer") as HTMLCanvasElement;
canvas.width = window.innerWidth;
Expand Down Expand Up @@ -87,6 +89,10 @@ capsule.material = Assets.DebugMaterial("capsule", true);
capsule.visibility = 0.5;
shadowGenerator.addShadowCaster(capsule);

const auroraModel = new StarModel(984);
const aurora = new Star("Aurora", scene, auroraModel);
aurora.transform.setAbsolutePosition(new Vector3(0, aurora.getRadius() * 10.0, 0));

const newtonModel = new TelluricPlanemoModel(152);
const newton = new TelluricPlanemo("newton", scene, newtonModel);
newton.transform.setAbsolutePosition(new Vector3(0, -newtonModel.radius - 11.18e3, 0));
Expand Down Expand Up @@ -132,11 +138,12 @@ function updateBeforeHavok() {

// planet thingy
newton.updateInternalClock(-deltaTime / 10);
aurora.updateInternalClock(-deltaTime / 10);
/*newton.updateRotation(deltaTime / 10);
newton.nextState.position = newton.transform.getAbsolutePosition();
newton.applyNextState();*/
newton.nextState.position = newton.transform.getAbsolutePosition();
newton.applyNextState();*/
newton.updateLOD(camera.globalPosition);
//newton.material.update(camera.globalPosition, [light]);
newton.material.update(camera.globalPosition, [aurora]);
Assets.ChunkForge.update();
}

Expand Down
4 changes: 2 additions & 2 deletions src/ts/randomizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StarSystem } from "./controller/starSystem";

import { randRange } from "extended-random";
import { Settings } from "./settings";
import { PlayerController } from "./spacelegs/playerController";
import { DefaultController } from "./spacelegs/defaultController";
import { positionNearObject } from "./utils/positionNearObject";
import { SpaceEngine } from "./controller/spaceEngine";
import { BODY_TYPE } from "./model/common";
Expand All @@ -25,7 +25,7 @@ const mouse = new Mouse(engine.canvas, 100);
const keyboard = new Keyboard();
const gamepad = new Gamepad();

const player = new PlayerController(scene);
const player = new DefaultController(scene);
player.speed = 0.2 * Settings.EARTH_RADIUS;
player.getActiveCamera().maxZ = Settings.EARTH_RADIUS * 100000;
player.addInput(keyboard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { TransformNode } from "@babylonjs/core/Meshes";
import { getForwardDirection, getRightDirection, getUpwardDirection, pitch, roll, translate, yaw } from "../controller/uberCore/transforms/basicTransform";
import { Settings } from "../settings";
import { Mouse } from "../controller/inputs/mouse";

export class PlayerController extends AbstractController {
export class DefaultController extends AbstractController {
private readonly transform: TransformNode;
private readonly camera: UberCamera;

Expand All @@ -33,7 +33,18 @@ export class PlayerController extends AbstractController {
}

protected override listenTo(input: Input, deltaTime: number): Vector3 {
if (input.type !== InputType.KEYBOARD) return Vector3.Zero();
if (input.type === InputType.MOUSE) {
const mouse = input as Mouse;
if (mouse.isLeftButtonPressed()) {
const dx = mouse.getDxNormalized() * 100;
const dy = mouse.getDyNormalized() * 100;

yaw(this.transform, -dx * this.rotationSpeed * deltaTime);
pitch(this.transform, dy * this.rotationSpeed * deltaTime);
}
mouse.reset();
return Vector3.Zero();
}
roll(this.transform, input.getRoll() * this.rotationSpeed * deltaTime);
pitch(this.transform, input.getPitch() * this.rotationSpeed * deltaTime);
yaw(this.transform, input.getYaw() * this.rotationSpeed * deltaTime);
Expand Down
6 changes: 5 additions & 1 deletion src/ts/spaceship/shipController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ShipController extends AbstractController {
this.firstPersonCamera.parent = this.instanceRoot;
this.firstPersonCamera.position = new Vector3(0, 1, 0);

this.thirdPersonCamera = new UberOrbitCamera("thirdPersonCamera", Vector3.Zero(), scene, 30, 3.14, 1.4);
this.thirdPersonCamera = new UberOrbitCamera("thirdPersonCamera", Vector3.Zero(), scene, 30, 3.14, 3.14/2);
this.thirdPersonCamera.parent = this.instanceRoot;

this.aggregate = new PhysicsAggregate(this.instanceRoot, PhysicsShapeType.CONTAINER, { mass: 10, restitution: 0.2 }, scene);
Expand Down Expand Up @@ -195,6 +195,8 @@ export class ShipController extends AbstractController {

rcsThruster.setThrottle(throttle);
}

mouse.reset();
}
} else {
if (input.type === InputType.MOUSE) {
Expand All @@ -204,6 +206,8 @@ export class ShipController extends AbstractController {

roll(this.aggregate.transformNode, rollContribution * deltaTime);
pitch(this.aggregate.transformNode, pitchContribution * deltaTime);

mouse.reset();
}

if (input.type === InputType.KEYBOARD) {
Expand Down
15 changes: 7 additions & 8 deletions src/ts/starmap/starMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlayerController } from "../spacelegs/playerController";
import { DefaultController } from "../spacelegs/defaultController";
import { Keyboard } from "../controller/inputs/keyboard";

import starTexture from "../../asset/textures/starParticle.png";
Expand Down Expand Up @@ -34,10 +34,11 @@ import { Settings } from "../settings";
import { getForwardDirection, translate } from "../controller/uberCore/transforms/basicTransform";
import { ThickLines } from "../utils/thickLines";
import { Observable } from "@babylonjs/core/Misc/observable";
import { Mouse } from "../controller/inputs/mouse";

export class StarMap {
readonly scene: Scene;
private readonly controller: PlayerController;
private readonly controller: DefaultController;

private isRunning = true;

Expand All @@ -60,7 +61,6 @@ export class StarMap {
private readonly recycledBlackHoles: InstancedMesh[] = [];

static readonly GENERATION_CADENCE = 100;
static readonly DELETION_CADENCE = 100;

static readonly RENDER_RADIUS = 6;

Expand Down Expand Up @@ -102,12 +102,13 @@ export class StarMap {
this.scene.skipPointerMovePicking = false;
this.scene.useRightHandedSystem = true;

this.controller = new PlayerController(this.scene);
this.controller = new DefaultController(this.scene);
this.controller.speed /= 10;
this.controller.getActiveCamera().minZ = 0.01;

this.scene.activeCamera = this.controller.getActiveCamera();
this.controller.addInput(new Keyboard());
this.controller.addInput(new Mouse(engine.getRenderingCanvas() as HTMLCanvasElement, 0));

this.starMapUI = new StarMapUI(this.scene);

Expand Down Expand Up @@ -221,27 +222,25 @@ export class StarMap {
this.densityRNG = (x: number, y: number, z: number) => (1.0 - Math.abs(perlinRNG(x * 0.2, y * 0.2, z * 0.2))) ** 8;

this.scene.onBeforeRenderObservable.add(() => {
if(!this.isRunning) return;
if (!this.isRunning) return;

const deltaTime = this.scene.getEngine().getDeltaTime() / 1000;

if (this.rotationAnimation !== null) this.rotationAnimation.update(deltaTime);

const playerDisplacementNegated = this.controller.update(deltaTime).negate();

this.controller.getTransform().position = Vector3.Zero();

if (this.translationAnimation !== null) {
const oldPosition = this.controller.getTransform().getAbsolutePosition().clone();
this.translationAnimation.update(deltaTime);
const newPosition = this.controller.getTransform().getAbsolutePosition().clone();

const displacementNegated = oldPosition.subtractInPlace(newPosition);

translate(this.controller.getTransform(), displacementNegated);
playerDisplacementNegated.addInPlace(displacementNegated);
}

translate(this.controller.getTransform(), playerDisplacementNegated);
this.starMapCenterPosition.addInPlace(playerDisplacementNegated);
for (const mesh of this.scene.meshes) mesh.position.addInPlace(playerDisplacementNegated);

Expand Down
Loading

0 comments on commit 5e61f05

Please sign in to comment.