Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get geometric shape from collision between objects?

Discussion in 'General Discussion' started by Chinafreak, Mar 26, 2017.

  1. Chinafreak

    Chinafreak

    Joined:
    Apr 6, 2013
    Posts:
    44
    Hello guys,

    I'mi scratching my head, because I've actually no idea/plan, how to get geometric shape from collision between objects. I mean, look at this image:




    Imagine, the sphere model is like brush from Photoshop and you want to draw the 3D model with the 3d sphere brush. But instead texture, it will create mesh. All should be dynamic.

    But one thing: The Brushes can be in any shape. So its not just sphere, it could be a box, capsule or even 3d model.

    Maybe Collider will help? I've actually no idea. Well, okay I got only one idea!

    You can see at GIF that if the brushes collide with box, you can see shadows at sphere from inside of box. Maybe it's possible to trigger this shadow to mesh?

    http://imgur.com/HQEZIyE.gif

    Anyways, I think I don't need complicated calculation with mesh vertices. Simple collider would be fine.

    I hope you got a idea how I can achieve this! :)

    I've actually "got" it, but this only works on Position Y=0 and there is no brushes. (It's kinda like TrailRenderer from Unity Compoment one)
    https://twitter.com/chinafreak/status/807997491002146816
     
    Last edited: Mar 26, 2017
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,328
    You can't.

    You want boolean operations in 3d. This is difficult and hasn't been implemented in unity.

    Unity physics system will only provide you information about contact points. Not about intersecting shapes.

    If you want boolean construction for the level, you could try SabreCSG (which is now free) from github/asset store.

    But that's pretty much it.
     
  3. Chinafreak

    Chinafreak

    Joined:
    Apr 6, 2013
    Posts:
    44
    I can (I found out), well, at least with heavy maths. I just calculated if the triangle from mesh intersect with other triangle from other mesh then you can simply calculate the "boolean" operations. (At least the result like I've from screenshot)
    But because the performance look bad, I'll use raycast then. Is not the exact result which I want but its enough. :)

    Btw. I need this ingame, and not in editor. So the asset from SabreCSG is useless for me. ^^

    And don't say "You can't". There is always possible ways, if you know math and programming very well lol

    But thank you so much for answer!
     
  4. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    You might can do it with command buffers like the deffered decals Unity did... Using object intersection.. Not sure how good it would be with meshes, but stuff like a Cube would work.
     
    Chinafreak likes this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,328
    I think I know math and programming well enoguh.

    The price is roughly half of a year of coding or maybe more, depending on your skills.

    That's amount of time spent implementing similar system in GeoMod 1 used In Red Faction 1. The dude in charge had to go to another city visit and get advice from professor specializing in CSG.

    It is a very difficult/complicated problem, and to the date there are two, maybe 3 games total that implement it in realtime. Nobody has this feature in their game.

    Booleans in 3d modeling software usually require quite a lot of time - years - to make them stop glitching. (Took couple of years for 3dsmax boolean, blender booleans still gltich).

    The issue is detecting separated objects, inside/outside portions of each mesh, then handling caps and degenerate triangles.

    Either way, you can't get this info from physics system.

    1. To have a quick and dirty solution you could try to voxelize objects being interesected, and then use voxel overlap information to speedup the query. Voxelization could be performed on GPU using UAV buffer.
    2. Another possibility is to convert input models into simplex-based form (because simplices are inherently volumetric, and perform operations on those.
    3. Yet another solution is to make objects voxel-based since the beginning, because handling voxel booleans is trivial.
    4. And one final possibility is to represent all obejcts being intersected as distance fields and construct actual polygons using dual countoring and marching cubes.
    5. You can also fake it. This will work if objects are sliced with something like plane, in which case you can assign different shader to interior to create illusion of a flat surface beign there. It was done in Dead Rising 2, for example.
    And this is all I could think of.

    Basically, you stumbled upon a very difficult problem, and "anything is possible" attitude is not very helpful in this case. At best there are games that can slice stuff with planes (metal gear rising), but that's it.

    I could outline basic contour based booleans too, if you need that...
     
    Last edited: Mar 26, 2017
    Socrates, Kiwasi and Chinafreak like this.
  6. Chinafreak

    Chinafreak

    Joined:
    Apr 6, 2013
    Posts:
    44
    Thank you so much for answer! Yeah, I know that you know programming and math very well. (Because I'm very often here at forum, but I write very rare lol).

    I was already working on this before, but this only works on Position Y=0 and there is no "brushes". (It's kinda like TrailRenderer from Unity Compoment one) https://twitter.com/chinafreak/status/807997491002146816

    I just wanted it to make better. :)

    Thanks for the tips! I really like the idea about convert input models into simplex-based form and the no. 5 tip! I'll try this tips!
     
  7. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    I have no idea how you could do something like that, but I know for sure blender has a boolean system that does the exact same thing you're asking, and the code is open-source. If you want to make your hands dirty, you can get into blender's source code and see how it's implemented there.
     
  8. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Check the shader in this thread.

    Edit: And I'll clarify that this isn't CSG, you can cut out primitives in certain ways, but maybe it's close enough.

    https://forum.unity3d.com/threads/simple-cross-section-shader.34508/page-2#post-2976366

     
    Last edited: Mar 27, 2017
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,328
    YEah, I know that the other way to fake it is via depth and/or stencil tricks, but it won't produce actual geometry. It is pixel level illusion.

    Kinda like this cube here:
    distancefields.jpg

    In practice I saw "faking it" in second dead rising, where they used an equivalent of a clipplane shader to make zombies sliced in half. They didn't even cap slice area with anything, they just placed some sort of shader-based texture there without even correcting visible depth.
     
  10. Eriks-indesign

    Eriks-indesign

    Joined:
    Aug 15, 2012
    Posts:
    50

    @neginfinity Any chance you share the code for that effect using shaders? :)
     
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,328