pixelgon/Assets/Scripts/PolyVector.cs
2020-06-05 11:54:36 -07:00

111 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class PolyVector
{
public float vectorScale = 1f;
public Mesh meshJoint, meshVector;
public Material matVector;
List<Matrix4x4> j4 = new List<Matrix4x4>();
List<Matrix4x4> v4 = new List<Matrix4x4>();
int length;
public void AddVectors(List<Vector3> vectors)
{
// vectors.Add(Vector3.zero); // break at end
length = 0;
for (int i = 0; i < vectors.Count; i++)
{
if (vectors[i] == Vector3.zero)
{
length = 0;
}
else
{
Matrix4x4 newJoint = new Matrix4x4();
newJoint.SetTRS(vectors[i],
Quaternion.identity,
Vector3.one * vectorScale
);
j4.Add(newJoint);
if (length > 0 && vectors[i] != vectors[i - 1]) // *-*-*
{
Matrix4x4 m4 = new Matrix4x4();
m4.SetTRS(vectors[i - 1],
Quaternion.LookRotation(vectors[i] - vectors[i - 1]),
new Vector3(vectorScale, vectorScale, Vector3.Distance(vectors[i - 1], vectors[i]))
);
v4.Add(m4);
}
length++;
}
}
}
public void Frame()
{
// trim before render
// v4.RemoveRange(vCount - 1, v4.Count - vCount);
Graphics.DrawMeshInstanced(meshJoint, 0, matVector, j4);
j4.Clear();
Graphics.DrawMeshInstanced(meshVector, 0, matVector, v4);
v4.Clear();
}
public Mesh ToMesh(List<Vector3> vectors)
{
List<Matrix4x4> js = new List<Matrix4x4>();
List<Matrix4x4> vs = new List<Matrix4x4>();
length = 0;
for (int i = 0; i < vectors.Count; i++)
{
if (vectors[i] == Vector3.zero)
{
length = 0;
}
else
{
Matrix4x4 newJoint = new Matrix4x4();
newJoint.SetTRS(vectors[i],
Quaternion.identity,
Vector3.one * vectorScale
);
js.Add(newJoint);
if (length > 0 && vectors[i] != vectors[i - 1]) // *-*-*
{
Matrix4x4 newVector = new Matrix4x4();
newVector.SetTRS(vectors[i - 1],
Quaternion.LookRotation(vectors[i] - vectors[i - 1]),
new Vector3(vectorScale, vectorScale, Vector3.Distance(vectors[i - 1], vectors[i]))
);
vs.Add(newVector);
}
length++;
}
}
CombineInstance[] combI = new CombineInstance[js.Count + vs.Count];
for (int i = 0; i < js.Count; i++)
{
combI[i].mesh = meshJoint;
combI[i].transform = js[i];
}
Debug.Log(vs.Count);
for (int i = 0; i < vs.Count; i++)
{
combI[js.Count + i].mesh = meshVector;
combI[js.Count + i].transform = vs[i];
}
Mesh mesh = new Mesh();
mesh.CombineMeshes(combI, true, true);
return mesh;
}
}