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.

Revisions

  1. @xshazwar xshazwar revised this gist May 31, 2021. 1 changed file with 34 additions and 26 deletions.
    60 changes: 34 additions & 26 deletions Sine.cs
    Original file line number Diff line number Diff line change
    @@ -14,66 +14,74 @@
    [GeneratorMenu(
    menu = "Geometric Generators",
    name = "Sine",
    colorType = typeof(MatrixWorld) )]
    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


    #if UNITY_EDITOR
    [UnityEditor.InitializeOnLoadMethod]
    static void EnlistInMenu () => MapMagic.Nodes.GUI.CreateRightClick.generatorTypes.Add(typeof(MyGenerator));
    #endif

    private float Sine(float x, float z){
    #endif

    private float Sine(float x, float z, float skew_x, float skew_z, float period)
    {
    // rectify the wave between 0 -> 1 (halve and offset)
    return 0.5f * Mathf.Sin(
    Mathf.Sqrt(
    (x * x * skew.x) + (z * z * skew.z)
    (x * x * skew_x) + (z * z * skew_z)
    ) * 2 * Mathf.PI / period
    ) + 0.5f;
    }

    private float[] ValuesParallel(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;

    Parallel.ForEach(Partitioner.Create(0, (res * res)),
    private float[] ValuesParallel(int res, float x_off, float z_off, float stepRatio, float period, float skew_x, float skew_z)
    {
    float[] output = new float[res * res];
    Parallel.ForEach(Partitioner.Create(0, (res * res)),
    (range) => {
    int x = 0;
    int z = 0;
    for (int i = range.Item1; i < range.Item2; i++){
    for (int i = range.Item1; i < range.Item2; i++)
    {
    x = i % res;
    z = (i - x) / res;
    output[i] = Sine(
    (x * stepRatio) + x_off,
    (z * stepRatio) + z_off
    );
    }}
    (z * stepRatio) + z_off,
    skew_x,
    skew_z,
    period
    );
    }
    }
    );
    return output;
    }


    public override void Generate (TileData data, StopToken stop)
    public override void Generate(TileData data, StopToken stop)
    {
    if (stop!=null && stop.stop) return;
    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);

    if (stop != null && stop.stop) return;
    // 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 = ValuesParallel(
    (int) dst.rect.size.x,
    dst.worldPos.x,
    dst.worldPos.z,
    stepRatio
    (int)dst.rect.size.x,
    dst.worldPos.x + offset.x,
    dst.worldPos.z + offset.z,
    stepRatio,
    period,
    skew.x,
    skew.z

    );
    if (stop!=null && stop.stop) return;
    if (stop != null && stop.stop) return;
    data.StoreProduct(this, dst);
    }
    }
  2. @xshazwar xshazwar revised this gist May 30, 2021. 1 changed file with 20 additions and 11 deletions.
    31 changes: 20 additions & 11 deletions Sine.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    using System;
    using System.Collections.Concurrent;
    using System.Threading.Tasks;

    using Den.Tools;
    using Den.Tools.GUI;
    using Den.Tools.Matrices;
    @@ -32,23 +35,29 @@ private float Sine(float x, float z){
    ) * 2 * Mathf.PI / period
    ) + 0.5f;
    }
    private float[] Values(int res, float x_offset, float z_offset, float stepRatio){

    private float[] ValuesParallel(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

    Parallel.ForEach(Partitioner.Create(0, (res * res)),
    (range) => {
    int x = 0;
    int z = 0;
    for (int i = range.Item1; i < range.Item2; i++){
    x = i % res;
    z = (i - x) / res;
    output[i] = 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;
    @@ -58,7 +67,7 @@ public override void Generate (TileData data, StopToken stop)
    // 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(
    dst.arr = ValuesParallel(
    (int) dst.rect.size.x,
    dst.worldPos.x,
    dst.worldPos.z,
  3. @xshazwar xshazwar created this gist May 23, 2021.
    70 changes: 70 additions & 0 deletions Sine.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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);
    }
    }