private SafeMesh GenerateCaveMap(Vector3 offsets) { int size = WorldBuilder.CHUNK_SIZE + 1; Tile currentTile = WorldBuilder.GetTile(offsets); // Gets added to coordinates, is a decimal to make sure it does not end up at an integer float addendum = 1000.17777f; // Coordinates are divided by scale, larger scale = larger/more spread out caves float scale = 20f; float[,,] caveMap = new float[size, CAVE_DEPTH * 2, size]; for (int x = 0; x < size; x++) { for (int z = 0; z < size; z++) { // -5 to make the amount of rocks sticking out of the terrain lower int coordinateHeight = Mathf.FloorToInt(currentTile.HeightMap[x, z]) - 5; int caveHeight = coordinateHeight + CAVE_DEPTH; // Cave height make height dynamic based on heightmap[x,z] for (int y = 0; y < caveHeight; y++) { if (y == 0) { caveMap[x, y, z] = 1; } else if (y <= 2) { double tempVal = ridgedMultifractal.GetValue((z + offsets.z + addendum) / scale, (y + addendum) / scale, (x + offsets.x + addendum) / scale); int isCave = tempVal < 0 ? 0 : 1; caveMap[x, y, z] = isCave; } else { double tempVal = ridgedMultifractal.GetValue((x + offsets.x + addendum) / scale, (y + addendum) / scale, (z + offsets.z + addendum) / scale); int isCave = tempVal < 0.35 ? 0 : 1; caveMap[x, y, z] = isCave; } } } } SafeMesh safeMesh = MarchingCubes.BuildMesh(caveMap); safeMesh.position = offsets; return(safeMesh); }
public SS_Cloud(int seed, int size, double frequency, double lacunarity, int octaves, Color tint, float brightness) { // Create sprite texture Sprite = new SS_Texture(size, size, Color.white); // Initialize noise with parameters RidgedMultifractal noise = new RidgedMultifractal(frequency, lacunarity, octaves, seed, QualityMode.Medium); // Create cloud for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { float distance = SS_Point.Distance(new SS_Point(x, y), Sprite.Center); float n = (float)noise.GetValue(x, y, 0); Color pixelColor = tint * n * brightness; float blackness = ((pixelColor.r + pixelColor.g + pixelColor.b) / 3.0f); float fade = distance / (size / 2); pixelColor.a = (1f - fade) * blackness; if (distance > (size / 2)) { pixelColor.a = 0f; } Sprite.SetPixel(x, y, pixelColor); } } }
/// <summary> /// Called when the parent sphere builds it's height /// </summary> public override void OnVertexBuildHeight(VertexBuildData data) { Double h = data.vertHeight - sphere.radiusMin; Single t; if (h <= this.simplexHeightStart) { t = 0f; } else if (h < simplexHeightEnd) { t = (Single)((h - simplexHeightStart) * (1 / (simplexHeightEnd - simplexHeightStart))); } else { t = 1f; } Double s = simplex.noiseNormalized(data.directionFromCenter) * simplexCurve.Evaluate(t); if (s == 0f) { return; } Double r = ridgedAdd.GetValue(data.directionFromCenter) - ridgedSub.GetValue(data.directionFromCenter); if (r < -1) { r = -1; } if (r > 1) { r = 1; } data.vertHeight += (r + 1) * 0.5 * deformity * s; }
public SS_Moon(int seed, int size, float frequency, float lacunarity, int octaves, float roughness, Color[] colors, float lightAngle) { Seed = seed; Size = size; Sprite = new SS_Texture(size, size, Color.clear); gradientColors = SS_Utilities.CreateGradient(colors, 16, 32); Perlin shapeNoise = new Perlin(0.01, 2, 0.5, 8, seed, QualityMode.High); RidgedMultifractal noise = new RidgedMultifractal(frequency, lacunarity, octaves, seed, QualityMode.Low); Vector2 lightPosition = new Vector2( Sprite.Center.x + (Mathf.Cos(lightAngle * Mathf.Deg2Rad) * (Size / 4)), Sprite.Center.y + (Mathf.Sin(lightAngle * Mathf.Deg2Rad) * (Size / 4))); for (int y = 0; y < Size; y++) { for (int x = 0; x < Size; x++) { float dist = Vector2.Distance(new Vector2(x, y), new Vector2(Sprite.Center.x, Sprite.Center.y)); float edgeNoise = (float)shapeNoise.GetValue(x, y, 0); edgeNoise = (edgeNoise + 1.0f) * 0.5f; edgeNoise = Mathf.Clamp(edgeNoise, 0f, 1f); edgeNoise *= (8 * roughness); if (dist < (Size / 2) - edgeNoise) { float pixelNoise = (float)noise.GetValue(x, y, 0); pixelNoise = (pixelNoise + 1.0f) * 0.5f; pixelNoise = Mathf.Clamp(pixelNoise, 0f, 1f); float n = pixelNoise * (gradientColors.Length - 1); // Generate color and noise so land doesn't look to smooth Color pixelColor = gradientColors[(int)n]; pixelColor.a = 1.0f; Sprite.SetPixel(x, y, pixelColor); // Shadow float lightDistance = Vector2.Distance(new Vector2(x, y), lightPosition); lightDistance = 1.25f - (lightDistance / (Size / 2)); if (lightDistance < 0.025f) { lightDistance = 0.025f; } pixelColor.r *= lightDistance; pixelColor.g *= lightDistance; pixelColor.b *= lightDistance; Sprite.SetPixel(x, y, pixelColor); } } } }
protected override Vector3 _ModifyOffset(VertexData vertexData) { if (!noiseModule.IsDisposed) { noiseModule.Dispose(); } var value = 0.5f + (float)noiseModule.GetValue(GetSampleCoordinate(vertexData.position)); return(FormatValue(value, vertexData)); }
void ApplyNoiseMap(float scale, AnimationCurve heightCurve, float heightMultiplyer) { foreach (Vertex vertex in vertices) { //vertex.Move (noiseMap [(int)((noiseMap.GetLength (0) - 1) * vertex.U), (int)((noiseMap.GetLength (1) - 1) * vertex.V)]); float perlinValue = (float)noise.GetValue(vertex.Coordinates.normalized * scale) + (float)ridge.GetValue(vertex.Coordinates.normalized * (scale)); perlinValue = heightCurve.Evaluate(perlinValue) * heightMultiplyer; vertex.Move(perlinValue); } }
public virtual void AddSoil(long X, long Z, RidgedMultifractal CavernNoise, Perlin CaveNoise, double[,] hm, ref byte[,,] b, BiomeType[,] biomes, int WaterHeight, int depth, MapGenMaterials mats) { int YH = b.GetLength(1) - 2; double xo = (double)(X * b.GetLength(0)); double zo = (double)(Z * b.GetLength(2)); for (int x = 0; x < b.GetLength(0); x++) { //Console.WriteLine(); for (int z = 0; z < b.GetLength(2); z++) { double hmY = (double)(System.Math.Min(hm[x, z], 1d) * (b.GetLength(1) - 3)); bool HavePloppedGrass = false; bool HaveTouchedSoil = false; // Caves first if (GenerateCaves) { for (int y = 0; y < b.GetLength(1); y++) { // If we're in rock, and CavernNoise value is under a threshold value calculated by height // or CaveNoise value is over threshold value, and the block we're removing won't fall on us... if ( b[x, y, z] == 1 && ( ((CavernNoise.GetValue(x + xo, y, z + zo) / 2) + 1) < Utils.Lerp(CavernThresholdMax, CavernThresholdMin, (((double)y / (hmY + 1)))) || (Utils.FixLibnoiseOutput(CaveNoise.GetValue(x + xo, y, z + zo)) > CaveThreshold) ) && !(b[x, y, z] == 9 || b[x, y, z] == 8 || b[x, y, z] == 12 || b[x, y, z] == 13)) { // Remove it b[x, y, z] = 0; } } } for (int y = (int)b.GetLength(1) - 1; y > 0; y--) { byte supportBlock = b[x, y - 1, z]; // Ensure there's going to be stuff holding us up. if (b[x, y, z] == mats.Rock && supportBlock == mats.Rock) { HaveTouchedSoil = true; if (y + depth >= YH) { continue; } byte ddt = b[x, y + depth, z]; switch (ddt) { case 0: // Air case 8: // Water case 9: // Water BiomeType bt = biomes[x, z]; if (bt == BiomeType.Tundra) { b[x, y, z] = mats.Sand; } else { if (y - depth <= WaterHeight && GenerateWater) { if ((bt == BiomeType.Taiga || bt == BiomeType.TemperateForest || bt == BiomeType.Tundra) && y > WaterHeight) { b[x, y, z] = (HavePloppedGrass) ? mats.Soil : mats.Grass; } else { b[x, y, z] = mats.Sand; } } else { b[x, y, z] = (HavePloppedGrass) ? mats.Soil : mats.Grass; } } if (!HavePloppedGrass) { HavePloppedGrass = true; } break; default: y = 0; break; } } else if (b[x, y, z] == 0 && y <= WaterHeight && !HaveTouchedSoil && GenerateWater) { b[x, y, z] = mats.Water; } } } } }
private void CreateWing(SS_Texture targetTexture, Color baseColor, bool highlights) { // Temporary texture SS_Texture tmpTexture = new SS_Texture(Size, Size, Color.clear); // Wing dimensions int wingLength = 64;// (int)(random.RangeEven(32, 64)); int wingSize = (int)(random.Range(4, 12)); int wingCenterX = wingSize / 2; int wingOffsetX = random.Range((Size / 2) - (BodyLength / 2), (Size / 2) + wingSize); // Data points for body edge List <SS_Point> fPoints = new List <SS_Point>(); List <SS_Point> bPoints = new List <SS_Point>(); // Noise generators RidgedMultifractal fWingNoise = new RidgedMultifractal(WingDetail, 2, 8, Seed, QualityMode.Medium); RidgedMultifractal bWingNoise = new RidgedMultifractal(WingDetail, 2, 8, Seed + 1, QualityMode.Medium); // Determine if wing has a flat edge int fEdgeMod = random.RangeEven(0, 8); int bEdgeMod = random.RangeEven(0, 8); // Start point of wing (this determinds if the wings are separated or joined int startY = 0; if (random.NextBool()) { startY = random.RangeEven(2, 8); } int fEndY = Sprite.Center.y + (wingLength / 2) - fEdgeMod; int bEndY = Sprite.Center.y + (wingLength / 2) - bEdgeMod; // Calculate steps based on length of modified wing length int fStep = (fEndY - Sprite.Center.y) / 4; int bStep = (bEndY - Sprite.Center.y) / 4; // Front Edge for (int y = Sprite.Center.y + startY; y <= fEndY + 1; y += fStep) { // Get some funky noise value for the back of the wing float noise = (float)fWingNoise.GetValue(0, y, 0); noise = (noise + 1.0f) * 0.5f; // Convert to 0 to 1 noise = Mathf.Clamp(noise, 0.05f, 1f); int x = (wingOffsetX + wingCenterX) + (int)(noise * wingSize); if (x > Size - 1) { x = Size - 1; // Clamp to bounds } fPoints.Add(new SS_Point(x, y)); } // Back Edge for (int y = Sprite.Center.y + startY; y <= bEndY + 1; y += bStep) { // Get some funky noise value for the front of the wing float noise = (float)bWingNoise.GetValue(0, y, 0); noise = (noise + 1.0f) * 0.5f; // Convert to 0 to 1 noise = Mathf.Clamp(noise, 0.05f, 1f); int x = (wingOffsetX - wingCenterX) - (int)(noise * wingSize); if (x < 0) { x = 0; // Clamp to bounds } bPoints.Add(new SS_Point(x, y)); } // Smoothing for (int j = 0; j < 2; j++) { for (int i = 0; i < fPoints.Count - 1; i++) { float x = (fPoints[i].x + fPoints[i + 1].x) / 2f; fPoints[i] = new SS_Point((int)x, fPoints[i].y); } for (int i = 0; i < bPoints.Count - 1; i++) { float x = (bPoints[i].x + bPoints[i + 1].x) / 2f; bPoints[i] = new SS_Point((int)x, bPoints[i].y); } } // Build polygon using both sets of points (left and right side) List <SS_Point> points = new List <SS_Point>(); for (int i = 0; i < fPoints.Count; i++) { points.Add(fPoints[i]); } // Add the back edge points backwards to the point list to keep the vertex ordering correct for (int i = bPoints.Count - 1; i >= 0; i--) { points.Add(bPoints[i]); } // Create wing weapons before drawing the actual wing so they appear underneigth CreateWeapon(targetTexture, new SS_Point(wingOffsetX + wingCenterX, (Size / 2) + (startY + (wingLength / 4))), Color.yellow); CreateWeapon(targetTexture, new SS_Point(wingOffsetX + wingCenterX, (Size / 2) - (startY + (wingLength / 4))), Color.yellow); // Draw polygon for the wing SS_Drawing.PolygonFill(tmpTexture, points.ToArray(), SS_StellarSprite.FillColor, SS_StellarSprite.FillColor); // Mirror Vertically for the bottom/right wing int cntr = 1; for (int y = Sprite.Center.y; y < Size; y++) { for (int x = 0; x < Size; x++) { int newY = y - cntr; tmpTexture.SetPixel(x, newY, tmpTexture.GetPixel(x, y)); } cntr += 2; } // Draw the wing(s) outline SS_Drawing.Outline(tmpTexture, SS_StellarSprite.OutlineColor); // Texturize and shade if (!DebugDrawing) { Texturize(tmpTexture, SS_StellarSprite.FillColor, baseColor, highlights, true); SS_StellarSprite.ShadeEdge(tmpTexture); } SS_Drawing.MergeColors(targetTexture, tmpTexture, 0, 0); }