mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-13 12:11:03 -06:00
Add Triangle rendering
This commit is contained in:
8
Assets/Resources.meta
Normal file
8
Assets/Resources.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09d39a851be568849a0f3b20e9b227f2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
77
Assets/Resources/BoidMaterial.mat
Normal file
77
Assets/Resources/BoidMaterial.mat
Normal file
@@ -0,0 +1,77 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!21 &2100000
|
||||
Material:
|
||||
serializedVersion: 6
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: New Material
|
||||
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_ShaderKeywords:
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: -1
|
||||
stringTagMap: {}
|
||||
disabledShaderPasses: []
|
||||
m_SavedProperties:
|
||||
serializedVersion: 3
|
||||
m_TexEnvs:
|
||||
- _BumpMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailAlbedoMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailMask:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _DetailNormalMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _EmissionMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _MainTex:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _MetallicGlossMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _OcclusionMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
- _ParallaxMap:
|
||||
m_Texture: {fileID: 0}
|
||||
m_Scale: {x: 1, y: 1}
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Floats:
|
||||
- _BumpScale: 1
|
||||
- _Cutoff: 0.5
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
- _GlossMapScale: 1
|
||||
- _Glossiness: 0.5
|
||||
- _GlossyReflections: 1
|
||||
- _Metallic: 0
|
||||
- _Mode: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.02
|
||||
- _SmoothnessTextureChannel: 0
|
||||
- _SpecularHighlights: 1
|
||||
- _SrcBlend: 1
|
||||
- _UVSec: 0
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
8
Assets/Resources/BoidMaterial.mat.meta
Normal file
8
Assets/Resources/BoidMaterial.mat.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 475251ab2c9abbc47a59428d1c3a3b24
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 2100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Assets/Triangle.cs
Normal file
43
Assets/Triangle.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Triangle : MonoBehaviour {
|
||||
public Color fillColor = Color.blue;
|
||||
|
||||
private void Start () {
|
||||
// Create Vector2 vertices
|
||||
var vertices2D = new Vector2[] {
|
||||
new Vector2(0,1),
|
||||
new Vector2(0.4f,0),
|
||||
new Vector2(-0.4f,0),
|
||||
};
|
||||
|
||||
var vertices3D = System.Array.ConvertAll<Vector2, Vector3>(vertices2D, v => v);
|
||||
|
||||
// Use the triangulator to get indices for creating triangles
|
||||
var triangulator = new Triangulator(vertices2D);
|
||||
var indices = triangulator.Triangulate();
|
||||
|
||||
// Generate a color for each vertex
|
||||
var colors = Enumerable.Repeat(fillColor, vertices3D.Length).ToArray();
|
||||
|
||||
// Create the mesh
|
||||
var mesh = new Mesh {
|
||||
vertices = vertices3D,
|
||||
triangles = indices,
|
||||
colors = colors
|
||||
};
|
||||
|
||||
mesh.RecalculateNormals();
|
||||
mesh.RecalculateBounds();
|
||||
|
||||
// Set up game object with mesh;
|
||||
var meshRenderer = gameObject.AddComponent<MeshRenderer>();
|
||||
meshRenderer.material = (Material) Resources.Load("BoidMaterial");
|
||||
|
||||
var filter = gameObject.AddComponent<MeshFilter>();
|
||||
filter.mesh = mesh;
|
||||
}
|
||||
}
|
||||
11
Assets/Triangle.cs.meta
Normal file
11
Assets/Triangle.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39c543193832a9745a8985fb0e672377
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
126
Assets/Triangulator.cs
Normal file
126
Assets/Triangulator.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// This script can be used to split a 2D polygon into triangles.
|
||||
/// The algorithm supports concave polygons, but not polygons with holes,
|
||||
/// or multiple polygons at once.
|
||||
/// Taken from <see cref="http://wiki.unity3d.com/index.php?title=Triangulator"/>
|
||||
/// </summary>
|
||||
public class Triangulator
|
||||
{
|
||||
private readonly List<Vector2> _mPoints;
|
||||
|
||||
public Triangulator(IEnumerable<Vector2> points)
|
||||
{
|
||||
_mPoints = new List<Vector2>(points);
|
||||
}
|
||||
|
||||
public int[] Triangulate()
|
||||
{
|
||||
var indices = new List<int>();
|
||||
|
||||
var n = _mPoints.Count;
|
||||
if (n < 3)
|
||||
return indices.ToArray();
|
||||
|
||||
var V = new int[n];
|
||||
if (Area() > 0) {
|
||||
for (var v = 0; v < n; v++)
|
||||
V[v] = v;
|
||||
} else {
|
||||
for (var v = 0; v < n; v++)
|
||||
V[v] = n - 1 - v;
|
||||
}
|
||||
|
||||
var nv = n;
|
||||
var count = 2 * nv;
|
||||
for (int m = 0, v = nv - 1; nv > 2;) {
|
||||
if (count-- <= 0)
|
||||
return indices.ToArray();
|
||||
|
||||
var u = v;
|
||||
if (nv <= u)
|
||||
u = 0;
|
||||
v = u + 1;
|
||||
if (nv <= v)
|
||||
v = 0;
|
||||
var w = v + 1;
|
||||
if (nv <= w)
|
||||
w = 0;
|
||||
|
||||
if (Snip(u, v, w, nv, V)) {
|
||||
int a, b, c, s, t;
|
||||
a = V[u];
|
||||
b = V[v];
|
||||
c = V[w];
|
||||
indices.Add(a);
|
||||
indices.Add(b);
|
||||
indices.Add(c);
|
||||
m++;
|
||||
for (s = v, t = v + 1; t < nv; s++, t++)
|
||||
V[s] = V[t];
|
||||
nv--;
|
||||
count = 2 * nv;
|
||||
}
|
||||
}
|
||||
|
||||
indices.Reverse();
|
||||
return indices.ToArray();
|
||||
}
|
||||
|
||||
private float Area()
|
||||
{
|
||||
var n = _mPoints.Count;
|
||||
var A = 0.0f;
|
||||
for (int p = n - 1, q = 0; q < n; p = q++) {
|
||||
var pval = _mPoints[p];
|
||||
var qval = _mPoints[q];
|
||||
A += pval.x * qval.y - qval.x * pval.y;
|
||||
}
|
||||
return A * 0.5f;
|
||||
}
|
||||
|
||||
private bool Snip(int u, int v, int w, int n, int[] V)
|
||||
{
|
||||
int p;
|
||||
var A = _mPoints[V[u]];
|
||||
var B = _mPoints[V[v]];
|
||||
var C = _mPoints[V[w]];
|
||||
if (Mathf.Epsilon > (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x))
|
||||
return false;
|
||||
for (p = 0; p < n; p++) {
|
||||
if (p == u || p == v || p == w)
|
||||
continue;
|
||||
var P = _mPoints[V[p]];
|
||||
if (InsideTriangle(A, B, C, P))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool InsideTriangle(Vector2 A, Vector2 B, Vector2 C, Vector2 P)
|
||||
{
|
||||
float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
|
||||
float cCROSSap, bCROSScp, aCROSSbp;
|
||||
|
||||
ax = C.x - B.x;
|
||||
ay = C.y - B.y;
|
||||
bx = A.x - C.x;
|
||||
by = A.y - C.y;
|
||||
cx = B.x - A.x;
|
||||
cy = B.y - A.y;
|
||||
apx = P.x - A.x;
|
||||
apy = P.y - A.y;
|
||||
bpx = P.x - B.x;
|
||||
bpy = P.y - B.y;
|
||||
cpx = P.x - C.x;
|
||||
cpy = P.y - C.y;
|
||||
|
||||
aCROSSbp = ax * bpy - ay * bpx;
|
||||
cCROSSap = cx * apy - cy * apx;
|
||||
bCROSScp = bx * cpy - by * cpx;
|
||||
|
||||
return aCROSSbp >= 0.0f && bCROSScp >= 0.0f && cCROSSap >= 0.0f;
|
||||
}
|
||||
}
|
||||
11
Assets/Triangulator.cs.meta
Normal file
11
Assets/Triangulator.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 959398144280d9145b8e5c4ec9f36837
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user