Skip to content

Instantly share code, notes, and snippets.

@twobob
Forked from xshazwar/Sine.cs
Created August 12, 2021 16:17
Show Gist options
  • Save twobob/41c964485179e599b5428315e5a24876 to your computer and use it in GitHub Desktop.
Save twobob/41c964485179e599b5428315e5a24876 to your computer and use it in GitHub Desktop.
MapMagic2 Custom Generator Node
using System;
using Den.Tools;
using Den.Tools.GUI;
using Den.Tools.Matrices;
using MapMagic.Products;
using MapMagic.Nodes;
using UnityEngine;
[Serializable]
[GeneratorMenu(
menu = "Geometric Generators",
name = "Sine",
colorType = typeof(MatrixWorld) )]
public class MyGenerator : Generator, IOutlet<MatrixWorld>
{
[Val("Offset")] public Vector2D offset;
[Val("Skew")] public Vector2D skew;
[Val("Radius")] public int period = 2000;
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
static void EnlistInMenu () => MapMagic.Nodes.GUI.CreateRightClick.generatorTypes.Add(typeof(MyGenerator));
#endif
private float Sine(float x, float z){
// rectify the wave between 0 -> 1 (halve and offset)
return 0.5f * Mathf.Sin(
Mathf.Sqrt(
(x * x * skew.x) + (z * z * skew.z)
) * 2 * Mathf.PI / period
) + 0.5f;
}
private float[] Values(int res, float x_offset, float z_offset, float stepRatio){
float[] output = new float[res * res];
float x_off = x_offset + offset.x;
float z_off = z_offset + offset.z;
int idx = 0;
for (float z = 0f; z < res; z += 1.0f){
for (float x = 0f; x < res; x += 1.0f){
output[idx] = Sine(
(x * stepRatio) + x_off,
(z * stepRatio) + z_off
);
idx += 1;
}
}
return output;
}
public override void Generate (TileData data, StopToken stop)
{
if (stop!=null && stop.stop) return;
MatrixWorld dst = new MatrixWorld(data.area.full.rect, data.area.full.worldPos, data.area.full.worldSize, data.globals.height);
// The draft tiles have a different resolution. To make them continuous with the non-draft,
// we have to scale the step size so that the output Y value is consistently based on the higher
// resolution
float stepRatio = dst.worldSize.x / dst.rect.size.x;
dst.arr = Values(
(int) dst.rect.size.x,
dst.worldPos.x,
dst.worldPos.z,
stepRatio
);
if (stop!=null && stop.stop) return;
data.StoreProduct(this, dst);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment