129 lines
No EOL
2.8 KiB
C#
129 lines
No EOL
2.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class ThePixelgon : MonoBehaviour
|
|
{
|
|
public List<Vector3> vertices;
|
|
|
|
public Mesh meshPoint;
|
|
public Material mat;
|
|
// just edit in the inspector (arrays and such)
|
|
|
|
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Mesh mesh = new Mesh();
|
|
mesh.SetVertices(vertices);
|
|
|
|
// triangle
|
|
// clock wise
|
|
// 0 1 2, 1 2 3 ?
|
|
List<int> tris = new List<int>();//{ 0, 1, 2 };
|
|
|
|
int trav = 1;
|
|
int numSides = vertices.Count;
|
|
|
|
|
|
for (int i = 0; i < numSides - 2; i++)
|
|
{
|
|
tris.Add(0);
|
|
tris.Add(trav);
|
|
tris.Add(trav + 1);
|
|
trav++;
|
|
}
|
|
|
|
/*
|
|
*/
|
|
|
|
mesh.SetTriangles(tris, 0);
|
|
// pixelgon
|
|
// ngon
|
|
// vertices, tris, uvs
|
|
|
|
List<Vector2> uvs = new List<Vector2>();
|
|
// uvs
|
|
Vector3 cv = Vector3.zero;
|
|
for (int i = 0; i < vertices.Count; i++)
|
|
{
|
|
cv += vertices[i];
|
|
}
|
|
|
|
cv = cv / vertices.Count;
|
|
|
|
Vector3 farthest = cv;
|
|
for (int i = 0; i < vertices.Count; i++)
|
|
{
|
|
if (Vector3.Distance(cv, vertices[i]) > Vector3.Distance(cv, farthest))
|
|
{
|
|
farthest = vertices[i];
|
|
}
|
|
}
|
|
float radius = Vector3.Distance(cv, farthest);
|
|
|
|
Vector3 normal = Vector3.Cross(vertices[1] - vertices[0], vertices[vertices.Count - 1] - vertices[0]).normalized;
|
|
|
|
for (int i = 0; i < vertices.Count; i++)
|
|
{
|
|
Vector3 local = vertices[i] - cv;
|
|
local = Quaternion.Inverse(Quaternion.LookRotation(normal)) * local;
|
|
Vector3 uvPos = new Vector2(local.x + radius, local.y + radius);
|
|
// scaling
|
|
Debug.Log(uvPos);
|
|
uvs.Add(uvPos);
|
|
}
|
|
mesh.SetUVs(0, uvs);
|
|
|
|
Graphics.DrawMesh(meshPoint, cv, Quaternion.identity, mat, 0);
|
|
Graphics.DrawMesh(meshPoint, farthest, Quaternion.identity, mat, 0);
|
|
Graphics.DrawMesh(meshPoint, cv + normal, Quaternion.identity, mat, 0);
|
|
|
|
|
|
int width = 4, height = 4;
|
|
|
|
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
|
|
|
|
bool black = true;
|
|
|
|
for (int j = 0; j < width; j++)
|
|
{
|
|
for (int k = 0; k < height; k++)
|
|
{
|
|
if (black)
|
|
texture.SetPixel(j, k, Color.black);
|
|
else
|
|
texture.SetPixel(j, k, Color.white);
|
|
black = !black;
|
|
}
|
|
black = !black;
|
|
}
|
|
/*
|
|
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
|
|
texture.SetPixel(0, 0, Color.black);
|
|
texture.SetPixel(1, 0, Color.white);
|
|
texture.SetPixel(1, 1, Color.black);
|
|
texture.SetPixel(0, 1, Color.white);
|
|
*/
|
|
texture.Apply();
|
|
|
|
texture.filterMode = FilterMode.Point;
|
|
mat.mainTexture = texture;
|
|
|
|
|
|
// pixels
|
|
// texture, scale, rotation, rendering options(point filtering, and cutout transparency)
|
|
|
|
// just reference a 2d texture for now
|
|
|
|
Graphics.DrawMesh(mesh,
|
|
Vector3.zero,
|
|
Quaternion.identity,
|
|
mat, 0
|
|
);
|
|
}
|
|
} |