Skip to content

Commit

Permalink
Merge pull request #301 from BarthPaleologue/refactor-playground
Browse files Browse the repository at this point in the history
Move some webpack endpoints to the playground
  • Loading branch information
BarthPaleologue authored Feb 8, 2025
2 parents 607af11 + c090db2 commit cee50eb
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 572 deletions.
2 changes: 1 addition & 1 deletion src/ts/assets/procedural/landingPad/landingPadMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class LandingPadMaterial extends ShaderMaterial {
samplers: [...Object.values(LandingPadSamplerNames)]
});

const numberTexture = Textures.LANDING_PAD_NUMBER_TEXTURES.at(padNumber);
const numberTexture = Textures.GetLandingPadNumberTexture(padNumber, scene);
if (numberTexture === undefined) {
throw new Error(`No texture for pad number ${padNumber}`);
}
Expand Down
43 changes: 25 additions & 18 deletions src/ts/assets/textures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class Textures {

static MILKY_WAY: CubeTexture;

static LANDING_PAD_NUMBER_TEXTURES: DynamicTexture[] = [];
private static LANDING_PAD_NUMBER_TEXTURES: Map<number, DynamicTexture> = new Map();

static EnqueueTasks(manager: AssetsManager, scene: Scene) {

Check warning on line 128 in src/ts/assets/textures.ts

View workflow job for this annotation

GitHub Actions / eslint (20)

'scene' is defined but never used
manager.addTextureTask("RockNormalMetallicMap", rockNormalMetallicMap).onSuccess = (task) =>
Expand Down Expand Up @@ -204,24 +204,31 @@ export class Textures {
this.CURSOR_IMAGE_URL = cursorImage;

manager.addTextureTask("EmptyTexture", empty).onSuccess = (task) => (Textures.EMPTY_TEXTURE = task.texture);
}

const padNumberTextureResolution = 1024;
for (let i = 0; i < 100; i++) {
const numberTexture = new DynamicTexture(
`PadNumberTexture${i}`,
{
width: padNumberTextureResolution,
height: padNumberTextureResolution * Settings.LANDING_PAD_ASPECT_RATIO
},
scene,
true
);

//Add text to dynamic texture
const font = `bold 256px ${Settings.MAIN_FONT}`;
numberTexture.drawText(`${i}`, null, null, font, "white", null, true, true);

Textures.LANDING_PAD_NUMBER_TEXTURES.push(numberTexture);
static GetLandingPadNumberTexture(padNumber: number, scene: Scene): DynamicTexture {
const texture = Textures.LANDING_PAD_NUMBER_TEXTURES.get(padNumber);
if (texture !== undefined) {
return texture;
}

const padNumberTextureResolution = 1024;
const numberTexture = new DynamicTexture(
`PadNumberTexture${padNumber}`,
{
width: padNumberTextureResolution,
height: padNumberTextureResolution * Settings.LANDING_PAD_ASPECT_RATIO
},
scene,
true
);

//Add text to dynamic texture
const font = `bold 256px ${Settings.MAIN_FONT}`;
numberTexture.drawText(`${padNumber}`, null, null, font, "white", null, true, true);

Textures.LANDING_PAD_NUMBER_TEXTURES.set(padNumber, numberTexture);

return numberTexture;
}
}
15 changes: 11 additions & 4 deletions src/ts/cosmosJourneyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,18 @@ export class CosmosJourneyer {
);
}

const landingPad = correspondingSpaceStation.getLandingPads().at(padNumber);
const wantedLandingPad = correspondingSpaceStation.getLandingPads().at(padNumber);

const isWantedPadFree =
wantedLandingPad !== undefined &&
correspondingSpaceStation.getAvailableLandingPads().includes(wantedLandingPad);

const landingPad = isWantedPadFree
? wantedLandingPad
: correspondingSpaceStation.getAvailableLandingPads().at(0);

if (landingPad === undefined) {
throw new Error(
`Could not find the pad with number ${padNumber} at this station: ${correspondingSpaceStation.model.name}`
);
throw new Error("The spacestation you are trying to spawn at is full. This should not happen.");
}

this.starSystemView.getSpaceshipControls().getSpaceship().spawnOnPad(landingPad);
Expand Down
107 changes: 0 additions & 107 deletions src/ts/debugAssets.ts

This file was deleted.

34 changes: 32 additions & 2 deletions src/ts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import "@babylonjs/core/Materials/standardMaterial";
import "@babylonjs/core/Loading/loadingScreen";
import "@babylonjs/core/Misc/screenshotTools";
import "@babylonjs/core/Meshes/thinInstanceMesh";
import { Tools } from "@babylonjs/core";
import { Scene, Tools } from "@babylonjs/core";
import { createOrbitalDemoScene } from "./playgrounds/orbitalDemo";
import { createAutomaticLandingScene } from "./playgrounds/automaticLanding";
import { createHyperspaceTunnelDemo } from "./playgrounds/hyperspaceTunnel";
import { createDebugAssetsScene } from "./playgrounds/debugAssets";
import { createSpaceStationScene } from "./playgrounds/spaceStation";
import { createXrScene } from "./playgrounds/xr";

const canvas = document.getElementById("renderer") as HTMLCanvasElement;
canvas.width = window.innerWidth;
Expand All @@ -33,7 +38,32 @@ const engine = new Engine(canvas, true);
engine.useReverseDepthBuffer = true;
engine.displayLoadingUI();

const scene = createOrbitalDemoScene(engine);
const urlParams = new URLSearchParams(window.location.search);
const requestedScene = urlParams.get("scene") ?? "";

let scene: Scene;
switch (requestedScene) {
case "orbitalDemo":
scene = createOrbitalDemoScene(engine);
break;
case "tunnel":
scene = await createHyperspaceTunnelDemo(engine);
break;
case "automaticLanding":
scene = await createAutomaticLandingScene(engine);
break;
case "debugAssets":
scene = await createDebugAssetsScene(engine);
break;
case "spaceStation":
scene = await createSpaceStationScene(engine);
break;
case "xr":
scene = await createXrScene(engine);
break;
default:
scene = await createAutomaticLandingScene(engine);
}

scene.executeWhenReady(() => {
engine.loadingScreen.hideLoadingUI();
Expand Down
79 changes: 79 additions & 0 deletions src/ts/playgrounds/automaticLanding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of Cosmos Journeyer
//
// 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 Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { AbstractEngine } from "@babylonjs/core/Engines/abstractEngine";
import { Scene } from "@babylonjs/core/scene";
import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { LandingPad, LandingPadSize } from "../assets/procedural/landingPad/landingPad";
import { Transformable } from "../architecture/transformable";
import { AssetsManager, TransformNode } from "@babylonjs/core";
import { enablePhysics } from "./utils";
import { DefaultControls } from "../defaultControls/defaultControls";
import { Spaceship } from "../spaceship/spaceship";
import { Objects } from "../assets/objects";
import { Textures } from "../assets/textures";
import { Sounds } from "../assets/sounds";

export async function createAutomaticLandingScene(engine: AbstractEngine): Promise<Scene> {
const scene = new Scene(engine);
scene.useRightHandedSystem = true;

await enablePhysics(scene);

const assetsManager = new AssetsManager(scene);
Sounds.EnqueueTasks(assetsManager, scene);
Objects.EnqueueTasks(assetsManager, scene);
Textures.EnqueueTasks(assetsManager, scene);
await assetsManager.loadAsync();

const ship = Spaceship.CreateDefault(scene);
ship.getTransform().position.copyFromFloats(0, 20, 0);

const defaultControls = new DefaultControls(scene);
defaultControls.getTransform().position.copyFromFloats(0, 10, -50);
defaultControls.speed *= 10;

const camera = defaultControls.getActiveCamera();
camera.minZ = 0.1;
camera.attachControl();

const landingPad = new LandingPad(42, LandingPadSize.SMALL, scene);

const hemi = new HemisphericLight("hemi", Vector3.Up(), scene);
hemi.intensity = 1.0;

const sunTransform = new TransformNode("sun", scene);
sunTransform.position.copyFromFloats(0, 50, 0);

const sun: Transformable = {
getTransform: () => sunTransform,
dispose: () => sunTransform.dispose()
};

scene.onBeforeRenderObservable.add(() => {
const deltaSeconds = engine.getDeltaTime() / 1000;

ship.update(deltaSeconds);

defaultControls.update(deltaSeconds);

landingPad.update([sun], camera.globalPosition);
});

return scene;
}
Loading

0 comments on commit cee50eb

Please sign in to comment.