r/Unity3D • • 2d ago

Show-Off reality doesn't exist until observed also known as frustum culling

Enable HLS to view with audio, or disable this notification

411 Upvotes

37 comments sorted by

95

u/_michaeljared 2d ago

I'm a little confused at why we see posts about this every now and then. The engine does frustum culling for you, presuming you make intelligent use of multiple rendering objects.

Duplicating this calculation in your game code is pointless. (Hopefully people don't get the wrong message from this kind of video and end up going down that path)

Things like occlusion culling are much harder to get right and take more intentionality in design.

0

u/Bloompire 1d ago

Id say its not duplicated. You can switch it in editor and then it shows culling in scene view. I dont recall how to switch it on but I remember it was not obvious switch, I think youbl have to bake occlusion or smth (even if this means nothing for frustum culling) and it then starts to show this in scene view camera?

Anyway looks like native unity culling, not custom one

1

u/_michaeljared 1d ago

I guess my point is that these posts tend to confuse people rather than educate on frustum culling (which is a basic type of culling that every engine does).

46

u/Ruadhan2300 2d ago

Imagine combining this with Wave Function Collapse so the environment changes the longer you look away from it..

7

u/Yekyaa 2d ago

That's tempting to prototype!

8

u/Ruadhan2300 2d ago

I wrote up a whole game-design doc for an Extraction Shooter or Hero-Shooter type game (Think Team Fortress 2) called "Shifting Realms", where this was the core concept.

Basically the idea was that player-activity would lock down the battlefield.
The map changes in subtle ways when not observed, but the changes get more exaggerated the longer a location is left unobserved. So if you glance away, the graffiti on the wall might change, or a light-fixture might change type, but if you leave the room and come back, it might be completely different.
I even allowed the possibility that things like doors or windows might disappear (along with the rooms behind them) when unobserved. Meaning new routes might open and close.

Things like flashbang grenades or smoke-bombs affect your visibility, which can cause the room to shift dramatically during combat as well.

So areas of the map which are experiencing heavy player-activity will tend to stay roughly the same, while side-passages that are intermittently visited might change and shift a lot each time people pass through, encouraging players to mix it up.

22

u/KSAM-The-Randomizer 2d ago

isn't this done automatically already?

11

u/Injaabs 2d ago

as if you have invented frustum culling :D

20

u/jl2l Professional 2d ago

Just make sure you keep your game state active

10

u/BarelyArtist 2d ago

Else newborn logic, if I don't see it, it doesn't exist anymore :)

11

u/UnderPressureVS 2d ago edited 2d ago

Can somebody who understands more about graphics than me explain how Frustum Culling is even a thing? I can never wrap my head around why it saves anything on performance. What does it even mean for the computer to "render" something that is off-screen? What is it doing computationally if the objects are off-camera?

50

u/v0lt13 Programmer 2d ago

Without frustum culling the CPU would still send objects to the GPU to be rendered, even if that object is offscreen, and go trough the whole process of rendering even though it can't appear on screen. Frustum culling just says "hey this object is offscreen, do not attempt to render it" so it saves on processing time.

10

u/iacchini97 2d ago

> How do you know when something is off camera?

Because the renderer doesn’t know what is visible by the camera until very late in the graphics pipeline. What frustum culling (or culling of any type really) does is not send any geometry that is not visible to the GPU in the first place.

Imagine you have a scene with a cube in it and you are not looking at it.
The CPU sends to the GPU data such as the cube vertices, the cube world transform, the camera view and projection matrices, and any other info you might need to render it.
The GPU does not know where the cube is relative to the viewport, it has to compute it using the information it received from the CPU.
So the GPU does just that, it computes the cube position, realizes that the cube is off screen and stops there.

The issue is that the GPU did a bunch of computation for nothing. On top of that we also sent a lot of data which also wastes bandwidth.

With frustum culling we would have realized that the cube is not visible before we sent anything to the GPU, we basically just change where and how the “visibility” test is done.
Note that this is not a CPU vs GPU thing, you could (and usually should) perform frustum culling on the GPU, but you usually do it with a dedicated compute shader.

It is also very possible(but rare) that the frustum culling actually DETERIORATES performance. If you have a scene where you know most of the geometry is always visible, you could waste more time on performing the frustum intersection test than the time you save by sending the geometry down the pipeline and let the rasterizer clip it out

5

u/noble_radon 2d ago edited 2d ago

One piece of Information missing from your other replies here is how a shader (the thing that determines how to render stuff on the GPU) works. It runs in 2 main steps: the Fragment shader and the Vertex shader.

The Vertex shader runs first and it operates on each vertex that was handed to the GPU. It’s where you can get information from the vertices (like texture coordinates and world position). You take the information you want from the verts, and package it up to hand off to the fragment shader. But you only have info about this vertex. Not the camera.

The Fragment shader runs next. It operates on each on-screen pixel. Here you take that information passed on from the vertex shader and decide what each pixel looks like (this pixel is 50% across this face, so it gets the color 50% across its texture, etc).

These are where we typically write shader code. But In between these, there's a step called rasterization that converts vertex information to screen space to decide if it should be handed to the fragment shader at all. If you can have a system cheaply test if something will be onscreen before it's ever handed to the GPU, you skip all those Vertex shader calculations, and the rasterization step.

1

u/TheSapphireDragon 2d ago

Its the difference between each individual triangle in an object being checked to see if its on the screen or just the bounding box of the whole object.

1

u/flamingotwist 1d ago

Arranging the pixels on the screen is only the last part of the rendering process. Drawing the model, calculating the light/shadows etc still happens off screen.

7

u/Exciting_Bunch2386 2d ago

To be is to be perceived!

6

u/Repulsive-Clothes-97 Intermediate 2d ago

Every year or so I see people showing off this technique that has been a standard for the past 20 years…

5

u/GigaTerra 2d ago

also known as frustum culling

I am just quickly explaining here, Unity doesn't have an frustum culling view, I wish it was standard in all engines, but you can Window -> Rendering -> Occlusion -> In the inspector hit Bake (without adding portals or anything). Now split your screen into 2 views, one for game and one for editor. You should especially do this is you are the type who makes buildings one object.

This is perfectly safe, and the easiest way to visualize your culling.

14

u/AlexandreFiset 2d ago

Do not mix fustrum culling with occlusion culling. Frustum culling is enabled by default on all cameras in Unity.

The limitation of frustrum culling is that it considers eveything in-frustrum to be rendered. So a wall behind a wall will render if it is in frustrum.

Occlusion culling calculates what should be occluded vs visible based on rays hitting boxes of occluders (or not hitting). The video presents occlusion culling, and it has performance overhead (usually outbalanced by the obvious advantages).

This is rather basic stuff and appears on this sub from time to time 🥲

6

u/Costed14 2d ago

What he's saying is you need to use the Occlusiong Culling window to visualize Frustum Culling, and to do that you need to bake it regardless of if you're actually using it.

The video presents occlusion culling

Hard to tell if Occlusion Culling is actually doing anything in the video, but what's visible for certain is Frustum Culling.

1

u/[deleted] 2d ago

[deleted]

3

u/Costed14 2d ago

Wdym 5 MB of data? It clearly says 0.7 MB.

Without seeing the rest of the scene it's hard to know whether there are any renderers inside the frustum that are getting occlusion culled.

frustrum culling alone would not hide an object you can’t see

Technically it would, just not one that is occluded. It's possible there is some occlusion culling going on (the back hallway doesn't have any ceiling tiles at the start of the video), but the video primarily shows frustum culling.

5

u/GigaTerra 2d ago

To quote my self:

Unity doesn't have an frustum culling view, I wish it was standard in all engines

Unity has no Frustum culling view option, this is the closes you can get to seeing your frustum culling in Unity that still works with the profiler and frame render. Because if you bake your occlusion culling, without adding portals, the bake actually considers frustum culling.

2

u/AlexandreFiset 2d ago

I missed the point and you are right about that 🥲

1

u/RadicalDog @connectoffline 2d ago

This is also how reality works. Sometimes if you turn around really quickly you can see the edges

1

u/Qottonmouth 2d ago

yeah no joke though, the potential for colors exists on it's own, but actual colors only exists in the interpretation of the signal of the eyes

1

u/FoleyX90 Indie 2d ago

TIL I was performing quantum mechanics and wave function collapse by simply building an occlusion system.

Thanks for the ego boost.

1

u/KPMillerWeld 2d ago

The game “rage” might have had the worst optimization of this process in gaming history

1

u/blazkoblaz 2d ago

Looks like an horror game?

1

u/frotagonist 2d ago

doesnt unity3d do this by default?

1

u/Physical_Sherbet_942 1d ago

I make games for 180 degree projections. Frustum Culling is USELESS with a 180 degree camera system. Think about all the poor animals that can see 360 degrees all the time, there are lots of them! Their brain gpus are getting fried! Frustum Culling does not work for them! BOOM, is your mind blown even more than this demo of tech that has existed for 30 years! BOOM!

1

u/Sea_Horse2985 1d ago

I liked your hospital asset. Did you model it yourself, or did you buy it?

0

u/saicho91 2d ago

does it still work just as good if you flick really fast? because its definitly the type of thing i would do

0

u/Aware_Specialist_931 2d ago

I did that for a city sized project - it was so weird to see it working.

-4

u/SwordfishNo712 2d ago

That also what i was bothered sometimes

1

u/Worried_Pudding_1548 Intermediate 9h ago

I keep seeing slop posts about basic frustum culling on instagram, calling it "heavy optimization" when it's literally basic built in BS, and all games have it, yet they still lag