-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolean.cs
More file actions
73 lines (52 loc) · 2.22 KB
/
Copy pathBoolean.cs
File metadata and controls
73 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections.Generic;
//using System.Drawing;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using static UnityEngine.Networking.UnityWebRequest;
using UnityEngine.ProBuilder.MeshOperations;
using UnityEngine.ProBuilder;
static class Boolean
{
// Tolerance used by `splitPolygon()` to decide if a point is on the plane.
internal const float k_Epsilon = 0.00001f;
public static Model Union(GameObject lhs, GameObject rhs)
{
Model csg_model_a = new Model(lhs);
Model csg_model_b = new Model(rhs);
Node a = new Node(csg_model_a.ToPolygons());
Node b = new Node(csg_model_b.ToPolygons());
List<Polygon> polygons = Node.Union(a, b).AllPolygons();
return new Model(polygons);
}
public static Model Subtract(GameObject lhs, GameObject rhs)
{
Model csg_model_a = new Model(lhs);
Model csg_model_b = new Model(rhs);
Node a = new Node(csg_model_a.ToPolygons());
Node b = new Node(csg_model_b.ToPolygons());
List<Polygon> polygons = Node.Subtract(a, b).AllPolygons();
return new Model(polygons);
}
public static Model Intersect(GameObject lhs, GameObject rhs)
{
Model csg_model_a = new Model(lhs);
Model csg_model_b = new Model(rhs);
Node a = new Node(csg_model_a.ToPolygons());
Node b = new Node(csg_model_b.ToPolygons());
List<Polygon> polygons = Node.Intersect(a, b).AllPolygons();
return new Model(polygons);
}
public static GameObject ModelToObject(Model result)
{
var materials = result.materials.ToArray();
ProBuilderMesh pb = ProBuilderMesh.Create();
pb.GetComponent<MeshFilter>().sharedMesh = (Mesh)result;
pb.GetComponent<MeshRenderer>().sharedMaterials = materials;
MeshImporter importer = new MeshImporter(pb.gameObject);
importer.Import(new MeshImportSettings() { quads = true, smoothing = true, smoothingAngle = 1f });
pb.CenterPivot(null);
MeshCollider mc = pb.gameObject.AddComponent<MeshCollider>();
mc.sharedMesh = (Mesh)result;
return pb.gameObject;
}
}