Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Mathematics;
- using Unity.Collections;
- public class MeshGenerator : MonoBehaviour
- {
- public static Vector3Int chunkDimensions = new Vector3Int(64, 64, 64);
- public Material material;
- public int brushSize;
- public float brushStrength;
- public int LOD;
- public int maxLOD;
- int lod;
- [Header("Noise Settings")]
- public float heightMultiplier;
- public float frequency;
- public float lacunarity;
- public float persistance;
- public int octaves;
- public float density;
- [Header("")]
- public bool autoUpdate;
- public string executionTime;
- float[][] voxelGrid;
- List<Vector3> vertices;
- Dictionary<Vector3, int> vertDictionary;
- List<int> indices;
- int[] caseToNumpolys;
- int[][] triTable;
- public static Vector3Int[] cornerOffsets;
- Camera cam;
- Noise noise;
- public GameObject ChunkObject(Vector2Int position)
- {
- GameObject chunk = new GameObject("Chunk");
- chunk.AddComponent<MeshFilter>().mesh = ChunkMesh(position);
- chunk.AddComponent<MeshRenderer>().material = material;
- return chunk;
- }
- public Mesh ChunkMesh(Vector2Int position)
- {
- //NativeArray<float> noiseMap2D; //= //noise.FractalNoiseMap();
- //NativeArray<float> noiseMap3D; //= new NativeArray<float>()//noise.Fractal3DNoiseMap();
- CreateVoxelGrid();
- for (int z = 0, i = 0; z < chunkDimensions.z; z += lod)
- {
- for (int y = 0; y < chunkDimensions.y; y += lod)
- {
- for (int x = 0; x < chunkDimensions.x; x += lod, i += 3)
- {
- AddVertices(x, y, z);
- }
- }
- }
- //noiseMap2D.Dispose();
- //noiseMap3D.Dispose();
- Mesh mesh = new Mesh();
- mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
- mesh.vertices = vertices.ToArray();
- mesh.triangles = indices.ToArray();
- mesh.RecalculateNormals();
- return mesh;
- }
- public void EditorMesh()
- {
- Initialize();
- float startTime = Time.realtimeSinceStartup;
- Mesh mesh = ChunkMesh(Vector2Int.zero);
- executionTime = ((Time.realtimeSinceStartup - startTime) * 1000f) + "ms";
- if (GameObject.Find("Mesh") == null)
- {
- GameObject obj = new GameObject("Mesh");
- obj.transform.parent = transform;
- obj.AddComponent<MeshFilter>().mesh = mesh;
- obj.AddComponent<MeshRenderer>().material = material;
- obj.AddComponent<MeshCollider>();
- }
- else
- {
- var mf = GameObject.Find("Mesh").GetComponent<MeshFilter>();
- var collider = GameObject.Find("Mesh").GetComponent<MeshCollider>();
- mf.mesh = mesh;
- collider.sharedMesh = null;
- collider.sharedMesh = mesh;
- }
- }
- private void Start()
- {
- Initialize();
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- EditorMesh();
- }
- //if (Input.GetMouseButton(1))
- //{
- // Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- // if (Physics.Raycast(ray, out RaycastHit hit))
- // {
- // ModifyTerrain(new Vector3Int((int)hit.point.x, (int)hit.point.y, (int)hit.point.z), true, hit.collider.gameObject);
- // }
- //}
- //if (Input.GetMouseButton(0))
- //{
- // Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- // if (Physics.Raycast(ray, out RaycastHit hit))
- // {
- // ModifyTerrain(new Vector3Int((int)hit.point.x, (int)hit.point.y, (int)hit.point.z), false, hit.collider.gameObject);
- // }
- //}
- }
- void Initialize()
- {
- caseToNumpolys = Tables.caseToNumPolys;
- triTable = Tables.triTable;
- cam = Camera.main;
- vertices = new();
- vertDictionary = new();
- indices = new();
- lod = LOD == 0 ? 1 : LOD * 2;
- cornerOffsets = new Vector3Int[]
- {
- new Vector3Int(0, 0, 0),
- new Vector3Int(0, 1 * lod, 0),
- new Vector3Int(1 * lod, 1 * lod, 0),
- new Vector3Int(1 * lod, 0, 0),
- new Vector3Int(0, 0, 1 * lod),
- new Vector3Int(0, 1 * lod, 1 * lod),
- new Vector3Int(1 * lod, 1 * lod, 1 * lod),
- new Vector3Int(1 * lod, 0, 1 * lod),
- };
- noise = new Noise(frequency, persistance, lacunarity, octaves, lod);
- }
- void AddVertices(int x, int y, int z)
- {
- int triCase = Utils.CalculateCase(voxelGrid[Utils.VoxelIndex(x/lod, y/lod, z/lod)]);
- int[] edges = triTable[triCase];
- for (int j = 0, i = 0; j < caseToNumpolys[triCase]; j++, i += 3)
- {
- for (int h = 0; h < 3; h++)
- {
- Vector3 vertex = GetEdgePosition(edges[i + (2 - h)], x, y, z);
- if (vertDictionary.ContainsKey(vertex))
- {
- indices.Add(vertDictionary[vertex]);
- }
- else
- {
- vertices.Add(vertex);
- indices.Add(vertices.Count - 1);
- vertDictionary.Add(vertex, vertices.Count - 1);
- }
- }
- }
- }
- Vector3 GetEdgePosition(int edge, int x, int y, int z)
- {
- float[] voxelData = voxelGrid[Utils.VoxelIndex(x/lod, y/lod, z/lod)];
- switch (edge)
- {
- case 0: return Utils.GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[1], 0, 1, 0);
- case 1: return Utils.GetInterpolatedEdgePosition(x, y + (1 * lod), z, voxelData[1], voxelData[2], 1, 0, 0);
- case 2: return Utils.GetInterpolatedEdgePosition(x + (1 * lod), y, z, voxelData[3], voxelData[2], 0, 1, 0);
- case 3: return Utils.GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[3], 1, 0, 0);
- case 4: return Utils.GetInterpolatedEdgePosition(x, y, z + (1 * lod), voxelData[4], voxelData[5], 0, 1, 0);
- case 5: return Utils.GetInterpolatedEdgePosition(x, y + (1 * lod), z + (1 * lod), voxelData[5], voxelData[6], 1, 0, 0);
- case 6: return Utils.GetInterpolatedEdgePosition(x + (1 * lod), y, z + (1 * lod), voxelData[7], voxelData[6], 0, 1, 0);
- case 7: return Utils.GetInterpolatedEdgePosition(x, y, z + (1 * lod), voxelData[4], voxelData[7], 1, 0, 0);
- case 8: return Utils.GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[4], 0, 0, 1);
- case 9: return Utils.GetInterpolatedEdgePosition(x, y + (1 * lod), z, voxelData[1], voxelData[5], 0, 0, 1);
- case 10: return Utils.GetInterpolatedEdgePosition(x + (1 * lod), y + (1 * lod), z, voxelData[2], voxelData[6], 0, 0, 1);
- case 11: return Utils.GetInterpolatedEdgePosition(x + (1 * lod), y, z, voxelData[3], voxelData[7], 0, 0, 1);
- default: throw new ArgumentOutOfRangeException(nameof(edge), "Invalid edge index: " + edge);
- }
- }
- private void ModifyTerrain(Vector3Int vertex, bool addTerrain, GameObject chunk)
- {
- for (int zOffset = -brushSize; zOffset <= brushSize; zOffset++)
- {
- for (int yOffset = -brushSize; yOffset <= brushSize; yOffset++)
- {
- for (int xOffset = -brushSize; xOffset <= brushSize; xOffset++)
- {
- bool xOutOfRange = vertex.x + xOffset < 0 || vertex.x + xOffset > chunkDimensions.x - 1;
- bool yOutOfRange = vertex.y + yOffset < 0 || vertex.y + yOffset > chunkDimensions.y - 1;
- bool zOutOfRange = vertex.z + zOffset < 0 || vertex.z + zOffset > chunkDimensions.z - 1;
- if (xOutOfRange || yOutOfRange || zOutOfRange)
- {
- continue;
- }
- float[] voxel = voxelGrid[Utils.VoxelIndex(vertex.x + xOffset, vertex.y + yOffset, vertex.z + zOffset)];
- for (int i = 0; i < 8; i++)
- {
- int x = vertex.x + xOffset + cornerOffsets[i].x;
- int y = vertex.y + yOffset + cornerOffsets[i].y;
- int z = vertex.z + zOffset + cornerOffsets[i].z;
- float dist = Vector3.Distance(vertex, new Vector3(x, y, z));
- if (dist < brushSize)
- {
- voxel[i] = addTerrain ? voxel[i] + brushStrength : voxel[i] - brushStrength;
- }
- }
- }
- }
- }
- chunk.GetComponent<MeshFilter>().mesh = ChunkMesh(new Vector2Int((int)chunk.transform.position.x, (int)chunk.transform.position.z));
- }
- public void CreateVoxelGrid()
- {
- voxelGrid = new float[(chunkDimensions.x * chunkDimensions.y * chunkDimensions.z / (lod * lod * lod))][];
- for (int z = 0, i2D = 0, i3D = 0; z < chunkDimensions.z; z+=lod)
- {
- for (int x = 0; x < chunkDimensions.x; x+=lod, i2D += 8)
- {
- for (int y = 0; y < chunkDimensions.y; y+=lod, i3D+=8)
- {
- CreateVoxel(new Vector3Int(x, y, z), i2D, i3D);
- }
- }
- }
- }
- public void CreateVoxel(Vector3Int pos, int noise2DIndex, int noise3DIndex)
- {
- Vector3Int index3D = pos / lod;
- if(Utils.VoxelIndex(index3D) - 1 > voxelGrid.Length - 1)
- {
- Debug.Log(Utils.VoxelIndex(index3D));
- Debug.Log(voxelGrid.Length);
- }
- voxelGrid[Utils.VoxelIndex(index3D) - 1] = new float[8];
- for (int i = 0; i < 8; i++)
- {
- Vector3Int cornerPos = pos + cornerOffsets[i];
- voxelGrid[Utils.VoxelIndex(index3D) - 1][i] = -cornerPos.y; //+ noiseMap2D[noise2DIndex + i] * heightMultiplier; //* density + noiseMap3D[noise3DIndex + i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement