Created
August 2, 2016 05:54
-
-
Save simonhildebrandt/43047fd62efc0f8898fc8fec9d2f18b4 to your computer and use it in GitHub Desktop.
Unity - extract plane intersections from mesh polygons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class interspect : MonoBehaviour { | |
ArrayList edges = new ArrayList(); | |
ArrayList points = new ArrayList(); | |
Plane plane = new Plane(Vector3.up, 0f); | |
// Use this for initialization | |
void Start () { | |
} | |
void FixedUpdate () { | |
Vector3 t = transform.position; | |
t.y = Mathf.Sin (Time.time) + 0.1f; | |
transform.position = t; | |
calculateIntersections (); | |
} | |
void calculateIntersections() { | |
float f = 0f; | |
edges.Clear (); | |
points.Clear (); | |
Mesh m = GetComponent<MeshFilter>().mesh; | |
for (int i = 0; i < m.triangles.Length; i += 3) { | |
Vector3 p1 = transform.TransformPoint(m.vertices [m.triangles [i]]); | |
Vector3 p2 = transform.TransformPoint(m.vertices [m.triangles [i + 1]]); | |
Vector3 p3 = transform.TransformPoint(m.vertices [m.triangles [i + 2]]); | |
Ray r1 = vectors2ray (p1, p2); | |
Ray r2 = vectors2ray (p2, p3); | |
Ray r3 = vectors2ray (p3, p1); | |
Vector3 i1, i2, i3; | |
if (plane.Raycast (r1, out f)) { | |
i1 = ; | |
} | |
if (plane.Raycast (r2, out f)) { | |
i2 = true; | |
} | |
if (plane.Raycast (r3, out f)) { | |
i3 = true; | |
} | |
if (i1) { | |
edges.Add(new Ray()) | |
} | |
} | |
} | |
Ray vectors2ray(Vector3 start, Vector3 end) { | |
return new Ray (start, end - start); | |
} | |
void OnDrawGizmos() { | |
Gizmos.color = Color.yellow; | |
foreach (Ray r in edges) { | |
Gizmos.DrawRay(r); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment