From db4ac0b72bb772d5f94d490e26c14b237f0015df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:00:57 +0200 Subject: [PATCH 01/11] fixed rings shadows not displaying --- src/ts/view/postProcesses/shadowPostProcess.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ts/view/postProcesses/shadowPostProcess.ts b/src/ts/view/postProcesses/shadowPostProcess.ts index cfb2c4c1e..c76d76ff8 100644 --- a/src/ts/view/postProcesses/shadowPostProcess.ts +++ b/src/ts/view/postProcesses/shadowPostProcess.ts @@ -34,21 +34,21 @@ export class ShadowPostProcess extends UberPostProcess implements ObjectPostProc ...getStellarObjectsUniforms(stellarObjects), ...getActiveCameraUniforms(scene), { - name: "shadow.hasRings", + name: "shadowUniforms.hasRings", type: UniformEnumType.Bool, get: () => { return shadowUniforms.hasRings; } }, { - name: "shadow.hasClouds", + name: "shadowUniforms.hasClouds", type: UniformEnumType.Bool, get: () => { return shadowUniforms.hasClouds; } }, { - name: "shadow.hasOcean", + name: "shadowUniforms.hasOcean", type: UniformEnumType.Bool, get: () => { return shadowUniforms.hasOcean; From e129889e675a976cdc3d7ed8a7339da6c5453cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:15:35 +0200 Subject: [PATCH 02/11] camera is aligned with ship for ease of use --- src/ts/spaceship/shipController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ts/spaceship/shipController.ts b/src/ts/spaceship/shipController.ts index 10b91d3a1..aa1e10fb9 100644 --- a/src/ts/spaceship/shipController.ts +++ b/src/ts/spaceship/shipController.ts @@ -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); From bf81bc1fbd30934eb9f657dbc71baca8384ea02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:16:08 +0200 Subject: [PATCH 03/11] star has physics aggregate --- src/ts/view/bodies/planemos/telluricPlanemo.ts | 4 ++-- src/ts/view/bodies/stellarObjects/star.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ts/view/bodies/planemos/telluricPlanemo.ts b/src/ts/view/bodies/planemos/telluricPlanemo.ts index 425d452a6..dadf82183 100644 --- a/src/ts/view/bodies/planemos/telluricPlanemo.ts +++ b/src/ts/view/bodies/planemos/telluricPlanemo.ts @@ -64,12 +64,12 @@ export class TelluricPlanemo extends AbstractBody implements Planemo, PlanemoMat this.transform, PhysicsShapeType.CONTAINER, { - mass: 1e10, + mass: 0, restitution: 0.2 }, scene ); - this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 1e10 }); + this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 0 }); this.aggregate.body.disablePreStep = false; this.sides = [ diff --git a/src/ts/view/bodies/stellarObjects/star.ts b/src/ts/view/bodies/stellarObjects/star.ts index 8318e4ff9..15c3b7099 100644 --- a/src/ts/view/bodies/stellarObjects/star.ts +++ b/src/ts/view/bodies/stellarObjects/star.ts @@ -12,6 +12,8 @@ import { PostProcessType } from "../../postProcesses/postProcessTypes"; import { Mesh } from "@babylonjs/core/Meshes/mesh"; import { Assets } from "../../../controller/assets"; import { setRotationQuaternion } from "../../../controller/uberCore/transforms/basicTransform"; +import { PhysicsAggregate } from "@babylonjs/core/Physics/v2/physicsAggregate"; +import { PhysicsShapeType } from "@babylonjs/core"; export class Star extends AbstractBody { readonly mesh: Mesh; @@ -20,6 +22,8 @@ export class Star extends AbstractBody { readonly model: StarModel; + readonly aggregate: PhysicsAggregate; + /** * New Star * @param name The name of the star @@ -45,6 +49,10 @@ export class Star extends AbstractBody { : Assets.CreateBananaClone(this.model.radius * 2); this.mesh.parent = this.transform; + this.aggregate = new PhysicsAggregate(this.mesh, PhysicsShapeType.SPHERE); + this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 0 }); + this.aggregate.body.disablePreStep = false; + this.light = new PointLight(`${name}Light`, Vector3.Zero(), scene); this.light.diffuse.fromArray(getRgbFromTemperature(this.model.physicalProperties.temperature).asArray()); this.light.falloffType = Light.FALLOFF_STANDARD; From e40a94cb0bec246bbc385965d51b3d69d477a5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:46:51 +0200 Subject: [PATCH 04/11] fixed playground lighting --- src/ts/playground.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ts/playground.ts b/src/ts/playground.ts index 05980167d..73e9fe48e 100644 --- a/src/ts/playground.ts +++ b/src/ts/playground.ts @@ -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; @@ -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)); @@ -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(); } From 4394e8d56bb2fd6aef9535f5a4a351b84aca005d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:18:26 +0200 Subject: [PATCH 05/11] added basic mouse control for default controller player controller is now called default controller --- src/ts/blackHoleDemo.ts | 4 +-- src/ts/controller/inputs/mouse.ts | 26 +++++++++++++++++-- src/ts/index.ts | 4 +-- src/ts/randomizer.ts | 4 +-- ...ayerController.ts => defaultController.ts} | 18 ++++++++++--- src/ts/starmap/starMap.ts | 9 ++++--- 6 files changed, 50 insertions(+), 15 deletions(-) rename src/ts/spacelegs/{playerController.ts => defaultController.ts} (81%) diff --git a/src/ts/blackHoleDemo.ts b/src/ts/blackHoleDemo.ts index 016a5c619..b7705173f 100644 --- a/src/ts/blackHoleDemo.ts +++ b/src/ts/blackHoleDemo.ts @@ -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"; @@ -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); diff --git a/src/ts/controller/inputs/mouse.ts b/src/ts/controller/inputs/mouse.ts index e76203ebd..c0c4c6ade 100644 --- a/src/ts/controller/inputs/mouse.ts +++ b/src/ts/controller/inputs/mouse.ts @@ -17,6 +17,8 @@ export class Mouse implements Input { deadAreaRadius = 100; private canvas: HTMLCanvasElement; + private isLeftButtonDown = false; + readonly onMouseEnterObservable: Observable = new Observable(); readonly onMouseLeaveObservable: Observable = new Observable(); @@ -25,8 +27,8 @@ export class Mouse implements Input { 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.dx = (e.x - this.x); + this.dy = (e.y - this.y); this.x = e.x; this.y = e.y; @@ -41,6 +43,18 @@ export class Mouse implements Input { document.addEventListener("mouseleave", () => { this.onMouseLeaveObservable.notifyObservers(); }); + + document.addEventListener("mousedown", (e) => { + if (e.button === 0) this.isLeftButtonDown = true; + }); + + document.addEventListener("mouseup", (e) => { + if (e.button === 0) this.isLeftButtonDown = false; + }); + } + + public isLeftButtonPressed(): boolean { + return this.isLeftButtonDown; } getRoll() { @@ -81,7 +95,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); + } } diff --git a/src/ts/index.ts b/src/ts/index.ts index d5b8e257d..69b7185b1 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -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"; @@ -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); diff --git a/src/ts/randomizer.ts b/src/ts/randomizer.ts index 01d387784..4875db933 100644 --- a/src/ts/randomizer.ts +++ b/src/ts/randomizer.ts @@ -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"; @@ -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); diff --git a/src/ts/spacelegs/playerController.ts b/src/ts/spacelegs/defaultController.ts similarity index 81% rename from src/ts/spacelegs/playerController.ts rename to src/ts/spacelegs/defaultController.ts index 2475ba323..3584fc8f4 100644 --- a/src/ts/spacelegs/playerController.ts +++ b/src/ts/spacelegs/defaultController.ts @@ -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; @@ -33,7 +33,19 @@ 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() * 70; + const dy = mouse.getDyNormalized() * 70; + + console.log(dx, dy); + + yaw(this.transform, -dx * this.rotationSpeed * deltaTime); + pitch(this.transform, dy * this.rotationSpeed * deltaTime); + } + 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); diff --git a/src/ts/starmap/starMap.ts b/src/ts/starmap/starMap.ts index f3d4eab02..ed73bc48a 100644 --- a/src/ts/starmap/starMap.ts +++ b/src/ts/starmap/starMap.ts @@ -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"; @@ -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; @@ -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; @@ -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); From 7e47920c12ce35632b8cb4986c1c7d4ed4c2ed43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:18:44 +0200 Subject: [PATCH 06/11] linting --- src/ts/controller/starSystem.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ts/controller/starSystem.ts b/src/ts/controller/starSystem.ts index 3272dad34..36a725162 100644 --- a/src/ts/controller/starSystem.ts +++ b/src/ts/controller/starSystem.ts @@ -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; } @@ -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); @@ -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); From ca3276ce333290e51e3b07bf9a99f74635652f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:18:57 +0200 Subject: [PATCH 07/11] working on lensflare occlusion --- .../postProcesses/lensFlarePostProcess.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/ts/view/postProcesses/lensFlarePostProcess.ts b/src/ts/view/postProcesses/lensFlarePostProcess.ts index 7150819cc..f233aced2 100644 --- a/src/ts/view/postProcesses/lensFlarePostProcess.ts +++ b/src/ts/view/postProcesses/lensFlarePostProcess.ts @@ -8,6 +8,8 @@ import { ShaderSamplers, ShaderUniforms, UniformEnumType } from "../../controlle import { StellarObject } from "../bodies/stellarObjects/stellarObject"; import { Star } from "../bodies/stellarObjects/star"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; +import { PhysicsEngineV2, PhysicsRaycastResult, Ray, RayHelper } from "@babylonjs/core"; +import { Color3 } from "@babylonjs/core/Maths/math.color"; const shaderName = "lensflare"; Effect.ShadersStore[`${shaderName}FragmentShader`] = lensFlareFragment; @@ -34,6 +36,29 @@ export class LensFlarePostProcess extends UberPostProcess implements ObjectPostP else return new Vector3(1, 1, 1); } }, + /*{ + name: "occulted", + type: UniformEnumType.Bool, + get: () => { + // send raycast from camera to object and check early intersections + const raycastResult = new PhysicsRaycastResult(); + const start = scene.getActiveUberCamera().getAbsolutePosition(); + const end = object.transform.getAbsolutePosition(); + (scene.getPhysicsEngine() as PhysicsEngineV2).raycastToRef(start, end, raycastResult); + if (raycastResult.hasHit) { + //console.log(Vector3.Distance(raycastResult.body!.getObjectCenterWorld(), object.transform.getAbsolutePosition())); + //console.log(raycastResult.body?.transformNode.name); + + const ray1 = new Ray(start, end.subtract(start).normalize(), Vector3.Distance(start, end)); + const ray1Helper = new RayHelper(ray1); + ray1Helper.show(scene, new Color3(1, 1, 0)); + + return true; + } + + return false; + } + },*/ { name: "aspectRatio", type: UniformEnumType.Float, From c12066215f694bf557f631e24a769a35d2c707c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Mon, 23 Oct 2023 20:24:54 +0200 Subject: [PATCH 08/11] removed star aggregate because bug - The physics engine is confused about its position, this will be fixed when i introduce hybrid floating origin --- src/ts/view/bodies/stellarObjects/star.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ts/view/bodies/stellarObjects/star.ts b/src/ts/view/bodies/stellarObjects/star.ts index 15c3b7099..088e1e09a 100644 --- a/src/ts/view/bodies/stellarObjects/star.ts +++ b/src/ts/view/bodies/stellarObjects/star.ts @@ -22,7 +22,7 @@ export class Star extends AbstractBody { readonly model: StarModel; - readonly aggregate: PhysicsAggregate; + //readonly aggregate: PhysicsAggregate; /** * New Star @@ -49,9 +49,9 @@ export class Star extends AbstractBody { : Assets.CreateBananaClone(this.model.radius * 2); this.mesh.parent = this.transform; - this.aggregate = new PhysicsAggregate(this.mesh, PhysicsShapeType.SPHERE); + /*this.aggregate = new PhysicsAggregate(this.mesh, PhysicsShapeType.SPHERE); this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 0 }); - this.aggregate.body.disablePreStep = false; + this.aggregate.body.disablePreStep = false;*/ this.light = new PointLight(`${name}Light`, Vector3.Zero(), scene); this.light.diffuse.fromArray(getRgbFromTemperature(this.model.physicalProperties.temperature).asArray()); From 7d1a61a3af76d7020f39e230de9fa1e051a18d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:37:11 +0200 Subject: [PATCH 09/11] fixed mouse control in starmap --- src/ts/controller/inputs/mouse.ts | 25 +++++++++++++++---------- src/ts/spacelegs/defaultController.ts | 7 +++---- src/ts/spaceship/shipController.ts | 4 ++++ src/ts/starmap/starMap.ts | 6 ++---- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/ts/controller/inputs/mouse.ts b/src/ts/controller/inputs/mouse.ts index c0c4c6ade..09b2153f7 100644 --- a/src/ts/controller/inputs/mouse.ts +++ b/src/ts/controller/inputs/mouse.ts @@ -26,29 +26,26 @@ export class Mouse implements Input { this.deadAreaRadius = deadAreaRadius; this.canvas = canvas; - document.addEventListener("mousemove", (e) => { - this.dx = (e.x - this.x); - this.dy = (e.y - this.y); - - 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(); }); - document.addEventListener("mousedown", (e) => { + window.addEventListener("pointerdown", (e) => { if (e.button === 0) this.isLeftButtonDown = true; }); - document.addEventListener("mouseup", (e) => { + window.addEventListener("pointerup", (e) => { if (e.button === 0) this.isLeftButtonDown = false; }); } @@ -57,6 +54,14 @@ export class Mouse implements Input { return this.isLeftButtonDown; } + public reset() { + this.x += this.dx; + this.y += this.dy; + + this.dx = 0; + this.dy = 0; + } + getRoll() { const d2 = this.dxToCenter ** 2 + this.dyToCenter ** 2; const adaptedLength = Math.max(Math.log(d2 / this.deadAreaRadius ** 2), 0) / 3; diff --git a/src/ts/spacelegs/defaultController.ts b/src/ts/spacelegs/defaultController.ts index 3584fc8f4..cc22c4bec 100644 --- a/src/ts/spacelegs/defaultController.ts +++ b/src/ts/spacelegs/defaultController.ts @@ -36,14 +36,13 @@ export class DefaultController extends AbstractController { if (input.type === InputType.MOUSE) { const mouse = input as Mouse; if (mouse.isLeftButtonPressed()) { - const dx = mouse.getDxNormalized() * 70; - const dy = mouse.getDyNormalized() * 70; - - console.log(dx, dy); + 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); diff --git a/src/ts/spaceship/shipController.ts b/src/ts/spaceship/shipController.ts index aa1e10fb9..d1141caa5 100644 --- a/src/ts/spaceship/shipController.ts +++ b/src/ts/spaceship/shipController.ts @@ -195,6 +195,8 @@ export class ShipController extends AbstractController { rcsThruster.setThrottle(throttle); } + + mouse.reset(); } } else { if (input.type === InputType.MOUSE) { @@ -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) { diff --git a/src/ts/starmap/starMap.ts b/src/ts/starmap/starMap.ts index ed73bc48a..4ac411d0a 100644 --- a/src/ts/starmap/starMap.ts +++ b/src/ts/starmap/starMap.ts @@ -222,7 +222,7 @@ 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; @@ -230,8 +230,6 @@ export class StarMap { 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); @@ -239,10 +237,10 @@ export class StarMap { 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); From f2d137788013186e98bb7527dfe5b23a8ba7fd33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:37:18 +0200 Subject: [PATCH 10/11] less mandelbulbs --- src/ts/model/starSystemModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ts/model/starSystemModel.ts b/src/ts/model/starSystemModel.ts index 266e7369b..5dae9781b 100644 --- a/src/ts/model/starSystemModel.ts +++ b/src/ts/model/starSystemModel.ts @@ -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; } From 5b7a0c1ec6b973cbe19d16e35c622fb7d80d4265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:59:08 +0200 Subject: [PATCH 11/11] better angular size calculation --- src/ts/controller/chunks/chunkTree.ts | 19 ++++++-------- src/ts/controller/starSystem.ts | 2 +- src/ts/utils/isObjectVisibleOnScreen.ts | 25 +++++++++++++------ src/ts/view/bodies/abstractObject.ts | 3 ++- src/ts/view/bodies/cullable.ts | 4 +-- src/ts/view/bodies/planemos/gasPlanet.ts | 5 ++-- src/ts/view/bodies/planemos/mandelbulb.ts | 4 +-- .../view/bodies/planemos/telluricPlanemo.ts | 5 ++-- .../view/bodies/stellarObjects/blackHole.ts | 3 ++- src/ts/view/bodies/stellarObjects/star.ts | 5 ++-- src/ts/view/spaceStation.ts | 5 ++-- 11 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/ts/controller/chunks/chunkTree.ts b/src/ts/controller/chunks/chunkTree.ts index e16fab765..0d29114f8 100644 --- a/src/ts/controller/chunks/chunkTree.ts +++ b/src/ts/controller/chunks/chunkTree.ts @@ -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"; /** * A quadTree is defined recursively @@ -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), @@ -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)); }); } diff --git a/src/ts/controller/starSystem.ts b/src/ts/controller/starSystem.ts index 36a725162..a1e757fdf 100644 --- a/src/ts/controller/starSystem.ts +++ b/src/ts/controller/starSystem.ts @@ -422,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); diff --git a/src/ts/utils/isObjectVisibleOnScreen.ts b/src/ts/utils/isObjectVisibleOnScreen.ts index 7952946e3..3a52bfecc 100644 --- a/src/ts/utils/isObjectVisibleOnScreen.ts +++ b/src/ts/utils/isObjectVisibleOnScreen.ts @@ -1,17 +1,28 @@ import { Vector3 } from "@babylonjs/core/Maths/math"; -import { BaseObject } from "../view/common"; -import { Settings } from "../settings"; +import { BoundingSphere, ITransformable } from "../view/common"; +import { Camera } from "@babylonjs/core/Cameras/camera"; + +/** + * Computes the angular size in radians of an object viewed by a camera + * @param objectPosition + * @param objectRadius + * @param cameraPosition The position of the observer camera + * @see https://en.wikipedia.org/wiki/Angular_diameter + */ +export function getAngularSize(objectPosition: Vector3, objectRadius: number, cameraPosition: Vector3) { + const distance = Vector3.Distance(cameraPosition, objectPosition); + return 2 * Math.atan(objectRadius / distance); +} /** * Checks if the size of the object on the screen is bigger than the threshold * @param object The object to check - * @param cameraPosition The position of the camera + * @param camera The camera looking at the object * @param threshold The size threshold * @returns Whether the object is bigger than the threshold */ -export function isSizeOnScreenEnough(object: BaseObject, cameraPosition: Vector3, threshold = 0.002) { - const distance = Vector3.Distance(cameraPosition, object.transform.getAbsolutePosition()); - const angularSize = (object.getBoundingRadius() * 2) / distance; +export function isSizeOnScreenEnough(object: BoundingSphere & ITransformable, camera: Camera, threshold = 0.002) { + const angularSize = getAngularSize(object.transform.getAbsolutePosition(), object.getBoundingRadius(), camera.globalPosition); - return angularSize / Settings.FOV > threshold; + return angularSize / camera.fov > threshold; } diff --git a/src/ts/view/bodies/abstractObject.ts b/src/ts/view/bodies/abstractObject.ts index ad57f2e6e..26782b676 100644 --- a/src/ts/view/bodies/abstractObject.ts +++ b/src/ts/view/bodies/abstractObject.ts @@ -7,6 +7,7 @@ import { PostProcessType } from "../postProcesses/postProcessTypes"; import { Cullable } from "./cullable"; import { TransformNode } from "@babylonjs/core/Meshes"; import { getRotationQuaternion, setRotationQuaternion } from "../../controller/uberCore/transforms/basicTransform"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export interface NextState { position: Vector3; @@ -128,7 +129,7 @@ export abstract class AbstractObject implements OrbitalObject, BaseObject, Culla setRotationQuaternion(this.transform, this.nextState.rotation); } - public abstract computeCulling(cameraPosition: Vector3): void; + public abstract computeCulling(camera: Camera): void; public dispose(): void { this.transform.dispose(); diff --git a/src/ts/view/bodies/cullable.ts b/src/ts/view/bodies/cullable.ts index 65913433e..6f0d314e3 100644 --- a/src/ts/view/bodies/cullable.ts +++ b/src/ts/view/bodies/cullable.ts @@ -1,5 +1,5 @@ -import { Vector3 } from "@babylonjs/core/Maths/math"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export interface Cullable { - computeCulling(cameraPosition: Vector3): void; + computeCulling(camera: Camera): void; } diff --git a/src/ts/view/bodies/planemos/gasPlanet.ts b/src/ts/view/bodies/planemos/gasPlanet.ts index ad2977f1c..407cda20e 100644 --- a/src/ts/view/bodies/planemos/gasPlanet.ts +++ b/src/ts/view/bodies/planemos/gasPlanet.ts @@ -11,6 +11,7 @@ import { Mesh } from "@babylonjs/core/Meshes/mesh"; import { PostProcessType } from "../../postProcesses/postProcessTypes"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { isSizeOnScreenEnough } from "../../../utils/isObjectVisibleOnScreen"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class GasPlanet extends AbstractBody implements Planemo, PlanemoMaterial { private readonly mesh: Mesh; @@ -53,8 +54,8 @@ export class GasPlanet extends AbstractBody implements Planemo, PlanemoMaterial this.material.update(controller, stellarObjects, deltaTime); } - public override computeCulling(cameraPosition: Vector3): void { - this.mesh.isVisible = isSizeOnScreenEnough(this, cameraPosition); + public override computeCulling(camera: Camera): void { + this.mesh.isVisible = isSizeOnScreenEnough(this, camera); } public override dispose(): void { diff --git a/src/ts/view/bodies/planemos/mandelbulb.ts b/src/ts/view/bodies/planemos/mandelbulb.ts index a4f351642..9aea3597d 100644 --- a/src/ts/view/bodies/planemos/mandelbulb.ts +++ b/src/ts/view/bodies/planemos/mandelbulb.ts @@ -3,8 +3,8 @@ import { UberScene } from "../../../controller/uberCore/uberScene"; import { Planemo } from "./planemo"; import { Axis } from "@babylonjs/core/Maths/math.axis"; import { PostProcessType } from "../../postProcesses/postProcessTypes"; -import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { MandelbulbModel } from "../../../model/planemos/mandelbulbModel"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class Mandelbulb extends AbstractBody implements Planemo { readonly model: MandelbulbModel; @@ -26,7 +26,7 @@ export class Mandelbulb extends AbstractBody implements Planemo { this.transform.rotate(Axis.X, this.model.physicalProperties.axialTilt); } - public override computeCulling(cameraPosition: Vector3): void { + public override computeCulling(camera: Camera): void { // do nothing } } diff --git a/src/ts/view/bodies/planemos/telluricPlanemo.ts b/src/ts/view/bodies/planemos/telluricPlanemo.ts index dadf82183..f29da8ed0 100644 --- a/src/ts/view/bodies/planemos/telluricPlanemo.ts +++ b/src/ts/view/bodies/planemos/telluricPlanemo.ts @@ -15,6 +15,7 @@ import { StellarObject } from "../stellarObjects/stellarObject"; import { PostProcessType } from "../../postProcesses/postProcessTypes"; import { PhysicsAggregate } from "@babylonjs/core/Physics/v2/physicsAggregate"; import { PhysicsShapeType } from "@babylonjs/core/Physics/v2/IPhysicsEnginePlugin"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class TelluricPlanemo extends AbstractBody implements Planemo, PlanemoMaterial { readonly sides: ChunkTree[] = new Array(6); // stores the 6 sides of the sphere @@ -105,8 +106,8 @@ export class TelluricPlanemo extends AbstractBody implements Planemo, PlanemoMat return super.getRadius() + this.model.physicalProperties.oceanLevel; } - public override computeCulling(cameraPosition: Vector3): void { - for (const side of this.sides) side.computeCulling(cameraPosition); + public override computeCulling(camera: Camera): void { + for (const side of this.sides) side.computeCulling(camera); } public override dispose(): void { diff --git a/src/ts/view/bodies/stellarObjects/blackHole.ts b/src/ts/view/bodies/stellarObjects/blackHole.ts index 1add82473..45997a6b0 100644 --- a/src/ts/view/bodies/stellarObjects/blackHole.ts +++ b/src/ts/view/bodies/stellarObjects/blackHole.ts @@ -6,6 +6,7 @@ import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { Scene } from "@babylonjs/core/scene"; import { Light } from "@babylonjs/core/Lights/light"; import { PostProcessType } from "../../postProcesses/postProcessTypes"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class BlackHole extends AbstractBody { readonly light: PointLight; @@ -28,7 +29,7 @@ export class BlackHole extends AbstractBody { this.postProcesses.push(PostProcessType.OVERLAY, PostProcessType.BLACK_HOLE); } - public override computeCulling(cameraPosition: Vector3): void { + public override computeCulling(camera: Camera): void { // nothing to do } diff --git a/src/ts/view/bodies/stellarObjects/star.ts b/src/ts/view/bodies/stellarObjects/star.ts index 088e1e09a..f112ce37f 100644 --- a/src/ts/view/bodies/stellarObjects/star.ts +++ b/src/ts/view/bodies/stellarObjects/star.ts @@ -14,6 +14,7 @@ import { Assets } from "../../../controller/assets"; import { setRotationQuaternion } from "../../../controller/uberCore/transforms/basicTransform"; import { PhysicsAggregate } from "@babylonjs/core/Physics/v2/physicsAggregate"; import { PhysicsShapeType } from "@babylonjs/core"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class Star extends AbstractBody { readonly mesh: Mesh; @@ -72,8 +73,8 @@ export class Star extends AbstractBody { this.material.update(this.getInternalClock()); } - public override computeCulling(cameraPosition: Vector3): void { - this.mesh.isVisible = true; + public override computeCulling(camera: Camera): void { + //this.mesh.isVisible = true; } public override dispose(): void { diff --git a/src/ts/view/spaceStation.ts b/src/ts/view/spaceStation.ts index 5e47544f9..874b382a0 100644 --- a/src/ts/view/spaceStation.ts +++ b/src/ts/view/spaceStation.ts @@ -7,6 +7,7 @@ import { Axis } from "@babylonjs/core/Maths/math.axis"; import { PostProcessType } from "./postProcesses/postProcessTypes"; import { Vector3 } from "@babylonjs/core/Maths/math"; import { isSizeOnScreenEnough } from "../utils/isObjectVisibleOnScreen"; +import { Camera } from "@babylonjs/core/Cameras/camera"; export class SpaceStation extends AbstractObject { readonly model: SpaceStationModel; @@ -43,8 +44,8 @@ export class SpaceStation extends AbstractObject { return 2e3; } - public override computeCulling(cameraPosition: Vector3): void { - const isVisible = isSizeOnScreenEnough(this, cameraPosition); + public override computeCulling(camera: Camera): void { + const isVisible = isSizeOnScreenEnough(this, camera); for (const mesh of this.instance.getChildMeshes()) { mesh.isVisible = isVisible; }