using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Ex = System.Exception;
public class TerrainGen : MonoBehaviour
{
public int width = 16;
public int height = 50;
public GameObject myPrefab;
private MeshFilter meshFilter;
private static float noiseScale = .05f;
Mesh mesh;
Vector3[] vertices;
int[] triangles;
void Start()
{
GeneratePerlinNoiseMesh(0, 0);
}
public void GeneratePerlinNoiseMesh(int offsetX, int offsetZ) {
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
vertices = new Vector3[(width+1) * (width+1)];
for (int i = 0, x = 0; x <= width; x++) {
for (int y = 0; y <= height; y++) {
for (int z = 0; z <= width; z++) {
if (getBlock(x + offsetX * width, y, z + offsetZ * width) && (!isSurrounded(x + offsetX * width, y, z + offsetZ * width))) {
Debug.Log(y);
GameObject cube = Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);
Debug.Log(i + "" + new Vector3(x, y, (i%(width + 1))));
vertices[i] = new Vector3(x, y, (i%(width + 1)));
i++;
}
}
}
}
triangles = new int[width * width* 6];
int vert = 0;
int tris = 0;
for (int x = 0; x < width; x++) {
for (int z = 0; z < width; z++) {
triangles[tris + 0] = vert + 0;
triangles[tris + 2] = vert + width + 1;
triangles[tris + 1] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 5] = vert + width + 1;
triangles[tris + 4] = vert + width + 2;
vert++;
tris += 6;
}
vert++;
}
UpdateMesh();
}
void UpdateMesh() {
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
public static bool getBlock(int x, int t, int z) {
float density = ((Perlin3D(x * noiseScale, t * noiseScale, z * noiseScale)*2)-1);
float yAverage = (t*2) / 100.0f;
yAverage = (yAverage * 2) - 1;
//Increase yAverage divison for more moutains, decrease for flatter
density = (density + (yAverage/1.2f)) / 2;
if (density >= 0) {
return true;
} else {
return false;
}
}
public static float Perlin3D(float x, float t, float z) {
float ab = Mathf.PerlinNoise(x, t);
float bc = Mathf.PerlinNoise(t, z);
float ac = Mathf.PerlinNoise(x, z);
float ba = Mathf.PerlinNoise(t, x);
float cb = Mathf.PerlinNoise(z, t);
float ca = Mathf.PerlinNoise(z, x);
float abc = ab + bc + ac + ba + cb + ca;
return abc / 6f;
}
bool isSurrounded(int x, int t, int z) {
int[] dx = {-1, 1, 0, 0, 0, 0};
int[] dy = {0, 0, -1, 1, 0, 0};
int[] dz = {0, 0, 0, 0, -1, 1};
for (int i = 0; i < 6; i++) {
if (!getBlock(x + dx[i], t + dy[i], z + dz[i])) {
return false;
}
}
return true;
}
}
In my GeneratePerlinNoiseMesh function I instantiate the cube object using
Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);. After I create a vertex at the same location as the cube using
vertices = new Vector3(x, y, (i%(width + 1)));. If you look at the image Debug.Log(i + “” + new Vector3(x, y, (i%(width + 1)))); outputs y value is 22 for vertex number 18 and Debug.Log(y); is also outputting 22 yet the position y for the cube instantiated at the same X and Z position is 23 using the same y variable as the vertex? Both X and Z are the same for all vertices but the y value is 1 off for some of them like showed in the image… anyone know the problem here? I have tried multiple different strategies to fix it and have spent plenty of time looking at the code with no success.