Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class MeshGenerator : MonoBehaviour
- {
- public Vector3Int chunkDimensions;
- public Material material;
- public int brushSize;
- public float brushStrength;
- [Header("Noise Settings")]
- public float heightMultiplier;
- public float frequency;
- public float lacunarity;
- public float persistance;
- public int octaves;
- public float density;
- float[,,][] voxelGrid;
- List<Vector3> vertices;
- Dictionary<Vector3, int> vertDictionary;
- List<int> indices;
- int[] caseToNumpolys;
- int[][] triTable;
- Vector3[] cornerOffset;
- Camera cam;
- private void Start()
- {
- Initialize();
- CreateVoxelGrid();
- GenerateMesh();
- }
- private void OnValidate()
- {
- Initialize();
- CreateVoxelGrid();
- GenerateMesh();
- }
- private void Update()
- {
- 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);
- }
- }
- 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);
- }
- }
- }
- void Initialize()
- {
- caseToNumpolys = Tables.caseToNumPolys;
- triTable = Tables.triTable;
- cam = Camera.main;
- vertices = new();
- vertDictionary = new();
- indices = new();
- cornerOffset = new Vector3[]
- {
- new Vector3(0, 0, 0),
- new Vector3(0, 1, 0),
- new Vector3(1, 1, 0),
- new Vector3(1, 0, 0),
- new Vector3(0, 0, 1),
- new Vector3(0, 1, 1),
- new Vector3(1, 1, 1),
- new Vector3(1, 0, 1),
- };
- }
- void GenerateMesh()
- {
- for (int z = 0, i = 0; z < chunkDimensions.z; z++)
- {
- for (int y = 0; y < chunkDimensions.y; y++)
- {
- for (int x = 0; x < chunkDimensions.x; x++, i+=3)
- {
- AddVertices(x, y, z, i);
- }
- }
- }
- Mesh mesh = new Mesh();
- mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
- mesh.vertices = vertices.ToArray();
- mesh.triangles = indices.ToArray();
- mesh.RecalculateNormals();
- 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;
- }
- }
- void AddVertices(int x, int y, int z, int index)
- {
- int triCase = CalculateCase(voxelGrid[x, y, z]);
- 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[x, y, z];
- switch (edge)
- {
- case 0: return GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[1], 0, 1, 0);
- case 1: return GetInterpolatedEdgePosition(x, y + 1, z, voxelData[1], voxelData[2], 1, 0, 0);
- case 2: return GetInterpolatedEdgePosition(x + 1, y, z, voxelData[3], voxelData[2], 0, 1, 0);
- case 3: return GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[3], 1, 0, 0);
- case 4: return GetInterpolatedEdgePosition(x, y, z + 1, voxelData[4], voxelData[5], 0, 1, 0);
- case 5: return GetInterpolatedEdgePosition(x, y + 1, z + 1, voxelData[5], voxelData[6], 1, 0, 0);
- case 6: return GetInterpolatedEdgePosition(x + 1, y, z + 1, voxelData[7], voxelData[6], 0, 1, 0);
- case 7: return GetInterpolatedEdgePosition(x, y, z + 1, voxelData[4], voxelData[7], 1, 0, 0);
- case 8: return GetInterpolatedEdgePosition(x, y, z, voxelData[0], voxelData[4], 0, 0, 1);
- case 9: return GetInterpolatedEdgePosition(x, y + 1, z, voxelData[1], voxelData[5], 0, 0, 1);
- case 10: return GetInterpolatedEdgePosition(x + 1, y + 1, z, voxelData[2], voxelData[6], 0, 0, 1);
- case 11: return GetInterpolatedEdgePosition(x + 1, y, z, voxelData[3], voxelData[7], 0, 0, 1);
- default: throw new ArgumentOutOfRangeException(nameof(edge), "Invalid edge index: " + edge);
- }
- }
- // Helper method to calculate interpolated edge positions
- Vector3 GetInterpolatedEdgePosition(int x, int y, int z, float voxelA, float voxelB, int offsetX, int offsetY, int offsetZ)
- {
- float interpolation = Interpolate(voxelA, voxelB);
- return new Vector3(x + offsetX * interpolation, y + offsetY * interpolation, z + offsetZ * interpolation);
- }
- float Interpolate(float a, float b)
- {
- return (0 - a) / (b - a);
- }
- int CalculateCase(float[] voxelData)
- {
- int triCase = 0;
- for (int i = 0; i < 8; i++)
- {
- int bit = voxelData[i] < 0 ? 1 : 0;
- triCase |= bit << i;
- }
- return triCase;
- }
- private void ModifyTerrain(Vector3Int vertex, bool addTerrain)
- {
- 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[vertex.x + xOffset, vertex.y + yOffset, vertex.z + zOffset];
- for (int i = 0; i < 8; i++)
- {
- int x = (int)(vertex.x + xOffset + cornerOffset[i].x);
- int y = (int)(vertex.y + yOffset + cornerOffset[i].y);
- int z = (int)(vertex.z + zOffset + cornerOffset[i].z);
- float dist = Vector3.Distance(vertex, new Vector3(x, y, z));
- if (dist < brushSize)
- {
- voxel[i] = addTerrain ? voxel[i] + brushStrength : voxel[i] - brushStrength;
- }
- }
- }
- }
- }
- GenerateMesh();
- }
- public void CreateVoxelGrid()
- {
- voxelGrid = new float[chunkDimensions.x, chunkDimensions.y, chunkDimensions.z][];
- for (int z = 0; z < chunkDimensions.z; z++)
- {
- for (int x = 0; x < chunkDimensions.x; x++)
- {
- //The noise value at all 8 corners
- float[] noise = new float[8];
- for (int i = 0; i < 8; i++)
- {
- noise[i] = FractalNoise((int)(x + cornerOffset[i].x), (int)(z + cornerOffset[i].z));
- }
- for (int y = 0; y < chunkDimensions.y; y++)
- {
- CreateVoxel(new Vector3Int(x, y, z), noise);
- }
- }
- }
- }
- public void CreateVoxel(Vector3Int pos, float[] noise)
- {
- voxelGrid[pos.x, pos.y, pos.z] = new float[8];
- for (int i = 0; i < 8; i++)
- {
- Vector3 cornerPos = pos + cornerOffset[i];
- voxelGrid[pos.x, pos.y, pos.z][i] = (-cornerPos.y + noise[i]) * density + Fractal3DNoise(cornerPos);
- }
- }
- float FractalNoise(int x, int y)
- {
- float noise = 0;
- for (int i = 0; i < octaves; i++)
- {
- float frequency = Mathf.Pow(lacunarity, i) * this.frequency;
- float amplitude = Mathf.Pow(persistance, i);
- noise += Mathf.PerlinNoise(x * frequency, y * frequency) * amplitude;
- }
- return noise * heightMultiplier;
- }
- float Fractal3DNoise(Vector3 coord)
- {
- float noise = 0;
- for (int i = 0; i < octaves; i++)
- {
- float frequency = Mathf.Pow(lacunarity, i) * this.frequency;
- float amplitude = Mathf.Pow(persistance, i);
- noise += Perlin.Noise(coord.x * frequency, coord.y * frequency, coord.z * frequency) * amplitude;
- }
- return noise - density;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement