SlicerExtensions.cs
4.08 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections;
using UnityEngine;
namespace EzySlice {
/**
* Define Extension methods for easy access to slicer functionality
*/
public static class SlicerExtensions {
/**
* SlicedHull Return functions and appropriate overrides!
*/
public static SlicedHull Slice(this GameObject obj, Plane pl, Material crossSectionMaterial = null) {
return Slice(obj, pl, new TextureRegion(0.0f, 0.0f, 1.0f, 1.0f), crossSectionMaterial);
}
public static SlicedHull Slice(this GameObject obj, Vector3 position, Vector3 direction, Material crossSectionMaterial = null) {
return Slice(obj, position, direction, new TextureRegion(0.0f, 0.0f, 1.0f, 1.0f), crossSectionMaterial);
}
public static SlicedHull Slice(this GameObject obj, Vector3 position, Vector3 direction, TextureRegion textureRegion, Material crossSectionMaterial = null) {
Plane cuttingPlane = new Plane();
Matrix4x4 mat = obj.transform.worldToLocalMatrix;
Matrix4x4 transpose = mat.transpose;
Matrix4x4 inv = transpose.inverse;
Vector3 refUp = inv.MultiplyVector(direction).normalized;
Vector3 refPt = obj.transform.InverseTransformPoint(position);
cuttingPlane.Compute(refPt, refUp);
return Slice(obj, cuttingPlane, textureRegion, crossSectionMaterial);
}
public static SlicedHull Slice(this GameObject obj, Plane pl, TextureRegion textureRegion, Material crossSectionMaterial = null) {
return Slicer.Slice(obj, pl, textureRegion, crossSectionMaterial);
}
/**
* These functions (and overrides) will return the final indtaniated GameObjects types
*/
public static GameObject[] SliceInstantiate(this GameObject obj, Plane pl) {
return SliceInstantiate(obj, pl, new TextureRegion(0.0f, 0.0f, 1.0f, 1.0f));
}
public static GameObject[] SliceInstantiate(this GameObject obj, Vector3 position, Vector3 direction) {
return SliceInstantiate(obj, position, direction, null);
}
public static GameObject[] SliceInstantiate(this GameObject obj, Vector3 position, Vector3 direction, Material crossSectionMat) {
return SliceInstantiate(obj, position, direction, new TextureRegion(0.0f, 0.0f, 1.0f, 1.0f), crossSectionMat);
}
public static GameObject[] SliceInstantiate(this GameObject obj, Vector3 position, Vector3 direction, TextureRegion cuttingRegion, Material crossSectionMaterial = null) {
EzySlice.Plane cuttingPlane = new EzySlice.Plane();
Matrix4x4 mat = obj.transform.worldToLocalMatrix;
Matrix4x4 transpose = mat.transpose;
Matrix4x4 inv = transpose.inverse;
Vector3 refUp = inv.MultiplyVector(direction).normalized;
Vector3 refPt = obj.transform.InverseTransformPoint(position);
cuttingPlane.Compute(refPt, refUp);
return SliceInstantiate(obj, cuttingPlane, cuttingRegion, crossSectionMaterial);
}
public static GameObject[] SliceInstantiate(this GameObject obj, Plane pl, TextureRegion cuttingRegion, Material crossSectionMaterial = null) {
SlicedHull slice = Slicer.Slice(obj, pl, cuttingRegion, crossSectionMaterial);
if (slice == null) {
return null;
}
GameObject upperHull = slice.CreateUpperHull(obj, crossSectionMaterial);
GameObject lowerHull = slice.CreateLowerHull(obj, crossSectionMaterial);
if (upperHull != null && lowerHull != null) {
return new GameObject[] { upperHull, lowerHull };
}
// otherwise return only the upper hull
if (upperHull != null) {
return new GameObject[] { upperHull };
}
// otherwise return only the lower hull
if (lowerHull != null) {
return new GameObject[] { lowerHull };
}
// nothing to return, so return nothing!
return null;
}
}
}