public void Voronoi(MatrixWorld matrix, Noise random, StopToken stop = null) { Vector3 matrixPos = matrix.worldPos; Vector3 matrixSize = matrix.worldSize; CoordRect rect = CoordRect.WorldToGridRect(ref matrixPos, ref matrixSize, cellSize); rect.offset -= 1; rect.size += 2; //leaving 1-cell margins matrixPos.x -= cellSize; matrixPos.z -= cellSize; matrixSize.x += cellSize * 2; matrixSize.z += cellSize * 2; PositionMatrix posMatrix = new PositionMatrix(rect, matrixPos, matrixSize); posMatrix.Scatter(uniformity, random); posMatrix = posMatrix.Relaxed(); float relativeIntensity = intensity * (matrix.worldSize.x / cellSize) * 0.05f; Coord min = matrix.rect.Min; Coord max = matrix.rect.Max; for (int x = min.x; x < max.x; x++) { if (stop != null && stop.stop) { return; //checking stop every x line } for (int z = min.z; z < max.z; z++) { //Vector3 worldPos = matrix.PixelToWorld(x,z); Vector2D relativePos = new Vector2D( (float)(x - matrix.rect.offset.x) / (matrix.rect.size.x - 1), (float)(z - matrix.rect.offset.z) / (matrix.rect.size.z - 1)); Vector2D worldPos = new Vector2D( relativePos.x * matrix.worldSize.x + matrix.worldPos.x, relativePos.z * matrix.worldSize.z + matrix.worldPos.z); Vector3 closest; Vector3 secondClosest; float minDist; float secondMinDist; posMatrix.GetTwoClosest((Vector3)worldPos, out closest, out secondClosest, out minDist, out secondMinDist); float val = 0; switch (blendType) { case BlendType.flat: val = closest.y; break; case BlendType.closest: val = minDist / (matrix.worldSize.x * 16); break; //(MapMagic.instance.resolution*16); //TODO: why 16? case BlendType.secondClosest: val = secondMinDist / (matrix.worldSize.x * 16); break; case BlendType.cellular: val = (secondMinDist - minDist) / (matrix.worldSize.x * 16); break; case BlendType.organic: val = (secondMinDist + minDist) / 2 / (matrix.worldSize.x * 16); break; } matrix[x, z] += val * relativeIntensity; } } }
public object Read(CoordRect worldRect, float worldHeight, CoordRect pixelRect) { Vector3 worldPos = worldRect.offset.vector3; Vector3 worldSize = worldRect.size.vector3; CoordRect rect = CoordRect.WorldToGridRect(ref worldPos, ref worldSize, (int)Mathf.Sqrt(count)); Noise random = new Noise(seed, permutationCount: 65536); PositionMatrix posMatrix = new PositionMatrix(rect, worldPos, worldSize); posMatrix.Scatter(uniformity, random); posMatrix = posMatrix.Relaxed(relax); return(posMatrix.ToTransitionsList()); }
public void Init(string jsonMap) { Map = new Map(jsonMap); VisibleEntities = new PositionMatrix(Map.Size); Population = new PopulationMatrix(Map.Size, VisibleEntities); foreach (var player in Players) { while (!VisibleEntities.Add(player, player.Position)) { player.Position = new Position(player.Position, DirectionUtils.RandomDirection()); } } }