public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new Color[t.resolution, t.resolution]; } for (int i = 1; i < t.resolution - 1; i++) { for (int j = 1; j < t.resolution - 1; j++) { float zC = t.WIPVertices[i * t.resolution + j].y; float zL = t.WIPVertices[i * t.resolution + j - 1].y; float zT = t.WIPVertices[(i - 1) * t.resolution + j].y; float zR = t.WIPVertices[i * t.resolution + j + 1].y; float zB = t.WIPVertices[(i + 1) * t.resolution + j].y; float slope = (Mathf.Sqrt(Tools.Square(zL - zC) + Tools.Square(zC - zT)) + Mathf.Sqrt(Tools.Square(zC - zR) + Tools.Square(zB - zC))) / 2 * t.resolution / t.size; values[i, j] = slope > slopeThreshold ? Color.red : Color.green; } //Border-filling 1 values[i, 0] = values[i, 1]; values[i, t.resolution - 1] = values[i, t.resolution - 2]; } //Border-filling 2 for (int j = 0; j < t.resolution; j++) { values[0, j] = values[1, j]; values[t.resolution - 1, j] = values[t.resolution - 2, j]; } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new float[t.resolution, t.resolution]; } }
public void SpawnEmptyTerrain() { GameObject terrainClone = Instantiate(BaseTerrain, _nextAttachLocation, Quaternion.identity); terrainClone.transform.SetParent(_transform); BaseTerrain baseTerrainClone = terrainClone.GetComponent <BaseTerrain>(); _nextAttachLocation = baseTerrainClone.GetAttachPosition(); _countToChange++; }
public void SpawnNewTerrain() { if (_countToChange >= NrOfTerrainsForGravityChange) { int gravityIndex = Random.Range(0, 4); switch (gravityIndex) { case 0: _currentGravity = new Vector3(0.0f, -9.81f); Physics.gravity = _currentGravity; GravityArrow.rotation = Quaternion.Euler(0.0f, 0.0f, -90.0f); break; case 1: _currentGravity = new Vector3(0.0f, 9.81f); Physics.gravity = _currentGravity; GravityArrow.rotation = Quaternion.Euler(0.0f, 0.0f, 90.0f); break; case 2: _currentGravity = new Vector3(-9.81f, 0.0f); Physics.gravity = _currentGravity; GravityArrow.rotation = Quaternion.Euler(0.0f, 0.0f, 180.0f); break; case 3: _currentGravity = new Vector3(9.81f, 0.0f); Physics.gravity = _currentGravity; GravityArrow.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); break; default: break; } _countToChange = 0; } GameObject terrainClone = Instantiate(BaseTerrain, _nextAttachLocation, Quaternion.identity); terrainClone.transform.SetParent(_transform); GameObject obstacleClone = Instantiate(Obstacles[Random.Range(0, Obstacles.Count)], _nextAttachLocation, Quaternion.identity); obstacleClone.transform.SetParent(terrainClone.transform); BaseTerrain baseTerrainClone = terrainClone.GetComponent <BaseTerrain>(); _nextAttachLocation = baseTerrainClone.GetAttachPosition(); _countToChange++; }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new float[t.resolution, t.resolution]; } for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = value; } } }
public override void Generate(bool reallocate) { base.Generate(reallocate); BaseTerrain t = gameObject.GetComponentInParent<BaseTerrain>(); if (noise == null) { return; } float[] set = noise.fastNoiseSIMD.GetNoiseSet(0, 0, 0, t.resolution, 1, t.resolution, t.size / t.resolution); for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = strength * set[i + j * t.resolution]; } } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new Color[t.resolution, t.resolution]; } for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = t.WIPVertices[i * t.resolution + j].y > altitudeThreshold ? Color.red : Color.green; } } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new Color[t.resolution, t.resolution]; } for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { float k = (t.WIPVertices[i * t.resolution + j].y - minAltitude) / (maxAltitude - minAltitude); values[i, j] = Color.Lerp(Color.green, Color.red, k); } } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new Color[t.resolution, t.resolution]; } /* * The slope of a triangle is calculated as sqrt(a² + b²) / c, where a, b and c are obtained from the triangle's plane's equation ax+by+cz=d (with z being the vertical axis) * Since d does not intervene in the slope calculation, it can be set arbitrarily so that c is always 1, simplifying calculations. * For each vertex, its "slope" is calculated from the average of the slopes of two triangles. Those triangles are shaped from the center vertex C as well as * its four directly neighbouring vertices T (top), R (right), L (left), and B (bottom). This way, all triangles are used once and only once in the global computation. * Obtaining the planar equation is greatly simplified thanks to the relative x and y coordinates of the vertices being known in advance. * This results in the slope calculation only needing the z (height) coordinate of each vertex. */ for (int i = 1; i < t.resolution - 1; i++) { for (int j = 1; j < t.resolution - 1; j++) { float zC = t.WIPVertices[i * t.resolution + j].y; float zL = t.WIPVertices[i * t.resolution + j - 1].y; float zT = t.WIPVertices[(i - 1) * t.resolution + j].y; float zR = t.WIPVertices[i * t.resolution + j + 1].y; float zB = t.WIPVertices[(i + 1) * t.resolution + j].y; float k = ((Mathf.Sqrt(Tools.Square(zL - zC) + Tools.Square(zC - zT)) + Mathf.Sqrt(Tools.Square(zC - zR) + Tools.Square(zB - zC))) / 2 * t.resolution / t.size - minSlope) / (maxSlope - minSlope); values[i, j] = Color.Lerp(Color.green, Color.red, k); } //Border-filling 1 values[i, 0] = values[i, 1]; values[i, t.resolution - 1] = values[i, t.resolution - 2]; } //Border-filling 2 for (int j = 0; j < t.resolution; j++) { values[0, j] = values[1, j]; values[t.resolution - 1, j] = values[t.resolution - 2, j]; } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new float[t.resolution, t.resolution]; } else { for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = 0; } } } if (baseLayer == null || baseLayer.values == null) { return; } if (radius <= 0) { for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = baseLayer.values[i, j]; } } return; } float squaredSigma = Tools.Square(radius / 3); float spacing = t.size / t.resolution; int drad = Mathf.CeilToInt(radius / t.size * t.resolution); if (drad > 10) { drad = 10; } int kres = 2 * drad + 1; float[,] kernel = new float[kres, kres]; float weightSum = 0; for (int i = 0; i < kres; i++) { for (int j = 0; j < kres; j++) { kernel[i, j] = Tools.Gauss2D(spacing * (i - drad), spacing * (j - drad), squaredSigma); weightSum += kernel[i, j]; } } float newWeightSum = 0; for (int i = 0; i < kres; i++) { for (int j = 0; j < kres; j++) { kernel[i, j] /= weightSum; newWeightSum += kernel[i, j]; } } for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { for (int x = 0; x < kres; x++) { for (int y = 0; y < kres; y++) { values[i, j] += kernel[x, y] * reflectGet(baseLayer.values, i - drad + x, j - drad + y); } } } } }
public override void OnInspectorGUI() { FastNoiseSIMDUnity fastNoiseSIMDUnity = ((FastNoiseSIMDUnity)target); FastNoiseSIMD fastNoiseSIMD = fastNoiseSIMDUnity.fastNoiseSIMD; bool changed = false; changed |= Refresh(ref fastNoiseSIMDUnity.noiseName, EditorGUILayout.TextField("Name", fastNoiseSIMDUnity.noiseName)); changed |= Refresh(ref fastNoiseSIMDUnity.generalSettingsFold, EditorGUILayout.Foldout(fastNoiseSIMDUnity.generalSettingsFold, "General Settings")); if (fastNoiseSIMDUnity.generalSettingsFold) { changed |= Refresh(ref fastNoiseSIMDUnity.noiseType, (FastNoiseSIMD.NoiseType)EditorGUILayout.EnumPopup("Noise Type", fastNoiseSIMDUnity.noiseType)); changed |= Refresh(ref fastNoiseSIMDUnity.seed, EditorGUILayout.IntField("Seed", fastNoiseSIMDUnity.seed)); changed |= Refresh(ref fastNoiseSIMDUnity.scale, EditorGUILayout.FloatField("Global Scale", fastNoiseSIMDUnity.scale)); fastNoiseSIMD.SetNoiseType(fastNoiseSIMDUnity.noiseType); fastNoiseSIMD.SetSeed(fastNoiseSIMDUnity.seed); BaseTerrain t = fastNoiseSIMDUnity.GetComponentInParent <BaseTerrain>(); fastNoiseSIMD.SetFrequency(1.0f / fastNoiseSIMDUnity.scale); Vector3 axisScales = EditorGUILayout.Vector3Field("Axis Scales", fastNoiseSIMDUnity.axisScales); if (axisScales.x == 0) { axisScales.x = 1.0f; } if (axisScales.y == 0) { axisScales.y = 1.0f; } if (axisScales.z == 0) { axisScales.z = 1.0f; } changed |= Refresh(ref fastNoiseSIMDUnity.axisScales, axisScales); fastNoiseSIMD.SetAxisScales(fastNoiseSIMDUnity.axisScales.x, fastNoiseSIMDUnity.axisScales.y, fastNoiseSIMDUnity.axisScales.z); } changed |= Refresh(ref fastNoiseSIMDUnity.fractalSettingsFold, EditorGUILayout.Foldout(fastNoiseSIMDUnity.fractalSettingsFold, "Fractal Settings")); if (fastNoiseSIMDUnity.fractalSettingsFold) { changed |= Refresh(ref fastNoiseSIMDUnity.fractalType, (FastNoiseSIMD.FractalType)EditorGUILayout.EnumPopup("Fractal Type", fastNoiseSIMDUnity.fractalType)); changed |= Refresh(ref fastNoiseSIMDUnity.octaves, EditorGUILayout.IntSlider("Octaves", fastNoiseSIMDUnity.octaves, 2, 9)); changed |= Refresh(ref fastNoiseSIMDUnity.lacunarity, EditorGUILayout.FloatField("Lacunarity", fastNoiseSIMDUnity.lacunarity)); changed |= Refresh(ref fastNoiseSIMDUnity.gain, EditorGUILayout.FloatField("Gain", fastNoiseSIMDUnity.gain)); fastNoiseSIMD.SetFractalType(fastNoiseSIMDUnity.fractalType); fastNoiseSIMD.SetFractalOctaves(fastNoiseSIMDUnity.octaves); fastNoiseSIMD.SetFractalLacunarity(fastNoiseSIMDUnity.lacunarity); fastNoiseSIMD.SetFractalGain(fastNoiseSIMDUnity.gain); } changed |= Refresh(ref fastNoiseSIMDUnity.cellularSettingsFold, EditorGUILayout.Foldout(fastNoiseSIMDUnity.cellularSettingsFold, "Cellular Settings")); if (fastNoiseSIMDUnity.cellularSettingsFold) { changed |= Refresh(ref fastNoiseSIMDUnity.cellularReturnType, (FastNoiseSIMD.CellularReturnType)EditorGUILayout.EnumPopup("Return Type", fastNoiseSIMDUnity.cellularReturnType)); fastNoiseSIMD.SetCellularReturnType(fastNoiseSIMDUnity.cellularReturnType); if (fastNoiseSIMDUnity.cellularReturnType == FastNoiseSIMD.CellularReturnType.NoiseLookup) { changed |= Refresh(ref fastNoiseSIMDUnity.cellularNoiseLookupType, (FastNoiseSIMD.NoiseType)EditorGUILayout.EnumPopup("Noise Lookup Type", fastNoiseSIMDUnity.cellularNoiseLookupType)); changed |= Refresh(ref fastNoiseSIMDUnity.cellularNoiseLookupFrequency, EditorGUILayout.FloatField("Noise Lookup Frequency", fastNoiseSIMDUnity.cellularNoiseLookupFrequency)); fastNoiseSIMD.SetCellularNoiseLookupType(fastNoiseSIMDUnity.cellularNoiseLookupType); fastNoiseSIMD.SetCellularNoiseLookupFrequency(fastNoiseSIMDUnity.cellularNoiseLookupFrequency); } changed |= Refresh(ref fastNoiseSIMDUnity.cellularDistanceFunction, (FastNoiseSIMD.CellularDistanceFunction)EditorGUILayout.EnumPopup("Distance Function", fastNoiseSIMDUnity.cellularDistanceFunction)); fastNoiseSIMD.SetCellularDistanceFunction(fastNoiseSIMDUnity.cellularDistanceFunction); changed |= Refresh(ref fastNoiseSIMDUnity.cellularDistanceIndex0, EditorGUILayout.IntSlider("Distance2 Index 0", Mathf.Min(fastNoiseSIMDUnity.cellularDistanceIndex0, fastNoiseSIMDUnity.cellularDistanceIndex1 - 1), 0, 2)); changed |= Refresh(ref fastNoiseSIMDUnity.cellularDistanceIndex1, EditorGUILayout.IntSlider("Distance2 Index 1", fastNoiseSIMDUnity.cellularDistanceIndex1, 1, 3)); fastNoiseSIMD.SetCellularDistance2Indicies(fastNoiseSIMDUnity.cellularDistanceIndex0, fastNoiseSIMDUnity.cellularDistanceIndex1); changed |= Refresh(ref fastNoiseSIMDUnity.cellularJitter, EditorGUILayout.Slider("Cell Jitter", fastNoiseSIMDUnity.cellularJitter, 0f, 1f)); fastNoiseSIMD.SetCellularJitter(fastNoiseSIMDUnity.cellularJitter); } changed |= Refresh(ref fastNoiseSIMDUnity.perturbSettingsFold, EditorGUILayout.Foldout(fastNoiseSIMDUnity.perturbSettingsFold, "Perturb Settings")); if (fastNoiseSIMDUnity.perturbSettingsFold) { changed |= Refresh(ref fastNoiseSIMDUnity.perturbType, (FastNoiseSIMD.PerturbType)EditorGUILayout.EnumPopup("Perturb Type", fastNoiseSIMDUnity.perturbType)); changed |= Refresh(ref fastNoiseSIMDUnity.perturbAmp, EditorGUILayout.FloatField("Amp", fastNoiseSIMDUnity.perturbAmp)); changed |= Refresh(ref fastNoiseSIMDUnity.perturbFrequency, EditorGUILayout.FloatField("Frequency", fastNoiseSIMDUnity.perturbFrequency)); fastNoiseSIMD.SetPerturbType(fastNoiseSIMDUnity.perturbType); fastNoiseSIMD.SetPerturbAmp(fastNoiseSIMDUnity.perturbAmp); fastNoiseSIMD.SetPerturbFrequency(fastNoiseSIMDUnity.perturbFrequency); if (fastNoiseSIMDUnity.perturbType == FastNoiseSIMD.PerturbType.GradientFractal || fastNoiseSIMDUnity.perturbType == FastNoiseSIMD.PerturbType.GradientFractal_Normalise) { changed |= Refresh(ref fastNoiseSIMDUnity.perturbOctaves, EditorGUILayout.IntSlider("Fractal Octaves", fastNoiseSIMDUnity.perturbOctaves, 2, 9)); changed |= Refresh(ref fastNoiseSIMDUnity.perturbLacunarity, EditorGUILayout.FloatField("Fractal Lacunarity", fastNoiseSIMDUnity.perturbLacunarity)); changed |= Refresh(ref fastNoiseSIMDUnity.perturbGain, EditorGUILayout.FloatField("Fractal Gain", fastNoiseSIMDUnity.perturbGain)); fastNoiseSIMD.SetPerturbFractalOctaves(fastNoiseSIMDUnity.perturbOctaves); fastNoiseSIMD.SetPerturbFractalLacunarity(fastNoiseSIMDUnity.perturbLacunarity); fastNoiseSIMD.SetPerturbFractalGain(fastNoiseSIMDUnity.perturbGain); } if (fastNoiseSIMDUnity.perturbType == FastNoiseSIMD.PerturbType.Normalise || fastNoiseSIMDUnity.perturbType == FastNoiseSIMD.PerturbType.Gradient_Normalise || fastNoiseSIMDUnity.perturbType == FastNoiseSIMD.PerturbType.GradientFractal_Normalise) { changed |= Refresh(ref fastNoiseSIMDUnity.perturbNormaliseLength, EditorGUILayout.FloatField("Normalise Length", fastNoiseSIMDUnity.perturbNormaliseLength)); fastNoiseSIMD.SetPerturbNormaliseLength(fastNoiseSIMDUnity.perturbNormaliseLength); } } if (GUILayout.Button("Reset")) { fastNoiseSIMD.SetSeed(fastNoiseSIMDUnity.seed = 1337); fastNoiseSIMDUnity.scale = 100f; fastNoiseSIMD.SetFrequency(1.0f / fastNoiseSIMDUnity.scale); fastNoiseSIMD.SetNoiseType(fastNoiseSIMDUnity.noiseType = FastNoiseSIMD.NoiseType.Simplex); fastNoiseSIMD.SetFractalOctaves(fastNoiseSIMDUnity.octaves = 3); fastNoiseSIMD.SetFractalLacunarity(fastNoiseSIMDUnity.lacunarity = 2.0f); fastNoiseSIMD.SetFractalGain(fastNoiseSIMDUnity.gain = 0.5f); fastNoiseSIMD.SetFractalType(fastNoiseSIMDUnity.fractalType = FastNoiseSIMD.FractalType.FBM); fastNoiseSIMD.SetCellularDistanceFunction( fastNoiseSIMDUnity.cellularDistanceFunction = FastNoiseSIMD.CellularDistanceFunction.Euclidean); fastNoiseSIMD.SetCellularReturnType( fastNoiseSIMDUnity.cellularReturnType = FastNoiseSIMD.CellularReturnType.CellValue); fastNoiseSIMD.SetCellularNoiseLookupType(fastNoiseSIMDUnity.cellularNoiseLookupType = FastNoiseSIMD.NoiseType.Simplex); fastNoiseSIMD.SetCellularNoiseLookupFrequency(fastNoiseSIMDUnity.cellularNoiseLookupFrequency = 0.2f); fastNoiseSIMD.SetCellularDistance2Indicies(fastNoiseSIMDUnity.cellularDistanceIndex0 = 0, fastNoiseSIMDUnity.cellularDistanceIndex1 = 1); fastNoiseSIMD.SetCellularJitter(fastNoiseSIMDUnity.cellularJitter = 0.45f); fastNoiseSIMD.SetPerturbType(fastNoiseSIMDUnity.perturbType = FastNoiseSIMD.PerturbType.None); fastNoiseSIMD.SetPerturbAmp(fastNoiseSIMDUnity.perturbAmp = 1.0f); fastNoiseSIMD.SetPerturbFrequency(fastNoiseSIMDUnity.perturbFrequency = 0.5f); fastNoiseSIMD.SetPerturbFractalOctaves(fastNoiseSIMDUnity.perturbOctaves = 3); fastNoiseSIMD.SetPerturbFractalLacunarity(fastNoiseSIMDUnity.perturbLacunarity = 2.0f); fastNoiseSIMD.SetPerturbFractalGain(fastNoiseSIMDUnity.perturbGain = 0.5f); changed = true; } fastNoiseSIMDUnity.modified |= changed; }
public override void Generate(bool reallocate) { base.Generate(reallocate); BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (!reallocate) { for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { elevationValues[i, j] = 0; colorValues[i, j] = Color.clear; } } } Random.InitState(seed); List <Vector2> craterPositions; if (poissonSampling) { craterPositions = FastPoissonDiskSampling.Sampling(Vector2.zero, Vector2.one * t.size, t.size / Mathf.Sqrt(craters)); } else { craterPositions = new List <Vector2>((int)craters); for (int i = 0; i < craters; i++) { craterPositions.Add(new Vector2(Random.value, Random.value) * t.size); } } FastNoiseSIMD shapeNoise = new FastNoiseSIMD(seed); shapeNoise.SetNoiseType(FastNoiseSIMD.NoiseType.Perlin); float[] shapeNoiseSet = shapeNoise.GetNoiseSet(0, 0, 0, t.resolution, 1, t.resolution, t.size / t.resolution / shapeNoiseScale); float[] rcNoiseSet = ridgeColorNoise.fastNoiseSIMD.GetNoiseSet(0, 0, 0, t.resolution, 1, t.resolution, t.size / t.resolution); float[] hcNoiseSet = holeColorNoise.fastNoiseSIMD.GetNoiseSet(0, 0, 0, t.resolution, 1, t.resolution, t.size / t.resolution); foreach (Vector2 craterPos in craterPositions) { float variation = 0; if (enableVariation) { variation = Mathf.Pow(Random.Range(0f, 1f), Mathf.Exp(-variationShapeFactor / 5)) * 2 - 1; } float r = Tools.Variate(radius, variation); float hd = Tools.Variate(holeDepth, variation); float hs = Tools.Variate(holeSteepness, variation); float rh = Tools.Variate(rimHeight, variation); float rs = Tools.Variate(rimSteepness, variation); float sf = Tools.Variate(smoothFactor, variation); float rcv = Tools.Variate(ridgeColorValue, variation); float rca = Tools.Variate(ridgeColorAlpha, variation); float rcspr = Tools.Variate(ridgeColorSpread, variation); float rcdc = Tools.Variate(ridgeColorDecay, variation); float rcnstr = Tools.Variate(ridgeColorNoiseStrength, variation); float hcv = Tools.Variate(holeColorValue, variation); float hca = Tools.Variate(holeColorAlpha, variation); float hcds = Tools.Variate(holeColorDropSteepness, variation); float hcnstr = Tools.Variate(holeColorNoiseStrength, variation); float elevationRadius = r + Mathf.Sqrt(rh / rs); float colorRadius = r + rcspr; float affectedRadius = Mathf.Max(elevationRadius, colorRadius); int minx = Math.Max(0, Mathf.FloorToInt((craterPos.x - affectedRadius) / t.size * t.resolution)); int miny = Math.Max(0, Mathf.FloorToInt((craterPos.y - affectedRadius) / t.size * t.resolution)); int maxx = Math.Min(t.resolution, Mathf.CeilToInt((craterPos.x + affectedRadius) / t.size * t.resolution)); int maxy = Math.Min(t.resolution, Mathf.CeilToInt((craterPos.y + affectedRadius) / t.size * t.resolution)); MouseIndicator mi = gameObject.GetComponentInParent <BaseTerrain>().GetComponentInChildren <MouseIndicator>(); for (int x = minx; x < maxx; x++) { for (int y = miny; y < maxy; y++) { float dist = (new Vector2(x, y) / t.resolution * t.size - craterPos).magnitude + shapeNoiseSet[x + t.resolution * y] * shapeNoiseStrength; // Adapted from S. Lague - https://github.com/SebLague/Solar-System float hole = (Tools.Square(dist / r) - 1) * (hd + rh) * hs + rh; float rimX = Mathf.Min(dist - elevationRadius, 0); float rim = rs * Tools.Square(rimX); float craterShape = Tools.SmoothMax(hole, -hd, sf); craterShape = Tools.SmoothMin(craterShape, rim, sf); elevationValues[x, y] += craterShape; float distToRidge = Mathf.Abs(dist - r); Color ridgeColor = new Color(rcv, rcv, rcv, rca); ridgeColor.a += rcNoiseSet[x + t.resolution * y] * rcnstr; ridgeColor.a *= Mathf.Pow(Mathf.Max(0, 1 - distToRidge / rcspr), rcdc); Color holeColor = new Color(hcv, hcv, hcv, hca); holeColor.a += hcNoiseSet[x + t.resolution * y] * hcnstr; holeColor.a *= Mathf.Max(0, Tools.SmoothMin(hcds * (1 - dist / r), 1, .5f)); colorValues[x, y] = Tools.OverlayColors(colorValues[x, y], holeColor); colorValues[x, y] = Tools.OverlayColors(colorValues[x, y], ridgeColor); } } } }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new float[t.resolution, t.resolution]; } switch (type) { case ModType.Add: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] + layer2.values[i, j]; } } break; case ModType.Substract: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] - layer2.values[i, j]; } } break; case ModType.Multiply: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] * layer2.values[i, j]; } } break; case ModType.Divide: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] / layer2.values[i, j]; } } break; case ModType.Increment: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] + 1; } } break; case ModType.Decrement: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = layer1.values[i, j] - 1; } } break; case ModType.Complement: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = 1 - layer1.values[i, j]; } } break; case ModType.Inverse: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = 1 / layer1.values[i, j]; } } break; case ModType.Absolute: for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = Mathf.Abs(layer1.values[i, j]); } } break; } }
public void GenerateTerrain(int Size, out BaseTerrain[,] Terrain) { Terrain = new BaseTerrain[Size, Size]; _terrain = Terrain; _size = Size; // bool Invalid = false; //BaseTerrain CurrentBlock; //Tuple<BaseTerrain, bool>[] Neighbors = new Tuple<BaseTerrain, bool>[3]; //generate an Island! int StartingX = this.baseRandom.Next(0, _size); int StartingY = this.baseRandom.Next(0, _size); int IslandSize = this.baseRandom.Next(MinIslandSize, MaxIslandSize); while(true) { } for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { //bool AllNull = true; //for(int i = 0; i < 4; i++) //{ // Point CurrentVector = NeighborNormals[i]; // CurrentBlock = GetTile(row + CurrentVector.X, column + CurrentVector.Y, out Invalid); // if(!Invalid) // { // if (AllNull && CurrentBlock != null) // AllNull = false; // Neighbors[i] = new Tuple<BaseTerrain, bool>(CurrentBlock, true); // } // else // { // Neighbors[i] = new Tuple<BaseTerrain, bool>(null, false); // } //} //if(AllNull) //{ //} } } //for (int row = 0; row < TerrainChunk.ChunkSize; row++) //{ // for (int column = 0; column < TerrainChunk.ChunkSize; column++) // { // // what am i next to, we need to generate islands... // // which is kind of hard without working the big picture. // // we might need to have a very basic design or have it change a static map?? // } //} //return new TerrainChunk { X = x, Y = y }; }
public override void Generate(bool reallocate) { BaseTerrain t = gameObject.GetComponentInParent <BaseTerrain>(); if (reallocate || values == null) { values = new float[t.resolution, t.resolution]; } BinaryReader br = null; try { br = new BinaryReader(File.Open(filename, FileMode.Open, FileAccess.Read)); } catch (Exception e) { Debug.LogError("Couldn't load file " + filename + "\n" + e.Message); return; } Debug.Log("Successfully loaded file " + filename); float spacing = t.size / (t.resolution - 1); //Where i and j end up when translated to map coordinates, as floating coordinates (will be used in interpolation) int[] iCoords_floor = new int[t.resolution]; int[] iCoords_ceil = new int[t.resolution]; float[] iCoords_coeff = new float[t.resolution]; int[] jCoords_floor = new int[t.resolution]; int[] jCoords_ceil = new int[t.resolution]; float[] jCoords_coeff = new float[t.resolution]; for (int i = 0; i < t.resolution; i++) { float iCoord = (float)lines / 2 - (centerPosition.y + t.size / 2 - i * spacing) / pixelSize; float jCoord = (float)lineSamples / 2 - (centerPosition.x + t.size / 2 - i * spacing) / pixelSize; iCoords_floor[i] = Mathf.FloorToInt(iCoord); iCoords_ceil[i] = Mathf.CeilToInt(iCoord); iCoords_coeff[i] = iCoord - iCoords_floor[i]; jCoords_floor[i] = Mathf.FloorToInt(jCoord); jCoords_ceil[i] = Mathf.CeilToInt(jCoord); jCoords_coeff[i] = jCoord - jCoords_floor[i]; } float min = float.MaxValue; float max = float.MinValue; for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { values[i, j] = float.MinValue; } } for (int i = 0; i < t.resolution; i++) { if (iCoords_floor[i] < 0 || iCoords_ceil[i] >= lines) { continue; } br.BaseStream.Seek(startOffset + iCoords_floor[i] * lineSamples * 4, SeekOrigin.Begin); byte[] upperLine = br.ReadBytes(lineSamples * 4); byte[] lowerLine = br.ReadBytes(lineSamples * 4); for (int j = 0; j < t.resolution; j++) { if (jCoords_floor[j] < 0 || jCoords_ceil[j] >= lineSamples) { continue; } float topLeft = BitConverter.ToSingle(upperLine, jCoords_floor[j] * 4); float topRight = BitConverter.ToSingle(upperLine, jCoords_ceil[j] * 4); float botLeft = BitConverter.ToSingle(lowerLine, jCoords_floor[j] * 4); float botRight = BitConverter.ToSingle(lowerLine, jCoords_ceil[j] * 4); float x = iCoords_coeff[i]; float y = jCoords_coeff[j]; float f = botLeft * (1 - x) * (1 - y) + botRight * x * (1 - y) + topLeft * (1 - x) * y + topRight * x * y; if (Mathf.Abs(f) < 10000) { values[t.resolution - i - 1, j] = f; min = Mathf.Min(min, f); max = Mathf.Max(max, f); } } } for (int i = 0; i < t.resolution; i++) { for (int j = 0; j < t.resolution; j++) { if (values[i, j] != float.MinValue) { values[i, j] -= (min + max) / 2; } else { values[i, j] = 0; } } } br.Close(); }