Search Unity

How do i preform a boolean operations(CSG)??

Discussion in 'Scripting' started by alexander11, Sep 20, 2016.

  1. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hello i have been learning about CSG i have checked out evanW. And Sander Van Rossen blog about CSG(real-time), i understand on what Sander is talking about but i don't know how to code it, i have been to many websites but its always in another code language and not in C#, so if anyone knows how to do CSG could you help me out? cheers
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Blender3D has a boolean modifier functionality that can do all of the above.

    Full source code is available at https://www.blender.org

    From there you should be able to get a pseudocode idea of what is involved step-wise.

    After that, coding it in C# is just an "implementation detail(tm)."
     
    io-games likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    Asset store has plugins to do that (so could check those how its done)

    seen few free ones in the forums too, search for unity csg
    https://github.com/karl-/pb_CSG
     
  4. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    i'll check it out
     
  5. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Now @mgear Kurt-Dekker i have looked at the pb_CSG but how do i a
    Perform boolean operation without creating a new mesh?

    I did try this
    Code (CSharp):
    1.         m.RecalculateBounds();
    2.         m.RecalculateNormals();
    and this, but it doesn't work.
    Code (CSharp):
    1. CSG.Subtract(cube, sphere);
     
    Last edited: Sep 21, 2016
  6. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    So @mgear would you know how to it??
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It gives you a new mesh back so just assign the new mesh to the one you want to replace.
     
  8. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not sure I understand?
    Code (csharp):
    1.  
    2. Mesh mesh = CSG.Subtract(cube, sphere);
    3. MeshFilter filter = replacement.GetComponent<MeshFilter>();
    4. filter.mesh = mesh;
    5.  
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    The mesh is supplied by a property within the MeshFilter component.

    The MeshFilter component in turn supplies the attached mesh to either a MeshRenderer or a SkinnedMeshRenderer.
     
    Last edited: Jul 24, 2019
  11. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    What is "replacement" in line 3 @KelsoMRK

    I don't really understand @Kurt-Dekker could you use code and describe on what it does or link me to a website with the info. thanks :D