Mesh Maker Forum
User Info
Welcome, Guest. Please login or register.
December 14, 2019, 09:13:45 PM

Login with username, password and session length
Search:     Advanced search
News
Latest addition to the collection: Prefab Maker
Forum Stats
168 Posts in 60 Topics by 75 Members
Latest Member: ozary
Home Help Search Login Register
Mesh Maker Forum  |  MeshMaker  |  Support  |  Boolean Ops runtime code
« previous next »
Pages: [1] Print
Author Topic: Boolean Ops runtime code  (Read 4301 times)
MediaGiant
Administrator
Jr. Member
*****
Posts: 78


View Profile
« on: September 15, 2014, 08:24:16 PM »

The Boolean operations were written to be performed while in editor mode but the classes are public so you can adapt the following script to perform the operations during runtime:

Code: [Select]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using CSG;

public class Test : MonoBehaviour
{
public enum Operation { Subtract, Union, Intersection };
List<Polygon> origPolygons;
GameObject gameObjectA;
GameObject gameObjectB;
Transform[] childs = new Transform[2];
public Operation operation = Operation.Subtract;

// Use this for initialization
void Start ()
{
gameObjectA = GameObject.Find("Target");
gameObjectB = GameObject.Find("Brush");
childs[0] = gameObjectA.transform;
childs[1] = gameObjectB.transform;
origPolygons = new List<Polygon>();
CSG.CSG.triIndex = 0; // always set this to zero before all operations
CSG.CSG.transform = gameObjectA.transform;
CSG.CSG A = CSG.CSG.fromMesh(childs[0].GetComponent<MeshFilter>().sharedMesh, childs[0], true);
CSG.CSG B = CSG.CSG.fromMesh(childs[1].GetComponent<MeshFilter>().sharedMesh, childs[1], false);
origPolygons.AddRange(A.polygons);
origPolygons.AddRange(B.polygons);
CSG.CSG result = null;

if (operation == Operation.Subtract)
{
result = A.subtract(B);
}

if (operation == Operation.Union)
{
result = A.union(B);
}

if (operation == Operation.Intersection)
{
result = A.intersect(B);
}

if (result != null)
{
result.toMesh(gameObjectA, gameObjectB);
}
}

// Update is called once per frame
void Update ()
{

}
}

To use the code start by copying the following three DLLs to your project folder:
MeshMaker.dll
ComputationalGeometry.dll
VectorGeometry.dll

(or just the BooleanOps.dll if you don't have MeshMaker)

Then attach the script above to a new empty game object.
Create two new cubes and rename them to "Target" and "Brush".
Position the cubes so that they are intersecting and click the play button.
« Last Edit: October 21, 2014, 06:04:28 AM by meshmakeGiant » Logged
Make Allry
Newbie
*
Posts: 1

View Profile
« Reply #1 on: October 20, 2014, 05:17:54 PM »

Hi, im new in MeshMaker, but that topic is exacly what i need to do, use  the boolean ops in runtime.

How I'm supose to import the library and use?

All package is in .dll format, and I never use dlls in unity before.
« Last Edit: October 20, 2014, 05:24:25 PM by Make Allry » Logged
MediaGiant
Administrator
Jr. Member
*****
Posts: 78


View Profile
« Reply #2 on: October 21, 2014, 05:57:04 AM »

Hi, welcome to the forum and thank you for your purchase of MeshMaker. I have updated the code above to show an example of how to perform the operations in runtime. I hope this helps but if have any probs please let me know.
Logged
remigius
Newbie
*
Posts: 2

View Profile
« Reply #3 on: January 07, 2015, 03:44:56 PM »

Hi, first I must say Mesh Maker (Boolean Ops in particular) seems like a super useful tool. I am trying to use Boolean Ops in runtime, so I made a function for quick access to the subtract functionality. I am creating the targetGhost copy because I want to do some calculations on it and then throw it away. The original GameObject must stay intact.

This is the function:
Code: [Select]
GameObject BooleanSubtract(GameObject target, GameObject remove) {
// Clone target for boolean operation
GameObject targetGhost = (GameObject) GameObject.Instantiate(target);

// Subtract candidate from target
CSG.CSG.triIndex = 0; // always set to 0 before all operations
CSG.CSG.transform = target.transform;
CSG.CSG A = CSG.CSG.fromMesh(target.GetComponent<MeshFilter>().sharedMesh, target.transform, true);
CSG.CSG B = CSG.CSG.fromMesh(remove.GetComponent<MeshFilter>().sharedMesh, remove.transform, false);
CSG.CSG result = A.subtract(B);
result.toMesh(targetGhost, targetGhost);

return targetGhost;
}

but occasionally (not always) I get a StackOverflow error:

Code: [Select]
StackOverflowException: The requested operation caused a stack overflow.
CSG.Node.build (System.Collections.Generic.List`1 polys)
CSG.Node.build (System.Collections.Generic.List`1 polys)
...etc


Any idea what could be the problem?
Logged
MediaGiant
Administrator
Jr. Member
*****
Posts: 78


View Profile
« Reply #4 on: January 08, 2015, 11:07:51 AM »

Hi remigius, a stack overflow usually indicates a memory problem. I've never seen this myself with this program but I would look at using DestroyImmeshmakete on the targetGhost mesh between operations.
 
Logged
remigius
Newbie
*
Posts: 2

View Profile
« Reply #5 on: January 09, 2015, 04:13:11 AM »

Hi meshmakeGiant, thanks for your reply. Unfortunately, DestroyImmeshmakete has no effect with regards to the StackOverflow. As a fallback, is there any way to catch this Exception and handle it?
« Last Edit: January 09, 2015, 04:18:56 AM by remigius » Logged
MediaGiant
Administrator
Jr. Member
*****
Posts: 78


View Profile
« Reply #6 on: January 09, 2015, 08:28:33 AM »

The core code is not optimized to reduce the number of triangles produced. What is most likely happening is that with each successive operation the number of triangles is growing so large that internal loops within the code are unable to deal with the number of iterations required to perform the operation.

The program as it stands is okay for one or two operations on the same mesh but will need to be optimized to reduce the triangle count after each operation which will require a new mesh data structure that contains adjacency information. A big job but if you are interested in creating your own version you can grab the original source here at https://github.com/omgwtfgames/csg.cs
Logged
Pages: [1] Print 
« previous next »
 

SMF 2.0.6 | SMF © 2013, Simple Machines | Theme by nesianstyles | Buttons by Andrea