Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirox committed Sep 5, 2023
1 parent 4014332 commit 782e006
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 132 deletions.
15 changes: 3 additions & 12 deletions src/bit-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,15 @@ export const MediaLoader = defineComponent({
});
MediaLoader.src[$isStringType] = true;
MediaLoader.fileId[$isStringType] = true;
export const MediaLoaded = defineComponent({
contentType: Types.ui32,
src: Types.ui32
});
MediaLoaded.contentType[$isStringType] = true;
MediaLoaded.src[$isStringType] = true;
export const MediaLoaded = defineComponent();
export const LoadedByMediaLoader = defineComponent();
export const MediaContentBounds = defineComponent({
bounds: [Types.f32, 3]
});
export const MediaInfo = defineComponent({
accessibleUrl: Types.ui32,
canonicalUrl: Types.ui32,
canonicalAudioUrl: Types.ui32,
contentType: Types.ui32,
mediaType: Types.ui8,
thumbnail: Types.ui32
accessibleUrl: Types.ui32
});
MediaInfo.accessibleUrl[$isStringType] = true;

export const SceneRoot = defineComponent();
export const NavMesh = defineComponent();
Expand Down
10 changes: 3 additions & 7 deletions src/bit-systems/media-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,8 @@ function* loadMedia(world: HubsWorld, eid: EntityID) {
}
media = yield* loader(world, eid, urlData);
addComponent(world, MediaLoaded, media);
MediaInfo.accessibleUrl[media] = APP.getSid(urlData.accessibleUrl);
MediaInfo.canonicalUrl[media] = APP.getSid(urlData.canonicalUrl);
urlData.canonicalAudioUrl && (MediaInfo.canonicalAudioUrl[media] = APP.getSid(urlData.canonicalAudioUrl));
MediaInfo.contentType[media] = APP.getSid(urlData.contentType);
urlData.mediaType && (MediaInfo.mediaType[media] = urlData.mediaType);
addComponent(world, MediaInfo, media);
MediaInfo.accessibleUrl[media] = APP.getSid(urlData.accessibleUrl);
} catch (e) {
console.error(e);
media = renderAsEntity(world, ErrorObject());
Expand Down Expand Up @@ -271,8 +267,8 @@ export function mediaLoadingSystem(world: HubsWorld) {
jobs.stop(eid);
});

mediaLoadedEnterQuery(world).forEach(eid => APP.scene?.emit("listed_media_changed"));
mediaLoadedExitQuery(world).forEach(eid => APP.scene?.emit("listed_media_changed"));
mediaLoadedEnterQuery(world).forEach(() => APP.scene?.emit("listed_media_changed"));
mediaLoadedExitQuery(world).forEach(() => APP.scene?.emit("listed_media_changed"));

jobs.tick();
}
57 changes: 23 additions & 34 deletions src/react-components/room/hooks/useObjectList.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import React, { useState, useEffect, useContext, createContext, useCallback, Children, cloneElement } from "react";
import PropTypes from "prop-types";
import { mediaSort, getMediaType } from "../../../utils/media-sorting.js";
import { shouldUseNewLoader } from "../../../utils/bit-utils";
import { defineQuery } from "bitecs";
import { MediaInfo } from "../../../bit-components.js";

function getDisplayString(elOrEid) {
let url;
if (!elOrEid.isEntity) {
const srcSid = MediaInfo.accessibleUrl[elOrEid];
url = APP.getString(srcSid);
} else {
// Having a listed-media component does not guarantee the existence of a media-loader component,
// so don't crash if there isn't one.
url = (elOrEid.components["media-loader"] && elOrEid.components["media-loader"].data.src) || "";
}

function getDisplayString(url) {
const split = url.split("/");
const resourceName = split[split.length - 1].split("?")[0];
let httpIndex = -1;
Expand Down Expand Up @@ -86,23 +77,27 @@ export function ObjectListProvider({ scene, children }) {

useEffect(() => {
function updateMediaEntities() {
const objects = scene.systems["listed-media"].els.sort(mediaSort).map(el => ({
id: el.object3D.id,
name: getDisplayString(el),
type: getMediaType(el),
el
}));

const bitObjects = queryListedMedia(APP.world)
.sort(mediaSort)
.map(eid => ({
id: APP.world.eid2obj.get(eid)?.id,
name: getDisplayString(eid),
type: getMediaType(eid),
el: eid
if (shouldUseNewLoader()) {
const objects = queryListedMedia(APP.world)
.sort(mediaSort)
.map(eid => ({
id: APP.world.eid2obj.get(eid)?.id,
name: getDisplayString(APP.getString(MediaInfo.accessibleUrl[eid])),
type: getMediaType(eid),
el: APP.world.eid2obj.get(eid)
}));
setObjects(objects);
} else {
const objects = scene.systems["listed-media"].els.sort(mediaSort).map(el => ({
id: el.object3D.id,
// Having a listed-media component does not guarantee the existence of a media-loader component,
// so don't crash if there isn't one.
name: getDisplayString((el.components["media-loader"] && el.components["media-loader"].data.src) || ""),
type: getMediaType(el),
el
}));

setObjects([...objects, ...bitObjects]);
setObjects(objects);
}
}

let timeout;
Expand Down Expand Up @@ -130,13 +125,7 @@ export function ObjectListProvider({ scene, children }) {
const inspectedEl = cameraSystem.inspectable && cameraSystem.inspectable.el;

if (inspectedEl) {
const object = objects.find(o => {
if (!o.el.isEntity) {
return o.el === inspectedEl.eid;
} else {
return o.el === inspectedEl;
}
});
const object = objects.find(o => o.el === inspectedEl);

if (object) {
setSelectedObject(object);
Expand Down
31 changes: 17 additions & 14 deletions src/react-components/room/object-hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState, useCallback, useMemo } from "react";
import { removeNetworkedObject } from "../../utils/removeNetworkedObject";
import { shouldUseNewLoader } from "../../utils/bit-utils";
import { rotateInPlaceAroundWorldUp, affixToWorldUp } from "../../utils/three-utils";
import { getPromotionTokenForFile } from "../../utils/media-utils";
import { hasComponent } from "bitecs";
Expand All @@ -12,23 +13,23 @@ export function isMe(object) {
}

export function isPlayer(object) {
if (object.el.isEntity) {
return !!object.el.components["networked-avatar"];
} else {
if (shouldUseNewLoader()) {
// TODO Add when networked avatar is migrated
return false;
} else {
return !!object.el.components["networked-avatar"];
}
}

export function getObjectUrl(object) {
let url;
if (object.el.isEntity) {
if (shouldUseNewLoader()) {
const urlSid = MediaInfo.accessibleUrl[object.el];
url = APP.getString(urlSid);
} else {
const mediaLoader = object.el.components["media-loader"];
url =
mediaLoader && ((mediaLoader.data.mediaOptions && mediaLoader.data.mediaOptions.href) || mediaLoader.data.src);
} else {
const urlSid = MediaInfo.accessibleUrl[object.el];
url = APP.getString(urlSid);
}

if (url && !url.startsWith("hubs://")) {
Expand Down Expand Up @@ -62,10 +63,12 @@ export function usePinObject(hubChannel, scene, object) {
}, [isPinned, pinObject, unpinObject]);

useEffect(() => {
const el = object.el;

// TODO Add when pinning is migrated
if (!el.isEntity) return;
if (shouldUseNewLoader()) {
return;
}

const el = object.el;

function onPinStateChanged() {
setIsPinned(getPinnedState(el.eid));
Expand All @@ -83,7 +86,7 @@ export function usePinObject(hubChannel, scene, object) {

let userOwnsFile = false;

if (!el.isEntity) {
if (shouldUseNewLoader()) {
// TODO Add when pinning is migrated
return false;
} else if (el.components["media-loader"]) {
Expand Down Expand Up @@ -131,10 +134,10 @@ export function useGoToSelectedObject(scene, object) {

export function useRemoveObject(hubChannel, scene, object) {
const removeObject = useCallback(() => {
if (object.el.isEntity) {
removeNetworkedObject(scene, object.el);
} else {
if (shouldUseNewLoader()) {
deleteTheDeletableAncestor(APP.world, object.el);
} else {
removeNetworkedObject(scene, object.el);
}
}, [scene, object]);

Expand Down
71 changes: 38 additions & 33 deletions src/systems/camera-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ const customFOV = qsGet("fov");
const enableThirdPersonMode = qsTruthy("thirdPerson");
import { Layers } from "../camera-layers";
import { Inspectable } from "../bit-components";
import { findAncestorWithComponent } from "../utils/bit-utils";

function getInspectableInHierarchy(elOrEid) {
if (elOrEid.isEntity) {
let inspectable = elOrEid;
while (inspectable) {
if (isTagged(inspectable, "inspectable")) {
return inspectable.object3D;
}
inspectable = inspectable.parentNode;
}
import { findAncestorWithComponent, shouldUseNewLoader } from "../utils/bit-utils";

function getInspectableInHierarchy(eid) {
let inspectable = findAncestorWithComponent(APP.world, Inspectable, eid);
if (!inspectable) {
console.warn("could not find inspectable in hierarchy");
return elOrEid.object3D;
} else {
let inspectable = findAncestorWithComponent(APP.world, Inspectable, elOrEid);
if (!inspectable) {
console.warn("could not find inspectable in hierarchy");
inspectable = elOrEid;
inspectable = eid;
}
return APP.world.eid2obj.get(inspectable);
}

function getInspectableInHierarchyAframe(el) {
let inspectable = el;
while (inspectable) {
if (isTagged(inspectable, "inspectable")) {
return inspectable.object3D;
}
return APP.world.eid2obj.get(inspectable);
inspectable = inspectable.parentNode;
}

console.warn("could not find inspectable in hierarchy");
return el.object3D;
}

function pivotFor(el) {
Expand All @@ -48,16 +48,16 @@ function pivotFor(el) {
return child.object3D;
}

function getInspectableAndPivot(elOrEid) {
const inspectable = getInspectableInHierarchy(elOrEid);
let pivot;
if (elOrEid.isEntity) {
pivot = pivotFor(inspectable.el);
} else {
function getInspectableAndPivot(el) {
if (shouldUseNewLoader()) {
const inspectable = getInspectableInHierarchy(el.eid);
// TODO Add support for pivotFor (avatars only)
pivot = inspectable;
return { inspectable, pivot: inspectable };
} else {
const inspectable = getInspectableInHierarchyAframe(el);
const pivot = pivotFor(inspectable.el);
return { inspectable, pivot };
}
return { inspectable, pivot };
}

const decompose = (function () {
Expand Down Expand Up @@ -264,15 +264,17 @@ export class CameraSystem {
this.mode = NEXT_MODES[this.mode] || 0;
}

inspect(elOrEid, distanceMod, fireChangeEvent = true) {
const { inspectable, pivot } = getInspectableAndPivot(elOrEid);

inspect(el, distanceMod, fireChangeEvent = true) {
this.verticalDelta = 0;
this.horizontalDelta = 0;
this.inspectZoom = 0;

if (this.mode === CAMERA_MODE_INSPECT) {
return;
}

const { inspectable, pivot } = getInspectableAndPivot(el);

const scene = AFRAME.scenes[0];
scene.object3D.traverse(ensureLightsAreSeenByCamera);
scene.classList.add("hand-cursor");
Expand All @@ -294,12 +296,15 @@ export class CameraSystem {
this.viewingCamera.updateMatrices();
this.snapshot.matrixWorld.copy(this.viewingRig.object3D.matrixWorld);

let preventAudioBoost = false;
if (inspectable.el) {
preventAudioBoost = isTagged(inspectable.el, "preventAudioBoost");
} else {
let preventAudioBoost;

if (shouldUseNewLoader()) {
// TODO Add when avatar is migrated
preventAudioBoost = false;
} else {
preventAudioBoost = inspectable.el && isTagged(inspectable.el, "preventAudioBoost");
}

this.snapshot.audio = !preventAudioBoost && getAudio(inspectable);
if (this.snapshot.audio) {
this.snapshot.audio.updateMatrices();
Expand Down
Loading

0 comments on commit 782e006

Please sign in to comment.