Build portable planetary field runtime
This commit is contained in:
parent
15f27173dd
commit
42fef7cdd7
24 changed files with 2214 additions and 0 deletions
36
unity/Runtime/PlanetaryPatchMeshAdapter.cs
Normal file
36
unity/Runtime/PlanetaryPatchMeshAdapter.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Reference in a new issue