Rendering objects within a small frame

I am curious about how you can render things that are only visible to a portion of your camera viewport. Essentially, objects on the client will stop rendering if they are outside the given area that I have set.
I just need to be guided in the right direction and I will be fine!

Here’s my theory: I have to change the parent of the objects if the objects are not inside the camera frame with the use of WorldToViewport. I remember someone mentioning that you can get the position (XY) and the depth (Z) of the screen.

So… I am assuming it would be something like this…

if objectPosition > camera:WorldToViewport(objectPosition) then
--code
2 Likes

I don’t understand this question.

Could you rephrase it? Maybe add a hand drawn picture?

Is this good enough?

I’m pretty sure Roblox already does this as a regular part of rendering, right? I could be wrong, but I don’t see why anything would be rendered if it’s not going to be visible. I think that was the reasoning for the artefacts when you use the Glass material.

1 Like

I mean they sort of do, but if these objects have a behaviour or an action, they will continue doing them despite them not being ‘rendered’. Which has an impact on the performance - aka the thing I am combatting right now!

I think your best bet would be viewport frames. Anything else will not give you the desired cut off effect.

4 Likes

Like @EgoMoose said, VPF’s are the best way to go since they do practically what you are asking. It will still be tricky but it’s manageable.

The other (probably a lot worse) option is to manually determine what needs to be rendered. At this point, you would need to have a pseudo-map for all the objects in your game, updating as you move around so you meet this render border, while removing the objects you just passed. i.e., you have the original map waaaay out away from the player, and a second basic map that allows them to move around. If you can map the coordinates of the original map to the basic map, then you can clone objects from there and transfer them to be in the player’s view on their basic map.

Of course, this is pretty hacky but it may work if you do it correctly. I believe objects behave weirdly as they get too far in XYZ so that is something to consider as well.

Hope this helps

You think the solution will still be viable if these objects are always moving?

So the code would do

  1. Check if object is within frame - With WorldToViewport
  2. Put them inside viewport

No, I’d put all objects in the vpf which is fully sized to your screen but cropped by its smaller parent frame with clip descendants. Just set the camera to your current camera and it’ll do the rest for you.

Before we go full XY problem here could you take a second to talk about the specific problem you’re actually trying to solve? I’m sensing that you don’t actually want a ViewportFrame here despite that being what ostensibly solves the question you asked.

The backstory is that I have hundreds-thousands of NPCs, and I only want to render the ones that are close to me/ or the NPCs that my camera can see. Although I only want a small portion to be rendered at time, so if I could render them within a small box WITHIN the camera it would solve my problem.

  1. By “render” do you mean “replicate to the client” or mean “create instances for in the hierarchy”?

  2. Do you actually know that creating instances for all of them will be too slow? Have you already tried running it without this optimization? Because when something isn’t in your view frustum it costs very little, the rendering system is already avoiding doing much work for stuff outside of your camera view.

By pulling stuff into and out of the game hierarchy when you think it’s “not visible”, you may actually make things slower rather than faster because you’ll cause the rendering layer a whole bunch of extra recalculation work when you show / hide those instances.

A better solution would be to always have all of the NPCs “visible”, ie, having instances in the game hierarchy, but only update the ones that you determine are in the field of view, not doing any animations or anything for the others.

2 Likes

I meant replicate to the client; but I see what you mean. Thanks for the help.