107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MegaCacheMeshConstructorOBJ : MegaCacheMeshConstructor
|
|
{
|
|
private static int FindVertGrid(Vector3 p, Vector3 n, Vector2 uv)
|
|
{
|
|
int num = MegaCacheMeshConstructor.subdivs - 1;
|
|
int num2 = 0;
|
|
int num3 = 0;
|
|
int num4 = 0;
|
|
if (MegaCacheMeshConstructor.size.x > 0f)
|
|
{
|
|
num2 = (int)((float)num * ((p.x - MegaCacheMeshConstructor.min.x) / MegaCacheMeshConstructor.size.x));
|
|
}
|
|
if (MegaCacheMeshConstructor.size.y > 0f)
|
|
{
|
|
num3 = (int)((float)num * ((p.y - MegaCacheMeshConstructor.min.y) / MegaCacheMeshConstructor.size.y));
|
|
}
|
|
if (MegaCacheMeshConstructor.size.z > 0f)
|
|
{
|
|
num4 = (int)((float)num * ((p.z - MegaCacheMeshConstructor.min.z) / MegaCacheMeshConstructor.size.z));
|
|
}
|
|
MegaCacheFaceGrid megaCacheFaceGrid = MegaCacheMeshConstructor.checkgrid[num2, num3, num4];
|
|
if (megaCacheFaceGrid == null)
|
|
{
|
|
megaCacheFaceGrid = new MegaCacheFaceGrid();
|
|
MegaCacheMeshConstructor.checkgrid[num2, num3, num4] = megaCacheFaceGrid;
|
|
}
|
|
for (int i = 0; i < megaCacheFaceGrid.verts.Count; i++)
|
|
{
|
|
int num5 = megaCacheFaceGrid.verts[i];
|
|
if (MegaCacheMeshConstructor.verts[num5].x == p.x && MegaCacheMeshConstructor.verts[num5].y == p.y && MegaCacheMeshConstructor.verts[num5].z == p.z && MegaCacheMeshConstructor.norms[num5].x == n.x && MegaCacheMeshConstructor.norms[num5].y == n.y && MegaCacheMeshConstructor.norms[num5].z == n.z && MegaCacheMeshConstructor.uvs[num5].x == uv.x && MegaCacheMeshConstructor.uvs[num5].y == uv.y)
|
|
{
|
|
return num5;
|
|
}
|
|
}
|
|
megaCacheFaceGrid.verts.Add(MegaCacheMeshConstructor.verts.Count);
|
|
MegaCacheMeshConstructor.verts.Add(p);
|
|
MegaCacheMeshConstructor.norms.Add(n);
|
|
MegaCacheMeshConstructor.uvs.Add(uv);
|
|
return MegaCacheMeshConstructor.verts.Count - 1;
|
|
}
|
|
|
|
public static void Construct(List<MegaCacheFace> faces, Mesh mesh, Vector3[] meshverts, bool optimize, bool recalc, bool tangents)
|
|
{
|
|
mesh.Clear();
|
|
if (meshverts == null || meshverts.Length == 0 || faces.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
MegaCacheMeshConstructor.verts.Clear();
|
|
MegaCacheMeshConstructor.norms.Clear();
|
|
MegaCacheMeshConstructor.uvs.Clear();
|
|
MegaCacheMeshConstructor.tris.Clear();
|
|
MegaCacheMeshConstructor.matfaces.Clear();
|
|
MegaCacheMeshConstructor.BuildGrid(meshverts);
|
|
int num = 0;
|
|
for (int i = 0; i < faces.Count; i++)
|
|
{
|
|
if (faces[i].mtlid > num)
|
|
{
|
|
num = faces[i].mtlid;
|
|
}
|
|
}
|
|
num++;
|
|
for (int j = 0; j < num; j++)
|
|
{
|
|
MegaCacheMeshConstructor.matfaces.Add(new MegaCacheMatFaces());
|
|
}
|
|
for (int k = 0; k < faces.Count; k++)
|
|
{
|
|
int mtlid = faces[k].mtlid;
|
|
int item = FindVertGrid(faces[k].v30, faces[k].n0, faces[k].uv0);
|
|
int item2 = FindVertGrid(faces[k].v31, faces[k].n1, faces[k].uv1);
|
|
int item3 = FindVertGrid(faces[k].v32, faces[k].n2, faces[k].uv2);
|
|
MegaCacheMeshConstructor.matfaces[mtlid].tris.Add(item);
|
|
MegaCacheMeshConstructor.matfaces[mtlid].tris.Add(item2);
|
|
MegaCacheMeshConstructor.matfaces[mtlid].tris.Add(item3);
|
|
}
|
|
mesh.vertices = MegaCacheMeshConstructor.verts.ToArray();
|
|
mesh.subMeshCount = MegaCacheMeshConstructor.matfaces.Count;
|
|
if (recalc)
|
|
{
|
|
mesh.RecalculateNormals();
|
|
}
|
|
else
|
|
{
|
|
mesh.normals = MegaCacheMeshConstructor.norms.ToArray();
|
|
}
|
|
mesh.uv = MegaCacheMeshConstructor.uvs.ToArray();
|
|
for (int l = 0; l < MegaCacheMeshConstructor.matfaces.Count; l++)
|
|
{
|
|
mesh.SetTriangles(MegaCacheMeshConstructor.matfaces[l].tris.ToArray(), l);
|
|
}
|
|
if (tangents)
|
|
{
|
|
MegaCacheMeshConstructor.BuildTangents(mesh);
|
|
}
|
|
if (optimize)
|
|
{
|
|
}
|
|
mesh.RecalculateBounds();
|
|
MegaCacheMeshConstructor.checkgrid = null;
|
|
}
|
|
}
|