internal Vector3[] generateTreePoints(int chunkX, int chunkY, int precision, float heightMultiplier, Vector3[] vertices) { // use poisson disk sampling to generate clustered points // multiply by vertex percision int mapSize = ((int)Mathf.Sqrt(vertices.Length) - 1); // depending on the precision of the vertices, we'll need to adjust the mapsize to be 1:1 with the precision List <Vector2> poissonPoints = PoissonDiscSampling.GeneratePoints(propRadius, new Vector2(mapSize, mapSize), 30); List <Vector3> trees = new List <Vector3>(); for (var i = 0; i < poissonPoints.Count; i++) { Vector2 current = poissonPoints[i]; int verticesIndex = (int)((mapSize + 1) * (int)current.x) + (int)current.y; float height = vertices[verticesIndex].y; // the heights aren't the heights from the heightmap, but the mesh. These // need to be scaled back down to the noisemap values to get the correct // data float normalizedHeight = Mathf.Lerp(0f, 1f, height / heightMultiplier); if (normalizedHeight >= minPropHeightThreshold && normalizedHeight <= maxPropHeightThreshold) { // position must scale with the precision trees.Add(new Vector3(current.y * precision, height, current.x * precision)); } } return(trees.ToArray()); }
public void Initiate() { heights = new List <float>(); polygon = new Polygon(); if (randomPoints == true) { for (int i = 0; i < pointDensity; i++) { var x = Random.Range(.0f, sizeX); var y = Random.Range(.0f, sizeY); polygon.Add(new Vertex(x, y)); } } else { poissonPoints = PoissonDiscSampling.GeneratePoints(minDistancePerPoint, new Vector2(sizeX, sizeY), rejectionSamples); for (int i = 0; i < poissonPoints.Count; i++) { polygon.Add(new Vertex(poissonPoints[i].x, poissonPoints[i].y)); } } ConstraintOptions constraints = new ConstraintOptions(); constraints.ConformingDelaunay = true; mesh = polygon.Triangulate(constraints) as TriangleNet.Mesh; ShapeTerrain(); GenerateMesh(); }
private void GenerateCacti() { List <Vector2> points = PoissonDiscSampling.GeneratePoints(cactusRadius, treeArea, 50); for (int i = 0; i < points.Count; i++) { if (Random.value < cactusProbability) { points[i] = points[i] - treeArea * 0.5f; Vector3 position = new Vector3(points[i].x, 0, points[i].y); RaycastHit hit; if (!Physics.Raycast(position + Vector3.up * 20, Vector3.down, out hit, 30, groundLayer)) { return; } position.y = hit.point.y; //Debug.Log(hit.point); GameObject go = Instantiate(cactusPrefab, position, Quaternion.identity); go.transform.localScale = go.transform.localScale * Random.Range(0.5f, 1.5f); } } }
private void GenerateTrees() { List <Vector2> points = PoissonDiscSampling.GeneratePoints(treeRadius, treeArea, 10); trees = new List <GameObject>(); for (int i = 0; i < points.Count; i++) { if (Random.value < treeProbability) { points[i] = points[i] - treeArea * 0.5f; Vector3 position = new Vector3(points[i].x, 0, points[i].y); RaycastHit hit; if (!Physics.Raycast(position + Vector3.up * 20, Vector3.down, out hit, 30, groundLayer)) { return; } position.y = hit.point.y; //Debug.Log(hit.point); trees.Add(Instantiate(treePrefab, position, Quaternion.identity)); } } }
private void Awake() { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); if (points != null) { int index = 0; foreach (Vector2 point in points) { // var randMine = Random.value; if (index % 100 == 1) { if (minePrefab != null) { Instantiate(minePrefab, point, Quaternion.identity, transform.GetChild(1).transform); } } else { int rand = Random.Range(0, objectTypes.Length); GameObject elem = Instantiate(objectTypes[rand], point, Quaternion.identity, transform.GetChild(0).transform); elemList.Add(elem); } index++; } LenghtElemList = elemList.Count; } }
void OnValidate() { if (IsUsingStaticRadius) { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); } else { pointsPlusRadius = PoissonDiscSampling.GeneratePointsOfDifferentSize(RadiusRange, regionSize, rejectionSamples); } }
public void GeneratePatches() { List <Vector2> points = PoissonDiscSampling.GeneratePoints(bushRadius, treeArea, 10); for (int j = 0; j < points.Count; j++) { if (Random.value < patchProbability) { Instantiate(patchPrefab, new Vector3(points[j].x, 0, points[j].y) - new Vector3(treeArea.x * 0.5f, 0, treeArea.y * 0.5f), Quaternion.identity); } } }
public void UpdatePlatforms() { for (int i = 0; i < transform.childCount; i++) { DestroyImmediate(transform.GetChild(i).gameObject); } List <Vector2> positions = PoissonDiscSampling.GeneratePoints(minDistanceBetweenPoints, sampleRegionSize); spawnPlatforms(positions.Count); for (int i = 0; i < transform.childCount; i++) { transform.GetChild(i).position = new Vector3(positions[i].x - sampleRegionSize.x / 2 + MinimapCamera.transform.position.x, transform.position.y, positions[i].y - sampleRegionSize.y / 2 + MinimapCamera.transform.position.z); } }
public List <Vector2> OnCreateNewTreesForChunk(TerrainChunk chunk) { if (chunk.hasCreatedTrees) { return(null); } float newSizeX = chunk.bounds.size.x / regionSize.x; float newSizeY = chunk.bounds.size.y / regionSize.y; points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); chunk.hasCreatedTrees = true; return(points); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); PoissonDiscSampling poissonDisc = target as PoissonDiscSampling; if (GUILayout.Button("GeneratePoints")) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Restart(); PoissonDiscSampling.circles.Clear(); poissonDisc.GeneratePoints(); Debug.Log(stopwatch.ElapsedMilliseconds.ToString()); } if (GUILayout.Button("Clear")) { PoissonDiscSampling.circles.Clear(); } }
void GenerateNatureSpawn() { Vector2 regionSize = new Vector2((float)size * screenScale, (float)size * screenScale); if (grassUpdate) { List <Vector2> grassPoints = PoissonDiscSampling.GeneratePoints(grassItemRadius, regionSize, grassItemSampleAttempts); List <Vector3> grassMap = Converter(grassPoints); List <Vector3> finalGrassPoints = ObjectHeightAdjuster(grassMap); GrassPlacer(finalGrassPoints); } if (flowerUpdate) { List <Vector2> flowerPoints = PoissonDiscSampling.GeneratePoints(flowerItemRadius, regionSize, flowerItemSampleAttempts); List <Vector3> flowerMap = Converter(flowerPoints); List <Vector3> finalFlowerPoints = ObjectHeightAdjuster(flowerMap); FlowerPlacer(finalFlowerPoints); } if (bushUpdate) { List <Vector2> bushPoints = PoissonDiscSampling.GeneratePoints(bushItemRadius, regionSize, bushItemSampleAttempts); List <Vector3> bushMap = Converter(bushPoints); List <Vector3> finalBushPoints = ObjectHeightAdjuster(bushMap); BushPlacer(finalBushPoints); } if (rockUpdate) { List <Vector2> rockPoints = PoissonDiscSampling.GeneratePoints(rockItemRadius, regionSize, rockItemSampleAttempts); List <Vector3> rockMap = Converter(rockPoints); List <Vector3> finalRockPoints = ObjectHeightAdjuster(rockMap); RockPlacer(finalRockPoints); } if (treeUpdate) { List <Vector2> treePoints = PoissonDiscSampling.GeneratePoints(treeItemRadius, regionSize, treeItemSampleAttempts); List <Vector3> treeMap = Converter(treePoints); List <Vector3> finalTreePoints = ObjectHeightAdjuster(treeMap); TreePlacer(finalTreePoints); } }
public void PoolActors() { points = PoissonDiscSampling.GeneratePoints(actorRadius, regionSize, rejectionSamples); int pointsCounter = 1; int shapeTypes = System.Enum.GetNames(typeof(Shape)).Length; int playerType = Random.Range(0, shapeTypes); GameObject playerPiece = Instantiate(piecePrefab); ShapePieces shapePiece = playerPiece.GetComponent <ShapePieces>(); shapePiece.Instantiate((Shape)playerType, points[0]); shapePiece.AllowControl = true; pieces.Add(shapePiece); GameObject homeToSpawn = Instantiate(homePrefab); homeToSpawn.GetComponent <HomePieces>().Instantiate((Shape)playerType, points[pointsCounter]); homes.Add(homeToSpawn); pointsCounter++; for (int i = 0; i < shapeTypes; i++) { GameObject pieceToSpawn = Instantiate(piecePrefab); shapePiece = pieceToSpawn.GetComponent <ShapePieces>(); shapePiece.Instantiate((Shape)i, points[pointsCounter]); shapePiece.transform.rotation = Quaternion.LookRotation(Vector3.forward, Random.insideUnitCircle); pointsCounter++; pieces.Add(shapePiece); int homesToSpawn = Random.Range(minHomesPerType, maxHomesPerType); for (int j = 0; j < homesToSpawn; j++) { homeToSpawn = Instantiate(homePrefab); homeToSpawn.GetComponent <HomePieces>().Instantiate((Shape)i, points[pointsCounter]); homeToSpawn.transform.rotation = Quaternion.LookRotation(Vector3.forward, Random.insideUnitCircle); homes.Add(homeToSpawn); pointsCounter++; } } }
public Dictionary <Vector2Int, Chunk> GenerateChunks(Vector2Int position, float[] pointRadiuses, int rejectionSamples) { for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++) { for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++) { int coordPairingNumber = MathUtils.SignedCantorPair(position.x + xOffset, position.y + yOffset); System.Random prng = new System.Random(seed + coordPairingNumber); Vector2Int viewedChunkCoord = new Vector2Int(position.x + xOffset, position.y + yOffset); Vector2Int chunkOffset = new Vector2Int((xOffset + position.x) * regionSize.x, (yOffset + position.y) * regionSize.y); if (!terrainChunkDictionary.ContainsKey(viewedChunkCoord)) { List <Vector3> points = PoissonDiscSampling.GeneratePoints(pointRadiuses, regionSize, prng, rejectionSamples); terrainChunkDictionary.Add(viewedChunkCoord, new Chunk(points, chunkOffset, coordPairingNumber)); } } } return(terrainChunkDictionary); }
public void SpawnToothOnPlate(int samplesAmount) { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples, samplesAmount); int foreachIndex = 0; for (int i = 0; i < toothesPool.Length; i++) { toothesPool[i].SetActive(false); } if (points != null) { foreach (Vector2 point in points) { toothesPool[foreachIndex].transform.localPosition = new Vector3(doctorsPlate.transform.localPosition.x + point.x * 10, doctorsPlate.transform.localPosition.y + point.y * 10, -0.054f); toothesPool[foreachIndex].SetActive(true); foreachIndex++; } } }
// Start is called before the first frame update void Start() { shapeCreator = GetComponent <ShapeCreator>(); foreach (Shape shape in shapeCreator.shapes) { top = -Mathf.Infinity; left = -Mathf.Infinity; bottom = Mathf.Infinity; right = Mathf.Infinity; // find the edges of the polygon foreach (Vector3 v in shape.points) { if (v.z > top) { top = v.z; } if (v.z < bottom) { bottom = v.z; } if (v.x > left) { left = v.x; } if (v.x < right) { right = v.x; } } points = PoissonDiscSampling.GeneratePoints(radius, new Vector2(left, top), new Vector2(right, bottom), shape, rejectionSamples); foreach (Vector2 point in points) { GameObject go = Instantiate(tree, new Vector3(point.x, 10f, point.y), Quaternion.identity); go.transform.parent = transform.parent; } } }
public void Initiate() { polygon = new Polygon(); for (int i = 0; i < pointDensity; i++) { var x = Random.Range(.0f, sizeX); var y = Random.Range(.0f, sizeY); polygon.Add(new Vertex(x, y)); } ConstraintOptions constraints = new ConstraintOptions(); constraints.ConformingDelaunay = true; mesh = polygon.Triangulate() as TriangleNet.Mesh; points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); Debug.Log(points.Count); ShapeTerrain(); GenerateMesh(); }
private void OnValidate() { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, Vector2.zero, rejectionSamples, 0); }
private void RegeneratePoints() { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); }
void OnValidate() { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples); }
public static List <Vector3> GenerateResourcePoints(float radius, Vector3[] vertices, GameObject gameObject, Vector3 gameObjectLoc, HeightMapSettings heightMapSettings) { Vector3 topLeftVertex = vertices[0]; Vector3 bottomRightVertex = vertices[vertices.Length - 1]; int chunkSizeX = (int)Mathf.Abs(topLeftVertex.x - bottomRightVertex.x); int chunkSizeZ = (int)Mathf.Abs(topLeftVertex.z - bottomRightVertex.z); Vector2 sampleSize = new Vector2(chunkSizeX, chunkSizeZ); Vector2 sampleCenter = new Vector2(bottomRightVertex.x - gameObjectLoc.x, topLeftVertex.z - gameObjectLoc.z); List <Vector2> v2points = PoissonDiscSampling.GeneratePoints(radius, sampleSize, sampleCenter, 2, heightMapSettings.noiseSettings.seed); // region NEW int verticesPerSide = (int)Mathf.Sqrt(vertices.Length); VertexInfo[,] vertexInfos = new VertexInfo[verticesPerSide, verticesPerSide]; for (int y = 0; y < verticesPerSide; y++) { for (int x = 0; x < verticesPerSide; x++) { int vertexIndex = (y * verticesPerSide) + x; Vector3 center, up, left, right, down; center = vertices[vertexIndex]; if (y == 0) { up = center; } else { up = vertices[vertexIndex - verticesPerSide]; } if (y == verticesPerSide - 1) { down = center; } else { down = vertices[vertexIndex + verticesPerSide]; } if (x == 0) { left = center; } else { left = vertices[vertexIndex - 1]; } if (x == verticesPerSide - 1) { right = center; } else { right = vertices[vertexIndex + 1]; } vertexInfos[x, y] = new VertexInfo(center, up, left, right, down); } } List <Vector3> v3points = new List <Vector3>(); v2points.ForEach(vector2 => { //Debug.Log(topLeftVertex + "->" + bottomRightVertex + " and " + vector2); float xPercentInChunk = Mathf.InverseLerp(topLeftVertex.x, bottomRightVertex.x, vector2.x - gameObjectLoc.x); float yPercentInChunk = Mathf.InverseLerp(topLeftVertex.z, bottomRightVertex.z, vector2.y - gameObjectLoc.z); //Debug.Log("xPercent: " + xPercentInChunk + " | " + "yPercent: " + yPercentInChunk); int xIndex = (int)Mathf.Lerp(0, verticesPerSide, xPercentInChunk); int yIndex = (int)Mathf.Lerp(0, verticesPerSide, yPercentInChunk); // Check slope FIRST //Debug.Log("xIndex: " + xIndex + " | " + "yIndex: " + yIndex); float slope = vertexInfos[xIndex, yIndex].GetLargestSlope(); if (slope < .1 || slope > .35) { return; } float height = vertexInfos[xIndex, yIndex].centerPoint.y + 2.75f; v3points.Add(new Vector3(vector2.x, height, vector2.y)); }); // endregion NEW return(v3points); }
void OnValidate() { // GeneratePoints(float[] minRadii, float[] radii, Vector2 sampleRegionSize) points = PoissonDiscSampling.GeneratePoints(minRadii, radius, regionSize); }
void Awake() { Points = PoissonDiscSampling.GeneratePoints(radiusSize, regionSize - new Vector2(-1, -1), rejectionSamples); //árvores minérios, lagos e maçãs, com chances diferentes para cada Points2 = PoissonDiscSampling.GeneratePoints(radiusSize2, regionSize, rejectionSamples); //sticks e rocks EnemiesPoints = PoissonDiscSampling.GeneratePoints(radiusSizeEnemies, regionSize, rejectionSamples); //inimigos if (Points != null) { foreach (Vector2 point in Points) { if (point.x >= 1 && point.x <= 99 && point.y >= 1 && point.y <= 99 && SpawnArea(Colliders, point)) { //para não ficarem tão próximos à muralha float chance = Random.value; if (chance <= TreeChance) { Instantiate(TreePrefab, point, transform.rotation); } else { if (chance <= TreeChance + IronVeinChance) { Instantiate(IronVeinPrefab, point, transform.rotation); } else { if (chance <= TreeChance + IronVeinChance + GoldVeinChance) { Instantiate(GoldVeinPrefab, point, transform.rotation); } else { if (!SpawnArea(ColliderLakes, point)) { Instantiate(LakePrefabs[Random.Range(0, 3)], point, transform.rotation); } else { Instantiate(ApplePrefab, point, transform.rotation); } } } } } } } if (Points2 != null) { foreach (Vector2 point in Points2) { if (point.x >= 1 && point.x <= 99 && point.y >= 1 && point.y <= 99 && SpawnArea(Colliders, point)) { if (Random.value < RockChance) { Instantiate(RockPrefab, point, transform.rotation); } else { Instantiate(StickPrefab, point, transform.rotation); } } } } if (EnemiesPoints != null) { foreach (Vector2 point in EnemiesPoints) { if (point.x >= 1 && point.x <= 99 && point.y >= 1 && point.y <= 99) { if (SpawnArea(SafeZone, point)) { //área segura, proximo ao spawn do player Instantiate(EnemyPrefab, point, transform.rotation); } } } } }
public void GeneratePoints() { points = PoissonDiscSampling.GeneratePoints(radius, regionSize, rejectionSamples, sampleCount); }