//Color function public static Color ColorDensity(float3 point, float3 gradient, TerrainColorData colorData) { gradient = math.normalize(gradient); //Blends between grass and dirt float grassBlend = math.saturate(math.pow(math.dot(gradient, new float3(0, 1, 0)) + colorData.offset, colorData.pow)); //Blends between dirt and stone float dirtBlend = math.saturate(math.pow(math.dot(gradient, new float3(0, 1, 0)) + colorData.offset2, colorData.pow2)); float4 grassColor = ColorToFloat4(colorData.grass) * ((noise.snoise(point * colorData.grassNoiseScale) * colorData.grassNoiseMul) + colorData.grassNoiseOffset); float4 color; //color = math.lerp(ColorToFloat4(colorData.dirt), ColorToFloat4(colorData.grass), math.clamp(grassBlend, 0, 1)); color = math.lerp(math.lerp(ColorToFloat4(colorData.stone), ColorToFloat4(colorData.dirt), dirtBlend), grassColor, grassBlend); //Clamping //color = new float4(gradient, 1); color = math.clamp(color, float4.zero, new float4(1, 1, 1, 1)); return(Float4ToColor(color)); }
//Generates the MarchingCube mesh public void GenerateMesh(TerrainGenerationData _terrainGenerationData, TerrainColorData _terrainColorData, Vector3Int chunkPos, TerrainGenerator _terrain, int[] _triTable, float3[] _edgeTable, float3[] _edgeTable2, int LOD = 0, bool resetMesh = true, bool immediate = false) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); if (resetMesh) { GetComponent <MeshFilter>().sharedMesh = new Mesh(); } completed = false; terrainGenerationData = _terrainGenerationData; terrainColorData = _terrainColorData; terrain = _terrain; chunkPosition = chunkPos; int resolution = terrainGenerationData.resolution - LOD; voxels = new NativeArray <float>((resolution + 2) * (resolution + 2) * (resolution + 2), Allocator.Persistent); vertices = new NativeArray <float3>(12 * resolution * resolution * resolution, Allocator.Persistent); finalVertices = new NativeList <Vector3>(Allocator.Persistent); triangles = new NativeArray <int>(16 * resolution * resolution * resolution, Allocator.Persistent); finalTriangles = new NativeList <int>(Allocator.Persistent); colors = new NativeArray <Color>(12 * resolution * resolution * resolution, Allocator.Persistent); finalColors = new NativeList <Color>(Allocator.Persistent); triTable = new NativeArray <int>(_triTable.Length, Allocator.Persistent); edgeTable = new NativeArray <float3>(12, Allocator.Persistent); edgeTable2 = new NativeArray <float3>(12, Allocator.Persistent); edgeTable.CopyFrom(_edgeTable); edgeTable2.CopyFrom(_edgeTable2); triTable.CopyFrom(_triTable); if (_terrainGenerationData.usePregeneratedVoxelData) { MarchingCubeVoxelJob voxelJob = new MarchingCubeVoxelJob() { voxels = voxels, chunkPosition = new float3(chunkPosition.x * 10, chunkPosition.y * 10, chunkPosition.z * 10), scale = (float)10 / (float)resolution, resolution = resolution + 2, terrainGenerationData = _terrainGenerationData }; voxelHandle = voxelJob.Schedule((resolution + 2) * (resolution + 2) * (resolution + 2), 64); } MarchingCubeJob job = new MarchingCubeJob() { edgeTable = edgeTable, edgeTable2 = edgeTable2, scale = (float)10 / (float)resolution, LOD = LOD, voxels = voxels, resolution = resolution, triangulationTable = triTable, chunkPosition = new float3(chunkPosition.x * 10, chunkPosition.y * 10, chunkPosition.z * 10), vertices = vertices, triangles = triangles, colors = colors, terrainGenerationData = _terrainGenerationData, terrainColorData = _terrainColorData }; if (_terrainGenerationData.usePregeneratedVoxelData) { jobHandle = job.Schedule(resolution * resolution * resolution, 64, voxelHandle); } else { jobHandle = job.Schedule(resolution * resolution * resolution, 64); } MarchingCubeOptimizeJob optimize = new MarchingCubeOptimizeJob() { vertices = vertices, finalVertices = finalVertices, triangles = triangles, finalTriangles = finalTriangles, colors = colors, finalColors = finalColors }; optimizeHandle = optimize.Schedule(jobHandle); if (immediate) { CompleteChunkJob(); stopwatch.Stop(); UnityEngine.Debug.Log(stopwatch.ElapsedMilliseconds); } }