ViewportFrame Camera System

Note: The early versions are not optimized for usage in a full game.
Side note: When recording, I drop my FPS. When not recording, it’s a steady 60 FPS.

After @LordMerc created his camera system, I wanted to create my own.

Version 1

My system has a few important features that his did not:

  • It has priority rendering. This means that not all objects are created equal.

This is very important, because it allows us to save on performance. For example, player characters are always rendered (even when hidden) and updated at the max FPS (Heartbeat). This is to ensure that players move smoothly, and never jitter out of existence. However, the Baseplate (if there is one) is updated at 10 FPS. The Baseplate almost never moves, whereas the player constantly does, so we can get away with checking the player movement more than the Baseplate.

See how my character is smoother than the falling boxes?
http://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/3/7/2/372e01adc3442644bae8c252f130f1663242269a.mp4

  • It updates, not recreates.

A naive implementation would clear all children of the ViewportFrame and then redo it every frame. Mine creates the Object, and leaves it in there (unless it moves out of the Camera’s view) and updates the CFrame or Transparency when it needs to be, rather than every frame. Because I check for changes, I can also set the FPS of the object (How often I check). This allows the priority rendering.

Also, in addition to the objects, it doesn’t also set the Camera CFrame every frame. It uses the :GetPropertyChangedSignal(“CFrame”) to update the Camera only when needed.
http://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/c/a/7/ca7487868efde2922dc6b6aaa5a74000a5ba570b.mp4

  • Supports Transparency

Mine allows parts to be counted as visible if they are behind objects with Transparency above 0.1. Under 0.1 Isn’t enough to see well, so it’s not worth rendering. Gotta save where you can :sweat_smile:
http://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/9/2/c/92ccd012a64f42007b6efbefb209f4b687901b76.mp4
At 0.1, you can see that the crate and barracks aren’t there, but only if you look hard enough.
At 0.7, it knows to render through it and adds them in (and they’ll be updated at 20FPS)

  • Model rendering

If any part within a model is visible, I render the whole model. This is actually faster than rendering only the visible parts, because I only have to check the first couple parts of a model before deciding to render. Fewer raycasts!

I’ve decided to open source my code (and I tried to comment it clearly) (sorry for the mess) so that we can learn from each other! Please critique, give suggestions, and report bugs!
CameraViewPortV2.rbxl (249.9 KB)

Version 2

The wonderful @sircfenner has pointed out to me that because the combination of raycasting, screen projection, and GetDescendants takes so much power, checking for visibility is actually slower! Relying on the ViewportFrame to decide what to render would be faster!
However, one cannot simply just keep cloning the entire workspace! Thus I proudy present… a new version!

This version still has this feature:

A naive implementation would clear all children of the ViewportFrame and then redo it every frame. Mine creates the Object, and leaves it in there and updates the Properties when it needs to be, rather than every frame.

Now, why is this one so special?
It doesn’t lower your FPS! Instead, it lowers its own FPS, updating less but keeping your game smooth!
However, it will still update players at max FPS, even if the rest is slowed down. Priorities!
Here’s an example:
http://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/6/8/b/68bcee1587e0e703551ffa521b61f28c3d971e7b.mp4
Note how the game is a perfect 60FPS (Even while recording!), and the player in the ViewportFrame is 60FPS, but the rest is slowed down a little.

There is instructions commented in the code on how to switch to update everything (not just players) at Max FPS. All you have to do is comment/uncomment the lines as instructed.

Here’s the file!
CameraViewPortV3.rbxl (160.9 KB)

Version 3

I polished up and optimized this rendering technique for a Dual Render scope system. (Super cool, go check it out!) Once I did that, I turned the polished version into a security camera system again!

This is running at 60 FPS with a giant, detailed map:
https://www.youtube.com/watch?v=MtaRQ450_iw

Here’s the file:
Security Camera System.rbxl (1.1 MB)

Edit: Using my new ViewportFrame Handler module, I rewrote this camera system.

It went from hundreds of lines to under 50 lines!

New file is in post is marked as solution below.




Enjoying my work? I love to create and share with the community, for free.

If you’d like to help fund my work, consider sponsoring me on GitHub/Patreon or donating on BuyMeACoffee/PayPal!

332 Likes

Do you think that ViewportFrames could be performant enough to create a scope system like that from escape from tarkov?

9 Likes

Almost certainly not (at least for now). But who knows what the future holds!

(I’ll try to do it when I get the next version up and running)

1 Like

Awesome work! Mine did had some issues as I was trying to improve the selective rendering but didn’t have a lot of time due to college and work pushing me away from deving, but I did get to a point where it was a bare-bones of sorts and hopefully enough for someone to learn and improve on.

4 Likes

Sadly the current build doesnt support a good render to do that, we must have to wait until Dual Render comes live.

1 Like

Definitely an improvement! I’m going to test this out in one of my games and report back with any bugs or glitches I may find.

1 Like

Seems to have an issue at times with this


image

I’ll look into that shortly.

I have to study for my physics midterm tomorrow :grimacing:

4 Likes

Is that the new version?

Yea, I was testing the limits at one of my games and noticed that. It wasn’t replicating the character at all.

Still got the same error.


Maybe try seeing if it has the CFrame property in a pcall?

1 Like

Reuploaded to OP, try the new file!

2 Likes

Yep, fixed!

2 Likes

As promised, I gave it a shot.


The render does have some distortion, and it really bugs me.

18 Likes

That has got to be the best P.I.P. emulation I’ve seen on Roblox yet.
Good work!

Edit:
Can you add a variable for determining framerate? I’m curious to see if it’s possible to get a constant 60FPS.

2 Likes

I was getting about ~55 there. You can see in the side frame:
Graphics->Heartbeat->56.3/s

Mmm, same here.

I’m just wondering if you could increase the FPS for non-NPC / Player models. A previous implementation of this that hid obscured objects was able to do 60 FPS in the rendered security camera - once I disabled the obscuring element of it of course.

Just wondering if you could add in a variable to prioritize players to always be at 60 FPS, and then everything else run at a set framerate. I.e. if the developer sets it to be 10 FPS, everything in the camera feed would move at 10 FPS, except for players. But if they set it at something higher like 60, then everything would run at 60. With “prioritize players” the players would always move at 60 FPS, regardless of the FPS setting.

Sure. Give me 40(?) minutes and I will try my best to get that done!

1 Like

Alright, awesome!

This is a really nice model, by the way! :slight_smile:

1 Like

You’re amazing! Where would you describe your hardware on a scale from low end to high end? I want to know if a scope system like that is viable the majority of the roblox playerbase.

2 Likes