Adetonics/PlanetFormer.cs
2026-03-02 13:52:26 +02:00

676 lines
27 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
public partial class PlanetFormer : MeshInstance3D
{
[Export] private FastNoiseLite ContinentalNoise;
[Export] private FastNoiseLite MountainNoise;
[Export] private FastNoiseLite HFNoise;
[Export] private int _plateCount = 14;
[Export] private float _landRatio = 0.4f;
MeshDataTool mdt = new MeshDataTool();
public enum GenerationStage
{
NotStarted,
Initialization,
PlateExpansion,
BorderSearch,
CalculateEdgeDistance,
CalculateEdgeStress,
BorderExpansion,
HeightCalc,
Completed,
}
public GenerationStage StopAt = GenerationStage.Completed;
public class PlateData(int Id = 0, Color Color = new(), bool IsLandform = false, List<int> Vertices = null)
{
public int Id { get; set; } = Id;
public Color Color { get; set; } = Color;
public bool IsLandform { get; set; } = IsLandform;
public List<int> Vertices { get; set; } = Vertices;
public int CenterVertexId = -1;
public float PlateExpansion { get; set; } = RandF(0.5f, 2f);
public Vector3 Dir { get; set; } = Vector3.Zero;
}
public class VertexData(int Id = 0, int PlateId = 0, List<int> Neighbours = null, bool StageComplete = false)
{
public int Id { get; set; } = Id;
public int PlateId { get; set; } = PlateId;
public List<StrainAnalysis> StrainSamples { get; set; } = new();
public List<int> Neighbours { get; set; } = Neighbours;
public bool StageComplete { get; set; } = StageComplete;
public bool IsEdge = false;
public bool IsTypeEdge = false;
public float EdgeDistance = -1f;
public float Height = 0f;
}
public enum StrainType
{
Tension, // Pulling apart
Compression, // Pushing together
Shear // Sliding past each other
}
public class StrainAnalysis
{
public float Magnitude; // Total magnitude of the strain
public StrainType Type; // The dominant type of force
public float NormalRate; // Rate of convergence/divergence
public float ShearRate; // Rate of sliding
}
public List<PlateData> Plates = new List<PlateData>();
public List<VertexData> Vertices = new List<VertexData>();
private MeshInstance3D MeshInstance;
private ArrayMesh arrayMesh;
private GenerationStage Stage = GenerationStage.NotStarted;
Stopwatch _generationStopwatch = new Stopwatch();
public static float RandF(float min, float max)
{
return min + (max - min) * Random.Shared.NextSingle();
}
public override void _Ready()
{
MeshInstance = this;
if (MeshInstance.Mesh is ArrayMesh mesh)
{
arrayMesh = mesh;
}
if (MeshInstance.GetSurfaceOverrideMaterial(0) is ShaderMaterial shaderMaterial)
{
shaderMaterial.SetShaderParameter("mode", 1);
}
}
public void InitializeGeneration()
{
GD.Print("Starting Generation!");
Plates = new();
Vertices = new();
ChangeStage(GenerationStage.Initialization);
mdt.CreateFromSurface(arrayMesh, 0);
for (int i = 0; i < mdt.GetVertexCount(); i++)
{
// Init to black
mdt.SetVertexColor(i, Colors.Black);
Vertices.Add(new VertexData(i, -1,GetNeighboringVertices(i, false).OrderBy(v => Guid.NewGuid()).ToList()));
}
// Initialize Plates
for (int i = 0; i < _plateCount; i++)
{
// Get a random un-assigned vertex.
VertexData vertex = Vertices.Where(v => v.PlateId == -1).OrderBy(v => Guid.NewGuid()).First();
vertex.PlateId = i;
var color = new Color(RandF(0f, 1f), RandF(0f, 1f), RandF(0f, 1f));
ColorVertex(vertex.Id, color);
PlateData plate = new PlateData(i, color, false, [vertex.Id]);
plate.Dir = GetRandomTangentialVelocity(mdt.GetVertex(vertex.Id), RandF(0f, 1f));
Plates.Add(plate);
}
ChangeStage(GenerationStage.PlateExpansion);
}
public void AssignOceanPlates(List<PlateData> areas)
{
int n = areas.Count;
double totalArea = areas.Sum(a => a.Vertices.Count * a.PlateExpansion);
double targetOcean = totalArea * _landRatio;
double bestDiff = double.MaxValue;
int bestMask = 0;
int combinations = 1 << n;
for (int mask = 0; mask < combinations; mask++)
{
int oceanArea = 0;
for (int i = 0; i < n; i++)
{
if ((mask & (1 << i)) != 0)
oceanArea += (int)(areas[i].Vertices.Count * areas[i].PlateExpansion);
}
double diff = Math.Abs(oceanArea - targetOcean);
if (diff < bestDiff)
{
bestDiff = diff;
bestMask = mask;
}
}
for (int i = 0; i < n; i++)
{
areas[i].IsLandform = (bestMask & (1 << i)) != 0;
Color color = GetInitialColor(areas[i].IsLandform);
areas[i].Color = color;
foreach (int v in areas[i].Vertices)
{
ColorVertex(v, color);
}
}
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("spacebar"))
{
InitializeGeneration();
if (MeshInstance.GetSurfaceOverrideMaterial(0) is ShaderMaterial shaderMaterial)
{
shaderMaterial.SetShaderParameter("mode", 1);
}
}
if (Stage is GenerationStage.Completed or GenerationStage.NotStarted)
return;
List<VertexData> availableVerts;
switch (Stage)
{
default:
case GenerationStage.Initialization:
break;
case GenerationStage.PlateExpansion:
availableVerts = Vertices.Where(d => d.StageComplete == false && d.PlateId != -1).OrderBy(v => Guid.NewGuid()).ToList();
foreach (PlateData plateData in Plates)
{
var plateVerts = availableVerts.Where(d => d.PlateId == plateData.Id);
foreach (VertexData vertexData in plateVerts.Take((int)((5 + plateVerts.Count() / 4) * plateData.PlateExpansion)))
{
int expandTo = GetFreeNeighbourIndex(vertexData);
if (expandTo != -1)
{
Vertices[expandTo].PlateId = plateData.Id;
plateData.Vertices.Add(expandTo);
ColorVertex(expandTo, plateData.Color);
}
else
{
vertexData.StageComplete = true;
}
}
}
if (!availableVerts.Any())
{
foreach (VertexData vertexData in Vertices)
vertexData.StageComplete = false;
AssignOceanPlates(Plates);
ChangeStage(GenerationStage.BorderSearch);
}
break;
case GenerationStage.BorderSearch:
availableVerts = Vertices.Where(d => d.StageComplete == false).Take(2500).ToList();
foreach (VertexData vertexData in availableVerts)
{
// Do we have any neighbours of another plate?
var neighbours = GetNeighboringVertices(vertexData.Id, false).ToList();
if (neighbours
.Any(v => Vertices[v].PlateId != vertexData.PlateId))
{
vertexData.IsEdge = true;
vertexData.IsTypeEdge = neighbours.Any(n => Plates[Vertices[n].PlateId].IsLandform != Plates[vertexData.PlateId].IsLandform);
if (vertexData.IsTypeEdge)
vertexData.EdgeDistance = 1f;
ColorVertex(vertexData.Id, vertexData.IsTypeEdge ? Colors.White : Colors.Black);
}
else
{
ColorVertex(vertexData.Id, Plates[vertexData.PlateId].Color);
}
vertexData.StageComplete = true;
}
if (!availableVerts.Any())
{
foreach (VertexData vertexData in Vertices)
vertexData.StageComplete = false;
ChangeStage(GenerationStage.CalculateEdgeDistance);
}
break;
case GenerationStage.CalculateEdgeDistance:
availableVerts = Vertices.Where(d => d.StageComplete == false && d.EdgeDistance > 0f).OrderBy(v => v.EdgeDistance).Take(2500).ToList();
foreach (VertexData vertexData in availableVerts)
{
var neighbours = GetNeighboringVertices(vertexData.Id, false).ToList();
foreach (int neighbour in neighbours)
{
if (Vertices[neighbour].EdgeDistance > 0f && Vertices[neighbour].EdgeDistance < vertexData.EdgeDistance + 1f)
continue;
VertexData neighbourVert = Vertices[neighbour];
neighbourVert.EdgeDistance = vertexData.EdgeDistance + 1f;
ColorVertex(neighbourVert.Id, Plates[vertexData.PlateId].Color * 0.8f);
}
vertexData.StageComplete = true;
}
if (!availableVerts.Any())
{
float maxDistance = Vertices.Max(v => v.EdgeDistance);
foreach (VertexData vertexData in Vertices)
{
vertexData.EdgeDistance /= maxDistance;
}
foreach (PlateData plateData in Plates)
{
plateData.CenterVertexId =
Vertices.Where(v => v.PlateId == plateData.Id).MaxBy(v => v.EdgeDistance).Id;
}
foreach (VertexData vertexData in Vertices)
vertexData.StageComplete = false;
ChangeStage(GenerationStage.CalculateEdgeStress);
}
break;
case GenerationStage.CalculateEdgeStress:
availableVerts = Vertices.Where(d => d.StageComplete == false && d.IsEdge).Take(2500).ToList();
foreach (VertexData vertexData in availableVerts)
{
var neighbours = GetNeighboringVertices(vertexData.Id, false).ToList();
foreach (int neighbour in neighbours)
{
if (!Vertices[neighbour].IsEdge)
continue;
if (Vertices[neighbour].PlateId == vertexData.PlateId)
continue;
PlateData plateA = Plates[vertexData.PlateId];
PlateData plateB = Plates[Vertices[neighbour].PlateId];
VertexData centerA = Vertices[plateA.CenterVertexId];
VertexData centerB = Vertices[plateB.CenterVertexId];
Vector3 p1, p2;
p1 = mdt.GetVertex(vertexData.Id).Cross(mdt.GetVertex(centerA.Id));
p2 = mdt.GetVertex(neighbour).Cross(mdt.GetVertex(centerB.Id));
vertexData.StrainSamples.Add(CalculateStrainMagnitude(p1, p2, plateA.Dir, plateB.Dir));
}
vertexData.StageComplete = true;
var majorStrain = AverageStrainList(vertexData.StrainSamples);
switch (majorStrain.Type)
{
case StrainType.Compression:
ColorVertex(vertexData.Id, Colors.Red * majorStrain.Magnitude);
break;
case StrainType.Shear:
ColorVertex(vertexData.Id, Colors.Yellow * majorStrain.Magnitude);
break;
case StrainType.Tension:
ColorVertex(vertexData.Id, Colors.Blue * majorStrain.Magnitude);
break;
}
}
if (!availableVerts.Any())
{
foreach (VertexData vertexData in Vertices)
vertexData.StageComplete = false;
ChangeStage(GenerationStage.BorderExpansion);
}
break;
case GenerationStage.BorderExpansion:
availableVerts = Vertices.Where(d => d.StageComplete == false && d.IsEdge && d.StrainSamples.Any()).OrderBy(d => Mathf.Abs(d.StrainSamples.Max(s => s.Magnitude))).Take(2500).ToList();
foreach (VertexData vertexData in availableVerts)
{
var neighbours = GetNeighboringVertices(vertexData.Id, false).ToList();
var majorStrain = AverageStrainList(vertexData.StrainSamples);
foreach (int neighbour in neighbours)
{
VertexData neighbourVert = Vertices[neighbour];
neighbourVert.IsEdge = true;
var newStrain = new StrainAnalysis();
newStrain.Magnitude = majorStrain.Magnitude * 0.9f;
newStrain.Type = majorStrain.Type;
newStrain.NormalRate = majorStrain.NormalRate * 0.9f;
newStrain.ShearRate = majorStrain.ShearRate * 0.9f;
neighbourVert.StrainSamples.Add(newStrain);
var newAverage = AverageStrainList(neighbourVert.StrainSamples);;
switch (majorStrain.Type)
{
case StrainType.Compression:
ColorVertex(neighbourVert.Id, Colors.Red * newAverage.Magnitude);
break;
case StrainType.Shear:
ColorVertex(neighbourVert.Id, Colors.Yellow * newAverage.Magnitude);
break;
case StrainType.Tension:
ColorVertex(neighbourVert.Id, Colors.Blue * newAverage.Magnitude);
break;
}
}
if (neighbours.All(n => Vertices[n].IsEdge))
{
vertexData.StageComplete = true;
}
}
if (!availableVerts.Any())
{
foreach (VertexData vertexData in Vertices)
{
vertexData.StageComplete = false;
ColorVertex(vertexData.Id, Colors.White * vertexData.Height);
}
ChangeStage(GenerationStage.HeightCalc);
}
break;
case GenerationStage.HeightCalc:
availableVerts = Vertices.Where(d => d.StageComplete == false).Take(2500).ToList();
foreach (VertexData vertexData in availableVerts)
{
PlateData plate = Plates[vertexData.PlateId];
float continentalNoise = ContinentalNoise.GetNoise3Dv(GetVertexPosition(vertexData.Id));
float mountainNoise = (1.0f + MountainNoise.GetNoise3Dv(GetVertexPosition(vertexData.Id))) * 0.5f;
float hfNoise = HFNoise.GetNoise3Dv(GetVertexPosition(vertexData.Id));
var majorStrain = AverageStrainList(vertexData.StrainSamples);
var normalRate = -majorStrain.NormalRate * majorStrain.Magnitude * (plate.IsLandform ? 1f : 0.5f);
var edgeDistance = vertexData.EdgeDistance * (plate.IsLandform ? 1f : -1f);
float height = 0.5f;
//height *= plate.PlateExpansion;
float mult = 2f;
height += hfNoise;
height = (height + 0.5f * mult) / (1f + mult);
height += continentalNoise;
height = (height + 0.5f * mult) / (1f + mult);
height += edgeDistance * 0.25f;
height = (height + 0.5f * mult) / (1f + mult);
height += normalRate * 0.35f;
height = Mathf.Clamp(height, 0.01f, 0.99f);
ColorVertex(vertexData.Id, Colors.White * height);
vertexData.StageComplete = true;
vertexData.Height = height;
}
if (!availableVerts.Any())
{
GD.Print($"Heights - min:'{Vertices.Min(v => v.Height)}' - max:'{Vertices.Max(v => v.Height)}' - average:'{Vertices.Average(v => v.Height)}'");
ScaleValues(Vertices);
foreach (VertexData vertexData in Vertices)
ColorVertex(vertexData.Id, Colors.White * vertexData.Height);
float oceanPercentage = Vertices.Count(v => v.Height < 0.5f) / (float)Vertices.Count;
GD.Print($"Ocean Percentage:'{oceanPercentage}'");
ChangeStage(GenerationStage.Completed);
if (MeshInstance.GetSurfaceOverrideMaterial(0) is ShaderMaterial shaderMaterial)
{
shaderMaterial.SetShaderParameter("mode", 2);
}
}
break;
}
UpdateMesh();
}
public Vector3 GetRandomTangentialVelocity(Vector3 pointOnSphere, float speed)
{
// 1. Normalize the input point to ensure it represents the normal vector
// (If your sphere is not at 0,0,0, use the vector from center to point)
Vector3 normal = pointOnSphere.Normalized();
// 2. Generate a random vector
Random rand = new Random();
Vector3 randomVec = new Vector3(
(float)(rand.NextDouble() - 0.5), // Range -0.5 to 0.5
(float)(rand.NextDouble() - 0.5),
(float)(rand.NextDouble() - 0.5)
);
// 3. Calculate the tangent using Cross Product
// Cross Product of (randomVec, normal) gives a vector perpendicular to both.
// Since it is perpendicular to the normal, it is tangential to the sphere.
Vector3 tangent = randomVec.Cross(normal);
// 4. Edge Case Handling
// If the random vector happens to be parallel to the normal,
// the cross product will result in a zero vector.
if (tangent.Dot(tangent) < 1e-6f)
{
// If parallel, force a non-zero vector by modifying the input slightly
// or simply recurse (try again). Here we force a modification for safety.
randomVec = new Vector3(0, 1, 0);
tangent = randomVec.Cross(normal);
}
// 5. Normalize to ensure unit length
Vector3 normalizedTangent = tangent.Normalized();
// 6. Scale by the desired speed
return normalizedTangent * speed;
}
public void ScaleValues(List<VertexData> values)
{
float maxDistance = Vertices.Max(s => Mathf.Abs(s.Height - 0.5f));
float scale = 0.5f/maxDistance;
values.ForEach(v => v.Height = Mathf.Clamp(0.5f + (v.Height - 0.5f) * scale, 0.01f, 0.99f));
GD.Print($"Heights Post Scaling - min:'{Vertices.Min(v => v.Height)}' - max:'{Vertices.Max(v => v.Height)}' - average:'{Vertices.Average(v => v.Height)}'");
}
public void Normalize()
{
float min = Vertices.Min(s => s.Height);
float max = Vertices.Max(s => s.Height);
float mult = 0f;
if (1f - max > min)
{
// closer to max
mult = 1f / max;
}
else
{
// closer to min
mult = 1f / 1f + min;
}
foreach (var v in Vertices)
{
v.Height *= mult;
}
GD.Print($"Heights Post Normalization - min:'{Vertices.Min(v => v.Height)}' - max:'{Vertices.Max(v => v.Height)}' - average:'{Vertices.Average(v => v.Height)}'");
}
public StrainAnalysis CalculateStrainMagnitude(Vector3 p1, Vector3 p2, Vector3 v1, Vector3 v2)
{
StrainAnalysis result = new StrainAnalysis();
// 1. Geometry and Relative Velocity
Vector3 edge = p2 - p1;
float edgeLength = edge.Length();
// If points are identical, strain is zero
if (edgeLength < float.Epsilon)
{
result.Magnitude = 0;
result.Type = StrainType.Shear; // Default
return result;
}
Vector3 relVelocity = v2 - v1;
float relVelMag = relVelocity.Length();
// 2. Calculate Components
// Normal component: How much they are moving parallel to the edge (Pulling apart/Pushing together)
// We project relVelocity onto edge vector
float dot = relVelocity.Dot(edge);
result.NormalRate = dot / edgeLength;
// Shear component: How much they are moving perpendicular to the edge (Sliding)
// Formula: v_tangent = sqrt(|v|^2 - (v_normal)^2)
// Note: We use the magnitude of the relVelocity for the subtraction to avoid float errors
float normalRateSq = result.NormalRate * result.NormalRate;
float shearRateSq = relVelMag * relVelMag - normalRateSq;
result.ShearRate = (shearRateSq > 0) ? (float)Math.Sqrt(shearRateSq) : 0;
// 3. Determine Magnitude
// Total strain = sqrt(normal^2 + shear^2)
result.Magnitude = (float)Math.Sqrt(normalRateSq + shearRateSq);
// 4. Classification Logic
// Compare the absolute values to see which force is "dominant"
float absNormal = Math.Abs(result.NormalRate);
float absShear = Math.Abs(result.ShearRate);
if (absNormal > absShear)
{
// Dominant force is edge-wise
result.Type = result.NormalRate > 0
? StrainType.Tension // Moving apart
: StrainType.Compression; // Moving together
}
else
{
// Dominant force is lateral (sliding)
result.Type = StrainType.Shear;
}
return result;
}
public static StrainAnalysis AverageStrainList(List<StrainAnalysis> strains)
{
if (strains == null || strains.Count == 0)
{
return new StrainAnalysis(); // Return default if empty
}
int count = strains.Count;
float sumMagnitude = 0;
float sumNormalRate = 0;
float sumShearRate = 0;
// Counters for the Type
int tensionCount = 0;
int compressionCount = 0;
int shearCount = 0;
foreach (var s in strains)
{
sumMagnitude += s.Magnitude;
sumNormalRate += s.NormalRate;
sumShearRate += s.ShearRate;
// Count occurrences of each type
switch (s.Type)
{
case StrainType.Tension:
tensionCount++;
break;
case StrainType.Compression:
compressionCount++;
break;
case StrainType.Shear:
shearCount++;
break;
}
}
// Calculate numerical averages
float avgMagnitude = sumMagnitude / count;
float avgNormalRate = sumNormalRate / count;
float avgShearRate = sumShearRate / count;
// Determine the most common StrainType (Mode)
StrainType averageType = StrainType.Shear; // Default
int maxCount = 0;
if (tensionCount > maxCount) { maxCount = tensionCount; averageType = StrainType.Tension; }
if (compressionCount > maxCount) { maxCount = compressionCount; averageType = StrainType.Compression; }
if (shearCount > maxCount) { maxCount = shearCount; averageType = StrainType.Shear; }
return new StrainAnalysis
{
Magnitude = avgMagnitude,
Type = averageType,
NormalRate = avgNormalRate,
ShearRate = avgShearRate
};
}
public Vector3 GetVertexPosition(int vertexId)
{
return mdt.GetVertex(vertexId);
}
public void ColorVertex(int vertexId, Color color)
{
mdt.SetVertexColor(vertexId, color);
}
public int GetRandomVertexId()
{
return Random.Shared.Next(0, Vertices.Count);
}
public Color GetInitialColor(bool isLand)
{
var color = isLand ? new Color(
0.2f,
1f,
0.2f
) : new Color(
0.2f,
0.2f,
1f
);
color.ToHsv(out float h, out float s, out float v);
h += RandF(-0.05f, 0.05f);
s += RandF(-0.2f, 0.2f);
v += RandF(-0.3f, 0.3f);
color = Color.FromHsv(h, s, v);
return color;
}
public void ChangeStage(GenerationStage stage)
{
if (stage != Stage)
GD.Print($"'{Stage.ToString()}' took '{_generationStopwatch.Elapsed}'");
Stage = Stage == StopAt ? GenerationStage.Completed : stage;
if (stage == GenerationStage.Completed)
_generationStopwatch.Stop();
else
_generationStopwatch.Restart();
GD.Print($"Stage Started: '{Stage.ToString()}'");
}
public int GetFreeNeighbourIndex(VertexData vertexData)
{
foreach (int neighbour in vertexData.Neighbours)
{
if (Vertices[neighbour].PlateId == -1)
return neighbour;
}
return -1;
}
public IEnumerable<int> GetNeighboringVertices(int vertexId, bool blackOnly = true)
{
if (Stage != GenerationStage.Initialization)
{
if (blackOnly)
return Vertices[vertexId].Neighbours.Where(n => Vertices[n].PlateId == -1);
return Vertices[vertexId].Neighbours;
}
var verts = mdt.GetVertexEdges(vertexId).AsEnumerable().SelectMany<int, int>(edge => [mdt.GetEdgeVertex(edge, 0), mdt.GetEdgeVertex(edge, 1)]).Distinct().Where(v => v != vertexId);
if (!blackOnly)
return verts.Except([vertexId]);
return verts.Where(v => mdt.GetVertexColor(v) == Colors.Black).Except([vertexId]);
}
public void UpdateMesh()
{
arrayMesh.ClearSurfaces();
mdt.CommitToSurface(arrayMesh);
}
}