Exemple #1
0
//threaded mesh creation
// basically just creates the triangles and vertices on a thread then adds them to a mesh
//in the main thread
/// <summary>
/// Creates the vertices.
/// </summary>
        public void CreateVertices()
        {
            canCreatemesh = false;

            //create the verts
            verts = MarchingCubes.CreateVertices(Voxels, this, 2, 2, lodLevel);

            //store the size so as to avoid garbage creation
            size = verts.Length;

            //create normals
            normals = MeshFactory.CalculateNormals(Voxels, size, verts, lodLevel);

            //create colors

            int V = size;

            uv = new Color[size];
            for (int i = 0; i < size; i++)
            {
                int x = Mathf.RoundToInt((verts[i].x - normals[i].x) / VoxelTerrainEngine.TriSize[lodLevel]);
                int y = Mathf.RoundToInt((verts[i].y - normals[i].y) / VoxelTerrainEngine.TriSize[lodLevel]);
                int z = Mathf.RoundToInt((verts[i].z - normals[i].z) / VoxelTerrainEngine.TriSize[lodLevel]);

                //conversion from voxel value to voxel type
                //seems to work well
                byte vox = Materials[x, y, z];

                //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.Stone)
                {
                    uv[i] = new Color(1, 0, 0, 0);
                }

                else if (vox == (int)VoxelType.Grass)
                {
                    uv[i] = new Color(0, 1, 0, 0);
                }

                else if (vox == (int)VoxelType.SandStone)
                {
                    uv[i] = new Color(0, 0, 1, 0);
                }

                else if (vox == (int)VoxelType.Dirt)
                {
                    uv[i] = new Color(0, 0, 0, 1);
                }
            }
            canCreatemesh = true;
            VoxelTerrainEngine.MeshChunks.Enqueue(this);
        }
Exemple #2
0
//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;
        }