Adetonics/src/Projector.cs

67 lines
2.2 KiB
C#
Raw Normal View History

2026-03-02 15:04:15 +02:00
using Godot;
using System;
using System.Linq;
using System.Threading.Tasks;
public static class Projector
{
2026-03-03 08:51:52 +02:00
public static int[,] Points = new int[512,256];
public static void GatherPoints(PlanetHelper helper, int resolutionH = 2048)
2026-03-02 15:04:15 +02:00
{
Points = new int[resolutionH,resolutionH / 2];
string filename = $"user://points-{resolutionH}-{resolutionH / 2}.dat";
if (FileAccess.FileExists(filename))
{
var readfile = FileAccess.Open(filename, FileAccess.ModeFlags.Read);
for (int x = 0; x < Points.GetLength(0); x++)
{
for (int y = 0; y < Points.GetLength(1); y++)
{
Points[x, y] = (int)readfile.Get32();
}
}
readfile.Close();
return;
}
Parallel.ForEach(Enumerable.Range(0, Points.GetLength(0)), x =>
{
for (int y = 0; y < Points.GetLength(1); y++)
{
float yaw = (float)x / Points.GetLength(0) * 360f;
float pitch = (float)y / Points.GetLength(1) * 180f;
Vector3 point = Vector3.Up;
point = point.Rotated(Vector3.Forward, Mathf.DegToRad(pitch));
point = point.Rotated(Vector3.Up, Mathf.DegToRad(yaw));
int index = helper.Octree.SearchNearest(point)?.Id ?? -1;
Points[x,y] = index;
}
});
var file = FileAccess.Open(filename, FileAccess.ModeFlags.Write);
for (int x = 0; x < Points.GetLength(0); x++)
for (int y = 0; y < Points.GetLength(1); y++)
file.Store32((uint)Points[x,y]);
file.Close();
2026-03-02 15:04:15 +02:00
}
public static ImageTexture Render(PlanetHelper helper)
2026-03-03 08:51:52 +02:00
{
return ImageTexture.CreateFromImage(RenderImage(helper));
}
public static Image RenderImage(PlanetHelper helper)
2026-03-02 15:04:15 +02:00
{
var image = Image.CreateEmpty(Points.GetLength(0), Points.GetLength(1), false, Image.Format.Rgb8);;
2026-03-02 15:04:15 +02:00
for (int x = 0; x < Points.GetLength(0); x++)
{
for (int y = 0; y < Points.GetLength(1); y++)
{
image.SetPixel(x,y, helper.Mdt.GetVertexColor(Points[x,y]));
}
}
2026-03-03 08:51:52 +02:00
return image;
2026-03-02 15:04:15 +02:00
}
}