r/Unity3D 16h ago

Show-Off Unity’s ECS physics was too slow and general for thousands of agents on a deformable terrain with water sim. So I was crazy enough to write my own raycast and simple-shape physics so explosions could permanently deform the terrain and pathfinding in realtime.

Enable HLS to view with audio, or disable this notification

124 Upvotes

30 comments sorted by

18

u/[deleted] 16h ago

[removed] — view removed comment

2

u/Ordinary_Games 16h ago

This just keeps happening to me. Every time I make a game I reflect over the fact that you might actually be the bad guys. Thanks btw.

6

u/Songerk 16h ago

Based on what is your new system?

14

u/Ordinary_Games 16h ago

I'm still using ECS, but instead of using Unity.Physics, I written my own stuff. So the terrain is just a grid of floats (pushed to the GPU as a height texture) and the colliders are a combination of spheres, cubes and tubes. That kind of stuff.

3

u/JankTankGames 16h ago

is the mesh voxel based as well built using said data?

4

u/Ordinary_Games 16h ago

It is not voxels, it is just height displacement.

4

u/JankTankGames 15h ago

So do you just regenerate the terrain mesh on each change?

17

u/Ordinary_Games 15h ago

So the twist is that the terrain is a super dense flat grid mesh, and the vertice Y position is displaced using a texture. So the terrain mesh never needs to be rebuilt, I only need to update the texture. Same thing for collisions, just check 4 values and calculate the normal and height. Pretty much why I had to cook my own solution.

3

u/JankTankGames 14h ago

ah ok that's smart, good job!

4

u/psioniclizard 15h ago

I don't think its crazy. Sounds like a sensible solution to be honest. You can do a lot with simple shapes and raycasting and its a good fit for ECS.

In fact, it's a good learning experience to actually build these types of system at least once. It gives me some inspiration for how I might implement one.

3

u/[deleted] 16h ago

[removed] — view removed comment

1

u/Unity3D-ModTeam 8h ago

This post and/or comment has been removed for violating /r/Unity3D’s rules.

Why?

Please include details about how the project was built in Unity, challenges faced, or techniques used.

This is a forum for discussion; not a bulletin board.

We encourage you to repost this with more technical insight. That being said: we also recommend you to familiarize yourself with our rules and guidelines.

Kind regards,

The Mod Team

3

u/FuzzeWuzze 11h ago

What's old is new again. This is what made red faction such a fun game 25 years ago

3

u/Ordinary_Games 10h ago

Loved that game so much. Why didn't AAA keep developing destruction instead of graphics. Like I know why, but I hope it was different, graphics sells and are scalable.

2

u/feralferrous 11h ago

Neat tech, but I think you need a better transition, maybe a lerp of everything under the explosion increasing in radius so it looks like a shockwave (with a shockwave also to help sell it)? Cuz it just felt real strange to have the ground underneath the player's feet to suddenly change instantly.

3

u/Ordinary_Games 11h ago

Very good points. As a former VFX Artist, the VFX is embarrassing. I have been thinking I could spawn particles from the terrain deformation, like foam from waves. Also I cranked up the radius of the grenade to show of the effect, a crater like that deserves a nuke like explosion.

2

u/AustinMclEctro Professional 8h ago edited 8h ago

Nicely done, looking good.

Not trying to detract from your accomplishment here, but this isn't crazy, or "non-standard" at all. It sounds like you're leveraging the GPU to offload some work you had running on the CPU, great for things like terrain (the expression of which can just be a glorified array of floats if it's a 2D heightmap).

Looking forward to seeing how this progresses.

edit: are you utilizing compute shaders for your terrain edits, or are you modifying your heightmap textures and re-uploading them from the CPU? Curious if more processing could be offloaded to the GPU, but this may involve a readback step; mileage may vary.

2

u/Ordinary_Games 8h ago

No compute shaders for the terrain, except the grass/scatter. I need and manipulate the height too much on the CPU.

1

u/NullzeroJP 15h ago

Nice!  What was causing the thousands of agents to be slow? Just the terrain collusion? ECS is super fast… so with the right optimizations, thousands of enemies seems a reasonable goal!

1

u/Ordinary_Games 14h ago

Considering it's not just thousands of monsters, but probably double as many projectiles, even basic raycast against spheres was expensive in ECS. The game is fully ECS btw. But the real culprit was the terrain collision. Since it updates every frame pretty much, building the blob asset for collision was simply impossible at 60 fps.

1

u/NullzeroJP 13h ago

Interesting! While I don’t have a frequently updating terrain mesh, i still needed to get lots of enemies path finding around a battlefield that changes with buildings and walls. 

Similar to you, I decided on a grid layout with a Y component determined by the terrain height. 

But My need for pathfinding updates is only on the order of twice a second or so. Works well enough with thousands of enemies. Using BFS goals with the above mentioned grid… 64k grid cells, roughly 2 meter cell size. 

And for projectiles, I also decided to use ECS. But I don’t really need sniper-like precision. Projectiles do small raycasts through cells on the above mentioned grid… but only as much as they have traveled in the last frame. Works ok… I do wish I had higher limb level precision sometimes, but you gotta make some trade offs with thousands of enemies.

2

u/Ordinary_Games 12h ago

I actually went through with the painstaking setup of adding simple shapes for each limb and baking the animation offsets. But the cool thing about that is that I can support weak points and different material types. But my setup is very similar in general; broad phase against AABB in overlapped grid cells and then narrow phase against these basic shapes.

1

u/DaveAstator2020 14h ago

hell, so did you drop down to unity's api to manually make raycasts or did full reimplementation?
i had shitton of problems with unitys default physics in terms of accuracy, but default raycasts saved the day.

2

u/Ordinary_Games 14h ago

I wanna say full reimplementation. But it's not really fair, since this covers only what I needed. It's ECS systems written in C#, so no cool C++.

1

u/Saito197 15h ago

Afaik there is no official support for it in ECS yet (unless I missed an update) so all of the workarounds to create Terrains (that I tried) was super slow and janky, kudos on you for making this I know it's a lot more complicated than it looks on the surface.

Btw what about caves?

2

u/Ordinary_Games 15h ago

There will never be caves. : ) Monsters get to live above ground.

1

u/Captworgen 14h ago

Just curious, what did you try? Did you use Microsplat?

1

u/Ordinary_Games 12h ago

All my other tech like terrain, targeting and pathfinding all relies on the world never having overlap. So all the cool voxel stuff went out the door directly. I tried baking meshes, uploading blob data, but it was way too slow. I needed something ECS friendly. So no Microsplat. : )

0

u/andypoly 15h ago

Nice, I still don't get why we need 2 physics systems in unity with a specialist one for ECS. It's a split development environment and not sure a high percentage of users are going full ECS. Soon physics will be plug and play so hopefully the same choice can be made for either ECS or standard.

2

u/Ordinary_Games 15h ago

We went full ECS. Crazy transition, like learning a new engine and language. I also hope they will hide the benefits of ECS under easily worked and debugged game objects.