public Topography GetStartingSurfaceCellHeight(WorleySurfaceNoise surfaceCell, int3 worldPosition) { float cellHeight = surfaceCell.distance2Edge; float scale = MaxSurfaceHeight * (cellHeight / 2); float surfaceHeight = scale; // start min surface height float adjSurfaceHeight = scale; if (surfaceHeight < MinSurfaceHeight) { surfaceHeight = MinSurfaceHeight; } if (surfaceHeight > MaxSurfaceHeight) { surfaceHeight = MaxSurfaceHeight; } return(new Topography { surfaceHeight = surfaceHeight, dist2Edge = surfaceCell.distance2Edge, adjSurfaceHeight = adjSurfaceHeight, topopgraphyType = (int)TTUtil.TerrainType(surfaceCell.currentSurfaceCellValue), adjTopographyType = (int)TTUtil.TerrainType(surfaceCell.adjacentSurfaceCellValue), }); }
public void Execute(Entity sectorEntity, int index, ref Sector sector, ref Translation position) { DynamicBuffer <WorleySurfaceNoise> surfaceNoiseBuffer = SurfaceNoiseBufferFrom [sectorEntity]; int requiredSize = (int)math.pow(SectorSize, 2); if (surfaceNoiseBuffer.Length < requiredSize) { surfaceNoiseBuffer.ResizeUninitialized(requiredSize); } float3 sectorPos = position.Value; for (int i = 0; i < surfaceNoiseBuffer.Length; i++) { float3 sCellWPos = Util.Unflatten2D(i, SectorSize) + new float3(sectorPos.x, 0, sectorPos.z); // remove Y WorleySurfaceNoise worleySurfaceNoise = Noise.GetEdgeData(sCellWPos.x, sCellWPos.z); surfaceNoiseBuffer [i] = worleySurfaceNoise; } }
public void Execute(Entity sectorEntity, int index, ref Sector sector, ref Translation sectorWPos) { DynamicBuffer <SurfaceCell> surfaceCellBuffer = SurfaceCellBufferFrom [sectorEntity]; DynamicBuffer <WorleySurfaceNoise> surfaceNoiseBuffer = SurfaceNoiseBufferFrom [sectorEntity]; if (surfaceCellBuffer.Length < surfaceNoiseBuffer.Length) { surfaceCellBuffer.ResizeUninitialized(surfaceNoiseBuffer.Length); } for (int c = 0; c < surfaceNoiseBuffer.Length; c++) { WorleySurfaceNoise worleyNoise = surfaceNoiseBuffer [c]; SurfaceCell cell = new SurfaceCell { surfaceGenValue = worleyNoise.currentSurfaceCellValue, indexInBuffer = worleyNoise.currentCellIndexInMap, position = worleyNoise.currentCellPosition }; surfaceCellBuffer [c] = cell; } SectorEntities.Enqueue(sectorEntity); }
public WorleySurfaceNoise GetEdgeData(float x, float y) { if (perterbAmp > 0) { SingleGradientPerturb(seed, perterbAmp, frequency, ref x, ref y); } x *= frequency; y *= frequency; int xr = FastRound(x); int yr = FastRound(y); float distance0 = 999999; float distance1 = 999999; // Store distance1 index int xc1 = 0, yc1 = 0; // Store distance0 index in case it is assigned to distance1 later int xc0 = 0, yc0 = 0; // All adjacent cell indices and distances NineInts otherX = new NineInts(); NineInts otherY = new NineInts(); NineFloats otherDist = new NineFloats(); for (int i = 0; i < 9; i++) { otherDist [i] = 999999; } int indexCount = 0; float3 currentCellPosition = float3.zero; int2 currentCellIndex = int2.zero; for (int xi = xr - 1; xi <= xr + 1; xi++) { for (int yi = yr - 1; yi <= yr + 1; yi++) { float2 vec = cell_2D [Hash2D(seed, xi, yi) & 255]; float vecX = xi - x + vec.x * cellularJitter; float vecY = yi - y + vec.y * cellularJitter; float cellX = xi + vec.x * cellularJitter; float cellY = yi + vec.y * cellularJitter; // Natural distance function //float newDistance = (math.abs(vecX) + math.abs(vecY)) + (vecX * vecX + vecY * vecY); // Euclidean distance function float newDistance = newDistance = vecX * vecX + vecY * vecY; if (newDistance <= distance1) // Math.Min(distance[i], newDistance) { if (newDistance >= distance0) // Math.Max((newDistance)), distance[i - 1]) { distance1 = newDistance; xc1 = xi; yc1 = yi; } else { distance1 = distance0; xc1 = xc0; yc1 = yc0; } } if (newDistance <= distance0) // Math.Min(distance0, newDistance) { distance0 = newDistance; xc0 = xi; yc0 = yi; currentCellPosition = new float3(cellX, 0, cellY) / frequency; currentCellIndex = new int2(xi, yi); } // Store all adjacent cells otherX [indexCount] = xi; otherY [indexCount] = yi; otherDist [indexCount] = newDistance; indexCount++; } } // Current cell float currentCellValue = To01(ValCoord2D(seed, xc0, yc0)); int currentBiome = TerrainTypeIndex(currentCellValue); // Final closest adjacent cell values float adjacentEdgeDistance = 999999; float adjacentCellValue = 0; // Iterate over all adjacent cells for (int i = 0; i < 9; i++) { // Find closest cell within smoothing radius float dist2Edge = otherDist [i] - distance0; if (dist2Edge < adjacentEdgeDistance) { float otherCellValue = To01(ValCoord2D(seed, otherX [i], otherY [i])); int otherBiome = TerrainTypeIndex(otherCellValue); /// Assign as final value if not current biome if (otherBiome != currentBiome) { adjacentEdgeDistance = dist2Edge; adjacentCellValue = otherCellValue; } } } if (adjacentEdgeDistance == 999999) { adjacentEdgeDistance = 0; } WorleySurfaceNoise cell = new WorleySurfaceNoise { currentSurfaceCellValue = currentCellValue, distance2Edge = adjacentEdgeDistance, adjacentSurfaceCellValue = adjacentCellValue, currentCellPosition = currentCellPosition, currentCellIndexInMap = currentCellIndex }; // Data for use in terrain generation return(cell); }