Files
Studio/Assets/Scripts/XED/Building/MeshCreator.cs
2025-02-21 11:57:09 +09:00

179 lines
6.5 KiB
C#
Raw Blame History

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TriLibCore.Dae.Schema;
using UnityEngine;
using XRLib;
namespace XED
{
public class MeshCreator : MonoBehaviour,ISingle
{
Vector3[] vertices;
int[] triangles;
public Vector3[] n_GonPoints ;
private MeshFilter meshfilter;
//private Vector3 offset = Vector3.zero;
public void MeshDeformer<T>(HashSet<T> type)
{
if(type is HashSet<Floor>)
{
var floors = type as HashSet<Floor>;
foreach(var floor in floors)
{
MeshDeformer(floor.meshfilter, floor.vertexPosition, 0.1f);
}
}
if(type is HashSet<Wall>)
{
var walls =type as HashSet<Wall>;
foreach (var wall in walls)
{
wall.SetMeshVertice();
MeshDeformer(wall.meshfilter, wall.n_GonPoints, wall.height, wall.offset);
}
}
}
public void MeshDeformer(MeshFilter filter, Vector3[] positions,float height ,Vector3 offset= default(Vector3))
{
meshfilter = filter;
//this.offset = offset;
n_GonPoints = positions;
var polygon = positions.Length;
SetMeshData(polygon, height);
MeshRefresh();
}
private void SetMeshData(int polygon, float height)
{
/////* -------------------- <20>ظ<EFBFBD> -------------------- */
vertices = new Vector3[(polygon + 1) + (polygon + 1) + (polygon * 4) - (polygon * 2)];
var sum = Vector3.zero;
for (int i = 0; i < n_GonPoints.Length; i++)
{
sum += n_GonPoints[i];
}
var center = sum / n_GonPoints.Length;
vertices[0] = new Vector3(center.x, -height * 0.5f, center.z) /*+ offset*/;
for (int i = 1; i <= polygon; i++)
{
vertices[i]
= new Vector3(n_GonPoints[i - 1].x, -height * 0.5f, n_GonPoints[i - 1].z) /*+ offset*/;
}
// 4
triangles = new int[(3 * polygon) + (3 * polygon) + (6 * polygon)];
for (int i = 0; i < polygon - 1; i++)
{
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 2;
triangles[i * 3 + 2] = i + 1;
}
triangles[3 * polygon - 3] = 0;
triangles[3 * polygon - 2] = 1;
triangles[3 * polygon - 1] = polygon;
/* -------------------- <20><><EFBFBD><EFBFBD> -------------------- */
int vIdx = polygon + 1;
vertices[vIdx++] = new Vector3(center.x, height * 0.5f, center.z) /*+ offset*/;
for (int i = 1; i <= polygon; i++)
{
vertices[vIdx++]
= new Vector3(n_GonPoints[i - 1].x, height * 0.5f, n_GonPoints[i - 1].z) /*+ offset*/;
}
//8
int tIdx = 3 * polygon;
for (int i = 0; i < polygon - 1; i++)
{
triangles[tIdx++] = polygon + 1;
triangles[tIdx++] = i + 1 + polygon + 1;
triangles[tIdx++] = i + 2 + polygon + 1;
}
triangles[tIdx++] = polygon + 1;
triangles[tIdx++] = polygon + polygon + 1;
triangles[tIdx++] = 1 + polygon + 1;
/* -------------------- <20><><EFBFBD><EFBFBD> + <20><><EFBFBD><EFBFBD> -------------------- */
vIdx = 2 * polygon + 2;
for (int i = 1; i <= polygon; i++)
{
vertices[vIdx++]
= new Vector3(n_GonPoints[i - 1].x, -height * 0.5f, n_GonPoints[i - 1].z) /*+ offset*/;
}
//11
for (int i = 1; i <= polygon; i++)
{
vertices[vIdx++]
= new Vector3(n_GonPoints[i - 1].x, height * 0.5f, n_GonPoints[i - 1].z) /*+ offset*/;
}
//
tIdx = 6 * polygon;
for (int i = 0; i < polygon - 1; i++)
{
triangles[tIdx++] = (2 * polygon + 2) + i + 0;
triangles[tIdx++] = (2 * polygon + 2) + i + 1;
triangles[tIdx++] = (2 * polygon + 2) + i + polygon;
triangles[tIdx++] = (2 * polygon + 2) + i + 1;
triangles[tIdx++] = (2 * polygon + 2) + i + 1 + polygon;
triangles[tIdx++] = (2 * polygon + 2) + i + polygon;
}
triangles[tIdx++] = (2 * polygon + 2) + polygon - 1;
triangles[tIdx++] = (2 * polygon + 2) + 0;
triangles[tIdx++] = (2 * polygon + 2) + polygon - 1 + polygon;
triangles[tIdx++] = (2 * polygon + 2) + 0;
triangles[tIdx++] = (2 * polygon + 2) + polygon;
triangles[tIdx++] = (2 * polygon + 2) + polygon - 1 + polygon;
}
void UVCreate()
{
float scaleFactor = 0.5f;
int[] tris = meshfilter.mesh.triangles;
Vector3[] verts = meshfilter.mesh.vertices;
Vector2[] uvs = new Vector2[verts.Length];
// Iterate over each face (here assuming triangles)
for (int index = 0; index < tris.Length; index += 3)
{
// Get the three vertices bounding this triangle.
Vector3 v1 = verts[tris[index]];
Vector3 v2 = verts[tris[index + 1]];
Vector3 v3 = verts[tris[index + 2]];
// Compute a vector perpendicular to the face.
Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);
// Form a rotation that points the z+ axis in this perpendicular direction.
// Multiplying by the inverse will flatten the triangle into an xy plane.
Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));
// Assign the uvs, applying a scale factor to control the texture tiling.
uvs[tris[index]] = (Vector2)(rotation * v1) * scaleFactor;
uvs[tris[index + 1]] = (Vector2)(rotation * v2) * scaleFactor;
uvs[tris[index + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}
meshfilter.mesh.uv = uvs;
}
void MeshRefresh()
{
meshfilter.mesh.Clear();
meshfilter.mesh.vertices = vertices;
meshfilter.mesh.triangles = triangles;
UVCreate();
meshfilter.mesh.RecalculateNormals();
}
}
}