mirror of
https://github.com/Xevion/Boids.git
synced 2025-12-11 04:06:35 -06:00
Add Triangle rendering
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user