Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CSG library for C#

Discussion in 'Made With Unity' started by Arakis, Nov 19, 2014.

  1. Arakis

    Arakis

    Joined:
    Nov 19, 2014
    Posts:
    2
    Hello 3dDevs,
    i've ported the java j3dbool library to C#. Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean operations like union and intersection to combine 3D solids. This library implements CSG operations on meshes. All edge cases involving overlapping coplanar polygons in both solids are correctly handled.

    It's a standalone library, it does not require Unity3D. A small example is included (using OpenTK OpenGL wrapper).
    It seems, the library does not generate normals. My 3D skills are not the best, so i cannot add this functionality.

    I've compared some CSG implementations, i think j3dbool/Net3dbool have nicer polygon cuts.

    It's free and open source!
    https://github.com/Arakis/Net3dBool

    (No, i will not create an asset :) )

    Greetings,
    Sebastian

     
    andrew-lukasik likes this.
  2. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Hi Arakis - you can generate normals automatically with mesh.
    Have you tried using something like:
    mesh.RecalculateBounds();
    mesh.RecalculateNormals();
    mesh.Optimize();

    Will have a look at your library.. to see if I can help at all - I'm looking for something similar to this.
     
  3. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Managed to get it working fine in Unity.
    I'll put up a package soon.
    Going to try out some different csg operations.
    csg_sample.png
     
  4. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Here's a replacement Demo.cs that works in Unity. The library should work with it.
    Attach to a GameObject and set the material (I used a defaut).
    using System;
    using UnityEngine;
    using Net3dBool;

    public class Demo : MonoBehaviour
    {
    public Material ObjMaterial;
    public Net3dBool.Solid mesh;

    public Net3dBool.Color3f[] getColorArray(int length, Color c)
    {
    var ar = new Net3dBool.Color3f[length];
    for (var i = 0; i < length; i++)
    ar = new Net3dBool.Color3f(c.r, c.g, c.b);
    return ar;
    }

    public void Start()
    {

    var box = new Net3dBool.Solid(Net3dBool.DefaultCoordinates.DEFAULT_BOX_VERTICES,
    Net3dBool.DefaultCoordinates.DEFAULT_BOX_COORDINATES,
    getColorArray(Net3dBool.DefaultCoordinates.DEFAULT_BOX_VERTICES.Length, Color.red));

    var sphere = new Net3dBool.Solid(Net3dBool.DefaultCoordinates.DEFAULT_SPHERE_VERTICES,
    Net3dBool.DefaultCoordinates.DEFAULT_SPHERE_COORDINATES,
    getColorArray(Net3dBool.DefaultCoordinates.DEFAULT_SPHERE_VERTICES.Length, Color.red));
    sphere.scale(0.68, 0.68, 0.68);

    var cylinder1 = new Net3dBool.Solid(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES,
    Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_COORDINATES,
    getColorArray(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES.Length, Color.green));
    cylinder1.scale(0.38, 1, 0.38);

    var cylinder2 = new Net3dBool.Solid(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES,
    Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_COORDINATES,
    getColorArray(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES.Length, Color.green));
    cylinder2.scale(0.38, 1, 0.38);
    cylinder2.rotate(Math.PI / 2, 0);

    var cylinder3 = new Net3dBool.Solid(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES,
    Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_COORDINATES,
    getColorArray(Net3dBool.DefaultCoordinates.DEFAULT_CYLINDER_VERTICES.Length, Color.green));
    cylinder3.scale(0.38, 1, 0.38);
    cylinder3.rotate(Math.PI / 2, 0);
    cylinder3.rotate(0, Math.PI / 2);

    //--

    //mesh = s;

    //--

    // var modeller = new Net3dBool.BooleanModeller(b, c1);
    // mesh = modeller.getDifference();

    //--

    var modeller = new Net3dBool.BooleanModeller(box, sphere);
    var tmp = modeller.getIntersection();

    modeller = new Net3dBool.BooleanModeller(tmp, cylinder1);
    tmp = modeller.getDifference();

    modeller = new Net3dBool.BooleanModeller(tmp, cylinder2);
    tmp = modeller.getDifference();

    modeller = new Net3dBool.BooleanModeller(tmp, cylinder3);
    tmp = modeller.getDifference();

    mesh = tmp;

    MeshFilter mf = gameObject.AddComponent<MeshFilter> ();
    Mesh tmesh = new Mesh();
    int mlen = mesh.getVertices().Length;
    Vector3 [] vertices = new Vector3[mlen];
    for(int i=0; i<mlen; i++)
    {
    Net3dBool.Point3d p = mesh.getVertices();
    vertices = new Vector3((float)p.x, (float)p.y, (float)p.z);
    }
    tmesh.vertices = vertices;

    tmesh.triangles = mesh.getIndices ();
    int clen = mesh.getColors ().Length;
    Color [] clrs = new Color[clen];
    for (int j=0; j<clen; j++) {
    Net3dBool.Color3f c = mesh.getColors()[j];
    clrs[j] = new Color((float)c.r, (float)c.g, (float)c.b);
    }
    tmesh.colors = clrs;
    tmesh.RecalculateNormals();
    mf.mesh = tmesh;

    MeshRenderer mr = gameObject.AddComponent<MeshRenderer> ();
    mr.materials = new Material[1];
    mr.materials[0] = ObjMaterial;
    mr.material = ObjMaterial;
    }
    }
     
    mgear likes this.
  5. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    First of all i want to say congratulations for transcoding and porting that code to U3D, i have tried some CSG code libraries for unity3d online recently and had major errors, and am keen to run this demo file...

    I am seeing something confusing already in the Demo.cs file... i.e., only at line 14:

    {
    var ar = new Net3dBool.Color3f[length];
    for (var i = 0; i < length; i++)
    ar = new Net3dBool.Color3f(c.r, c.g, c.b);

    cannot implicitly convert [] to float... i am not sure how you managed to run that demo file?!?
     
  6. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hi i had a session with it last night, have it running, it does take 20-30 seconds to render the cube-sphere-3cylinders with about 450 vertices to run through... here is a unitypackage with demo.cs:

    So lets get on our 3dkini's and play net3dbool.
     

    Attached Files:

    Last edited: Aug 21, 2015
  7. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Awesome. Sorry I must have put up an old copy of Demo.cs.
    It wasn't something I was concentrating on anyway :)
    However.. you may be a little more interested in what I made last night. I have built a CSG Toolkit using the awesome Node Editor kit made elsewhere in the forums (I'll add the links in.. dont have them at the moment).
    So.. the result was this:
    csg_toolkit01.png

    And because the majority of the code and systems is open. I'll be putting this all up for free. Its _great_fun_ to play around with. I need to fix some bugs and general use case issues (probably a day or so to sort out). I'll post it here hopefully tomorrow sometime.

    Thanks greatly to Sebastian and the Unity community (for Node Editor).. it stuns me how fast this sort of complex system can be built.
    Cheers,
    Dave Lannan

    <edit> Im also going to look into beefing up the performance a bit. There are a number of loops and such that could be optimized. Im even thinking of pushing some of it into CUDA.
     
    Last edited: Aug 21, 2015
    Seneral and SAOTA like this.
  8. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hi Dave, Seems like a very cool project, Did you manage to get Unity3d primitives working with the net3dbool code? If ever you have any kind of working version in unity, I'd love a unitypackage version and i'd probably fork it into a different project using coded formations of primitives like structuresynth program.

    I am currently transferring a U3d project to another program which is more adapted for CSG of mesh, it's trouble tough i have to rewrite primitives by points and triangles and translations and rotations as matrices. I would love a copy.

    I timed the performance with union of cubes at 50s for 100 cubes on a Q8200 processor, and i found that it crashed if i try to get the difference of cubes in a line because there is an array error when the modelling function doesn't find shared vertices.

    would love to see the node editor, there are many and i haven't seen that one, i actually learnt to code using maths nodes. the shader is very cool, what shader is it?

    Antony Stewart
     
    Last edited: Aug 22, 2015
  9. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Hi Antony,
    Heres my current build. Now few warnings:
    - Its in development so go knows what it can and cannot do :) .. be careful. Ive had a couple of odd operations completely crash Unity (still trying to find these sorts of bugs - sounds a little like the problem you had).
    - Its going to change alot.. The more I work with this lib, the more I want to rewrite/reorganize some things to improve performance. Generally the node editor interface will remain the same, but I will be adding a number of operators and effects to do all sorts of things.
    - The code is not all mine, in fact only a small portion is, so I cannot vouch for most of it, and I do not know what much of it actually does :) .. The Node Editor is great.. but Im very new to it.

    Links to Node Editor:
    https://github.com/Baste-RainGames/Node_Editor
    http://forum.unity3d.com/threads/simple-node-editor.189230/page-3

    Below is the package - it should be installed into Assets/Plugins.. folder.
    All operations work in editor mode - no need to run.
    Beware that some operations can run for minutes!!! I need to put a display in the operator node for that!
     

    Attached Files:

  10. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hi David,

    Did you manage to get it working with Unity3d primitives?

    Whooa that's very kind of you. Thanks very much. I installed it to a new project and Assets/Plugins folder, I couldn't find node editor in the windows menu, and the demo file couldnt import to an empty gameobject as there is a compilation error somewhere regarding BeginClip and EndClip and Unityenging.GUI...

    Still the Node-Editor is awesome i think i can bring over some of my untidy projects to that, I have seen your coding is amazing tidy... as soon as i start a project it rapidly becomes 20 fast tests of different things all in the same file with no files structure...

    :D

    Ok see you, will have a go afterwards. I just spend all day working on rotation matrix things. :D fun!

    have to say my priorities are to have it working with unity3d mesh formats and also in realtime... in playmode will help to find the array errors and can be interesting for optimising it. will see.
     
  11. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Last edited: Aug 22, 2015
  12. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    That's a good idea. Currently with this CSG implementation, you can expect that making 100 pyramids into some mountains will take around 30 seconds, Same with another implementation i have called openSCAD, the CSG is slow. I think that a processor can run under 10 cubes in realtime, and it will struggle afterwards at slow frame rates. I think that example with 4 houses is probably at the limits of a processor for csg, perhaps it's even on a graphics card. you can save meshes and make nice meshes using a landscape generator with this code very easily, just place some pyramids randomly around on top of a giant cube that can be an underlying plane or something. The code isnt tested at all though, i can figure it could crash with more than 100 cubes. 1000 cubes takes me 5 minutes on a purpuse built csg program, and i am wondering if this program will freeze far beyond 100 i'll have to give it a try. actually, i don't know why they used 4 houses, maybe its the same house for all 4 instances, so indeed less than 10 cubes at current frame rates.
     
  13. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    I am currently getting a crash when i try to merge 200 cubes into a chain.

    at least unity still hasn't finised 10 minutes later, and it's a 2 minute task.

    A CSG engine shouldn't do that. openscad can run 2000 cubes into a mesh so this one should else it is unreliable.
     
  14. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    It's a plain C# using OpenTK for rendering.

    ~55 fps with 4 houses
    ~120 fps with 2 houses
    40% CPU (i5-2500K @4GHz)

    As far as I understand, all the houses are built from a bunch of primitives/brushes every frame. Each house consists of 112 brushes.
     
    Last edited: Aug 23, 2015
  15. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Hi Antony - yes, it works works with any normal mesh as well as unity primitives.
    Not sure what bug you have there. You should not use it with the Demo.cs - its a different system now.
    You should see a Menu Item under the Windows menu:
    Windows->CSG Editor
    Hi alex - yes, in fact I am using the tool I have made to build constructive geometry features like walls with windows and so on. The main problem is the way unions and intersections are being done is for generic solutions. So I need to optimize a number of the loops or push it to CUDA (this is my current preference).
    The pic below is a quick wall with window node canvas - you can save each canvas too. Then I want to make a node tool that lets you iterate objects with transforms, instances and distortions (warps, shears etc). so you can construct buildings and so on.
    csg_toolkit02.png

    The CSG system is actually quite ok. It just needs to be made to suit either GPU;s or some Vector/Multicore CPU features. There is a _huge_ amount of room for optimizing this package. It will take me some time (probably a week or so, for the first pass) but I expect at least a 10x improvement with fairly basic optimization methods.

    If you are having problems getting the package to work, I'll try to do some testing on it tonight. It should work fine from an empty scene. Im not sure how it will work with other packages (again, very new to the Node Editor tool).
    Cheers,
    Dave
     
  16. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Thanks Dave i'll see if i can find the CSG option in window i didn't see it previously...

    Indeed optimization is disastrous...

    I think other CSG libraries must use octrees and vector proximity from the current mesh to see which mesh to process, here is the performance with cubes:

    1-2 cubes = 10fps, 50 cubes = 3fps, 100 cubes = 0.8 fps, 200 cubes is 0.25 fps....

    to add a cube to a 1200 vertex object takes 4 seconds.
    Didn't find topics for optimize CSG very easily, optimizing obscure programming routines is difficult to have specific info's about and especially code examples, so i don't know how it's done, indeed CGAL take 5 mins for 1000 cubes and here it would be over 1 hour.

    CGAL library is optimized, perhaps see from them how it's done.

    To CUDA would be awesome, perhaps it is like making a cuda version of MC in terms of complexity, i.e. coding doctor challenge :D would be fascinating to learn how that is done :D
     
  17. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Alex: Quick question. I havent looked at the library you mentioned. Do you think it might be worth putting in place of the original one from the OP? Im not sure I want to try out another library, but the toolkit I have is now fairly detached from the underlying library, and before moving into do some perf work it might be worth looking at the other one - does it support UV / Material mapping during operations? Im currently implementing an environment based mapping method (planar, sphere etc), but it would be nice if this worked already.

    Also, I have added a primitive node into the toolkit. This way you dont need to supply game objects if you dont want to. However all gameobjects must have the mesh at its root and single mesh only (start simple).

    Should have another update in a few hours.
    Cheers,
    Dave
     
  18. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I don't know yet. I'm still figuring out the principles it's based on and trying to assemble a working prototype in Unity.
     
  19. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    I don't know very much about the CGAL library, just have used an implementation in a openSCAD which is a very simple free algorythmic 3d mesh program. CGAL doesn't have a .net version, it's c++ only and i don't know anything about libraries they seem very advanced programming tasks, it may be worth seeing if there is some csg documentation in the cgal website, to have an idea of csg loops structures. the ultimate processing power of a csg library is crucial to know in advance of a development.
     
  20. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Alex - thanks. I'll look at it anyway.
    Antony - thanks, CGAL is a library I have used before. Not likely to use it again due to its license issues. The csg editor I want to be pretty much MIT/Apache type licensing - so anyone can use it or commercialize it if they want (no restrictions - GPL is very restrictive).

    I didn't get a build up last night. Still working on some more operators and effects as well as fixing some bugs in the primitive generators. Should have something up tonight.
     
  21. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Ok. Heres the latest. Im going to put this up on github too, so its a little easier to manage.
    csg_toolkit03.png

    This image shows the use of primitives nodes. The nodes only create basic elements, you can still scale, rotate and translate them as you need. The data is lost when you reload the canvas, so I need to figure out that - its probably simple enough to just save the scene with the canvas.

    Also added a Warp node that currently supports X,Y,Z plane "shear" operation. This will be extended to include extrusion, and projection. Im also starting to look at the other library - Alex whats the license for that library?
    I have attached a full zip of my assets folder - only 700KB.. this way you can be sure it should work :)

    Github here: https://github.com/dlannan/csg-toolkit
     

    Attached Files:

    Last edited: Aug 24, 2015
  22. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hiya, nice image, please run a project prior to sharing to see if it indeed does work ok. It's good for me to practice the unity file system exporting... if i export an entire project i would just zip the project, the assets is only part of it. And recently i figured how to select dependencies with right click on a scene file and save unitypackage.

    I just had a go overwriting the old unitypackage and also the assets on it's own, i get this error:
     

    Attached Files:

  23. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Im not sure what you are talking about. The Asset folder _is_ the whole project. If you put the assets folder in an empty directory and then when starting unity "open project" to the new folder, it will work fine. The reason I did this, is so you can get the scene files an the save files (The above scenes should be able to be loaded).

    I do not recommend "overwriting" anything. delete the original plugins folder and replace it with the new one - the files have changed and so will not be compatible with previous files (like demo.cs).

    The errors you have are unrelated to my work (they are unmodified Node Editor.cs file) - I suspect you have multiple files causing conflicts.

    Have a look at loading assets folder in unity for more info on this:
    http://answers.unity3d.com/questions/8924/how-do-i-use-unitys-example-projects.html
    When you load in the project (Asset folder parent) the other folders Library, Project Settings and Temp folders will be created by Unity automatically.

    If you only want the plugin, the copy the Assets/Plugins folder to <Your Project>/Assets/Plugins folder.
    Hope this helps.
     
    Last edited: Aug 25, 2015
  24. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Thanks David, I've been using unity since 2011 roughly, There is probably a small error in the files or something that i can't figure out...

    So here is the Modus Operandi with project files: Unity only recommends sharing entire projects or unitypackages.

    The page your recommended me states that you can only open top level project folders as new projects in U3d.

    A project includes the Assets folder, libraries, and so on, and carries the name of the project and .meta files... the asset folder is only a generic subfolder of Unity Project Folder. Both versions of the "plugin" are the ones i downloaded from you on this page which is probably why they are not working.

    I'm surprised that you don't know that and Asset folder is only a sub folder of a Unity Project, and you can't submit Asset folders to the Asset Store, you can only submit complete project folders and unitypackages and multimedia work because it's not supported by Unity. read up about it.

    Unity example files have never been supplied as Asset folders without the accompanying other files, and you may wish to figure out how to export scenes with their dependencies as unitypackages, or test what you share first in a bare folder.
     
  25. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    who mentioned my wanting plugin only so i can go to the node editor page? can you upload stuff that runs even?

    This is the page you quote:

    1. Start Unity, menu File/Open Project ..., click on the button "Open Other..." and browse to the top folder of the project which contains Assets and Library as subfolders

    2. Assets contains your Assets (models, textures, scripts, sound-files and so on), Library is used by Unity ... you may look at it, but don't touch ;-)

    3. Wherever you feel is appropriate ... I have my own folder with all example projects.
     
  26. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Ok. Lets look at the errors before you start making rather poor assumptions about whats happening and how unity works.
    Error 1 & 2: These are in the file Node Editor.cs - I have not touched this file, so if it has problems then probably visit the forums about it. A possible issue could be if you have created an object called GUI.. then you could be trashing the namespace and its losing it references to BeginClip and EndClip which are static methods in UnityEngine.GUI.
    Error 3: Is in the Tint function and as far as I know, I'm not even using it. And it is a possible error with Node Editor, but again it shouldn't matter because I have not changed any of it.
    Firstly I will point out too, that I have been working with Unity since 2009 both in the simulation industry and after work on my own projects. And have published works and many simulation customers with Unity products. So I have a vague idea of how to go about things in Unity.

    So, in response to some of your erm.. points:

    "A project includes the Assets folder, libraries, and so on, and carries the name of the project and .meta files... the asset folder is only a generic subfolder of Unity Project Folder. Both versions of the "plugin" are the ones i downloaded from you on this page which is probably why they are not working."

    This is quite incorrect. Open your project window. The ONLY accessible folder is the Assets folder. Quite literally the Assets folder is everything Unity needs to create a project. The other folders are generated for user preferences, machine specific settings and caches.

    I did explain how to use Assets.zip and I'll simplify again:
    1. Create a new folder called "My Project"
    2. Place the Assets folder (from the Assets.zip file) into the "My Project" folder.
    3. Open the "My Project" folder with Unity (Open Other). And you can then use the CSG Editor and Scenes.

    To explain how Unity works. Try this (it works on all versions of Unity)
    1. Create a new empty folder called "My Project"
    2. Create a new empty folder inside "My Project" called "Assets"
    3. Open the "My Project" folder with Unity. Unity will happily open and create all the necessary files and folders. This is normal and correct.

    "I'm surprised that you don't know that and Asset folder is only a sub folder of a Unity Project,...." - you obviously didnt read my previous post where I asked you to put the Assets into a new folder and open it. And its fairly obvious you dont realise Unity is generating everything else. What you are saying is a little misleading and untrue. The Asset folder is the key structure to the way Unity works - has been for a very long time, and its probably one of its primary limitations.

    "Unity example files have never been supplied as Asset folders without the accompanying other files..." - originally before packages they were as zipped Asset folders. Theres nothing wrong with this. However its is _very_ bad to copy entire project folders around due to the fact you are copying machine and user settings around to different machines - In larger projects you cannot do this. Same with checking in Library folders to repositories, this is also a very bad practice. You should always build from source and let Unity generate the rest.

    The reason I'm not using packages is because otherwise a package will need to be built everytime some new changes go in, which could be often. If you want a package, then do as I suggest above and then learn how to export the package - its pretty simple. It is much more effective to work direct with the Asset folder in github. Have a look around github, this is how unity projects work.

    " who mentioned my wanting plugin only so i can go to the node editor page? can you upload stuff that runs even?"
    Again.. really dont know what you mean? Have tested on a number of machines and different people have used it already, so not sure what you are talking about - you need to be more specific about what you are doing, and what your problems are.

    To make clear what I have built here:
    It is a plugin that lives in the Assets/Plugins folder. This is not MY choice, this is how the system Node Editor works and so I used that. This is why its all in the Plugins directory - I dont quite see why this is an issue either?

    The page I quoted was to explain how to open a new project with an asset folder in it - it seemed to me you were having problems doing this and even understanding this process. I really dont have time to write up explanations like the above, so I would appreciate it if you have a specific problem, to clearly indicate the issue, and how you actually achieved the issue (repeatable) so I can attempt to replicate it and resolve it.

    So far it seems you have only given me some very odd errors (unrelated to my work) and not really explained how you managed to generate them?
     
  27. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    1. Create a new folder called "My Project"
    2. Place the Assets folder (from the Assets.zip file) into the "My Project" folder.
    3. Open the "My Project" folder with Unity (Open Other). And you can then use the CSG Editor and Scenes.

    That is what i did, twice, to get the odd errors, perhaps i have to upgrade unity, i'm on 4.6 as i prefer it.

    it's easier to localize bugs when supplied with a complete project. The libraries and other folders specify which platform your are building for/running on, web, pc, and which version of .net, of dx10/11.

    I am getting the same error from the node editor file from the forum page, will figure it out later. thanks.
     
  28. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Thanks for trying, U3D was more reliable last time i was coding with it.

    I think that BeginClip() from the node editor .cs doesnt exist any more in the unity engine documentation... so i changed it to BeginGroup and that line runs... and i changed the third error, and it compiles ok, no errors... except that the window .unity scenes are empty and the csg editor is freezing unity/running slow.

    anyways, it's a tricky one, i am sending unity matrix arrays to another program for doing csg now.

    I am interested in which programs can also do boolean ops on geometry, blender, rhino, i don't know there must be many, preferably scriptable ones.
     
    Last edited: Aug 25, 2015
  29. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Ok. Thats quite likely the issue - 4.6 has a fair amount of deprecated code. I currently don't have any 4.6 machines I can test on (oldest is 5.01) so I cant be sure any of my suggestions will necessarily help.
    I believe Begin Group is a different style of call - I would recommend just removing the BeginClip and EndClip - this may result in overflow within the window - not sure. Seems to work in Unity5 if you remove them (I suspect clipping has been implemented internally for the GUI).
    There are a number of warnings from both mine and the Node Editor code - so far they are fairly meaningless and I expect to update the code a few times this week as the features go in (more warping features due tomorrow).
    If you see any errors relating to this, please let me know.
    Cheers,
    Dave Lannan
     
  30. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Still working on this. Have spent some time trying other libraries. The main problem is most are not as "clean" as this library in terms of being able to generate a good mesh without errors. So Im now going back to this original library and work on performance.Updates to come:
    - Performance boost
    - Texturing (simple "one material" pass first, then a dual material method to do later).
    - Reduced interface - make the combination of primitives, operations and outputs more sensible.
    Cheers,
    Dave Lannan
     
  31. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Very nice to see NodeEditor 'in action', great work david:)

    Regarding the errors - I develop the NodeEditor in Unity 5.0.0 so some things might not work, though I started working in 4.6... Begin/EndClip should work regardless of the version. It's only used for clipping the background, so it can run without. Just uncomment if the errors still show up:)

    Furthermore, I'd recommend to not deliver NodeEditor with your package as that will give me the ability to respond and fix errors popping up:) Usually you do not need to modify NodeEditor, you can simply put your content along with the NodeEditor inside the Assets folder. You can keep it though - but a runtime version is currently on the way and you're missing it :p

    Anyways, cool to see it being used for this awesome project:)
     
  32. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Thanks Seneral. I'll move NodeEditor out - a runtime would probably a bit more manageable.
    Currently sorting out the texture coord for this, so I'll try to get these changes in that update.
    Cheers,
    Dave Lannan
     
  33. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hi Dave, I still have unity4.6 so i couldnt load this. I have been busy learning a lot about CSG mostly with the program openscad which implement CGAL very efficiently.

    How did your CSG project develop, what are the current challenges?

    I am convinced that the mulitnational university version of CSG, CGAL will inevitably make less errors that all the other CSG libraries available. It's such an impossibly difficult task, by judging from papers by CSG mathematicians, i think that most implementations will develop errors after a certain amount of time.

    For example, just adding cubes in google sketchup with bool add ends up making errors, and that's google and the trimble company with error codes. Perhaps CGAL has masses of errors too, but at least it thinks that the current mesh is ok enough to continue adding the subsequent meshes, whereas many programs would generate an error.

    I'd be interested in using cgal in unity3d, at least any library that has end results without giant missing triangles, and that doesnt error half way through. Because cgal is in C++ i think it has to be made into a DLL and then called in Unity3d. I am surprised that no one has made a DLL of it for unity geometry, seing as CGAL is a mathetmatics lab of geometry with a superb set of features.

    Perhaps one person has queried about implementing cgal in u3d, It's a mega difficult project?
     
  34. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Hello everybody,

    I'm trying to make use of Arakis' CSG Library in order to apply boolean operations to Unity's primitives. So basically I'm turning two meshes of Unity's primitves (cube) into two solid objects. Then the BooleanModeller for example runs getUnion() and returns a new solid, which I turn back in to a Mesh and apply it to a MeshFilter(). Well, making a union of two cubes (nearly located to each other) turns out into just one cube. I'm not exactly sure how to narrow down this issue, due to my lack of knowledge concerning triangles and vertices.

    So this is the component which I implemented:
    hastebin

    I attached a demonstrating project to this post.


    While implementation, I ran into the issue of having a Color[] with the length of 0 (concerning one cube). So I made that array as long as the vertices[] and just instantiated a Color object for each index. I probably miss out on something major.


    I'm thankful for any advice!
     

    Attached Files:

  35. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Hello MarcoMeter,

    I've tried your package but nothing happens. I just end up with a cube.

    Do you have any idea what might be the problem?

    Kind regards,

    Peter
     
  36. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    738
    Thanks!, really helpful addition!
     
  37. AndrewKovalev

    AndrewKovalev

    Joined:
    May 10, 2016
    Posts:
    1
    Hello, David. Could you help me please? I would like to create primitives in playmode and subtract them (not in editor but in playmode). How can I do that with your asset? That is really important for me. Sorry for my bad English.
     
  38. BlakeSchreurs

    BlakeSchreurs

    Joined:
    Aug 10, 2016
    Posts:
    51
    This is an interesting library, and I've started to get some cool geometry out of it.

    Can this stuff be used at run-time? So far everything's been happening in the editor. I see components as if I might be able to script something at run-time, but I haven't seen any examples on how to do that.
     
  39. davidlannan

    davidlannan

    Joined:
    Jul 11, 2013
    Posts:
    48
    Hi, it has been a long while since I worked on this :) Sorry for the delay.
    Yes, it certainly can be used in runtime (and thats what it has evolved to being used for).

    The components need some work to unhook from the editor and OnGUI calls. However it shouldnt be too hard. The main ones to look at are: OperatorNode, PrimitiveNode, InputNode and OutputNode scripts.
    These do most of the work of generating the appropriate meshes using the bool library.

    I will be looking at this again in a few weeks time (I hope). And I will update this if I get time.
     
  40. BlakeSchreurs

    BlakeSchreurs

    Joined:
    Aug 10, 2016
    Posts:
    51
    Tinkered with it some, and was able to get some meshes via pure script. This made me quite happy... Editor GUIs are nice, but in-game editing is even better.
     
  41. mojinhen

    mojinhen

    Joined:
    Oct 4, 2016
    Posts:
    1

    Hi, I have tried your package.

    It actually works, but you need to find a way to shift the position of both solid object. It seems like after convert gameobject to solid. The positions all shift back to origin.

    I changed one of the cube to sphere and use

    solidB.scale(1.2, 1.2, 1.2);

    This is the end result.
     

    Attached Files:

    • snap.png
      snap.png
      File size:
      148.5 KB
      Views:
      1,579
  42. logicalerror

    logicalerror

    Joined:
    Jan 2, 2013
    Posts:
    207
  43. BlakeSchreurs

    BlakeSchreurs

    Joined:
    Aug 10, 2016
    Posts:
    51
    I think I sent you an e-mail a while back. That asset only works at editor-time, right? I'm specifically looking for a run-time CSG.
     
  44. CodeMonkey24

    CodeMonkey24

    Joined:
    Aug 4, 2017
    Posts:
    1
    Hello,

    I found your code while looking for an efficient system for CSG. I decided to test it out against some complex objects that I am working on for a project. It's really good for performance compared to some other solutions I've seen (mainly those using BSP trees). I have encountered a few issues, but was able to address them. If you are still maintaining the code, you might consider adding these fixes:

    I encountered an odd infinite loop while using this code for boolean intersections. In Object3d.cs, the line "if (face1.equals(getFace(getNumFaces() -1)"

    Face.equals uses the individual vectors that define the face and does an equivalence check on those (v1.equals(face.v1) and so on).

    The Vector3d.equals then does a tolerance comparison of the x, y and z coordinates but also checks if the colors are equal.

    It calls "Color3f.Equals()" which is the default equivalence check inherited from Object. In the model I was testing, the color values were (1,1,0) for both color values, however they were not the same object, so the referenceEquals test failed and as a result caused the "face1.equals" test to fail, and create an infinite loop.

    I resolved this by changing the Color3f class to inherit from IEquatable<Color3f> and implemented a similar tolerance check for the r, g, and b values.

    One other thing I noticed was that Solid.translate only accepted x and y delta values. It was a simple matter to add a dz to the parameters and increment the internal z value accordingly.
     
  45. JakartaIlI

    JakartaIlI

    Joined:
    Feb 6, 2015
    Posts:
    30
    Its possible to find editor-time CSG on skinned mesh? I'm looking for asset.
     
  46. Arakis

    Arakis

    Joined:
    Nov 19, 2014
    Posts:
    2
    Hello 3dDevs,
    here's the author of the port again.

    Some changed was made to improve the performance (thx to Lars), and other minor changes. Feel free to checkout the latest version. Pull requests on github are very welcome.
     
  47. ReniTheGhost

    ReniTheGhost

    Joined:
    Dec 8, 2014
    Posts:
    16
    Hello,

    with a simple unity cube and sphere, difference calculation toke 1.2 sec. I thinks it far too much with these simple meshes.
    I think something is broken because GetBound are called 324756 times for a single calculation.