/// <summary> /// Creates the voxels. /// </summary> public void CreateVoxels() { if (VoxelSaver.GetBool("hasSavedChunk") == false && hasVoxels == false) { Voxels = MeshFactory.CreateVoxels(Voxels, RealPos - new Vector3(2, 2, 2), this, lodLevel); hasVoxels = true; } else if (VoxelSaver.GetBool("hasSavedChunk") && hasVoxels == false && hasloaded == false) { LoadVoxels(); hasVoxels = true; hasloaded = true; } VoxelTerrainEngine.GenerateVertices.Enqueue(this); }
//threaded mesh creation // basically just creates the triangles and vertices on a thread then adds them to a mesh //in the main thread public void CreateMeshesAndvoxels(bool MakeNewVoxels) { canCreatemesh = false; if (MakeNewVoxels && VoxelSaver.GetBool("hasSavedChunk") == false) { Voxels = MeshFactory.CreateVoxels(Voxels, m_pos, this, generator.noise); } else { LoadVoxels(); } //create the verts verts = MeshFactory.MarchingCubes.CreateVertices(Voxels, this, 2, 2); //store the size so as to avoid garbage creation size = verts.Length; //create normals normals = MeshFactory.CalculateNormals(Voxels, size, verts); //create colors control = new Color[size]; float R = 0; float G = 0; float B = 0; float A = 0; for (int i = 0; i < size; i++) { R = 0; G = 0; B = 0; A = 0; int x = Mathf.RoundToInt(verts[i].x - normals[i].x); int y = Mathf.RoundToInt(verts[i].y - normals[i].y); int z = Mathf.RoundToInt(verts[i].z - normals[i].z); //conversion from voxel value to voxel type //seems to work well byte vox = (byte)((float)Voxels[x, y, z] / 255 * 8); //basically each value gets assigned a color of 0.5 to 1.0 //in theory each decimal could be a voxel type if (vox == (int)VoxelType.Dirt) { R = 0.5f; } if (vox == (int)VoxelType.Stone) { G = 0.5f; } if (vox == (int)VoxelType.SandStone) { B = 0.5f; } if (vox == (int)VoxelType.Grass) { A = 0.5f; } if (vox == (int)VoxelType.Iron) { R = 1; } if (vox == (int)VoxelType.Gold) { G = 1; } if (vox == (int)VoxelType.GunPowder) { B = 1; } if (vox == (int)VoxelType.Tungsten) { A = 1; } control[i] = new Color(R, G, B, A); } canCreatemesh = true; }