Skip to content

Commit

Permalink
Merge pull request #153 from 4lukexp/disable-xr
Browse files Browse the repository at this point in the history
WebXR
  • Loading branch information
slowsynapse authored Jan 17, 2025
2 parents f391a97 + c7c644c commit a7c7908
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 110 deletions.
10 changes: 6 additions & 4 deletions src/features/vrmViewer/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export class Model {
loader.load(
url,
async (gltf) => {
setLoadingProgress("Processing VRM");
// Temp Disable : WebXR
// setLoadingProgress("Processing VRM");

/*
{
Expand Down Expand Up @@ -224,9 +225,10 @@ export class Model {
resolve();
},
(xhr) => {
setLoadingProgress(
`${Math.floor((xhr.loaded / xhr.total) * 10000) / 100}% loaded`,
);
// Temp Disable : WebXR
// setLoadingProgress(
// `${Math.floor((xhr.loaded / xhr.total) * 10000) / 100}% loaded`,
// );
},
(error) => {
reject(error);
Expand Down
224 changes: 118 additions & 106 deletions src/features/vrmViewer/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,32 @@ export class Viewer {
renderer.xr.setFoveation(0);
}

// Temp Disable : WebXR
// initialize phyics
// we have this weird construct because ammo is loaded globally
// and things get funny with hot reloading
if (typeof window.Ammo === 'undefined') {
console.error("Ammo not found");
} else if (typeof window.Ammo === 'function') {
this.ammo = await window.Ammo();
} else {
this.ammo = window.Ammo;
}
if (this.ammo) {
this.collisionConfiguration = new this.ammo.btDefaultCollisionConfiguration();
this.dispatcher = new this.ammo.btCollisionDispatcher(this.collisionConfiguration);
this.broadphase = new this.ammo.btDbvtBroadphase();
this.solver = new this.ammo.btSequentialImpulseConstraintSolver();
this.physicsWorld = new this.ammo.btDiscreteDynamicsWorld(
this.dispatcher,
this.broadphase,
this.solver,
this.collisionConfiguration
);
this.physicsWorld.setGravity(new this.ammo.btVector3(0, -7.8, 0));
this.transformAux1 = new this.ammo.btTransform();
this.tempBtVec3_1 = new this.ammo.btVector3(0, 0, 0);
}
// if (typeof window.Ammo === 'undefined') {
// console.error("Ammo not found");
// } else if (typeof window.Ammo === 'function') {
// this.ammo = await window.Ammo();
// } else {
// this.ammo = window.Ammo;
// }
// if (this.ammo) {
// this.collisionConfiguration = new this.ammo.btDefaultCollisionConfiguration();
// this.dispatcher = new this.ammo.btCollisionDispatcher(this.collisionConfiguration);
// this.broadphase = new this.ammo.btDbvtBroadphase();
// this.solver = new this.ammo.btSequentialImpulseConstraintSolver();
// this.physicsWorld = new this.ammo.btDiscreteDynamicsWorld(
// this.dispatcher,
// this.broadphase,
// this.solver,
// this.collisionConfiguration
// );
// this.physicsWorld.setGravity(new this.ammo.btVector3(0, -7.8, 0));
// this.transformAux1 = new this.ammo.btTransform();
// this.tempBtVec3_1 = new this.ammo.btVector3(0, 0, 0);
// }

const scene = new THREE.Scene();
this.scene = scene;
Expand Down Expand Up @@ -478,6 +479,7 @@ export class Viewer {
new Stats.Panel("stats_ms", "#8f8", "#212"),
);

// Temp Disable : WebXR
const statsMesh = new HTMLMesh(stats.dom);
this.statsMesh = statsMesh;

Expand All @@ -490,67 +492,69 @@ export class Viewer {
this.bvhWorker = new GenerateMeshBVHWorker();
this.raycaster.firstHitOnly = true;

// Temp Disable : WebXR
// add joint / hand meshes
{
const geometry = new THREE.BoxGeometry(0.005, 0.005, 0.005);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1.0,
metalness: 0.0,
});

const mesh = new THREE.Mesh(geometry, material);

const lineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, -1, 0),
]);

const line = new THREE.Line(lineGeometry);
line.scale.z = 5;

for (const _ of joints) {
// Make joint mesh invisible
const clonedMesh = mesh.clone();
clonedMesh.visible = false;

this.jointMeshes1.push(clonedMesh);
this.jointMeshes2.push(clonedMesh);
// this.jointMeshes1[this.jointMeshes1.length - 1].add(line.clone());
// this.jointMeshes2[this.jointMeshes2.length - 1].add(line.clone());

this.handGroup.add(this.jointMeshes1[this.jointMeshes1.length - 1]);
this.handGroup.add(this.jointMeshes2[this.jointMeshes2.length - 1]);
}

this.handGroup.visible = false;
scene.add(this.handGroup);
}
// {
// const geometry = new THREE.BoxGeometry(0.005, 0.005, 0.005);
// const material = new THREE.MeshStandardMaterial({
// color: 0xffffff,
// roughness: 1.0,
// metalness: 0.0,
// });

// const mesh = new THREE.Mesh(geometry, material);

// const lineGeometry = new THREE.BufferGeometry().setFromPoints([
// new THREE.Vector3(0, 0, 0),
// new THREE.Vector3(0, -1, 0),
// ]);

// const line = new THREE.Line(lineGeometry);
// line.scale.z = 5;

// for (const _ of joints) {
// // Make joint mesh invisible
// const clonedMesh = mesh.clone();
// clonedMesh.visible = false;

// this.jointMeshes1.push(clonedMesh);
// this.jointMeshes2.push(clonedMesh);
// // this.jointMeshes1[this.jointMeshes1.length - 1].add(line.clone());
// // this.jointMeshes2[this.jointMeshes2.length - 1].add(line.clone());

// this.handGroup.add(this.jointMeshes1[this.jointMeshes1.length - 1]);
// this.handGroup.add(this.jointMeshes2[this.jointMeshes2.length - 1]);
// }

// this.handGroup.visible = false;
// scene.add(this.handGroup);
// }

{
const geometry = new THREE.SphereGeometry(1, 16, 16);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00,
transparent: true,
opacity: 0.5,
});
const mesh = new THREE.Mesh(geometry, material);
this.closestPart1 = mesh.clone();
this.closestPart2 = mesh.clone();
this.closestPart1.visible = false;
this.closestPart2.visible = false;
scene.add(this.closestPart1);
scene.add(this.closestPart2);
}
// {
// const geometry = new THREE.SphereGeometry(1, 16, 16);
// const material = new THREE.MeshBasicMaterial({
// color: 0xffff00,
// transparent: true,
// opacity: 0.5,
// });
// const mesh = new THREE.Mesh(geometry, material);
// this.closestPart1 = mesh.clone();
// this.closestPart2 = mesh.clone();
// this.closestPart1.visible = false;
// this.closestPart2.visible = false;
// scene.add(this.closestPart1);
// scene.add(this.closestPart2);
// }

this.particleRenderer = new BatchedParticleRenderer();
scene.add(this.particleRenderer);
// this.particleRenderer = new BatchedParticleRenderer();
// scene.add(this.particleRenderer);

new QuarksLoader().load('particles/cartoon_star_field', (obj) => {
this.particleCartoonStarField = obj;
// Temp Disable : WebXR
// new QuarksLoader().load('particles/cartoon_star_field', (obj) => {
// this.particleCartoonStarField = obj;

this.newParticleInstance();
});
// this.newParticleInstance();
// });

window.addEventListener("resize", () => {
this.resize();
Expand Down Expand Up @@ -676,17 +680,20 @@ export class Viewer {
if (this.model?.vrm) {
this.unloadVRM();
}
setLoadingProgress("Loading VRM");
// Temp Disable : WebXR
// setLoadingProgress("Loading VRM");

// gltf and vrm
this.model = new Model(this.camera || new THREE.Object3D());
await this.model.loadVRM(url, setLoadingProgress);
setLoadingProgress("VRM loaded");
// Temp Disable : WebXR
// setLoadingProgress("VRM loaded");
if (!this.model?.vrm) return;

// Temp Disable : WebXR
// build bvh
this.modelBVHGenerator = new StaticGeometryGenerator(this.model.vrm.scene);
setLoadingProgress("Creating geometry");
// this.modelBVHGenerator = new StaticGeometryGenerator(this.model.vrm.scene);
// setLoadingProgress("Creating geometry");

// TODO show during debug mode
const wireframeMaterial = new THREE.MeshBasicMaterial({
Expand Down Expand Up @@ -714,7 +721,8 @@ export class Viewer {

// TODO since poses still work for procedural animation, we can use this to debug
if (config("animation_procedural") !== "true") {
setLoadingProgress("Loading animation");
// Temp Disable : WebXR
// setLoadingProgress("Loading animation");
const animation =
config("animation_url").indexOf("vrma") > 0
? await loadVRMAnimation(config("animation_url"))
Expand All @@ -725,23 +733,26 @@ export class Viewer {
}
}

this.model?.vrm?.springBoneManager?.joints.forEach((e) => {
const geometry = new THREE.SphereGeometry(0.07, 16, 16);
const material = new THREE.MeshBasicMaterial({
color: 0xffff00,
transparent: true,
opacity: 0.5,
depthWrite: false,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(e.bone.getWorldPosition(new THREE.Vector3()));
// this.scene!.add(mesh);
});
// Temp Disable : WebXR
// this.model?.vrm?.springBoneManager?.joints.forEach((e) => {
// const geometry = new THREE.SphereGeometry(0.07, 16, 16);
// const material = new THREE.MeshBasicMaterial({
// color: 0xffff00,
// transparent: true,
// opacity: 0.5,
// depthWrite: false,
// });
// const mesh = new THREE.Mesh(geometry, material);
// mesh.position.copy(e.bone.getWorldPosition(new THREE.Vector3()));
// // this.scene!.add(mesh);
// });

setLoadingProgress("Regenerating BVH");
await this.regenerateBVHForModel();
// Temp Disable : WebXR
// setLoadingProgress("Regenerating BVH");
// await this.regenerateBVHForModel();

setLoadingProgress("Complete");
// Temp Disable : WebXR
// setLoadingProgress("Complete");

// HACK: Adjust the camera position after playback because the origin of the animation is offset
this.resetCamera();
Expand Down Expand Up @@ -1239,14 +1250,15 @@ export class Viewer {
// console.error("scenario update error", e);
// }
// this.scenarioMsPanel.update(performance.now() - ptime, 100);

ptime = performance.now();
try {
this.physicsWorld.stepSimulation(delta, 10);
} catch (e) {
console.error("physics update error", e);
}
this.physicsMsPanel.update(performance.now() - ptime, 100);

// Temp Disable : WebXR
// ptime = performance.now();
// try {
// this.physicsWorld.stepSimulation(delta, 10);
// } catch (e) {
// console.error("physics update error", e);
// }
// this.physicsMsPanel.update(performance.now() - ptime, 100);

ptime = performance.now();
try {
Expand Down

0 comments on commit a7c7908

Please sign in to comment.