Skip to content

Commit

Permalink
Michaels big refactor ™️ (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelthatsit authored Feb 19, 2024
1 parent fd3635f commit 05c9884
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 85 deletions.
20 changes: 10 additions & 10 deletions dist/mr.js

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions samples/examples/anchors.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@

<mr-div class="col-2">

<mr-button onclick="floating()" class=" label button" >
floating
</mr-button>

<mr-button onclick="fixed()" class=" label button" >
fixed
</mr-button>
Expand Down Expand Up @@ -98,13 +94,13 @@
positions the anchored element directly in front of the user whenever an XR sessions is started.
</mr-text>

<mr-text class="label-2 col-2">
<!-- <mr-text class="label-2 col-2">
floating
</mr-text>
<mr-text class="col-2">
allows the user to place the anchored element where they choose using a look/pinch gesture. The user can place it floating in their space or on the scene mesh
</mr-text>
</mr-text> -->

<mr-text class="label-2 col-2">
plane
Expand Down Expand Up @@ -293,9 +289,11 @@
panel.components.set('anchor', {type: 'fixed'})
}

let wallVisible = false
function wall() {
wallAnchor.style.visibility = wallAnchor.style.visibility == 'hidden' ? 'visible' : 'hidden'
wallAnchor.components.set('anchor', {occlusion: !wallAnchor.object3D.visible})
wallVisible = !wallVisible
wallAnchor.style.visibility = wallVisible ? 'visible' : 'hidden'
wallAnchor.components.set('anchor', {occlusion: !wallVisible})
}

function table() {
Expand All @@ -307,9 +305,12 @@

}

let ceilingVisible = false

function ceiling() {
ceilingAnchor.style.visibility = ceilingAnchor.style.visibility == 'hidden' ? 'visible' : 'hidden'
ceilingAnchor.components.set('anchor', {occlusion: !ceilingAnchor.object3D.visible})
ceilingVisible = !ceilingVisible
ceilingAnchor.style.visibility = ceilingVisible ? 'visible' : 'hidden'
ceilingAnchor.components.set('anchor', {occlusion: !ceilingVisible})

}

Expand All @@ -320,7 +321,7 @@
}
}

document.addEventListener('exitXR', () => {
document.addEventListener('exitxr', () => {
reset()
})

Expand Down
2 changes: 1 addition & 1 deletion samples/examples/panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

}

document.addEventListener('exitXR', () => {
document.addEventListener('exitxr', () => {
unfolded = false
spread()
})
Expand Down
4 changes: 2 additions & 2 deletions samples/examples/skybox.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@
}
}

document.addEventListener('enterXR', () => {
document.addEventListener('enterxr', () => {
reset();
});

document.addEventListener("DOMContentLoaded", (event) => {
reset();
});

document.addEventListener('exitXR', () => {
document.addEventListener('exitxr', () => {
reset();
});

Expand Down
5 changes: 2 additions & 3 deletions src/core/MRApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ export class MRApp extends MRElement {
mrjsUtils.xr.session = this.renderer.xr.getSession();
mrjsUtils.xr.referenceSpace = mrjsUtils.xr.getReferenceSpace();

this.dispatchEvent(new CustomEvent('enterXR', { bubbles: true }));
console.log('enter xr');
this.dispatchEvent(new CustomEvent('enterxr', { bubbles: true }));

mrjsUtils.xr.session.addEventListener('end', () => {
this.user.position.set(0, 0, 1);
Expand All @@ -460,7 +459,7 @@ export class MRApp extends MRElement {
this.classList.remove('inXR');

this.onWindowResize();
this.dispatchEvent(new CustomEvent('exitXR', { bubbles: true }));
this.dispatchEvent(new CustomEvent('exitxr', { bubbles: true }));
});
}

Expand Down
14 changes: 7 additions & 7 deletions src/core/MREntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class MREntity extends MRElement {
});
});

this.addEventListener('touch-start', (event) => {
this.addEventListener('touchstart', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand All @@ -305,7 +305,7 @@ export class MREntity extends MRElement {
}
this.onTouch(event);
});
this.addEventListener('touch', (event) => {
this.addEventListener('touchmove', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand All @@ -314,7 +314,7 @@ export class MREntity extends MRElement {
}
this.onTouch(event);
});
this.addEventListener('touch-end', (event) => {
this.addEventListener('touchend', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand All @@ -323,7 +323,7 @@ export class MREntity extends MRElement {
}
this.onTouch(event);
});
this.addEventListener('hover-start', (event) => {
this.addEventListener('hoverstart', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand All @@ -332,7 +332,7 @@ export class MREntity extends MRElement {
}
this.onHover(event);
});
this.addEventListener('hover-end', (event) => {
this.addEventListener('hoverend', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand Down Expand Up @@ -360,15 +360,15 @@ export class MREntity extends MRElement {
}
});

document.addEventListener('enterXR', (event) => {
document.addEventListener('enterxr', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
if (!this.alwaysNeedsStyleUpdate) {
this.needsStyleUpdate = true;
}
});
document.addEventListener('exitXR', (event) => {
document.addEventListener('exitxr', (event) => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand Down
15 changes: 15 additions & 0 deletions src/core/MRSystem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MREntity } from 'mrjs/core/MREntity';
const GLOBAL_UPDATE_EVENTS = ['enterxr', 'exitxr', 'load', 'anchored', 'panelupdate']

/**
* @class MRSystem
Expand Down Expand Up @@ -44,6 +45,12 @@ export class MRSystem {
this.onNewEntity(event.target);
});

for(const eventType of GLOBAL_UPDATE_EVENTS) {
document.addEventListener(eventType, (event) => {
this.eventUpdate()
});
}

const entities = document.querySelectorAll(`[${this.componentName}]`);
for (const entity of entities) {
if (!(entity instanceof MREntity)) {
Expand Down Expand Up @@ -130,6 +137,14 @@ export class MRSystem {
*/
update(deltaTime, frame) {}

/**
* @function
* @description An event triggered update, called when any scene level events occur.
*/
eventUpdate() {

}

/**
* @function
* @description Called when a new entity is added to the scene
Expand Down
2 changes: 1 addition & 1 deletion src/core/MRTextEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MRTextEntity extends MRDivEntity {

// This event listener is added so anytime a panel changes (resize, etc), the text changes
// accordingly
document.addEventListener('panel-mutated', () => {
document.addEventListener('panelupdate', () => {
if (!this.alwaysNeedsGeometryUpdate) {
this.needsGeometryUpdate = true;
}
Expand Down
43 changes: 27 additions & 16 deletions src/core/componentSystems/AnchorSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AnchorSystem extends MRSystem {
this.currentEntity = null;
this.tempMatrix = new THREE.Matrix4();

this.planeManager = new MRPlaneManager(this.app.scene, this.app.physicsWorld);
this.planeManager = new MRPlaneManager(this.app.scene, this.app.physicsWorld, this.app.getAttribute('occlusion'));
this.anchoringQueue = new Set();

this.hitResults;
Expand All @@ -41,12 +41,14 @@ export class AnchorSystem extends MRSystem {

let existing = document.querySelectorAll('[data-comp-anchor]');

this.originPosition = new THREE.Vector3()

for (const entity of existing) {
this.attachedComponent(entity);
this.registry.add(entity);
}

this.app.addEventListener('enterXR', () => {
this.app.addEventListener('enterxr', () => {
if (this.sourceRequest == false) {
mrjsUtils.xr.session.requestReferenceSpace('viewer').then((viewerSpace) => {
mrjsUtils.xr.session.requestHitTestSource({ space: viewerSpace }).then((source) => {
Expand All @@ -64,7 +66,7 @@ export class AnchorSystem extends MRSystem {
}
});

this.app.addEventListener('exitXR', () => {
this.app.addEventListener('exitxr', () => {
this.deleteAnchor(this.app)
this.app.origin.matrix.copy(new THREE.Matrix4())
});
Expand Down Expand Up @@ -98,7 +100,6 @@ export class AnchorSystem extends MRSystem {
mrjsUtils.xr.session.requestAnimationFrame((t, frame) => {
frame.createAnchor(this.matrix4ToXRRigidTransform(this.currentEntity.object3D.matrixWorld), mrjsUtils.xr.referenceSpace).then(
(anchor) => {
console.log('anchored floating');
this.currentEntity.anchor = anchor;
this.anchoringQueue.delete(this.currentEntity);
this.currentEntity = null;
Expand Down Expand Up @@ -157,13 +158,16 @@ export class AnchorSystem extends MRSystem {
if (mrjsUtils.xr.isPresenting) {
let anchorComp = entity.components.get('anchor');
if (entity.anchor == null && !this.anchoringQueue.has(entity)) {
entity.object3D.matrixAutoUpdate = false;
entity.object3D.matrixWorldAutoUpdate = false;
this.createAnchor(entity, anchorComp);
} else if (entity.anchor) {
let pose = frame.getPose(entity.anchor.anchorSpace, mrjsUtils.xr.referenceSpace);
let transform = this.multiplyQuaternionWithXRRigidTransform(this.axisSwapQuat, pose.transform);

entity.object3D.matrixWorld.copy(this.adjustTransform(transform));
entity.object3D.matrix.copy(this.adjustTransform(transform));

} else {
this.createAnchor(entity, anchorComp);
}
} else if (entity.anchor) {
entity.object3D.matrix.copy(entity.object3D.userData.originalMatrix);
Expand Down Expand Up @@ -214,7 +218,7 @@ export class AnchorSystem extends MRSystem {
entity.plane = null;
}
entity.anchor = null;
entity.dispatchEvent(new CustomEvent('anchor-removed', { bubbles: true }));
entity.dispatchEvent(new CustomEvent('anchorremoved', { bubbles: true }));
}

/**
Expand Down Expand Up @@ -244,8 +248,10 @@ export class AnchorSystem extends MRSystem {
if (!mrjsUtils.xr.isPresenting) {
return;
}
let originMatrix = new THREE.Matrix4()
mrjsUtils.xr.session.requestAnimationFrame((t, frame) => {
frame.createAnchor(this.matrix4ToXRRigidTransform(this.app.forward.matrixWorld), mrjsUtils.xr.referenceSpace).then(
originMatrix.copyPosition(this.app.forward.matrixWorld)
frame.createAnchor(this.matrix4ToXRRigidTransform(originMatrix), mrjsUtils.xr.referenceSpace).then(
(anchor) => {
this.app.origin.matrixAutoUpdate = false;
this.app.anchor = anchor;
Expand All @@ -262,8 +268,9 @@ export class AnchorSystem extends MRSystem {
let pose = frame.getPose(this.app.anchor.anchorSpace, mrjsUtils.xr.referenceSpace);
let transform = this.multiplyQuaternionWithXRRigidTransform(this.axisSwapQuat, pose.transform);

this.app.origin.matrix.copy(this.adjustTransform(transform));
this.app.origin.matrix.copy(this.adjustTransform(transform, true));

this.originPosition.setFromMatrixPosition(this.app.origin.matrixWorld)
}

/**
Expand Down Expand Up @@ -314,12 +321,9 @@ export class AnchorSystem extends MRSystem {
* @param comp
*/
plane(entity, comp) {
if (this.planeManager.currentPlanes.size == 0) {
return;
}
this.anchoringQueue.add(entity);
this.userWorldPosition.setFromMatrixPosition(this.app.forward.matrixWorld);
let sort = Array.from(this.planeManager.currentPlanes.values());
let sort = Array.from(this.planeManager.planeDictionary[comp.label].values());
sort.sort((a, b) => {
return a.mesh.position.distanceTo(this.userWorldPosition) - b.mesh.position.distanceTo(this.userWorldPosition);
});
Expand All @@ -339,10 +343,13 @@ export class AnchorSystem extends MRSystem {
frame.createAnchor(this.matrix4ToXRRigidTransform(mrPlane.mesh.matrixWorld), mrjsUtils.xr.referenceSpace).then(
(anchor) => {
this.anchoringQueue.delete(entity);
if(!this.planeManager.planeDictionary[comp.label].has(mrPlane)) {
return
}
if(entity.anchor) { return }
entity.anchor = anchor;
entity.plane = mrPlane;
entity.dispatchEvent(new CustomEvent('anchored', { bubbles: true }));

if (comp.occlusion == false) {
mrPlane.mesh.visible = false;
}
Expand All @@ -352,7 +359,7 @@ export class AnchorSystem extends MRSystem {
}
);
});
return;
return
}
}

Expand All @@ -368,7 +375,7 @@ export class AnchorSystem extends MRSystem {
* @param xrRigidTransform
* @returns a THREE.js Matrix4
*/
adjustTransform(xrRigidTransform) {
adjustTransform(xrRigidTransform, origin = false) {
// Create a Three.js Quaternion for the XRRigidTransform's orientation
let quaternion = new THREE.Quaternion(xrRigidTransform.orientation.x, xrRigidTransform.orientation.y, xrRigidTransform.orientation.z, xrRigidTransform.orientation.w);

Expand All @@ -387,6 +394,10 @@ export class AnchorSystem extends MRSystem {
// Create a new Three.js Vector3 for the position
let position = new THREE.Vector3(xrRigidTransform.position.x, xrRigidTransform.position.y, xrRigidTransform.position.z);

if(!origin) {
position.sub(this.originPosition)
}

this.originalAnchorMatrix.compose(position, quaternion, new THREE.Vector3(1, 1, 1));
this.rotationMatrix.extractRotation(this.originalAnchorMatrix);

Expand Down
Loading

0 comments on commit 05c9884

Please sign in to comment.