Using distance or colliders to determine what to render - unity3d

I have some objects I only want to render within a certain distance of the player. The objects don't have any colliders originally, so I am wondering what would be best for performance. Do I:
Check distance between object and player and render within a cerain distance
Add a trigger around my player, add colliders to all objects (using project->physics to make sure the layers only look for each other) and use OnTriggerEnter/Exit to determine when to render the object?

You can use cameras near/far clipping planes (Frustum Culling) to adjust the distance for rendering.
If you are looking for something more advanced use occlusion culling or combination of both:
Occlusion Culling is a feature that disables rendering of objects when they are not currently seen by the camera
because they are obscured (occluded) by other objects. This does not happen automatically in 3D computer graphics since most of the time objects farthest away from the camera are drawn first and closer objects are drawn over the top of them (this is called “overdraw”). Occlusion Culling is different from Frustum Culling. Frustum Culling only disables the renderers for objects that are outside the camera’s viewing area but does not disable anything hidden from view by overdraw. Note that when you use Occlusion Culling you will still benefit from Frustum Culling.
You can also use Camera.layerCullDistances if you want different camera cull distances for different objects:
Normally Camera skips rendering of objects that are further away than farClipPlane. You can set up some Layers to use smaller culling distances using layerCullDistances. This is very useful to cull small objects early on, if you put them into appropriate layers.

Related

how can I make a player hidden by terrain always visible?

I am making a 3D isometric game, in my game, there is a lot of holes and the player can go into them, but the problem is when he goes into one of them he becomes invisible, I can,t move the camera it will change the concept I tried using shader like the one in this video but it makes the entire terrain transparent and glitchy (I am using unity terrain) so I am stuck and I need Idea how to make the layer visible inside the hole
What you're asking can be achieved in multiple ways, depending on your render pipeline and requirements.
If you're working with the Universal Render Pipeline (URP), you could create a Forward Renderer asset and create a custom render pass whenever your player is occluded by terrain.
You could assign a new Layer to the player, such as "Player", then select or deselect said mask from the Filters > Layer Mask properties of the Forward Render Data. Then assign the same or a custom material for when the player is occluded by terrain.
Alternatively, you could create either a cutout or a dither shader using Shader Graph, on which there are many tutorials
Camera GameObjects give you the option to select what layer you want them to "see" (render) using the culling mask. Think of layers as grouping GameObjects and giving that group a name.
You can have multiple cameras at the same time each one with a different name and different layer to render, or even change the viewing layer of a single camera depending on changes happening in the game.
Assign a layer tag in each of the terrain elements in your scene and have the camera render them accordingly and "cull" the rest.
Very helpful documentation on layers and camera culling mask.

Prevent explosion particle effect clipping through terrain

I have implemented blast particle within my game but when it spawns, it gets cutout to correspond to the nearest environment object.
This problem, I was getting:
Overall there are multiple particle systems are running to achieve this but I am attaching one particle system inspector panel details:
A similar kind of renderer exists for mostly all particle systems. So please guide me to solve the above effect cut related problem with the wall.
EDIT-1:
I have added a VFX rendering camera and created separate later too for the effects but there is no change in the result.
EDIT-2:
Here you have a screenshot for the Main Camera of the game:
You could add collisions to the particles so that they either bounce away from the world object or are just destroyed.
Example Collision Setup
It will add a little overhead but depending on how many particles the effect uses it shouldn't be an issue, and if it is you could test it with a lower collision quality.
I found this advice on the polycount forums written by user AlecMoody:
[1.] Create a second camera parented to the primary camera...
[2.] Create a render layer for your [particle effect] (or assign it to an existing layer).
[3.] Set the main camera culling mask to everything except your explosion layer.
[4.] Set the child camera culling mask to only be your explosion layer. The child camera should have the clear flag set to "don't clear"
[5.] and put a positive value into the [child] camera depth.
Since for you having a higher depth on the child camera doesn't make it render above the lower depth cameras, setting the clear flag to "depth only" may help you.

Recommendations for clipping an entire scene in Unity

I'm looking for ways to clip an entire unity scene to a set of 4 planes. This is for an AR game, where I want to be able to zoom into a terrain, yet still have it only take up a given amount of space on a table (i.e: not extend over the edges of the table).
Thus far I've got clipping working as I want for the terrain and a water effect:
The above shows a much larger terrain being clipped to the size of the table. The other scene objects aren't clipped, since they use unmodifed standard shaders.
Here's a pic showing the terrain clipping in the editor.
You can see the clipping planes around the visible part of the terrain, and that other objects (trees etc) are not clipped and appear off the edge of the table.
The way I've done it involves adding parameters to each shader to define the clipping planes. This means customizing every shader I want to clip, which was fine when I was considering just terrain.
While this works, I'm not sure it's a great approach for hundreds of scene objects. I would need to modify whatever shaders I'm using, and then I'd have to be setting additional shader parameters every update for every object.
Not being an expert in Unity, I'm wondering if there are other approaches that are not "per shader" based that I might investigate?
The end goal is to render a scene within the bounds of some plane.
One easy way would be to use Box Colliders as triggers on each side of your plane. You could then turn off Renderers on objects staying in the trigger with OnTriggerEnter/OnTriggerStay and turn them on with OnTriggerExit.
You can also use Bounds.Contains.

Render shadows of objects culled by camera

My setup is as follows:
Two layers with GameObjects in them: walls and shortWalls
Single camera, which is culling either walls or shortWalls
Single light, which is culling shortWalls
The effect I'm trying to create is a room where the walls can be fully rendered walls or only render the lowest part shortWalls (kind of like The Sims wall height).
The problem I am facing is that, when I change the camera culling mask to ignore walls, the light seems to ignore walls too. The culling mask of this light is constant, set to ignore shortWalls and to illuminate and cast shadows from walls.
I assume that, since the Unity Manual's layers page separates the use of layers by cameras and lights, the functionality is separated too. But it seems that the camera is ignoring the shadows cast by an object that is being culled.
Is there any way to allow the camera to render shadows casts by objects that are culled by the camera (but not the light)?

lowering draw calls by not drawing things that are not visible

I was searching for methods to optimise my game.
I was thinking if there is a way to not draw the objects that are in the camera view but are not visible to the viewer?
The thing is that i need to lower my draw calls and at any given time, there are at least 12 game objects that are not visible to the camera. Can this be done?
What you are looking for is called occlusion culling and it's a built-in feature in Unity. In order to lower the number of draw calls there are some more techniques interesting for your problem:
Frustum culling
Layer specific culling
Texture atlasses i.e. several different objects share the same material
Static and dynamic draw call batching
LOD (level of detail)
Kay is right to reduce draw call of objects which are not visible you need to use occulusion culling,
and if you still wants to reduce more draw calls you should use Free unity plugin "Draw Call Minimizer" by Purdyjo available on asset store.
and here is its unity video tutorial