-
-
Save twobob/41c964485179e599b5428315e5a24876 to your computer and use it in GitHub Desktop.
Revisions
-
xshazwar revised this gist
May 31, 2021 . 1 changed file with 34 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,66 +14,74 @@ [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, 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) ) * 2 * Mathf.PI / period ) + 0.5f; } 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++) { x = i % res; z = (i - x) / res; output[i] = Sine( (x * stepRatio) + x_off, (z * stepRatio) + z_off, skew_x, skew_z, period ); } } ); 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); 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 + offset.x, dst.worldPos.z + offset.z, stepRatio, period, skew.x, skew.z ); if (stop != null && stop.stop) return; data.StoreProduct(this, dst); } } -
xshazwar revised this gist
May 30, 2021 . 1 changed file with 20 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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[] 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)), (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 ); }} ); 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 = ValuesParallel( (int) dst.rect.size.x, dst.worldPos.x, dst.worldPos.z, -
xshazwar created this gist
May 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } }