Skip to content

Commit

Permalink
Merge pull request #20 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
BarthPaleologue authored Jan 22, 2024
2 parents 0ab58ae + f48a71b commit ac21c29
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 249 deletions.
Binary file added src/asset/landingpad.glb
Binary file not shown.
4 changes: 1 addition & 3 deletions src/ts/alphaTestis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ engine.init(true);

positionNearObjectBrightSide(starSystemView.scene.getActiveController(), planet, starSystem, 2);

spaceshipController.toggleWarpDrive();

const aresAtmosphere = starSystem.postProcessManager.getAtmosphere(ares);
if (aresAtmosphere) {
aresAtmosphere.atmosphereUniforms.redWaveLength = 500;
Expand Down Expand Up @@ -202,4 +200,4 @@ document.addEventListener("keydown", (e) => {

starSystemView.ui.setEnabled(true);
starSystemView.showUI();
starSystemView.getSpaceshipControls().enableWarpDrive();
starSystemView.getSpaceshipControls().spaceship.enableWarpDrive();
19 changes: 19 additions & 0 deletions src/ts/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import banana from "../asset/banana/banana.glb";
import endeavorSpaceship from "../asset/spaceship/endeavour.glb";
import character from "../asset/character.glb";
import rock from "../asset/rock.glb";
import landingPad from "../asset/landingpad.glb";

import tree from "../asset/tree/tree.babylon";
import treeTexturePath from "../asset/tree/Tree.png";
Expand Down Expand Up @@ -95,6 +96,8 @@ export class Assets {
private static Banana: Mesh;
private static Character: Mesh;

private static LandingPad: Mesh;

public static Rock: Mesh;
public static Tree: Mesh;
public static ScatterCube: Mesh;
Expand Down Expand Up @@ -202,6 +205,18 @@ export class Assets {
console.log("Rock loaded");
};

const landingPadTask = Assets.manager.addMeshTask("landingPadTask", "", "", landingPad);
landingPadTask.onSuccess = function (task: MeshAssetTask) {
Assets.LandingPad = task.loadedMeshes[0] as Mesh;
Assets.LandingPad.isVisible = false;

for (const mesh of Assets.LandingPad.getChildMeshes()) {
mesh.isVisible = false;
}

console.log("LandingPad loaded");
};

const treeTask = Assets.manager.addMeshTask("treeTask", "", "", tree);
treeTask.onSuccess = function (task: MeshAssetTask) {
Assets.Tree = task.loadedMeshes[0] as Mesh;
Expand Down Expand Up @@ -300,6 +315,10 @@ export class Assets {
return Assets.Rock.instantiateHierarchy(null, { doNotInstantiate: false }) as InstancedMesh;
}

static CreateLandingPadInstance(): InstancedMesh {
return Assets.LandingPad.instantiateHierarchy(null, { doNotInstantiate: false }) as InstancedMesh;
}

static DebugMaterial(name: string, diffuse = false, wireframe = false) {
const mat = new StandardMaterial(`${name}DebugMaterial`);
if (!diffuse) {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/blackHoleDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ positionNearObjectBrightSide(scene.getActiveController(), BH, starSystem, 20);

starSystemView.ui.setEnabled(true);
starSystemView.showUI();
starSystemView.getSpaceshipControls().enableWarpDrive();
starSystemView.getSpaceshipControls().spaceship.enableWarpDrive();
13 changes: 10 additions & 3 deletions src/ts/cosmosJourneyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import { SaveFileData } from "./saveFile/saveFileData";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { Quaternion } from "@babylonjs/core/Maths/math";
import { setRotationQuaternion } from "./uberCore/transforms/basicTransform";
import { positionNearObjectBrightSide } from "./utils/positionNearObject";
import { ShipControls } from "./spaceship/shipControls";
import { PhysicsMotionType } from "@babylonjs/core";
import { setMaxLinVel } from "./utils/havok";

enum EngineState {
RUNNING,
Expand Down Expand Up @@ -146,7 +147,7 @@ export class CosmosJourneyer {

const activeControls = this.getStarSystemView().scene.getActiveController();
if (activeControls instanceof ShipControls) {
activeControls.enableWarpDrive();
activeControls.spaceship.enableWarpDrive();
activeControls.thirdPersonCamera.radius = 30;
}
});
Expand All @@ -159,7 +160,7 @@ export class CosmosJourneyer {
this.mainMenu.onStartObservable.add(() => {
this.getStarMap().setCurrentStarSystem(this.getStarSystemView().getStarSystem().model.seed);
this.getStarSystemView().switchToSpaceshipControls();
this.getStarSystemView().getSpaceshipControls().enableWarpDrive();
this.getStarSystemView().getSpaceshipControls().spaceship.enableWarpDrive();
this.getStarSystemView().showUI();
this.getStarSystemView().ui.setEnabled(true);
});
Expand Down Expand Up @@ -383,6 +384,12 @@ export class CosmosJourneyer {
);
const currentWorldRotationQuaternion = currentLocalRotationQuaternion.multiply(nearestOrbitalObjectWorldRotation);
setRotationQuaternion(playerTransform, currentWorldRotationQuaternion);

// updates camera position
this.getStarSystemView().getSpaceshipControls().getActiveCamera().getViewMatrix(true);

// re-centers the star system
this.getStarSystemView().getStarSystem().applyFloatingOrigin();
});

this.getStarSystemView().initStarSystem();
Expand Down
18 changes: 7 additions & 11 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,40 @@ import "../styles/index.scss";

import { CosmosJourneyer } from "./cosmosJourneyer";
import { getForwardDirection, getRotationQuaternion, setRotationQuaternion, translate } from "./uberCore/transforms/basicTransform";
import { StarSystemController } from "./starSystem/starSystemController";
import { SystemSeed } from "./utils/systemSeed";

const engine = new CosmosJourneyer();

await engine.setup();

const starSystemView = engine.getStarSystemView();

//const starSystem = new StarSystemController(new SystemSeed(0, 0, 0, 0), starSystemView.scene);
//starSystemView.setStarSystem(starSystem, true);

engine.init();

const spaceshipController = starSystemView.getSpaceshipControls();
const shipControls = starSystemView.getSpaceshipControls();
const characterController = starSystemView.getCharacterControls();
const defaultController = starSystemView.getDefaultControls();

document.addEventListener("keydown", (e) => {
if (engine.isPaused()) return;

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

console.log("disembark");

characterController.getTransform().setEnabled(true);
characterController.getTransform().setAbsolutePosition(spaceshipController.getTransform().absolutePosition);
translate(characterController.getTransform(), getForwardDirection(spaceshipController.getTransform()).scale(10));
characterController.getTransform().setAbsolutePosition(shipControls.getTransform().absolutePosition);
translate(characterController.getTransform(), getForwardDirection(shipControls.getTransform()).scale(10));

setRotationQuaternion(characterController.getTransform(), getRotationQuaternion(spaceshipController.getTransform()).clone());
setRotationQuaternion(characterController.getTransform(), getRotationQuaternion(shipControls.getTransform()).clone());

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

characterController.getTransform().setEnabled(false);
starSystemView.scene.setActiveController(spaceshipController);
starSystemView.scene.setActiveController(shipControls);
starSystemView.getStarSystem().postProcessManager.rebuild();
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/ts/landingPad/landingPad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TransformNode } from "@babylonjs/core/Meshes";
import { Assets } from "../assets";
import { PhysicsAggregate } from "@babylonjs/core/Physics/v2/physicsAggregate";
import { PhysicsShapeType } from "@babylonjs/core/Physics/v2/IPhysicsEnginePlugin";
import { PhysicsShapeConvexHull } from "@babylonjs/core/Physics/v2/physicsShape";
import { Mesh } from "@babylonjs/core/Meshes/mesh";
import { CollisionMask } from "../settings";
import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math";
import { Transformable } from "../architecture/transformable";

export class LandingPad implements Transformable {
readonly instanceRoot: TransformNode;
readonly aggregate: PhysicsAggregate;

constructor(scene: Scene) {
this.instanceRoot = Assets.CreateLandingPadInstance();

this.aggregate = new PhysicsAggregate(
this.instanceRoot,
PhysicsShapeType.CONTAINER,
{
mass: 0,
restitution: 0.2
},
scene
);

this.aggregate.body.setMassProperties({ inertia: Vector3.Zero(), mass: 0 });

for (const child of this.instanceRoot.getChildMeshes()) {
const childShape = new PhysicsShapeConvexHull(child as Mesh, scene);
childShape.filterMembershipMask = CollisionMask.LANDING_PADS;
this.aggregate.shape.addChildFromParent(this.instanceRoot, childShape, child);
}
this.aggregate.body.disablePreStep = false;
}

getTransform(): TransformNode {
return this.aggregate.transformNode;
}

dispose() {
this.aggregate.dispose();
this.instanceRoot.dispose();
}
}
101 changes: 101 additions & 0 deletions src/ts/landingSimulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import "../styles/index.scss";

import { Engine } from "@babylonjs/core/Engines/engine";
import HavokPhysics from "@babylonjs/havok";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import { setMaxLinVel } from "./utils/havok";
import { UberScene } from "./uberCore/uberScene";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { Assets } from "./assets";
import { roll, translate } from "./uberCore/transforms/basicTransform";
import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight";
import { Color4 } from "@babylonjs/core/Maths/math.color";
import "@babylonjs/core/Physics/physicsEngineComponent";
import { Keyboard } from "./inputs/keyboard";
import { LandingPad } from "./landingPad/landingPad";
import { PhysicsViewer } from "@babylonjs/core/Debug/physicsViewer";
import { DefaultControls } from "./defaultController/defaultControls";
import { Spaceship } from "./spaceship/spaceship";
import "@babylonjs/core/Lights/Shadows/shadowGeneratorSceneComponent";
import { ShadowGenerator } from "@babylonjs/core/Lights/Shadows/shadowGenerator";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight";
import { PointLight } from "@babylonjs/core/Lights/pointLight";

const canvas = document.getElementById("renderer") as HTMLCanvasElement;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const engine = new Engine(canvas, true);
engine.useReverseDepthBuffer = true;

// Init Havok physics engine
const havokInstance = await HavokPhysics();
const havokPlugin = new HavokPlugin(true, havokInstance);
setMaxLinVel(havokPlugin, 10000, 10000);
console.log(`Havok initialized`);

const scene = new UberScene(engine);
scene.useRightHandedSystem = true;
scene.enablePhysics(Vector3.Zero(), havokPlugin);
scene.clearColor = new Color4(0.2, 0.2, 0.6, 1);

const hemi = new HemisphericLight("hemi", new Vector3(0, 1, 0), scene);
const light = new DirectionalLight("light", new Vector3(-2, -5, 1).normalize(), scene);
const pointLight = new PointLight("pointLight", new Vector3(0, 0, 0), scene);
pointLight.intensity = 0;
const shadowGenerator = new ShadowGenerator(2048, light);

await Assets.Init(scene);

const spaceship = new Spaceship(scene);
shadowGenerator.addShadowCaster(spaceship.instanceRoot, true);
spaceship.getTransform().position = new Vector3(0, 0, -10);
roll(spaceship.getTransform(), Math.random() * 6.28);

const landingPad = new LandingPad(scene);
landingPad.getTransform().position = new Vector3(0, -20, 0);
landingPad.instanceRoot.getChildMeshes().forEach((mesh) => {
mesh.receiveShadows = true;
});

/*const ground = MeshBuilder.CreateGround("ground", { width: 50, height: 50 });
ground.position.y = -40;
ground.receiveShadows = true;*/

/*const physicsViewer = new PhysicsViewer();
physicsViewer.showBody(spaceship.aggregate.body);
physicsViewer.showBody(landingPad.aggregate.body);*/

const defaultControls = new DefaultControls(scene);
defaultControls.speed *= 15;
defaultControls.addInput(new Keyboard());
scene.setActiveController(defaultControls);

translate(defaultControls.getTransform(), new Vector3(50, 0, 0));

defaultControls.getTransform().lookAt(Vector3.Lerp(spaceship.getTransform().position, landingPad.getTransform().position, 0.5));

scene.onBeforeRenderObservable.add(() => {
const deltaTime = scene.deltaTime / 1000;
scene.getActiveController().update(deltaTime);
spaceship.update(deltaTime);
});

scene.executeWhenReady(() => {
engine.runRenderLoop(() => {
scene.render();
});
});

document.addEventListener("keydown", (event) => {
if (event.key === "o") {
spaceship.engageLanding(landingPad);
}
});

window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
engine.resize(true);
});
5 changes: 3 additions & 2 deletions src/ts/proceduralAssets/butterfly/butterflyMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export function createButterflyMaterial(scene: Scene, player?: TransformNode) {

if (scene.activeCamera === null) throw new Error("Active camera is null");

const star = scene.lights[1];
if (!(star instanceof PointLight)) throw new Error("Could not find star light");
const starIndex = scene.lights.findIndex((light) => light instanceof PointLight);
if (starIndex === -1) throw new Error("Could not find star light");
const star = scene.lights[starIndex] as PointLight;

const lightDirection = star.position.subtract(scene.activeCamera.globalPosition).normalize();
butterflyMaterial.setVector3("lightDirection", lightDirection);
Expand Down
5 changes: 3 additions & 2 deletions src/ts/proceduralAssets/grass/grassMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export function createGrassMaterial(scene: Scene) {

if (scene.activeCamera === null) throw new Error("Active camera is null");

const star = scene.lights[1];
if (!(star instanceof PointLight)) throw new Error("Could not find star light");
const starIndex = scene.lights.findIndex((light) => light instanceof PointLight);
if (starIndex === -1) throw new Error("Could not find star light");
const star = scene.lights[starIndex] as PointLight;

const lightDirection = star.position.subtract(scene.activeCamera.globalPosition).normalize();
grassMaterial.setVector3("lightDirection", lightDirection);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/randomizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ positionNearObjectBrightSide(scene.getActiveController(), starSystem.planets.len

starSystemView.ui.setEnabled(true);
starSystemView.showUI();
starSystemView.getSpaceshipControls().enableWarpDrive();
starSystemView.getSpaceshipControls().spaceship.enableWarpDrive();
3 changes: 2 additions & 1 deletion src/ts/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const Settings = {

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

const seedableRNG = seededSquirrelNoise(Settings.UNIVERSE_SEED);
Expand Down
Loading

0 comments on commit ac21c29

Please sign in to comment.