Backup #1
Tue May 05 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VoxelData {
public int[,,] data;
float noiseScale;
float threshold;
public VoxelData(float noiseScale, float threshold, Vector3Int meshSize, Vector3Int offset) {
this.noiseScale = noiseScale;
this.threshold = threshold;
data = new int[meshSize.x, meshSize.y, meshSize.z];
for (int x = 0; x < meshSize.x; x++) {
for (int y = 0; y < meshSize.y; y++) {
for (int z = 0; z < meshSize.z; z++) {
if(GetNoise(x + offset.x, y + offset.y, z + offset.z) > threshold) {
data[x, y, z] = 1;
} else {
data[x, y, z] = 0;
}
}
}
}
}
public int getNeighbor(int x, int y, int z, int dir) {
Vector3Int offsetToCheck = meshData.offsetTable[dir];
Vector3Int neighborCoord = new Vector3Int(x + offsetToCheck.x, y + offsetToCheck.y, z + offsetToCheck.z);
if(neighborCoord.x < 0 || neighborCoord.x >= data.GetLength(0) || neighborCoord.y < 0 || neighborCoord.y >= data.GetLength(1) || neighborCoord.z < 0 || neighborCoord.z >= data.GetLength(2)) {
return 0;
} else {
return data[neighborCoord.x, neighborCoord.y, neighborCoord.z];
}
}
float GetNoise(float x, float y, float z) {
float sampleX = x * noiseScale + 785;
float sampleY = y * noiseScale + 785;
float sampleZ = z * noiseScale + 785;
float AB = Mathf.PerlinNoise(sampleX, sampleY);
float BC = Mathf.PerlinNoise(sampleY, sampleZ);
float AC = Mathf.PerlinNoise(sampleX, sampleZ);
float BA = Mathf.PerlinNoise(sampleY, sampleX);
float CB = Mathf.PerlinNoise(sampleZ, sampleY);
float CA = Mathf.PerlinNoise(sampleZ, sampleX);
float ABC = AB + BC + AC + BA + CB + CA;
return ABC / 6f;
}
}
// 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World : MonoBehaviour {
Dictionary<Vector2, GameObject> chunks = new Dictionary<Vector2, GameObject>();
public Vector2 size;
public GameObject chunk;
void Start() {
for (int x = 0; x < size.x; x++) {
for (int y = 0; y < size.y; y++) {
chunks.Add(new Vector2(x, y), GameObject.Instantiate(chunk, new Vector3(x * 16, 0, y * 16), Quaternion.identity));
}
}
}
}
public class ChunkObject {
public GameObject gameObject;
public int[,,] data;
public ChunkObject(GameObject gameObject, int[,,] data) {
this.gameObject = gameObject;
this.data = data;
}
}
// 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider))]
public class Chunk : MonoBehaviour
{
Mesh mesh;
List<Vector3> vertices;
List<int> triangles;
public float cubeScale = 1;
float adjScale;
MeshCollider col;
void Awake() {
mesh = GetComponent<MeshFilter>().mesh;
col = GetComponent<MeshCollider>();
adjScale = cubeScale * 0.5f;
}
void Start() {
vertices = new List<Vector3>();
triangles = new List<int>();
generateMesh(new VoxelData(0.05f, 0.4f, new Vector3Int(16, 32, 16), new Vector3Int((int)transform.position.x, (int)transform.position.y, (int)transform.position.z)));
buildMesh();
}
private void generateMesh(VoxelData data) {
for (int x = 0; x < data.data.GetLength(0); x++) {
for (int y = 0; y < data.data.GetLength(1); y++) {
for (int z = 0; z < data.data.GetLength(2); z++) {
if(data.data[x, y, z] == 1) {
addVoxel(adjScale, new Vector3(x * cubeScale, y * cubeScale, z * cubeScale), x, y, z, data);
}
}
}
}
}
void addVoxel(float cubeScale, Vector3 cubePos, int x, int y, int z, VoxelData data) {
for (int i = 0; i < 6; i++) {
if(data.getNeighbor(x, y, z, i) == 0) {
addFace(i, cubeScale, cubePos);
}
}
}
void addFace(int dir, float scale, Vector3 pos) {
vertices.AddRange(meshData.getVertices(dir, scale, pos));
int vCount = vertices.Count;
triangles.Add(vCount - 4);
triangles.Add(vCount - 3);
triangles.Add(vCount - 2);
triangles.Add(vCount - 4);
triangles.Add(vCount - 2);
triangles.Add(vCount - 1);
}
void buildMesh() {
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
col.sharedMesh = mesh;
mesh.RecalculateNormals();
}
}