Skip to content

Commit

Permalink
Add spherical projection flag
Browse files Browse the repository at this point in the history
Also, pass mediaLoader flags to loaders.
  • Loading branch information
johnshaughnessy committed Jan 20, 2023
1 parent a5ed4a0 commit ea32b37
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 31 deletions.
25 changes: 12 additions & 13 deletions src/bit-systems/media-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ import { loadPDF } from "../utils/load-pdf";
import { MediaType, mediaTypeName, resolveMediaInfo } from "../utils/media-utils";
import { EntityID } from "../utils/networking-types";

// TODO Should every loader have the same signature ( HubsWorld, flags, MediaInfo )?
const loaderForMediaType = {
[MediaType.IMAGE]: (
world: HubsWorld,
{ accessibleUrl, contentType }: { accessibleUrl: string; contentType: string }
) => loadImage(world, accessibleUrl, contentType),
[MediaType.VIDEO]: (world: HubsWorld, { accessibleUrl }: { accessibleUrl: string }) =>
loadVideo(world, accessibleUrl),
[MediaType.MODEL]: (
world: HubsWorld,
{ accessibleUrl, contentType }: { accessibleUrl: string; contentType: string }
) => loadModel(world, accessibleUrl, contentType, true),
[MediaType.PDF]: (world: HubsWorld, { accessibleUrl }: { accessibleUrl: string }) => loadPDF(world, accessibleUrl)
[MediaType.IMAGE]: (world: HubsWorld, flags: number, { accessibleUrl, contentType }: MediaInfo) =>
loadImage(world, flags, accessibleUrl, contentType),
[MediaType.VIDEO]: (world: HubsWorld, flags: number, { accessibleUrl }: MediaInfo) =>
loadVideo(world, flags, accessibleUrl),
[MediaType.MODEL]: (world: HubsWorld, flags: number, { accessibleUrl, contentType }: MediaInfo) =>
loadModel(world, flags, accessibleUrl, contentType, true),
[MediaType.PDF]: (world: HubsWorld, flags: number, { accessibleUrl }: MediaInfo) =>
loadPDF(world, flags, accessibleUrl)
};

export const MEDIA_LOADER_FLAGS = {
RECENTER: 1 << 0,
RESIZE: 1 << 1,
ANIMATE_LOAD: 1 << 2,
IS_OBJECT_MENU_TARGET: 1 << 3
IS_OBJECT_MENU_TARGET: 1 << 3,
SPHERICAL_PROJECTION: 1 << 4
};

function resizeAndRecenter(world: HubsWorld, media: EntityID, eid: EntityID) {
Expand Down Expand Up @@ -138,7 +137,7 @@ function* loadMedia(world: HubsWorld, eid: EntityID) {
if (!loader) {
throw new UnsupportedMediaTypeError(eid, urlData.mediaType);
}
media = yield* loader(world, urlData);
media = yield* loader(world, MediaLoader.flags[eid], urlData);
} catch (e) {
console.error(e);
media = renderAsEntity(world, ErrorObject());
Expand Down
3 changes: 2 additions & 1 deletion src/bit-systems/object-spawner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function* spawnObjectJob(world: HubsWorld, spawner: EntityID) {
recenter: false,
resize: false,
animateLoad: false,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
});

if (ObjectSpawner.flags[spawner] & OBJECT_SPAWNER_FLAGS.APPLY_GRAVITY) {
Expand Down
2 changes: 1 addition & 1 deletion src/bit-systems/scene-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function* loadScene(
throw new Error("Scene loading failed. No src url provided to load.");
}

const { value: scene, canceled } = yield* cancelable(loadModel(world, src, "model/gltf", false), signal);
const { value: scene, canceled } = yield* cancelable(loadModel(world, 0, src, "model/gltf", false), signal);
if (canceled) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/inflators/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function inflateImageLoader(world: HubsWorld, eid: number, params: ImageL
recenter: false,
resize: false,
animateLoad: false,
isObjectMenuTarget: false
isObjectMenuTarget: false,
sphericalProjection: params.projection === ProjectionMode.SPHERE_EQUIRECTANGULAR
});

// TODO: Use projection
Expand Down
4 changes: 3 additions & 1 deletion src/inflators/media-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ export type MediaLoaderParams = {
recenter: boolean;
animateLoad: boolean;
isObjectMenuTarget: boolean;
sphericalProjection: boolean;
};

export function inflateMediaLoader(
world: HubsWorld,
eid: number,
{ src, recenter, resize, animateLoad, isObjectMenuTarget }: MediaLoaderParams
{ src, recenter, resize, animateLoad, isObjectMenuTarget, sphericalProjection }: MediaLoaderParams
) {
addComponent(world, MediaLoader, eid);
let flags = 0;
if (recenter) flags |= MEDIA_LOADER_FLAGS.RECENTER;
if (resize) flags |= MEDIA_LOADER_FLAGS.RESIZE;
if (animateLoad) flags |= MEDIA_LOADER_FLAGS.ANIMATE_LOAD;
if (isObjectMenuTarget) flags |= MEDIA_LOADER_FLAGS.IS_OBJECT_MENU_TARGET;
if (sphericalProjection) flags |= MEDIA_LOADER_FLAGS.SPHERICAL_PROJECTION;
MediaLoader.flags[eid] = flags;
MediaLoader.src[eid] = APP.getSid(src)!;
}
3 changes: 2 additions & 1 deletion src/inflators/pdf-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function inflatePDFLoader(world: HubsWorld, eid: number, params: PDFLoade
recenter: false,
resize: false,
animateLoad: false,
isObjectMenuTarget: false
isObjectMenuTarget: false,
sphericalProjection: false
});

// TODO Should the PDF be controlled by users? (Should it be a PDFMenuTarget?)
Expand Down
3 changes: 2 additions & 1 deletion src/inflators/spawner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function inflateSpawner(world: HubsWorld, eid: number, props: SpawnerPara
recenter: false,
resize: false,
animateLoad: false,
isObjectMenuTarget: false
isObjectMenuTarget: false,
sphericalProjection: false
});

addComponent(world, HandCollisionTarget, eid);
Expand Down
3 changes: 2 additions & 1 deletion src/inflators/video-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function inflateVideoLoader(world: HubsWorld, eid: number, params: VideoL
recenter: false,
resize: false,
animateLoad: false,
isObjectMenuTarget: false
isObjectMenuTarget: false,
sphericalProjection: params.projection === ProjectionMode.SPHERE_EQUIRECTANGULAR
});

// TODO: Use the rest of VideoLoaderParams
Expand Down
9 changes: 6 additions & 3 deletions src/load-media-on-paste-or-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function spawnFromUrl(text: string) {
recenter: true,
resize: true,
animateLoad: true,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
});
const avatarPov = (document.querySelector("#avatar-pov-node")! as AElement).object3D;
const obj = APP.world.eid2obj.get(eid)!;
Expand All @@ -51,7 +52,8 @@ async function spawnFromFileList(files: FileList) {
recenter: true,
resize: true,
animateLoad: true,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
};
})
.catch(e => {
Expand All @@ -61,7 +63,8 @@ async function spawnFromFileList(files: FileList) {
recenter: true,
resize: true,
animateLoad: true,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
};
});

Expand Down
3 changes: 2 additions & 1 deletion src/message-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export default class MessageDispatch extends EventTarget {
resize: true,
recenter: true,
animateLoad: true,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
});
const obj = APP.world.eid2obj.get(eid);
obj.position.copy(avatarPov.localToWorld(new THREE.Vector3(0, 0, -1.5)));
Expand Down
8 changes: 6 additions & 2 deletions src/utils/load-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import { renderAsEntity } from "../utils/jsx-entity";
import { HubsWorld } from "../app";
import { Texture } from "three";
import { AlphaMode } from "./create-image-mesh";
import { MEDIA_LOADER_FLAGS } from "../bit-systems/media-loading";

export function* loadImage(world: HubsWorld, url: string, contentType: string) {
export function* loadImage(world: HubsWorld, flags: number, url: string, contentType: string) {
const { texture, ratio, cacheKey }: { texture: Texture; ratio: number; cacheKey: string } =
yield loadTextureCancellable(url, 1, contentType);
const projection =
flags & MEDIA_LOADER_FLAGS.SPHERICAL_PROJECTION ? ProjectionMode.SPHERE_EQUIRECTANGULAR : ProjectionMode.FLAT;

return renderAsEntity(
world,
<entity
name="Image"
image={{
texture,
ratio,
projection: ProjectionMode.FLAT,
projection,
alphaMode: AlphaMode.Opaque,
cacheKey
}}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/load-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HubsWorld } from "../app";
import { loadModel as loadGLTFModel } from "../components/gltf-model-plus";
import { renderAsEntity } from "../utils/jsx-entity";

export function* loadModel(world: HubsWorld, src: string, contentType: string, useCache: boolean) {
export function* loadModel(world: HubsWorld, _flags: number, src: string, contentType: string, useCache: boolean) {
// TODO: Write loadGLTFModelCancelable
const { scene, animations } = yield loadGLTFModel(src, contentType, useCache, null);

Expand Down
4 changes: 3 additions & 1 deletion src/utils/load-pdf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import * as pdfjs from "pdfjs-dist";
import { HubsWorld } from "../app";
import { createElementEntity, renderAsEntity } from "../utils/jsx-entity";

export function* loadPDF(world: HubsWorld, url: string) {
export function* loadPDF(world: HubsWorld, _flags: number, url: string) {
const pdf = (yield pdfjs.getDocument(url).promise) as pdfjs.PDFDocumentProxy;

// TODO Should we create the canvas texture here and load/render the first page?

// TODO Use flags to determine whether this should be a controllable object

return renderAsEntity(
world,
<entity name="PDF" networked grabbable={{ cursor: true, hand: false }} pdf={{ pdf }}></entity>
Expand Down
7 changes: 5 additions & 2 deletions src/utils/load-video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { VideoTexture } from "three";
import { renderAsEntity } from "../utils/jsx-entity";
import { loadVideoTexture } from "../utils/load-video-texture";
import { HubsWorld } from "../app";
import { MEDIA_LOADER_FLAGS } from "../bit-systems/media-loading";

export function* loadVideo(world: HubsWorld, url: string) {
export function* loadVideo(world: HubsWorld, flags: number, url: string) {
const { texture, ratio }: { texture: VideoTexture; ratio: number } = yield loadVideoTexture(url);
const projection =
flags & MEDIA_LOADER_FLAGS.SPHERICAL_PROJECTION ? ProjectionMode.SPHERE_EQUIRECTANGULAR : ProjectionMode.FLAT;

return renderAsEntity(
world,
Expand All @@ -20,7 +23,7 @@ export function* loadVideo(world: HubsWorld, url: string) {
texture,
ratio,
autoPlay: true,
projection: ProjectionMode.FLAT
projection
}}
></entity>
);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/message-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export function messageForLegacyRoomObjects(objects: LegacyRoomObject[]) {
resize: true,
recenter: true,
animateLoad: true,
isObjectMenuTarget: true
isObjectMenuTarget: true,
sphericalProjection: false
};
const createMessage: CreateMessage = [nid, "media", initialData];
message.creates.push(createMessage);
Expand Down

0 comments on commit ea32b37

Please sign in to comment.