private UTIL.EPosition GetCameraPositionToCenterTerrain(Vector3 _position)
    {
        Vector2 pos2D = StaticMaths.ThreeDTo2D(_position, StaticMaths.EPlane.E_XZ);

        UTIL.EPosition pos = UTIL.EPosition.ECenter;

        int halfDim = GetTerrainDimCenter();

        if (StaticMaths.WithinBoundingBox(pos2D,
                                          StaticMaths.ThreeDTo2D(StaticMaths.MultiplyVector3D(terrainChunks[halfDim, halfDim].GetCenter(), TILES.Offset), StaticMaths.EPlane.E_XZ),
                                          planetData.GetTerrainSize() + StaticMaths.ThreeDTo2D(TILES.Offset, StaticMaths.EPlane.E_XZ) * 2))
        {
        }
        else
        {
            if (pos2D.y < terrainChunks[halfDim, halfDim].GetCenter().z - planetData.GetTerrainSize().y / 2)
            {
                pos = UTIL.EPosition.EAbove;
            }
            else if (pos2D.y > terrainChunks[halfDim, halfDim].GetCenter().z + planetData.GetTerrainSize().y / 2)
            {
                pos = UTIL.EPosition.EBelow;
            }
            else if (pos2D.x < terrainChunks[halfDim, halfDim].GetCenter().x - planetData.GetTerrainSize().x / 2)
            {
                pos = UTIL.EPosition.ELeft;
            }
            else if (pos2D.x > terrainChunks[halfDim, halfDim].GetCenter().x + planetData.GetTerrainSize().x / 2)
            {
                pos = UTIL.EPosition.ERight;
            }
        }

        return(pos);
    }
    private UTIL.EPosition GetCameraPositionToCenterMap(Vector3 _position)
    {
        Vector2 pos2D = StaticMaths.ThreeDTo2D(_position, StaticMaths.EPlane.E_XZ);

        UTIL.EPosition pos = UTIL.EPosition.ECenter;


        if (StaticMaths.WithinBoundingBox(pos2D,
                                          StaticMaths.ThreeDTo2D(map.GetActualCenter(), StaticMaths.EPlane.E_XZ),
                                          new Vector2(chunkTileCountX * TILES.Offset.x, chunkTileCountZ * TILES.Offset.z) + StaticMaths.ThreeDTo2D(TILES.Offset, StaticMaths.EPlane.E_XZ) * 2))
        {
            pos = UTIL.EPosition.ECenter;
        }
        else
        {
            if (pos2D.y < mapObject.transform.position.z + (chunkTileCountZ * TILES.Offset.z))
            {
                pos = UTIL.EPosition.EAbove;
            }
            else if (pos2D.y > mapObject.transform.position.z - (chunkTileCountZ * TILES.Offset.z))
            {
                pos = UTIL.EPosition.EBelow;
            }
            else if (pos2D.x < mapObject.transform.position.x + (chunkTileCountX * TILES.Offset.x))
            {
                pos = UTIL.EPosition.ELeft;
            }
            else if (pos2D.x > mapObject.transform.position.x - (chunkTileCountX * TILES.Offset.x))
            {
                pos = UTIL.EPosition.ERight;
            }
        }

        return(pos);
    }
    public TerrainChunkData GetData()
    {
        TerrainChunkData d = new TerrainChunkData();

        d.height     = terrainData.GetHeight();
        d.position   = StaticMaths.ThreeDTo2D(center, StaticMaths.EPlane.E_XZ);
        d.smoothness = terrainData.GetSmoothness();

        return(d);
    }
Beispiel #4
0
    public void GenerateMesh(float[] _charges)
    {
        if (cs_MarchingCubes == null)
        {
            cs_MarchingCubes = Resources.Load <ComputeShader>("Shaders/cs_MarchingCubes");
        }

        int   numThreadsPerX = Mathf.CeilToInt(resolution.x / (float)threadGroupSize);
        int   numThreadsPerY = Mathf.CeilToInt(resolution.y / (float)threadGroupSize);
        int   numThreadsPerZ = Mathf.CeilToInt(resolution.z / (float)threadGroupSize);
        float isoLevel       = Meta_CellEditor.SCULPTING.MARCHING_CUBES.ISOLEVEL;

        int kernel = cs_MarchingCubes.FindKernel("CSMain");

        triangleBuffer.SetCounterValue(0);
        pointsBuffer.SetData(_charges);
        cs_MarchingCubes.SetBuffer(kernel, "points", pointsBuffer);
        cs_MarchingCubes.SetBuffer(kernel, "triangles", triangleBuffer);
        cs_MarchingCubes.SetInt("numPointsPerX", (int)resolution.x);
        cs_MarchingCubes.SetInt("numPointsPerY", (int)resolution.y);
        cs_MarchingCubes.SetInt("numPointsPerZ", (int)resolution.z);
        cs_MarchingCubes.SetFloat("scale", Meta_CellEditor.SCULPTING.GRID.SCALE);
        cs_MarchingCubes.SetVector("center", transform.position);
        cs_MarchingCubes.SetFloat("isoLevel", isoLevel);

        cs_MarchingCubes.Dispatch(kernel, numThreadsPerX, numThreadsPerY, numThreadsPerZ);

        // Get number of triangles in the triangle buffer
        ComputeBuffer.CopyCount(triangleBuffer, triCountBuffer, 0);
        int[] triCountArray = { 0 };
        triCountBuffer.GetData(triCountArray);
        int numTris = triCountArray[0];

        // Get triangle data from shader
        Triangle[] tris = new Triangle[numTris];
        triangleBuffer.GetData(tris, 0, 0, numTris);

        mesh.Clear();
        var vertices      = new Vector3[numTris * 3];
        var meshTriangles = new int[numTris * 3];

        if (editor)
        {
            for (int i = 0; i < numTris; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    meshTriangles[i * 3 + j] = i * 3 + j;
                    vertices[i * 3 + j]      = tris[i][j];
                }
            }
        }
        else
        {
            for (int i = 0; i < numTris; i++)
            {
                for (int j = 2; j >= 0; j--)
                {
                    meshTriangles[i * 3 + j] = i * 3 + j;
                    vertices[i * 3 + j]      = tris[i][2 - j];
                }
            }
        }

        //generating uv coordiantes
        Vector2[] uv = new Vector2[vertices.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            uv[i] = StaticMaths.ThreeDTo2D(vertices[i], StaticMaths.EPlane.E_XZ);
        }

        mesh.vertices  = vertices;
        mesh.triangles = meshTriangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }