Unity: Help needed with rendering voxels and performance - unity3d

So here is the deal, i have an enemy that is rigged and consists out of quite a few voxels.
When i run the game i get very bad performance overall when its rendering the model because of the amount of objects it needs to render. How can i improve that?
Here's the things i have thought about:
Only rendering the faces we actually can see,
Or maybe using some sort of GPU Instancing?
Anyways, i would like to know how i could resolve such a problem. Any help is much appreciated!

Without knowing how you've actually implemented your voxels in game (i.e what components each voxel consists of and how you're currently managing them) it's hard to offer much specific advice.
In general though there are a few things to consider:
Faces that aren't oriented towards the camera won't be
rendered by default. What you might want to investigate is
Occlusion
Culling, but
since your character is not static within the scene you might not get
much performance improvement from that. The code to
constantly check whether objects are occluded might end up costing
more CPU than not implementing it.
Rendering might not be your actual bottleneck for performance. If each of your voxels is doing some work every update (such as collision detection), then that's going to hit your performance much harder than rendering the objects, particularly for cubic voxels. You'll need to use the Profiler to figure that out.
You need to consider whether you actually REALLY need all those voxels to be in the scene all the time or whether you can replace them with a single GameObject until you need to interact with them, then bring them into the scene as needed. For example, you might replace the upper arm with a mesh that mirrors the shape of the total voxels for that body part. Then when that part is shot, for example, you can detect the point of collision and instantiate the necessary voxels around that point to react as desired, then rebuild the arm mesh to reflect the changed shape.
It might also be worth looking into Unity's Data-oriented Technology Stack (DOTS) features, although that could be overkill for this situation.

Related

Improvement for a lightweight and rapid object interaction using Unity

I have a question about the interaction of objects with each other.
Especially with regard to the application on Hololens 2 and the computing power that comes with it.
What would I like to do:
I have two objects. One is a simple staff and is moved by the player / user of HoloLens 2. The other object is a complex construct into which the staff shall be inserted.
Current behaviour:
After adding a BoxCollider and Rigidbody component to the staff and a MeshCollider to the complex object, I was able to create some interaction. Unfortunately the whole process is so expensive that it causes the HoloLens to crash.
My question:
Since I don't need any physics, but only pure non-penetrable objects, does anybody have a hint how I can achieve better performance?
Or what I have to change completely to make it as fast and lightweight as possible?
Notes:
Most entries online that I have found related to firing events or triggers that I do not need at all.
Even in PlayMode on the local computer it stutters.
Due to the complexity of the mesh I cannot simplify the target object.
Any help is welcome.
You could always decimate the complex object in something like blender: https://docs.blender.org/manual/en/latest/modeling/modifiers/generate/decimate.html
Import the decimated object into unity then use that mesh for the mesh collider instead of the rendered mesh.
That should allow you to simplify the mesh so it's more performant but still be plenty complex enough to closely follow the geometry of the original model.
I've used a similar method to reduce improve shadow rendering performance quite effectively.

Unity3D Wheel Collider - Friction Curve? Alternatives?

so I've been trying to see if I can make use of Wheel Colliders for the past several months now. As much as I've managed to figure out more and more things about how to set them up properly, there's some things I've been noticing that seem impossible to avoid:
Even if your friction sideways stiffness is lower, there's a chance that your car will continue to "spin" or "rotate" in the direction you were steering if you JUST hold down your input long enough to get the car beyond just rotating. I've noticed this will happen, whether for example the "SteerHelper" or "TractionControl" functions are doing their work or not. This will put a dent in ensuring smooth turn movement. Now, I don't know if maybe this is just due to realistic car physics (I mean, I can picture the car skidding in scenarios where they lost grip of the road for sure), but it just feels kinda glitchy. Sometimes, even when I'm not turning, the car will start to turn a little to the left or right and then gradually seem to "lean" that way in terms of applied torque to the rigidbody. I've seen many suggestions for trying to stop the rigidbody from doing this. Here is one way I'm trying to work against this:
rb.AddTorque(-rb.angularVelocity * 2);
However, it seems that the car will still "spin" more than intended. What would be ideal is to be able to MAYBE allow the car to turn a little extra after the left or right steer buttons were released (maybe more or less depending on the vehicle), and no more than that to ensure there is maximum control to give the engaged and maybe arcadey game play I've been going for for a long while.
It's been difficult to adapt an ideal friction curve value that would give the most ideal feel of a drift turn, especially a more arcadey one. I'm not trying to go for wide turns that slow you down, I'm trying to go for tight (yet controllable) turns that allow you to preserve most of your speed. I find that especially at higher values of stiffness, as I know many people have observed in other posts, that again, the car will turn back an extremely high amount sometimes (you drift left enough, a force is turning your vehicle way to the right). It's sad, because I've wanted to be able to say I've "mastered this beast" and used it for my purposes, but I don't know if that's really a good practical expectation for anyone. I even worked my own alternate friction curve values that would be used in the controller:
But I guess as some people say, you can't polish something that's broken? Moving to the third point...
I've read so many posts that show how to adjust a vehicle's center of mass, or to add more colliders in different spots to correct it, using scripting to add an offset to the center of mass, etc. So many tips that say, "lower the center of mass, you'll find it" and I give that a try. When the center of mass is too low, my car can get pretty shaky on the terrain (not that it hasn't in the past, but it's often been things I could correct, like the weird initialization of the attachedRigidBody of the wheel colliders themselves in the beginning, etc). High enough, and of course, (even when it's lower sometimes???) the car will just start spinning in the air on either multiple axes or specifically the forward z, when you drive off a ledge or bump into something with a high enough speed. It just seems inevitable.
I've been trying to give my benefits of a doubt. I like to think there's a correct way to use this thing, and that I'm just not familiar enough with Unity3D physics concepts. However, it just seems more and more that I'm investing too much time in a broken component - or, maybe I just never got the best grip of physics.
I was about ready to try just convex mesh colliders around my tires, and just abandon the idea of gripping physics altogether, but I'd love to hear suggestions to either address anything I've mentioned above, or just a more ideal package to move onto. I've glanced at packages like Vehicle Physics Pro, but I do want to be sure I'm getting something that makes sense.
Full disclosure: I'm in the middle of trying to make a game that feels incredibly similar to F-Zero, but with wheels.
Thanks in advance for any thoughts or suggestions you can provide.
(Maybe not an answer per se, but hopefully helpful.)
Note Unity also suggests to possibly give the car a constant downward force via script. It might be telling that such workarounds are officially given, one would think proper physics would, well, properly work without them.
There's some assets you might want to give a try generally:
One is the Unity Asset Store asset called Arcade Car Physics, and it's free. I've tried it and it works (but not sure if it works for your needs). It's using Unity's native Wheel Collider plus extra scripts.
Another is the Arcade Car Physics github project. It has nice plane stabilization and more, and works well. It's not using the native Wheel Collider.
Then there's this asset called Vehicle Physics. Instead of native Wheel Colliders, they've create a fully custom wheel system so that it would be more physically workable. The asset is not free, but they offer a free demo executable where you can drive around different vehicles, and that works quite well. (I haven't yet bought this asset myself.) As a downside, some reviews mention there's some complexity in setting this up (and I suppose future support for this custom Wheel Collider hinges on the company continuing to exist).
Good luck!

Unity best practices for unloading unused areas of a map

I'm wondering about the best way of reducing my CPU and memory overhead for areas of the game world that do not need to be updated every frame.
I have just started to consider this issue as I'm currently implementing a shadow detection system using Raycasts.
My problem is this:
I can have about 100 lights in my level that on every frame send a Raycast to nearby characters to determine if these characters are in shadows.
My game is a low poly PC game, and I understand the overhead from Raycasting isn't that drastic. So its not a major concern. But I'm still not sure of the best approach to optimising this.
I have been thinking of a few soltions, but am unsure if there is "standard" per say.
1. exit update loop if player is too far away
void Update() {
if (Vector3.Distance(playerPos, transform.position) > someRadius) {
return
}
}
This is the most glaringly obvious solution, with even more obvious concerns.
This update loop will still be hogging CPU cycles , performing 2 calculations on every frame, for every light point.
2. Disable Light gameObjects when the player is too far away
This method is more efficient in terms of CPU overhead, as those will be negated. However I'm still hogging uneccesary memory.
In order to make this solution more scalable I would have to design some kind of "enabler" that keeps track of game objects that should be enabled/disabled based on the player position.
But at this point I know I'm re-inventing the wheel, and feel very sure that there is an industry standard for this.
Is there an alternative to enabling/disabling?
I see a lot of game developers talking about physically unloading areas of their game from memory and writing those areas to disk, when the player is not nearby.
I wonder is this achieved by simply destroying and re-instantiating the objects.
Question
Is Unity opionated about this?
The page here lists an example, similar to my first solution. But they are talking about 100s of thousands of updates per frame here.
Maybe I don't need to worry as much as I think
Thanks!
If you want not render objects, that are not in your camera area, Occlusion culling is what you exactly need. Watch this link:
https://docs.unity3d.com/Manual/OcclusionCulling.html
If You want change how your object looks based on range between camera and your object, You have to use LOD(Level Of Detail). Here is documentation:
https://docs.unity3d.com/Manual/class-LODGroup.html

Multiple Tilemap Maps Loaded Dynamically

Feel free to close this question if it is deemed too subjective. I realize that it might very well be, as it may focus heavily on best practices, which, if not an industry standard, may be subjective.
The Problem & Idea:
I'm using the tilemap system in unity (2D), and its been working great, but I have a few concerns regarding my particular usage. What I'd like to have is multiple "battle maps" that will get instantiated when the battle scene is switched to. My current idea is to just make each type of battle map a prefab (prefab on the grid, since the tilemaps are just layers), and then instantiate the grid when the scene loads in.
Is this best practice or is there any better way? Does having 10 maps vs 200 maps make a difference?
Other Things I've Considered:
Would it be a better idea to make one huge tilemap with all of the battle maps drawn a distance away from each other and just restrict the camera to moving only within the "current" battle map? The absolute max size I could see a map being is, let's say 30x20 tops, probably closer to half that though.
An earlier idea was to use small pixel map images to render the maps in. This was before I found out about Unity's tilemap system, which, admittedly, I'd rather use because it is so much simpler to visualize, and less work to develop.
The Scenario:
For simplicity's sake, let us assume the game has 2 scenes, the main menu, and a battle scene. Basically, the idea is to enter the battle scene and have a random battle map to be spawned out of what's available. The character(s) get placed, and any additionals also get placed, say, items, or what have you.
Is what I proposed above best practice? And should I consider any other other two systems I've also described? Or is there something I'm not thinking of that would be even better?
I don't believe that any of the above ways wouldn't work, I'm just curious if any of them is the best way to go about doing this.
Your consideration to have all of your battle maps drawn on a single tilemap and restricting the camera to only the region of the current map is an instant red-flag.
In my experience with unity, if I want to have a large level drawn on a grid I will have to break it up into chunks (each chunk is a separate tilemap) for performance reasons in the editor and in the game, so I would not recommend doing that.
If your goal is fast load times, then instancing a prefab as you suggested is not a terrible idea as long as the respective tilemap is fairly small (less than 10000 tiles), otherwise you would almost difinitely notice the map getting loaded in. If you are trying to load large tilemaps seemlessly, it may help to load the tilemap asynchronously before the actual switch occurs.
I would keep only the current map and next map loaded in the scene to ensure a seamless transition and optimal resource usage. Unity's tilemaps are not light.

scnBox with scnTube through the middle

I have been digging around trying to find a way to show a game board of sorts.
It is basically a square board with a round hole in the middle, I am able to render the scnBox and the scnTube, but I would like the area where the scnTube sits in the box for the box to be transparent and see through the game board, but can't seem to find anywhere that has an example. Any help would be much appreciated. I am hoping that I am just missing something very simple, but this is my first time using scene kit.
Thank you.
Before Unreal Engine 4, (UDK and prior) Epic's modelling space was subtraction - a filled block was your game world and its extents. From inside this block you took chunks out to create space for players to run around in, and shoot each other. All's fair in love and war.
I'm telling you this because it's a good example of how contrived 3D modelling is compared to real world scenarios, and should (hopefully) put you at sufficient unease to digest what follows.
This approach of carving out of a finite block is still in Unreal Engine 4 and popular with older users, but it now defaults to an open, infinite world into which things are added. Most new users gravitate towards building into an infinite space of nothingness rather than carving space out of a solid, finite block.
Everything about 3D modelling is virtual, and virtually impossible to relate to the real world. Instead of thinking in terms of how things could be done if objects were real and literal, you need to think in terms of the limitations (and there are many) of geometry definitions as used in most 3D modelling and game engines.
The programming equivalent of this mental gymnastics is going from the concept of classes and objects to their realities within languages and frameworks. On the one hand the ideas and their ideals are wonderful, and on the other the realities are a bleak reminder that programming languages haven't really progressed very far, at all.
3D modelling is exactly like this. It's not much further along than it was decades ago, and is still using archaic ways to solve many of its original problems.
Cutting a nice, clean, efficient round hole in a cube is one such original problem.
A very simple shape is being intersected and cut by a shape with the potential for infinite complexity. What should happen? Should the simple become complex or the complex become simple? How to make the most graceful transition between the two?
That's the problem you're facing: a cube is a simple geometric shape, easily defined by minimal line segments. A cylinder introduces infinite possibilities for line segmentation around its circumference.
So somewhere along the lines of development, the architects of 3D modelling had to come up with a way to make these contrasting line complexities play well together for lightweight presentation on limited hardware. Their solution, in most cases, is a hybrid and a disaster of user operability, but masterful in its geometric efficiency: Polygon modelling, UVW unwrapping and subdivision!
All of which means that if you want to achieve this in the best way possible, with today's tools, for the purposes of Scene Kit, I suggest polygon modelling this board in Maya for 4 reasons.
It's got a 30 day free trial.
It works on a Mac
It's polygon modelling tools are second only to 3ds Max
It's easier to learn (for a complete newcomer) than MODO, and miles easier to learn than Blender.
MODO is interesting if you're already skilled in Polygon modelling, but it's so utterly discombobulating if you don't have that prior experience that I'd recommend using just about anything else first. Except Blender. Blender is free, but don't be tempted. It will cost you more in learning time than buying a copy of every other professional 3D app.
In MODO's favour, and the reason I mention it, it does export nicely for Scene Kit. I know that for a fact, but am not yet sure how well Maya exports for Scene Kit.
Which is the next problem you're going to come up against. All COLLADA files are not born equal.
New Maya does have Unity and Unreal export presets, so I presume it's possible to calibrate its COLLADA exports to match the demands of Scene Kit perfectly, just haven't yet needed to do it. This will (very likely) involve trial and error to get the settings right. It would be nice if Apple would tell us exactly how to configure export from all major 3D apps for Scene Kit, but instead they're giving us the half baked Model I/O, so we can double the effort of importing artwork.
All context aside (which has largely been to demonstrate that 3D is no simpler nor more refined than using an IDE and frameworks like Xcode and Cocoa), here's the meat and potatoes:
A video on one aspect of what's best to make holes, and starts out as you are, with a cube and a cylinder:
https://www.youtube.com/watch?v=zaEv5rio8bk
But it does presume a certain amount of Maya familiarity, some of which you can gain from this rather slow and ponderous examination of two other ways to make holes in cubes:
https://www.youtube.com/watch?v=lvMfoH5Ikrc
Yes, if you're counting, that's 3 ways to make holes. Actually four, because the first video starts with the boolean operation you might have been expecting to be how this could/should be done. In some parallel future we'll have well working boolean geometry operations. We're not there, yet.
Hopefully that same parallel future will offer us a programming language, frameworks and terminology that's not confusing and maintains metaphors long enough to make teaching easy and usage elegant and simple.
I dont know about that long answer but this can be achieved with Boolean Subtraction. You create a cube and a cylinder. You subtract the cylinder from the cube. In 3ds Max this is under compound objects-modifiers-boolean subtraction. I guess Maya has a similair function somwhere in the menus.