Skip to content

Commit 86ef296

Browse files
committed
initial commit.
0 parents  commit 86ef296

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1804
-0
lines changed

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Ll]ibrary/
2+
[Tt]emp/
3+
[Oo]bj/
4+
[Bb]uild/
5+
[Pp]rojectSettings/
6+
Papers/
7+
8+
# Autogenerated VS/MD solution and project files
9+
*.csproj
10+
*.unityproj
11+
*.sln
12+
*.suo
13+
*.user
14+
*.userprefs
15+
*.pidb
16+
*.booproj
17+
18+
# Unity3D Generated File On Crash Reports
19+
sysinfo.txt

Assets/Packages.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo/Materials.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Packages/Triangulation2D/Demo/Materials/CanvasLine.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Packages/Triangulation2D/Demo/Materials/DemoMesh.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Packages/Triangulation2D/Demo/Materials/DemoMeshLine.mat.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo/Prefabs.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Packages/Triangulation2D/Demo/Prefabs/DemoMesh.prefab.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo/Scenes.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Packages/Triangulation2D/Demo/Scenes/Demo.unity.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo/Scripts.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using UnityEngine;
2+
3+
#if UNITY_EDITOR
4+
using UnityEditor;
5+
#endif
6+
7+
using System;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
13+
using mattatz.Utils;
14+
15+
namespace mattatz.Triangulation2D {
16+
17+
public class Demo : MonoBehaviour {
18+
19+
[SerializeField, Range(10f, 30f)] float angle = 20f;
20+
[SerializeField, Range(0.2f, 2f)] float threshold = 1.5f;
21+
[SerializeField] GameObject prefab;
22+
[SerializeField] Material lineMat;
23+
[SerializeField] bool debug;
24+
25+
List<Vector2> points;
26+
27+
Camera cam;
28+
float depth = 0f;
29+
bool dragging;
30+
31+
void Start () {
32+
cam = Camera.main;
33+
depth = Mathf.Abs(cam.transform.position.z - transform.position.z);
34+
points = new List<Vector2>();
35+
36+
if(debug) {
37+
points = LocalStorage.LoadList<Vector2>("points.json");
38+
Build();
39+
}
40+
}
41+
42+
void Update () {
43+
if(Input.GetMouseButtonDown(0)) {
44+
dragging = true;
45+
Clear();
46+
} else if(Input.GetMouseButtonUp(0)) {
47+
// LocalStorage.SaveList<Vector2>(points, "points.json");
48+
49+
dragging = false;
50+
Build();
51+
}
52+
53+
if(dragging) {
54+
var screen = Input.mousePosition;
55+
screen.z = depth;
56+
var p = cam.ScreenToWorldPoint(screen);
57+
var p2D = new Vector2(p.x, p.y);
58+
if(points.Count <= 0 || Vector2.Distance(p2D, points.Last()) > threshold) {
59+
points.Add(p2D);
60+
}
61+
}
62+
}
63+
64+
void Build () {
65+
points = Utils2D.Constrain(points, threshold);
66+
var polygon = Polygon2D.Contour(points.ToArray());
67+
68+
var vertices = polygon.Vertices;
69+
if(vertices.Length < 3) return; // error
70+
71+
var triangulation = new Triangulation2D(polygon, angle);
72+
var go = Instantiate(prefab);
73+
go.transform.SetParent(transform, false);
74+
go.GetComponent<DemoMesh>().SetTriangulation(triangulation);
75+
76+
Clear();
77+
}
78+
79+
void Clear () {
80+
points.Clear();
81+
}
82+
83+
void OnRenderObject () {
84+
if(points != null) {
85+
GL.PushMatrix();
86+
GL.MultMatrix (transform.localToWorldMatrix);
87+
lineMat.SetColor("_Color", Color.white);
88+
lineMat.SetPass(0);
89+
GL.Begin(GL.LINES);
90+
for(int i = 0, n = points.Count - 1; i < n; i++) {
91+
GL.Vertex(points[i]); GL.Vertex(points[i + 1]);
92+
}
93+
GL.End();
94+
GL.PopMatrix();
95+
}
96+
}
97+
98+
void OnDrawGizmos () {
99+
}
100+
101+
}
102+
103+
}
104+

Assets/Packages/Triangulation2D/Demo/Scripts/Demo.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using UnityEngine;
2+
using Random = UnityEngine.Random;
3+
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
7+
namespace mattatz.Triangulation2D {
8+
9+
[RequireComponent (typeof(MeshFilter))]
10+
[RequireComponent (typeof(Rigidbody))]
11+
public class DemoMesh : MonoBehaviour {
12+
13+
[SerializeField] Material lineMat;
14+
15+
Triangle2D[] triangles;
16+
17+
void Start () {
18+
var body = GetComponent<Rigidbody>();
19+
body.AddForce(Vector3.forward * Random.Range(150f, 160f));
20+
body.AddTorque(Random.insideUnitSphere * Random.Range(10f, 20f));
21+
}
22+
23+
void Update () {}
24+
25+
public void SetTriangulation (Triangulation2D triangulation) {
26+
var mesh = triangulation.Build();
27+
GetComponent<MeshFilter>().sharedMesh = mesh;
28+
this.triangles = triangulation.Triangles;
29+
}
30+
31+
void OnRenderObject () {
32+
if(triangles == null) return;
33+
34+
GL.PushMatrix();
35+
GL.MultMatrix (transform.localToWorldMatrix);
36+
37+
lineMat.SetColor("_Color", Color.black);
38+
lineMat.SetPass(0);
39+
GL.Begin(GL.LINES);
40+
for(int i = 0, n = triangles.Length; i < n; i++) {
41+
var t = triangles[i];
42+
GL.Vertex(t.s0.a.Coordinate); GL.Vertex(t.s0.b.Coordinate);
43+
GL.Vertex(t.s1.a.Coordinate); GL.Vertex(t.s1.b.Coordinate);
44+
GL.Vertex(t.s2.a.Coordinate); GL.Vertex(t.s2.b.Coordinate);
45+
}
46+
GL.End();
47+
GL.PopMatrix();
48+
}
49+
50+
}
51+
52+
}
53+

Assets/Packages/Triangulation2D/Demo/Scripts/DemoMesh.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using UnityEngine;
2+
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
namespace mattatz.Utils {
10+
11+
[Serializable]
12+
public class JsonSerialization<T> {
13+
[SerializeField] List<T> target;
14+
public List<T> ToList() { return target; }
15+
public JsonSerialization(List<T> target) {
16+
this.target = target;
17+
}
18+
}
19+
20+
public class LocalStorage {
21+
22+
public static void SaveList<T> (List<T> target, string fileName) {
23+
var text = JsonUtility.ToJson(new JsonSerialization<T>(target));
24+
Save(text, fileName);
25+
}
26+
27+
public static List<T> LoadList<T> (string fileName) {
28+
string json = Load(fileName);
29+
return JsonUtility.FromJson<JsonSerialization<T>>(json).ToList();
30+
}
31+
32+
public static void Save (string text, string fileName) {
33+
string path = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
34+
FileInfo fi = new FileInfo(path);
35+
StreamWriter sw = fi.CreateText();
36+
sw.WriteLine(text);
37+
sw.Flush();
38+
sw.Close();
39+
}
40+
41+
public static string Load (string fileName) {
42+
string path = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
43+
return System.IO.File.ReadAllText(path);
44+
}
45+
46+
}
47+
48+
}
49+

Assets/Packages/Triangulation2D/Demo/Scripts/LocalStorage.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Packages/Triangulation2D/Demo/Shaders.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)