126 lines
3.3 KiB
C#
126 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MegaCacheMeshConstructor
|
|
{
|
|
public class MegaCacheFaceGrid
|
|
{
|
|
public List<int> verts = new List<int>();
|
|
}
|
|
|
|
public static List<Vector3> verts = new List<Vector3>();
|
|
|
|
public static List<Vector3> norms = new List<Vector3>();
|
|
|
|
public static List<Vector2> uvs = new List<Vector2>();
|
|
|
|
public static List<int> tris = new List<int>();
|
|
|
|
public static List<MegaCacheMatFaces> matfaces = new List<MegaCacheMatFaces>();
|
|
|
|
public static MegaCacheFaceGrid[,,] checkgrid;
|
|
|
|
public static Vector3 min;
|
|
|
|
public static Vector3 max;
|
|
|
|
public static Vector3 size;
|
|
|
|
public static int subdivs = 16;
|
|
|
|
public static void BuildGrid(Vector3[] verts)
|
|
{
|
|
checkgrid = new MegaCacheFaceGrid[subdivs, subdivs, subdivs];
|
|
min = verts[0];
|
|
max = verts[0];
|
|
for (int i = 1; i < verts.Length; i++)
|
|
{
|
|
Vector3 vector = verts[i];
|
|
if (vector.x < min.x)
|
|
{
|
|
min.x = vector.x;
|
|
}
|
|
if (vector.x > max.x)
|
|
{
|
|
max.x = vector.x;
|
|
}
|
|
if (vector.y < min.y)
|
|
{
|
|
min.y = vector.y;
|
|
}
|
|
if (vector.y > max.y)
|
|
{
|
|
max.y = vector.y;
|
|
}
|
|
if (vector.z < min.z)
|
|
{
|
|
min.z = vector.z;
|
|
}
|
|
if (vector.z > max.z)
|
|
{
|
|
max.z = vector.z;
|
|
}
|
|
}
|
|
size = max - min;
|
|
}
|
|
|
|
public static void BuildTangents(Mesh mesh)
|
|
{
|
|
int num = mesh.triangles.Length;
|
|
int num2 = mesh.vertices.Length;
|
|
Vector3[] array = new Vector3[num2];
|
|
Vector3[] array2 = new Vector3[num2];
|
|
Vector4[] array3 = new Vector4[num2];
|
|
Vector3[] vertices = mesh.vertices;
|
|
Vector2[] uv = mesh.uv;
|
|
Vector3[] normals = mesh.normals;
|
|
int[] triangles = mesh.triangles;
|
|
if (uv.Length > 0)
|
|
{
|
|
for (int i = 0; i < num; i += 3)
|
|
{
|
|
long num3 = triangles[i];
|
|
long num4 = triangles[i + 1];
|
|
long num5 = triangles[i + 2];
|
|
Vector3 vector = vertices[num3];
|
|
Vector3 vector2 = vertices[num4];
|
|
Vector3 vector3 = vertices[num5];
|
|
Vector2 vector4 = uv[num3];
|
|
Vector2 vector5 = uv[num4];
|
|
Vector2 vector6 = uv[num5];
|
|
float num6 = vector2.x - vector.x;
|
|
float num7 = vector3.x - vector.x;
|
|
float num8 = vector2.y - vector.y;
|
|
float num9 = vector3.y - vector.y;
|
|
float num10 = vector2.z - vector.z;
|
|
float num11 = vector3.z - vector.z;
|
|
float num12 = vector5.x - vector4.x;
|
|
float num13 = vector6.x - vector4.x;
|
|
float num14 = vector5.y - vector4.y;
|
|
float num15 = vector6.y - vector4.y;
|
|
float num16 = 1f / (num12 * num15 - num13 * num14);
|
|
Vector3 vector7 = new Vector3((num15 * num6 - num14 * num7) * num16, (num15 * num8 - num14 * num9) * num16, (num15 * num10 - num14 * num11) * num16);
|
|
Vector3 vector8 = new Vector3((num12 * num7 - num13 * num6) * num16, (num12 * num9 - num13 * num8) * num16, (num12 * num11 - num13 * num10) * num16);
|
|
array[num3] += vector7;
|
|
array[num4] += vector7;
|
|
array[num5] += vector7;
|
|
array2[num3] += vector8;
|
|
array2[num4] += vector8;
|
|
array2[num5] += vector8;
|
|
}
|
|
for (int j = 0; j < num2; j++)
|
|
{
|
|
Vector3 normal = normals[j].normalized;
|
|
Vector3 tangent = array[j].normalized;
|
|
Vector3.OrthoNormalize(ref normal, ref tangent);
|
|
array3[j].x = tangent.x;
|
|
array3[j].y = tangent.y;
|
|
array3[j].z = tangent.z;
|
|
array3[j].w = ((!(Vector3.Dot(Vector3.Cross(normal, tangent), array2[j]) < 0f)) ? 1f : (-1f));
|
|
array3[j] = array3[j].normalized;
|
|
}
|
|
mesh.tangents = array3;
|
|
}
|
|
}
|
|
}
|