コード例 #1
0
    // Start is called before the first frame update
    void Start()
    {
        //also from youtube
        for (int x = 0; x < chunkSizeVerts; x++)
        {
            for (int y = 0; y < chunkSizeVerts; y++)
            {
                for (int z = 0; z < chunkSizeVerts; z++)
                {
                    float noise = Perlin3D(x * noiseScale, y * noiseScale, z * noiseScale);
                    if (noise >= threshold) //here we pass floats instead so that Perlin behaves
                    {
                        //sampling unity's perlin noise with an integer returns the same value, which was less than the threshold
                        //Instantiate(blockPrefab, new Vector3(x, y, z), Quaternion.identity);//not rotated is what quaternion.identity says
                        //^^ this is if you treat each point as a cube. I will treat each point as a vertex instead.

                        /*bool xSafe = false;
                         * bool ySafe = false;
                         * bool zSafe = false;
                         * if (x == chunkSize - 1)
                         * {
                         *  //check edges (x - 1)
                         * }
                         * else
                         * {
                         *  //check edges (x + 1)
                         * }
                         * if (y == chunkSize - 1)
                         * {
                         *  //check y edges accordingly
                         * }
                         * else
                         * {
                         *  //check edges (y + 1)
                         * }
                         * if (z == chunkSize - 1)
                         * {
                         *  //check z edges accordingly
                         * }
                         * else
                         * {
                         *  //check edges (z + 1)
                         * }
                         * if (xSafe && ySafe && zSafe) { //make sure that we have checked edges, if all edges are filled, then it is safe to set to 0; we are checking if a cube is like covered or not
                         *  chunkData[chunkSize - x - 1, chunkSize - y - 1, z] = 0;
                         * }
                         * else
                         * {
                         *  chunkData[chunkSize - x - 1, chunkSize - y - 1, z] = 1;
                         * }*/
                        chunkData[chunkSizeVerts - x - 1, chunkSizeVerts - y - 1, z] = 1;
                    }
                    else
                    {
                        chunkData[chunkSizeVerts - x - 1, chunkSizeVerts - y - 1, z] = 0;//idk if it instantiates to all 0's
                    }
                }
            }
        }

        chunkData = hollowOut(chunkData);

        //now that we have our verticies, lets march some cubes?

        //some for loop logic to get to one cube
        //now that we are at a cube:
        //check each of the 8 verticies to see if it is on or off
        //index = 0;
        //for all of the 8
        //index += (vertex value [1 or 0]) * 2^(vertex index)
        //lookup, then make triangle

        CombineInstance[] wholeMesh = new CombineInstance[(int)Mathf.Pow(chunkSizeBlocks, 3)];

        Mesh mesh = GetComponent <MeshFilter>().mesh;

        mesh.Clear();

        int[] initialVoxels = { (int)chunkData[0, 0, 0], (int)chunkData[1, 0, 0], (int)chunkData[1, 0, 1], (int)chunkData[0, 0, 1], (int)chunkData[0, 1, 0], (int)chunkData[1, 1, 0], (int)chunkData[1, 1, 1], (int)chunkData[0, 1, 1] };

        Mesh temp = MarchingCubes3.perCube(new Vector3(0, 0, 0), initialVoxels, 0);

        //foreach (int i in initialVoxels)
        //    Debug.Log(i);

        //mesh.triangles = temp.triangles;
        //mesh.vertices = temp.vertices;

        //mesh.RecalculateBounds();
        //mesh.RecalculateNormals();

        wholeMesh[0]      = new CombineInstance();
        wholeMesh[0].mesh = temp;

        //per chunk
        for (int x = 0; x < chunkSizeVerts - 1; x++) //or x < chunkSizeBlocks
        {
            for (int y = 0; y < chunkSizeVerts - 1; y++)
            {
                for (int z = 0; z < chunkSizeVerts - 1; z++)
                {
                    if (x == 0 && y == 0 && z == 0)
                    {
                        continue;
                    }
                    //set blockNum somehow
                    int blockNum = (int)(z + y * (Mathf.Pow(chunkSizeBlocks, 1)) + x * (Mathf.Pow(chunkSizeBlocks, 2)));
                    //percube
                    int[] voxels = { (int)chunkData[x, y, z], (int)chunkData[x + 1, y, z], (int)chunkData[x + 1, y, z + 1], (int)chunkData[x, y, z + 1], (int)chunkData[x, y + 1, z], (int)chunkData[x + 1, y + 1, z], (int)chunkData[x + 1, y + 1, z + 1], (int)chunkData[x, y + 1, z + 1] }; //this is a line worth checking, I made it match up with MarchingCubes3.localVerticies
                    Mesh  m      = MarchingCubes3Clean.perCube(new Vector3(0, 0, 0), voxels, blockNum);
                    //^^ this causes unity to freeze up

                    //if combineInstance has too many verts: //IMPLEMENT THIS LOGIC LATER, MAYBE IF COMBINING CHUNKS???
                    //save the current combineInstance to a mesh
                    //make a new one
                    //Debug.Log(wholeMesh[blockNum]);
                    wholeMesh[blockNum]      = new CombineInstance();
                    wholeMesh[blockNum].mesh = m;
                    //Debug.Log(wholeMesh[blockNum]);
                    //wholeMesh[blockNum].transform. = MarchingCubes3Clean.beginningCoordinates(new Vector3(0, 0, 0), blockNum);//comes with the mesh???
                    //merge meshes
                }
            }
        }

        /*mesh.Clear();
         *
         * mesh.CombineMeshes(wholeMesh);
         *
         * mesh.RecalculateBounds();
         * mesh.RecalculateNormals();*/

        //Mesh mesh = GetComponent<MeshFilter>().mesh;

        mesh = GetComponent <MeshFilter>().mesh; //mesh is invisible now.

        mesh.Clear();

        mesh.CombineMeshes(wholeMesh);

        //mesh.Optimize();

        //MeshCollider meshCollider = GetComponent<MeshCollider>();

        //meshCollider.sharedMesh = GetComponent<MeshFilter>().mesh; //this won't work but i want it to :(, also it shows up as black and unlit???
        //I think the mesh is centered but idk...

        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }
コード例 #2
0
    // Start is called before the first frame update
    void Start()
    {
        //also from youtube
        for (int x = 0; x < chunkSizeBlocks; x++)
        {
            for (int y = 0; y < chunkSizeBlocks; y++)
            {
                for (int z = 0; z < chunkSizeBlocks; z++)
                {
                    float noise = Perlin3D(x * noiseScale, y * noiseScale, z * noiseScale);
                    if (noise >= threshold) //here we pass floats instead so that Perlin behaves
                    {
                        //sampling unity's perlin noise with an integer returns the same value, which was less than the threshold
                        //Instantiate(blockPrefab, new Vector3(x, y, z), Quaternion.identity);//not rotated is what quaternion.identity says
                        ogChunkData[chunkSizeBlocks - x - 1, chunkSizeBlocks - y - 1, z] = 1;
                    }
                    else
                    {
                        ogChunkData[chunkSizeBlocks - x - 1, chunkSizeBlocks - y - 1, z] = 0;//idk if it instantiates to all 0's
                    }
                }
            }
        }

        hollowedChunkData = hollowOut(ogChunkData);

        //now that we have our verticies, lets march some cubes?

        //some for loop logic to get to one cube
        //now that we are at a cube:
        //check each of the 8 verticies to see if it is on or off
        //index = 0;
        //for all of the 8
        //index += (vertex value [1 or 0]) * 2^(vertex index)
        //lookup, then make triangle

        /*CombineInstance[] wholeMesh = new CombineInstance[(int)Mathf.Pow(chunkSizeBlocks, 3)];
         *
         * Mesh mesh = GetComponent<MeshFilter>().mesh;
         *
         * mesh.Clear();
         *
         * //int[] initialVoxels = { (int) hollowedChunkData[0, 0, 0], (int) hollowedChunkData[1, 0, 0], (int) hollowedChunkData[1, 0, 1], (int) hollowedChunkData[0, 0, 1], (int) hollowedChunkData[0, 1, 0], (int) hollowedChunkData[1, 1, 0], (int) hollowedChunkData[1, 1, 1], (int) hollowedChunkData[0, 1, 1] };
         *
         * Mesh temp = prefab.GetComponent<MeshFilter>().sharedMesh;//MarchingCubes3.perCube(new Vector3(0, 0, 0), initialVoxels, 0);
         *
         * ////foreach (int i in initialVoxels)
         * ////    Debug.Log(i);
         *
         * ////mesh.triangles = temp.triangles;
         * ////mesh.vertices = temp.vertices;
         *
         * ////mesh.RecalculateBounds();
         * ////mesh.RecalculateNormals();
         *
         * wholeMesh[0] = new CombineInstance();
         * wholeMesh[0].mesh = temp;
         *
         * //Debug.Log(chunkSizeBlocks);*/

        //per chunk
        for (int x = 0; x < chunkSizeBlocks; x++)
        {
            for (int y = 0; y < chunkSizeBlocks; y++)
            {
                for (int z = 0; z < chunkSizeBlocks; z++)
                {
                    /*if (x == 0 && y == 0 && z == 0)
                     * {
                     *  continue;
                     * }*/
                    /*//set blockNum somehow
                     * int blockNum = (int)(z + y * (Mathf.Pow(chunkSizeBlocks, 1)) + x * (Mathf.Pow(chunkSizeBlocks, 2)));
                     * //percube
                     * int[] voxels = { (int)chunkData[x, y, z], (int)chunkData[x + 1, y, z], (int)chunkData[x + 1, y, z + 1], (int)chunkData[x, y, z + 1], (int)chunkData[x, y + 1, z], (int)chunkData[x + 1, y + 1, z], (int)chunkData[x + 1, y + 1, z + 1], (int)chunkData[x, y + 1, z + 1] }; //this is a line worth checking, I made it match up with MarchingCubes3.localVerticies
                     * Mesh m = MarchingCubes3Clean.perCube(new Vector3(0, 0, 0), voxels, blockNum);
                     * //^^ this causes unity to freeze up
                     *
                     * //if combineInstance has too many verts: //IMPLEMENT THIS LOGIC LATER, MAYBE IF COMBINING CHUNKS???
                     * //save the current combineInstance to a mesh
                     * //make a new one
                     * //Debug.Log(wholeMesh[blockNum]);
                     * wholeMesh[blockNum] = new CombineInstance();
                     * wholeMesh[blockNum].mesh = m;
                     * //Debug.Log(wholeMesh[blockNum]);
                     * //wholeMesh[blockNum].transform. = MarchingCubes3Clean.beginningCoordinates(new Vector3(0, 0, 0), blockNum);//comes with the mesh???
                     * //merge meshes*/

                    int blockNum = (int)(z + y * (Mathf.Pow(chunkSizeBlocks, 1)) + x * (Mathf.Pow(chunkSizeBlocks, 2)));
                    //int voxel = (int)chunkData[x, y, z];
                    //Debug.Log("Block #:" + blockNum + ", isOn: " + chunkData[x, y, z]);
                    if ((int)hollowedChunkData[x, y, z] > 0)
                    {
                        Instantiate(prefab, MarchingCubes3Clean.beginningCoordinates(new Vector3(0, 0, 0), blockNum, chunkSizeBlocks), Quaternion.identity);

                        /*wholeMesh[blockNum] = new CombineInstance();
                         * wholeMesh[blockNum].mesh = prefab.GetComponent<MeshFilter>().sharedMesh;
                         * Debug.Log(prefab.GetComponent<MeshFilter>().mesh.vertices.Length);
                         * wholeMesh[blockNum].mesh.vertices = new Vector3[] { new Vector3(wholeMesh[blockNum].mesh.vertices[0].x + x, wholeMesh[blockNum].mesh.vertices[0].y + y, wholeMesh[blockNum].mesh.vertices[0].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[1].x + x, wholeMesh[blockNum].mesh.vertices[1].y + y, wholeMesh[blockNum].mesh.vertices[1].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[2].x + x, wholeMesh[blockNum].mesh.vertices[2].y + y, wholeMesh[blockNum].mesh.vertices[2].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[3].x + x, wholeMesh[blockNum].mesh.vertices[3].y + y, wholeMesh[blockNum].mesh.vertices[3].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[4].x + x, wholeMesh[blockNum].mesh.vertices[4].y + y, wholeMesh[blockNum].mesh.vertices[4].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[5].x + x, wholeMesh[blockNum].mesh.vertices[5].y + y, wholeMesh[blockNum].mesh.vertices[5].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z),
                         *                                                  new Vector3(wholeMesh[blockNum].mesh.vertices[7].x + x, wholeMesh[blockNum].mesh.vertices[7].y + y, wholeMesh[blockNum].mesh.vertices[7].z + z),
                         * new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z), new Vector3(wholeMesh[blockNum].mesh.vertices[6].x + x, wholeMesh[blockNum].mesh.vertices[6].y + y, wholeMesh[blockNum].mesh.vertices[6].z + z),};
                         */
                    }
                }
            }
        }


        /*mesh = GetComponent<MeshFilter>().mesh; //mesh is invisible now.
         *
         * mesh.Clear();
         *
         * mesh.CombineMeshes(wholeMesh);
         *
         * //mesh.Optimize();
         *
         * //MeshCollider meshCollider = GetComponent<MeshCollider>();
         *
         * //meshCollider.sharedMesh = GetComponent<MeshFilter>().mesh; //this won't work but i want it to :(, also it shows up as black and unlit???
         * //I think the mesh is centered but idk...
         *
         * mesh.RecalculateBounds();
         * mesh.RecalculateNormals();
         * /*mesh.Clear();
         *
         * mesh.CombineMeshes(wholeMesh);
         *
         * mesh.RecalculateBounds();
         * mesh.RecalculateNormals();*
         *
         * //Mesh mesh = GetComponent<MeshFilter>().mesh;
         *
         * mesh = GetComponent<MeshFilter>().mesh; //mesh is invisible now.
         *
         * mesh.Clear();
         *
         * mesh.CombineMeshes(wholeMesh);
         *
         * //mesh.Optimize();
         *
         * //MeshCollider meshCollider = GetComponent<MeshCollider>();
         *
         * //meshCollider.sharedMesh = GetComponent<MeshFilter>().mesh; //this won't work but i want it to :(, also it shows up as black and unlit???
         * //I think the mesh is centered but idk...
         *
         * mesh.RecalculateBounds();
         * mesh.RecalculateNormals();*/
    }