forked from JereKuusela/BetterContinents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageMapFloat.cs
95 lines (84 loc) · 2.35 KB
/
ImageMapFloat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Diagnostics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using UnityEngine;
namespace BetterContinents;
internal class ImageMapFloat : ImageMapBase
{
public static ImageMapFloat? Create(string path)
{
if (string.IsNullOrEmpty(path))
return null;
ImageMapFloat map = new()
{
FilePath = path
};
if (!map.LoadSourceImage())
return null;
if (!map.CreateMap())
return null;
return map;
}
public static ImageMapFloat? Create(byte[] data, string path, bool legacy = false)
{
ImageMapFloat map = new()
{
FilePath = path,
SourceData = data
};
if (legacy)
{
if (!map.CreateMapLegacy())
return null;
}
else
{
if (!map.CreateMap())
return null;
}
return map;
}
public static ImageMapFloat? Create(byte[] data)
{
ImageMapFloat map = new()
{
SourceData = data
};
if (!map.CreateMap())
return null;
return map;
}
private float[] Map = [];
public bool CreateMap() => CreateMap<L16>();
public bool CreateMapLegacy() => CreateMap<Rgba32>();
protected override bool LoadTextureToMap<T>(Image<T> image)
{
var sw = new Stopwatch();
sw.Start();
Map = LoadPixels(image, pixel => pixel.ToVector4().X);
BetterContinents.Log($"Time to process {FilePath}: {sw.ElapsedMilliseconds} ms");
return true;
}
public float GetValue(float x, float y)
{
float xa = x * (Size - 1);
float ya = y * (Size - 1);
int xi = Mathf.FloorToInt(xa);
int yi = Mathf.FloorToInt(ya);
float xd = xa - xi;
float yd = ya - yi;
int x0 = Mathf.Clamp(xi, 0, Size - 1);
int x1 = Mathf.Clamp(xi + 1, 0, Size - 1);
int y0 = Mathf.Clamp(yi, 0, Size - 1);
int y1 = Mathf.Clamp(yi + 1, 0, Size - 1);
float p00 = Map[y0 * Size + x0];
float p10 = Map[y0 * Size + x1];
float p01 = Map[y1 * Size + x0];
float p11 = Map[y1 * Size + x1];
return Mathf.Lerp(
Mathf.Lerp(p00, p10, xd),
Mathf.Lerp(p01, p11, xd),
yd
);
}
}