123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Chunks;
- using Sirenix.OdinInspector;
- namespace KairoEngine.TerrainEngine
- {
- public class ChunkTerrainGenerator : MonoBehaviour
- {
- public MarchingCubes3 marchingCubes3;
- public MarchingCubes2 marchingCubes2;
- public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
- public Vector3Int chunks = new Vector3Int(2, 1, 2);
- public float noiseMultiplier = 1.5f;
- public ChunkSystem<BlockBase> chunkSystem;
- [Button("Generate")]
- void CreateChunks()
- {
- chunkSystem = new ChunkSystem<BlockBase>(chunkSize, true);
- for (int x = 0; x < chunks.x; x++) {
- for (int z = 0; z < chunks.z; z++) {
- for (int y = 0; y < chunks.y; y++) {
- Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
- chunkSystem.CreateChunk(chunkPos, (Chunk<BlockBase> c, Vector3Int pos) => new BlockBase(0, 0));
- GenerateChunkTerrain(chunkPos);
- }
- }
- }
- for (int x = 0; x < chunks.x; x++) {
- for (int z = 0; z < chunks.z; z++) {
- for (int y = 0; y < chunks.y; y++) {
- Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
- if(marchingCubes3 != null) marchingCubes3.Generate(chunkSystem, chunkPos);
- else if(marchingCubes2 != null) marchingCubes2.Generate(chunkSystem, chunkPos);
- }
- }
- }
- }
- void GenerateChunkTerrain(Vector3Int initialPos)
- {
- for (int x = 0; x < chunkSize.x; x++) {
- for (int z = 0; z < chunkSize.z; z++) {
- for (int y = 0; y < chunkSize.y; y++) {
- // Get a terrain height using regular old Perlin noise.
- Vector3Int pos = initialPos + new Vector3Int(x, y, z);
- float width = (float)(chunks.x * chunkSize.x);
- float height = (float)(chunks.y * chunkSize.y);
- float length = (float)(chunks.z * chunkSize.z);;
- float thisHeight = height * Mathf.PerlinNoise((float)pos.x / width * noiseMultiplier + 0.001f, (float)pos.z / length * noiseMultiplier + 0.001f);
- BlockBase block = new BlockBase(0, (uint)Mathf.FloorToInt(thisHeight * 1000));
- chunkSystem.SetBlock(pos, block);
- }
- }
- }
- }
- }
- }
|