Skip to content

Commit

Permalink
Merge pull request #5 from BarthPaleologue/WIP
Browse files Browse the repository at this point in the history
Faster XR experiment
  • Loading branch information
BarthPaleologue authored Dec 16, 2023
2 parents 307e934 + 4a9ad38 commit 0b23d1e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 59 deletions.
64 changes: 9 additions & 55 deletions src/ts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { OceanPostProcess } from "./postProcesses/oceanPostProcess";
import { UberScene } from "./uberCore/uberScene";
import { translate } from "./uberCore/transforms/basicTransform";
import { PointLightWrapper, TransformNodeWrapper } from "./utils/wrappers";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
import { PointLight } from "@babylonjs/core/Lights/pointLight";
import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
import { Engine } from "@babylonjs/core/Engines/engine";

import "@babylonjs/core";
import HavokPhysics from "@babylonjs/havok";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import { setMaxLinVel } from "./utils/havok";
Expand All @@ -22,8 +18,9 @@ import { Quaternion } from "@babylonjs/core/Maths/math";
import { FlatCloudsPostProcess } from "./postProcesses/clouds/flatCloudsPostProcess";
import { AtmosphericScatteringPostProcess } from "./postProcesses/atmosphericScatteringPostProcess";
import { Star } from "./stellarObjects/star/star";
import { VolumetricLight } from "./postProcesses/volumetricLight";
import { LensFlarePostProcess } from "./postProcesses/lensFlarePostProcess";
import { Settings } from "./settings";
import { ScenePerformancePriority } from "@babylonjs/core";

const canvas = document.getElementById("renderer") as HTMLCanvasElement;
canvas.width = window.innerWidth;
Expand All @@ -38,7 +35,7 @@ const havokPlugin = new HavokPlugin(true, havokInstance);
setMaxLinVel(havokPlugin, 10000, 10000);
console.log(`Havok initialized`);

const scene = new UberScene(engine);
const scene = new UberScene(engine, ScenePerformancePriority.Intermediate);
scene.useRightHandedSystem = true;
scene.enablePhysics(Vector3.Zero(), havokPlugin);

Expand All @@ -52,74 +49,32 @@ camera.speed *= sphereRadius * 0.1;
scene.setActiveCamera(camera);
camera.attachControl(canvas, true);

const xr = await scene.createDefaultXRExperienceAsync();
if (!xr.baseExperience) {
// no xr support
throw new Error("No XR support");
} else {
// all good, ready to go
console.log("XR support");
}

const webXRInput = xr.input; // if using the experience helper, otherwise, an instance of WebXRInput
webXRInput.onControllerAddedObservable.add((xrController) => {
console.log("Controller added");
xrController.onMotionControllerInitObservable.add((motionController) => {
console.log("Motion controller initialized");

const mainComponent = motionController.getMainComponent();

mainComponent.onButtonStateChangedObservable.add((component) => {
if (component.changes.pressed) {
if (component.changes.pressed.current) {
console.log("Pressed");
}
if (component.pressed) {
console.log("Pressed");
}
}
});
});
});

const xrCamera = xr.baseExperience.camera;
xrCamera.setTransformationFromNonVRCamera(camera);

const planet = new TelluricPlanemo("xrPlanet", scene, 0.51, undefined);
translate(planet.getTransform(), new Vector3(0, 0, sphereRadius * 4));

const star = new Star("star", scene, 0.2); //PointLightWrapper(new PointLight("dir01", new Vector3(0, 1, 0), scene));
translate(star.getTransform(), new Vector3(0, 0, -sphereRadius * 1000));
translate(star.getTransform(), new Vector3(0, 0, -sphereRadius * 5000));

const starfield = new StarfieldPostProcess(scene, [star], [planet], Quaternion.Identity());
camera.attachPostProcess(starfield);
xrCamera.attachPostProcess(starfield);

const ocean = new OceanPostProcess("ocean", planet, scene, [star]);
camera.attachPostProcess(ocean);
xrCamera.attachPostProcess(ocean);

if (planet.model.cloudsUniforms === null) throw new Error("Clouds uniforms are null");
FlatCloudsPostProcess.CreateAsync("clouds", planet, planet.model.cloudsUniforms, scene, [star]).then((clouds) => {
camera.attachPostProcess(clouds);
xrCamera.attachPostProcess(clouds);

const atmosphere = new AtmosphericScatteringPostProcess("atmosphere", planet, 100e3, scene, [star]);
camera.attachPostProcess(atmosphere);
xrCamera.attachPostProcess(atmosphere);
});

const volumetricLight = new VolumetricLight(star, scene);
camera.attachPostProcess(volumetricLight);
xrCamera.attachPostProcess(volumetricLight);

const lensflare = new LensFlarePostProcess(star, scene);
camera.attachPostProcess(lensflare);
xrCamera.attachPostProcess(lensflare);
const lensflare = new LensFlarePostProcess(star, scene);
camera.attachPostProcess(lensflare);
});

const chunkForge = new ChunkForge(64);
const chunkForge = new ChunkForge(Settings.VERTEX_RESOLUTION);

scene.onBeforePhysicsObservable.add(() => {
scene.onBeforeRenderObservable.add(() => {
const deltaTime = scene.deltaTime / 1000;

if (scene.activeCamera === null) throw new Error("Active camera is null");
Expand All @@ -131,7 +86,6 @@ scene.onBeforePhysicsObservable.add(() => {
}

planet.updateLOD(scene.activeCamera.globalPosition, chunkForge);
planet.computeCulling(camera);
planet.updateMaterial(camera, [star], deltaTime);

chunkForge.update();
Expand Down
148 changes: 148 additions & 0 deletions src/ts/xr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import "../styles/index.scss";

import { Assets } from "./assets";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { OceanPostProcess } from "./postProcesses/oceanPostProcess";
import { UberScene } from "./uberCore/uberScene";
import { translate } from "./uberCore/transforms/basicTransform";
import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
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 { TelluricPlanemo } from "./planemos/telluricPlanemo/telluricPlanemo";
import { ChunkForge } from "./planemos/telluricPlanemo/terrain/chunks/chunkForge";
import { StarfieldPostProcess } from "./postProcesses/starfieldPostProcess";
import { Quaternion } from "@babylonjs/core/Maths/math";
import { FlatCloudsPostProcess } from "./postProcesses/clouds/flatCloudsPostProcess";
import { AtmosphericScatteringPostProcess } from "./postProcesses/atmosphericScatteringPostProcess";
import { Star } from "./stellarObjects/star/star";
import { LensFlarePostProcess } from "./postProcesses/lensFlarePostProcess";
import { Settings } from "./settings";
import { ScenePerformancePriority } from "@babylonjs/core";

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

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

Settings.VERTEX_RESOLUTION = 32;

// 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, ScenePerformancePriority.Intermediate);
scene.useRightHandedSystem = true;
scene.enablePhysics(Vector3.Zero(), havokPlugin);

await Assets.Init(scene);

const sphereRadius = Settings.EARTH_RADIUS;

const camera = new FreeCamera("camera", new Vector3(0, 0, 0), scene);
camera.maxZ = 1e9;
camera.speed *= sphereRadius * 0.1;
scene.setActiveCamera(camera);
camera.attachControl(canvas, true);

const xr = await scene.createDefaultXRExperienceAsync();
if (!xr.baseExperience) {
// no xr support
throw new Error("No XR support");
} else {
// all good, ready to go
console.log("XR support");
}

const webXRInput = xr.input; // if using the experience helper, otherwise, an instance of WebXRInput
webXRInput.onControllerAddedObservable.add((xrController) => {
console.log("Controller added");
xrController.onMotionControllerInitObservable.add((motionController) => {
console.log("Motion controller initialized");

const mainComponent = motionController.getMainComponent();

mainComponent.onButtonStateChangedObservable.add((component) => {
if (component.changes.pressed) {
if (component.changes.pressed.current) {
console.log("Pressed");
}
if (component.pressed) {
console.log("Pressed");
}
}
});
});
});

const xrCamera = xr.baseExperience.camera;
xrCamera.setTransformationFromNonVRCamera(camera);

const planet = new TelluricPlanemo("xrPlanet", scene, 0.51, undefined);
translate(planet.getTransform(), new Vector3(0, 0, sphereRadius * 4));

const star = new Star("star", scene, 0.2); //PointLightWrapper(new PointLight("dir01", new Vector3(0, 1, 0), scene));
translate(star.getTransform(), new Vector3(0, 0, -sphereRadius * 5000));

const starfield = new StarfieldPostProcess(scene, [star], [planet], Quaternion.Identity());
camera.attachPostProcess(starfield);
xrCamera.attachPostProcess(starfield);

const ocean = new OceanPostProcess("ocean", planet, scene, [star]);
camera.attachPostProcess(ocean);
xrCamera.attachPostProcess(ocean);

if (planet.model.cloudsUniforms === null) throw new Error("Clouds uniforms are null");
FlatCloudsPostProcess.CreateAsync("clouds", planet, planet.model.cloudsUniforms, scene, [star]).then((clouds) => {
camera.attachPostProcess(clouds);
xrCamera.attachPostProcess(clouds);

const atmosphere = new AtmosphericScatteringPostProcess("atmosphere", planet, 100e3, scene, [star]);
camera.attachPostProcess(atmosphere);
xrCamera.attachPostProcess(atmosphere);

const lensflare = new LensFlarePostProcess(star, scene);
camera.attachPostProcess(lensflare);
xrCamera.attachPostProcess(lensflare);
});

const chunkForge = new ChunkForge(Settings.VERTEX_RESOLUTION);

scene.onBeforeRenderObservable.add(() => {
const deltaTime = scene.deltaTime / 1000;

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

if (camera.globalPosition.length() > 0) {
translate(planet.getTransform(), camera.globalPosition.negate());
translate(star.getTransform(), camera.globalPosition.negate());
camera.position.set(0, 0, 0);
}

planet.updateLOD(scene.activeCamera.globalPosition, chunkForge);
planet.updateMaterial(camera, [star], deltaTime);

chunkForge.update();

star.updateMaterial();

ocean.update(deltaTime);
});

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

window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
engine.resize(true);
});
12 changes: 8 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const isProduction = process.env.NODE_ENV === "production";
const htmlPath = path.join(__dirname, "/src/html/");

const config = {

entry: {
showcase: "./src/ts/index.ts",
random: "./src/ts/randomizer.ts",
blackHole: "./src/ts/blackHoleDemo.ts",
physicSpaceship: "./src/ts/physicSpaceship.ts",
planetWalk: "./src/ts/planetWalk.ts",
playground: "./src/ts/playground.ts",
xr: "./src/ts/xr.ts",
debugAssets: "./src/ts/debugAssets.ts"
},
output: {
Expand All @@ -27,7 +27,7 @@ const config = {
historyApiFallback: false,
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "same-origin"
}
},

Expand Down Expand Up @@ -68,6 +68,12 @@ const config = {
template: path.join(htmlPath, "index.html"),
chunks: ["playground"]
}),
new HtmlWebpackPlugin({
title: "XR",
filename: "xr.html",
template: path.join(htmlPath, "index.html"),
chunks: ["xr"]
}),
new HtmlWebpackPlugin({
title: "Debug Texture",
filename: "debugassets.html",
Expand All @@ -77,7 +83,6 @@ const config = {
new MiniCssExtractPlugin()
],


module: {
rules: [
{
Expand Down Expand Up @@ -111,7 +116,6 @@ const config = {
use: ["ts-shader-loader"]
}


// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
]
Expand Down

0 comments on commit 0b23d1e

Please sign in to comment.