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

Bit media frames physics #6003

Merged
merged 16 commits into from
May 11, 2023
Merged

Bit media frames physics #6003

merged 16 commits into from
May 11, 2023

Conversation

keianhzo
Copy link
Contributor

@keianhzo keianhzo commented Mar 21, 2023

Built on top of #5978. Only the last commit applies.

As commented in a previous PR,

It's a bit unfortunate that we have to go back and forth between bitECS and Ammo to keep everything in sync. As the
entry point for the physics life cycle is the physics system, I wonder it would be better to encapsulate all the > component life cycle there and let that system manage it all. Instead of going through the hops of adding a component and have that intermediate bit-physics-system to map that to the physics system, just call the physics system and it will add/update the component under the hood and keep everything in sync.

Copy link
Contributor

@takahirox takahirox left a comment

Choose a reason for hiding this comment

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

I reviewed the PR mainly for bitecs design and readability. Most my comments are for future clean up and discussion, and don't block merging.

I haven't reviewed the core logic because I'm not familiar with media frames physics implementation. It may be ok if it keeps working because we focus on migration now. Or another reviewer may need to check.

Honestly bit-systems/media-loading.ts, systems/bit-media-frames (and others) are getting complex and harder to be maintained. But we may refactor them later because our development is at the migration phase now.

Copy link
Contributor

@netpro2k netpro2k left a comment

Choose a reason for hiding this comment

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

This is looking good. I am happy to see the media frame system didn't need to change all that much and most of the code changes there were are just around generating the preview and getting the object bounds (which is what you would expect)

I made a bunch of little suggestions and observations, but none are sticking out to me as extreme blockers. (I think the disposal stuff probably sticks out the most, but its mostly an issue of "correctness", since in reality media frames are very rarely removed except for at scene transition time when the objects they are previewing are also going away).

The object bounding box stuff still strikes me as incredibly more complicated than I would expect (not a slight on your code, its happened ever time we try and write code like it). I have a very strong suspicion we are just tripping over ourselves because of some underlying complexity, and my best bet is that its in shape generation code in the physics system. When I think back I sort of remember the "just write the obvious thing" version of that code working except "but the physics shape doesn't line up now"... Been a long time since I looked at it though so I could be miss-remembering. I think once we are down to 1 codepath for all this stuff and we have had time to rethink physics a bit, its worth a revisit.

src/bit-systems/media-loading.ts Outdated Show resolved Hide resolved
src/bit-systems/media-loading.ts Outdated Show resolved Hide resolved
src/bit-systems/media-loading.ts Outdated Show resolved Hide resolved
@@ -19,6 +20,39 @@ import { loadAudio } from "../utils/load-audio";
import { MediaType, mediaTypeName, resolveMediaInfo } from "../utils/media-utils";
import { EntityID } from "../utils/networking-types";

export const MediaContentBounds = (MediaLoaded as any).contentBounds as Map<EntityID, Vector3>;

const getBox = (() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This code definitely feels pretty funky, and I remember having similar feelings the first time similar code was written for original media. I think a lot of it was around getting physics shapes to line up, but I suspect thats all actually just a sympton f the physics code being wrong and then we bend over backwards to accommodate those weird assumptions in the rest of our code. Definitely wroth revisiting when we get around to a cleanup pass on physics.

src/bit-systems/media-loading.ts Outdated Show resolved Hide resolved
src/systems/bit-media-frames.js Outdated Show resolved Hide resolved
src/systems/bit-media-frames.js Outdated Show resolved Hide resolved
const isFrameDeleting = findAncestorWithComponent(world, Deleting, frame);
const isFrameOwned = hasComponent(world, Owned, frame);

if (captured && isCapturedOwned && !isCapturedHeld && !isFrameDeleting && colliding) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was slightly uneasy when I saw the addition of the Deleting component at the start of reviewing this PR. I see here we are using it to prevent scaling the object down as the media frame it was in gets shrunk during the delete animation. It definitely seems really odd to have to think about that delete animation in systems and this definitely wont be the last time we have to... Don't have a nicer solution off the top of my head but we should definitely brainstorm on this more.

@@ -229,10 +316,16 @@ function mediaTypesOf(world, entities) {
return mask;
}

export function cleanupMediaFrame(obj) {
obj.traverse(child => {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the wrapping function.

That said not sure this is correct. The only thing the media frame "owns" in terms of disposable resources is the geometry for the guide. The cloned preview object is all just references to geometries/textures that something else owns. Its actually incorrect to try and .dispose() a geometry/texture for an object we happened to be previewing just because this media frame is going away, since the object that actually "owns" those things is not going away.

With the way threejs resource management works you won't actually notice this issue since it will lazily re-create everything. So I think what will happen in practice is when you delete a media frame capturing an object we will delete the GPU side resources for them and then re-upload them to the GPU next frame.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oof, actually looking at the code some more, for videos we actually are creating a new Geometry so we technically do want to clean that up. We actually don't need a new geometry for each media frame and I am fine with statically keeping that around for the lifetime of the application so we shouldn't need to clean that up. Will add a note above about that as well.

Copy link
Contributor Author

@keianhzo keianhzo May 9, 2023

Choose a reason for hiding this comment

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

This should only be disposing the guide as the preview is not a child of the media frame and is removed when hidden so it shouldn't be affected by the cleanup. But you are right, the video geometry is not being cleaned up correctly. I didn't want to pre-allocate that to avoid allocating resources for something that can potentially never be used but maybe that's better that disposing it every time a preview is hidden so I'm going o do that.

previewMaterial.transparent = true;
previewMaterial.opacity = 0.5;

const geometry = createPlaneBufferGeometry(1, 1, 1, 1, TEXTURES_FLIP_Y);
Copy link
Contributor

Choose a reason for hiding this comment

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

I noted in the cleanup code, we would technically need to clean this up (since we are the ones creating a disposable resource), but we don't want to dipose of the previewMesh's geometry for the other paths (since we are just referencing them. Since we don't actually mutate this geometry at all we can just statically define it once and share it among all media frames, and never dispose() of it.

@keianhzo keianhzo requested a review from netpro2k May 9, 2023 17:47
@keianhzo keianhzo merged commit d707f9d into master May 11, 2023
10 of 12 checks passed
@keianhzo keianhzo deleted the bit-media-frames-physics branch May 11, 2023 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants