Files
grass-cutters/Assets/Scripts/Core/MeshUtil.cs
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

42 lines
1.8 KiB
C#

using UnityEngine;
namespace GrassCutter
{
public static class MeshUtil
{
/// <summary>Axis-aligned box mesh with per-face UVs scaled in metres (so tiling textures stay square).</summary>
public static Mesh Box(Vector3 size, Vector3 center)
{
var h = size * 0.5f;
Vector3[] n = { Vector3.up, Vector3.down, Vector3.forward, Vector3.back, Vector3.right, Vector3.left };
var verts = new Vector3[24];
var norms = new Vector3[24];
var uvs = new Vector2[24];
var tris = new int[36];
for (int f = 0; f < 6; f++)
{
Vector3 nz = n[f];
Vector3 ax = Mathf.Abs(nz.y) > 0.5f ? Vector3.right : Vector3.Cross(Vector3.up, nz);
Vector3 ay = Vector3.Cross(nz, ax);
float ex = Vector3.Scale(ax, h).magnitude, ey = Vector3.Scale(ay, h).magnitude, ez = Vector3.Scale(nz, h).magnitude;
for (int k = 0; k < 4; k++)
{
float sx = (k == 1 || k == 2) ? 1 : -1, sy = (k >= 2) ? 1 : -1;
int vi = f * 4 + k;
verts[vi] = center + nz * ez + ax * (sx * ex) + ay * (sy * ey);
norms[vi] = nz;
uvs[vi] = new Vector2((sx * 0.5f + 0.5f) * ex * 2f, (sy * 0.5f + 0.5f) * ey * 2f);
}
int t = f * 6, b = f * 4;
tris[t] = b; tris[t + 1] = b + 1; tris[t + 2] = b + 2;
tris[t + 3] = b; tris[t + 4] = b + 2; tris[t + 5] = b + 3;
}
var m = new Mesh { name = "Box" };
m.vertices = verts; m.normals = norms; m.uv = uvs; m.triangles = tris;
m.RecalculateBounds();
m.RecalculateTangents();
return m;
}
}
}