Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mirror to bitecs #5919

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bit-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@ export const VideoTextureTarget = defineComponent({
flags: Types.ui8
});
export const SimpleWater = defineComponent();
export const Mirror = defineComponent();
53 changes: 53 additions & 0 deletions src/inflators/mirror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { addComponent } from "bitecs";
import { PlaneGeometry } from "three";
import { Reflector } from "three/examples/jsm/objects/Reflector";
import { addObject3DComponent } from "../utils/jsx-entity";
import { Mirror } from "../bit-components";
import { HubsWorld } from "../app";
import { Layers } from "../camera-layers";

const DEFAULT_MIRROR_GEOMETRY = new PlaneGeometry();
// We may need to revisit the default texture size
// because the full window resolution may be too huge.
const DEFAULT_TEXTURE_WIDTH = window.innerWidth * window.devicePixelRatio;
const DEFAULT_TEXTURE_HEIGHT = window.innerHeight * window.devicePixelRatio;

export type MirrorParams = {
color?: string;
};

const DEFAULTS = {
color: '#7f7f7f'
};

export function inflateMirror(world: HubsWorld, eid: number, params: MirrorParams) {
params = Object.assign({}, DEFAULTS, params);
const geometry = DEFAULT_MIRROR_GEOMETRY;
Copy link
Contributor Author

@takahirox takahirox Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old code, this.el.object3DMap?.mesh?.geometry is used if defined.
https://github.com/mozilla/hubs/blob/40bbcc41c6b5c2eac2982b0a8bf2bbebd44db195/src/components/mirror.js#L24

In the new framework, is there a chance that geometry is already defined when inflating, too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since mirror is currently only exposed in Spoke, and Spoke doesn't do this, I don't think we need to support that right now.

Just to think through what it would look like if we wanted to support this later): It would be slightly tricky since in infiltrators you can not access the Object3D for the entity you are inflating on (since inflators may be what creates the Object3D) so we would need to instead write a system that swaps the existing mesh with a Reflector using the same Geometry.

const reflector = new Reflector(geometry, {
color: params.color,
textureWidth: DEFAULT_TEXTURE_WIDTH,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not new and we don't necessarily need to fix this right now, but I quite dislike that we make this so high resolution... Not sure what a more sane value would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably lower resolution will be better. Added a comment for now.

textureHeight: DEFAULT_TEXTURE_HEIGHT
});

// HACK the first time we render this, add the appropriate camera layers
// to the virtual camera. There is no other way to easily access camera
// used for Reflector. We may remove with Three.js r149 or newer because
// Reflector will expose camera.
const originalOnBeforeRender = reflector.onBeforeRender;
reflector.onBeforeRender = function (renderer, scene, camera, geometry, material, group) {
const originalRender = renderer.render;
renderer.render = function (scene, camera) {
camera.layers.enable(Layers.CAMERA_LAYER_THIRD_PERSON_ONLY);
camera.layers.enable(Layers.CAMERA_LAYER_VIDEO_TEXTURE_TARGET);
reflector.onBeforeRender = originalOnBeforeRender;
originalRender.call(renderer, scene, camera);
};
originalOnBeforeRender(renderer, scene, camera, geometry, material, group);
renderer.render = originalRender;
};

addObject3DComponent(world, eid, reflector);
addComponent(world, Mirror, eid);
return eid;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated code with https://github.com/mozilla/hubs/blob/master/src/components/mirror.js I thought it would be ok because not so many codes and src/components/mirror.js may be removed when the new framework will land.

If we want to avoid duplicated code we can add a new file and let src/inflators/mirror.ts and src/components/mirror.js refer to it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its ok, since the plan is to remove the old code once we remove the newLaoder flag and flip to using the new system by default.

3 changes: 3 additions & 0 deletions src/systems/remove-object3D-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MediaFrame,
MediaImage,
MediaVideo,
Mirror,
Object3DTag,
SimpleWater,
Skybox,
Expand Down Expand Up @@ -74,6 +75,7 @@ const cleanupSimpleWaters = cleanupObjOnExit(SimpleWater, obj => {
obj.material.dispose();
}
});
const cleanupMirrors = cleanupObjOnExit(Mirror, obj => obj.dispose());

// TODO This feels messy and brittle
//
Expand Down Expand Up @@ -135,6 +137,7 @@ export function removeObject3DSystem(world) {
cleanupAudioEmitters(world);
cleanupSkyboxes(world);
cleanupSimpleWaters(world);
cleanupMirrors(world);

// Finally remove all the entities we just removed from the eid2obj map
entities.forEach(removeObjFromMap);
Expand Down
8 changes: 6 additions & 2 deletions src/utils/jsx-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
NetworkedFloatyObject,
Billboard,
MaterialTag,
VideoTextureSource
VideoTextureSource,
Mirror
} from "../bit-components";
import { inflateMediaLoader } from "../inflators/media-loader";
import { inflateMediaFrame } from "../inflators/media-frame";
Expand Down Expand Up @@ -73,6 +74,7 @@ import { inflateVideoTextureTarget, VideoTextureTargetParams } from "../inflator
import { inflateUVScroll, UVScrollParams } from "../inflators/uv-scroll";
import { SimpleWaterParams, inflateSimpleWater } from "../inflators/simple-water";
import { inflatePDF, PDFParams } from "../inflators/pdf";
import { MirrorParams, inflateMirror } from "../inflators/mirror";

preload(
new Promise(resolve => {
Expand Down Expand Up @@ -225,6 +227,7 @@ export interface ComponentData {
grabbable?: GrabbableParams;
billboard?: { onlyY: boolean };
simpleWater?: SimpleWaterParams;
mirror?: MirrorParams;
}

export interface JSXComponentData extends ComponentData {
Expand Down Expand Up @@ -366,7 +369,8 @@ export const commonInflators: Required<{ [K in keyof ComponentData]: InflatorFn

// inflators that create Object3Ds
directionalLight: inflateDirectionalLight,
simpleWater: inflateSimpleWater
simpleWater: inflateSimpleWater,
mirror: inflateMirror
};

const jsxInflators: Required<{ [K in keyof JSXComponentData]: InflatorFn }> = {
Expand Down