This repository has been archived on 2026-10-29. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
CultMath/unity/Runtime/PlanetaryPatchMeshAdapter.cs
2026-07-13 02:14:11 +01:00

36 lines
1.4 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Rendering;
namespace CultMath.Unity;
public static class PlanetaryPatchMeshAdapter
{
public static Mesh CreateFaceMesh(PlanetaryCubeFace face, int cellsPerAxis, float radius = 1)
{
if (!float.IsFinite(radius) || radius <= 0) throw new ArgumentOutOfRangeException(nameof(radius));
var source = PlanetaryPatch.CreateFace(face, cellsPerAxis);
var vertices = new Vector3[source.Vertices.Length];
var normals = new Vector3[source.Vertices.Length];
var uv = new Vector2[source.Vertices.Length];
for (var i = 0; i < source.Vertices.Length; i++)
{
var direction = source.Vertices[i].UnitDirection;
var normal = new Vector3(direction.x, direction.y, direction.z);
vertices[i] = normal * radius;
normals[i] = normal;
uv[i] = new Vector2(source.Vertices[i].LocalCoordinate.x, source.Vertices[i].LocalCoordinate.y);
}
var mesh = new Mesh
{
name = $"CultMath Planetary {face} {cellsPerAxis}x{cellsPerAxis}",
indexFormat = vertices.Length > ushort.MaxValue ? IndexFormat.UInt32 : IndexFormat.UInt16,
vertices = vertices,
normals = normals,
uv = uv,
triangles = source.Indices,
};
mesh.RecalculateBounds();
return mesh;
}
}