/// <summary> /// Creates a ValueMap instance /// </summary> public static ValueMap CreateInstance(int width, int height) { ValueMap map = CreateInstance <ValueMap>(); map.Initialize(width, height); return(map); }
/// <summary> /// Selects certain values from a map, and sets them to 1, and the rest to zero. Usefull for conversions /// </summary> public ValueMap Select(SelectOperation operation, float value) { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { switch (operation) { case SelectOperation.EqualTo: output[x, y] = values[x, y] == value ? 1 : 0; break; case SelectOperation.GreaterThan: output[x, y] = values[x, y] > value ? 1 : 0; break; case SelectOperation.LessThan: output[x, y] = values[x, y] < value ? 1 : 0; break; } } } return(output); }
/// <summary> /// Creates a ValueMap instance from a gradient /// </summary> public static ValueMap CreateInstance(int width, int height, Gradient gradient, bool vertical = false) { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(width, height, gradient, vertical); return(output); }
/// <summary> /// Maps all values to a range /// </summary> public ValueMap MapToRange(float min, float max) { float cmin = float.MaxValue; float cmax = float.MinValue; ValueMap output = CreateInstance <ValueMap>(); output.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (values[x, y] < cmin) { cmin = values[x, y]; } else if (values[x, y] > cmax) { cmax = values[x, y]; } } } for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { output[x, y] = Mathf.Lerp(min, max, Mathf.InverseLerp(cmin, cmax, values[x, y])); } } return(output); }
/// <summary> /// Creates a ValueMap instance from a Texture2D (converted to grayscale) /// </summary> public static ValueMap CreateInstance(Texture2D image) { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(image); return(output); }
/// <summary> /// Creates a ValueMap instance /// </summary> public static ValueMap CreateInstance(Vector2Int size) { ValueMap map = CreateInstance <ValueMap>(); map.Initialize(size.x, size.y); return(map); }
public ValueMap Generate() { map = input.Clone(); for (int i = 0; i < iterations; i++) { buffer.Initialize(size); for (int x = 0; x < size.x; x++) { for (int y = 0; y < size.y; y++) { // Count alive neighbours int neighbours = map.GetNeighbours(new Vector2Int(x, y), searchRadius, true).FindAll(n => map[n] > threshold).Count; // cell is alive if (neighbours > birthRule) { buffer[x, y] = 1; } // cell is dead else if (neighbours < deathRule) { buffer[x, y] = 0; } } } map = buffer.Clone(); } return(map); }
public override object GetValue(NodePort port) { var graph = (Terra2DGraph)this.graph; if (graph.isComputing) { try { var input = GetInputValue("input", this.input); if (input == null) { input = CreateInstance <ValueMap>(); input.Initialize(graph.size); } output = new CaveGenerator(input, threshold, iterations, searchRadius, birthRule, deathRule).Generate(); return(output); } catch (System.Exception e) { graph.isComputing = false; throw e; } } return(null); }
/// <summary> /// Detects and returns edges in a map /// </summary> public ValueMap DetectEdges(float threshold) { ValueMap firstPass = CreateInstance <ValueMap>(); firstPass.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { firstPass[x, y] = 0; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (IsInsideBounds(i, j)) { if (Mathf.Abs(this[x, y] - this[i, j]) > threshold) { firstPass[x, y] = 1; break; } } } } } } ValueMap secondPass = CreateInstance <ValueMap>(); secondPass.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (firstPass.CountItemsInRegion(new Vector2Int(x - 1, y - 1), new Vector2Int(x + 1, y + 1), 1) > 8) { secondPass[x, y] = 0; } else { secondPass[x, y] = firstPass[x, y]; } } } return(secondPass); }
/// <summary> /// Create a clone of this map /// </summary> public ValueMap Clone() { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { output[x, y] = values[x, y]; } } return(output); }
/// <summary> /// Clamps values between a range /// </summary> public ValueMap Clamp(float min, float max) { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { output[x, y] = (this[x, y] < min) ? min : ((this[x, y] > max) ? max : this[x, y]); } } return(output); }
/// <summary> /// Applies am AnimationCurve on all values /// </summary> public ValueMap ApplyCurve(AnimationCurve curve) { ValueMap output = CreateInstance <ValueMap>(); output.Initialize(Width, Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { output[x, y] = curve.Evaluate(this[x, y]); } } return(output); }
public static ValueMap WhiteNoise(int seed, Vector2Int size, float threshold) { System.Random rand = new System.Random(seed); var noise = new FastNoise(seed); ValueMap output = ScriptableObject.CreateInstance <ValueMap>(); output.Initialize(size.x, size.y); for (int x = 0; x < size.x; x++) { for (int y = 0; y < size.y; y++) { output[x, y] = rand.NextDouble() > threshold?noise.GetWhiteNoise(x, y) : 0; } } return(output); }
public static ValueMap FractalNoise(int seed, Vector2Int size, Vector2 offset, FractalNoiseType noiseType, FastNoise.FractalType fractalType, int depth, float frequency = 0.02f) { var noise = new FastNoise(seed); noise.SetFractalType(fractalType); noise.SetFractalOctaves(depth); noise.SetFrequency(frequency); ValueMap output = ScriptableObject.CreateInstance <ValueMap>(); output.Initialize(size.x, size.y); for (int x = 0; x < size.x; x++) { for (int y = 0; y < size.y; y++) { float value = 0; if (noiseType == FractalNoiseType.Cubic) { value = noise.GetCubicFractal(offset.x + x, offset.y + y); } else if (noiseType == FractalNoiseType.Perlin) { value = noise.GetPerlinFractal(offset.x + x, offset.y + y); } else if (noiseType == FractalNoiseType.Value) { value = noise.GetValueFractal(offset.x + x, offset.y + y); } output[x, y] = value; } } return(output); }
public ValueMap Generate(Vector2Int start) { tree = ScriptableObject.CreateInstance <ValueMap>(); tree.Initialize(size.x, size.y); var leaves = SpawnLeaves(); List <Branch> branches = new List <Branch>(); branches.Add(new Branch(start, null)); int step = 0; while (true) { step++; if (step > maxSteps) { break; } List <Vector2> leavesToRemove = new List <Vector2>(); foreach (var leaf in leaves) { Branch closest = null; float closestBranchDistance = float.MaxValue; for (int i = 0; i < branches.Count; i++) { var direction = leaf - branches[i].position; var distance = direction.magnitude; if (distance < maxDistance) { if (distance < closestBranchDistance) { closest = branches[i]; closestBranchDistance = distance; } } if (distance < minDistance) { leavesToRemove.Add(leaf); } } if (closest != null) { var direction = leaf - closest.position; closest.direction += direction.normalized; closest.growCount++; } } foreach (var leaf in leavesToRemove) { leaves.Remove(leaf); } List <Branch> newBranches = new List <Branch>(); foreach (var branch in branches) { if (branch.growCount > 0 && branch.growCount != branch.prevGrowCount) { Vector2 newDirection = branch.direction; newDirection /= branch.growCount; newDirection = newDirection.normalized; newBranches.Add(new Branch(branch.position + (newDirection * stepSize), branch)); ; branch.prevGrowCount = branch.growCount; branch.growCount = 0; branch.ResetDirection(); } } branches.AddRange(newBranches); if (newBranches.Count == 0) { break; } } foreach (var branch in branches) { if (branch.parent != null) { tree.ConnectCells(branch.parent.position, branch.position, 1); } } return(tree); }