138 lines
No EOL
3.4 KiB
C#
138 lines
No EOL
3.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[Serializable]
|
|
public class ToolNewQuad
|
|
{
|
|
public Transform planar;
|
|
|
|
[HideInInspector]
|
|
public Vector3 cursorPos;
|
|
[HideInInspector]
|
|
public Mesh mesh;
|
|
[HideInInspector]
|
|
public Vector3[] quad = new Vector3[4];
|
|
[HideInInspector]
|
|
public int indexQuad = 0;
|
|
|
|
int[] tris = new int[6];
|
|
Vector2[] uvs = new Vector2[4];
|
|
|
|
public void Frame(Input input, Pixelgon pixelgon)
|
|
{
|
|
List<Mesh> quads = new List<Mesh>();
|
|
// pixelgon.quads; // for convenience
|
|
cursorPos = input.twistCursor; // can do more ^-^
|
|
|
|
// vertice snapping
|
|
if (input.mainCon.one.held)
|
|
{
|
|
Vector3 closestVertice = Vector3.one * 666; // lol
|
|
for (int i = 0; i < quads.Count; i++)
|
|
{
|
|
for (int j = 0; j < quads[i].vertices.Length; j++)
|
|
{
|
|
if ((quads[i].vertices[j] - cursorPos).sqrMagnitude <
|
|
(closestVertice - cursorPos).sqrMagnitude)
|
|
{
|
|
closestVertice = quads[i].vertices[j];
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((closestVertice - cursorPos).magnitude < 0.02f) // hard coded
|
|
{
|
|
cursorPos = closestVertice;
|
|
}
|
|
}
|
|
|
|
planar.position = (quad[0] + quad[1] + quad[2] + quad[3]) / 4;
|
|
|
|
switch (indexQuad)
|
|
{
|
|
case (0):
|
|
if (input.mainCon.trigger.down)
|
|
{
|
|
for (int i = 0; i < quad.Length; i++)
|
|
{
|
|
quad[i] = cursorPos;
|
|
}
|
|
|
|
indexQuad++;
|
|
}
|
|
break;
|
|
case (1):
|
|
quad[indexQuad] = cursorPos;
|
|
|
|
if (input.mainCon.trigger.down)
|
|
{
|
|
indexQuad++;
|
|
}
|
|
break;
|
|
case (2):
|
|
quad[indexQuad] = cursorPos;
|
|
|
|
if (input.mainCon.trigger.down)
|
|
{
|
|
indexQuad++;
|
|
}
|
|
break;
|
|
case (3):
|
|
Vector3 v3 = Vector3.Cross(quad[0] - quad[1], quad[2] - quad[1]).normalized;
|
|
planar.rotation = Quaternion.LookRotation(v3, quad[2] - quad[1]);
|
|
cursorPos = planar.InverseTransformPoint(cursorPos);
|
|
cursorPos.z = 0;
|
|
cursorPos = planar.TransformPoint(cursorPos);
|
|
|
|
quad[indexQuad] = cursorPos;
|
|
|
|
if (input.mainCon.trigger.down)
|
|
{
|
|
// w/triangle seam between the two closest points
|
|
if (Vector3.Distance(quad[0], quad[2]) < Vector3.Distance(quad[1], quad[3]))
|
|
{
|
|
tris[0] = 0; tris[3] = 2;
|
|
tris[1] = 1; tris[4] = 3;
|
|
tris[2] = 2; tris[5] = 0;
|
|
}
|
|
else
|
|
{
|
|
tris[0] = 0; tris[3] = 3;
|
|
tris[1] = 1; tris[4] = 1;
|
|
tris[2] = 3; tris[5] = 2;
|
|
}
|
|
|
|
int furthest = 0;
|
|
for (int i = 1; i < quad.Length; i++)
|
|
{
|
|
if ((quad[i] - planar.position).magnitude >
|
|
(quad[furthest] - planar.position).magnitude)
|
|
{
|
|
furthest = i;
|
|
}
|
|
}
|
|
|
|
float scale = planar.InverseTransformPoint(quad[furthest]).magnitude;
|
|
|
|
for (int i = 0; i < quad.Length; i++)
|
|
{
|
|
uvs[i] = ((Vector2)planar.InverseTransformPoint(
|
|
quad[i]) / (scale * 2)) + new Vector2(0.5f, 0.5f
|
|
);
|
|
}
|
|
|
|
mesh = new Mesh();
|
|
mesh.SetVertices(quad);
|
|
mesh.SetTriangles(tris, 0);
|
|
mesh.SetUVs(0, uvs);
|
|
|
|
quads.Add(mesh);
|
|
|
|
indexQuad = 0;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} |