Esempio n. 1
0
    void UpdateVisibleChunks()
    {
        HashSet <Vector2> alreadyUpdatedChunkCoords = new HashSet <Vector2>();

        for (int i = visibleTerrainChunks.Count - 1; i >= 0; i--)
        {
            alreadyUpdatedChunkCoords.Add(visibleTerrainChunks[i].coord);
            visibleTerrainChunks[i].UpdateTerrainChunk();
        }

        int currentChunkCoordX = Mathf.RoundToInt(viewerPosition.x / meshWorldSize);
        int currentChunkCoordY = Mathf.RoundToInt(viewerPosition.y / meshWorldSize);

        for (int yOffset = -chunksVisibleViewDistance; yOffset <= chunksVisibleViewDistance; yOffset++)
        {
            for (int xOffset = -chunksVisibleViewDistance; xOffset <= chunksVisibleViewDistance; xOffset++)
            {
                Vector2 viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
                if (!alreadyUpdatedChunkCoords.Contains(viewedChunkCoord))
                {
                    if (terrainChunkDict.ContainsKey(viewedChunkCoord))
                    {
                        terrainChunkDict[viewedChunkCoord].UpdateTerrainChunk();
                    }
                    else
                    {
                        Terrainchunk newChunk = new Terrainchunk(viewedChunkCoord, heightMapSettings, meshSettings, detailLevels, colliderLODIndex, transform, viewer, mapMaterial);
                        terrainChunkDict.Add(viewedChunkCoord, newChunk);
                        newChunk.onVisibilityChanged += OnTerrainChunkVisibilityChanged;
                        newChunk.Load();
                    }
                }
            }
        }
    }
Esempio n. 2
0
 void OnTerrainChunkVisibilityChanged(Terrainchunk chunk, bool isVisible)
 {
     if (isVisible)
     {
         visibleTerrainChunks.Add(chunk);
     }
     else
     {
         visibleTerrainChunks.Remove(chunk);
     }
 }
Esempio n. 3
0
    void ModifyMesh(Terrainchunk t, Vector3 hitpoint)
    {
        Mesh mesh = t.meshFilter.mesh;

        Vector3[] vertices = mesh.vertices;

        for (int i = 0; i < vertices.Length; ++i)
        {
            Vector3 v = t.meshFilter.transform.TransformPoint(vertices[i]);
            //vertices[i] += hit.normal * Gaussian(v, hit.point, radius);
            vertices[i] += Vector3.up * Gaussian(v, hit.point, radius);
        }
        mesh.vertices = vertices;
        mesh.RecalculateBounds();

        if (t.meshCollider != null)
        {
            var colliMesh = new Mesh();
            colliMesh.vertices        = mesh.vertices;
            colliMesh.triangles       = mesh.triangles;
            t.meshCollider.sharedMesh = colliMesh;
        }
    }
Esempio n. 4
0
    public List <Terrainchunk> GetTerrainchunks(Vector3 nearestPoint, float radius = 0)
    {
        List <Terrainchunk> returnChunks = new List <Terrainchunk>();

        returnChunks.Add(GetNearestChunk(nearestPoint));

        //Check for Neibors
        Terrainchunk t = GetNearestChunk(new Vector3(nearestPoint.x + radius, nearestPoint.y, nearestPoint.z));

        if (returnChunks[0] != null)
        {
            //x neibors
            if (GetNearestChunk(new Vector3(nearestPoint.x + radius, nearestPoint.y, nearestPoint.z)) != returnChunks[0])
            {
                returnChunks.Add(GetNearestChunk(new Vector3(nearestPoint.x + radius, nearestPoint.y, nearestPoint.z)));
            }
            else if (GetNearestChunk(new Vector3(nearestPoint.x - radius, nearestPoint.y, nearestPoint.z)) != returnChunks[0])
            {
                returnChunks.Add(GetNearestChunk(new Vector3(nearestPoint.x - radius, nearestPoint.y, nearestPoint.z)));
            }
            //y neibors
            if (GetNearestChunk(new Vector3(nearestPoint.x, nearestPoint.y, nearestPoint.z + radius)) != returnChunks[0])
            {
                returnChunks.Add(GetNearestChunk(new Vector3(nearestPoint.x, nearestPoint.y, nearestPoint.z + radius)));
            }
            else if (GetNearestChunk(new Vector3(nearestPoint.x, nearestPoint.y, nearestPoint.z - radius)) != returnChunks[0])
            {
                returnChunks.Add(GetNearestChunk(new Vector3(nearestPoint.x, nearestPoint.y, nearestPoint.z - radius)));
            }
            if (returnChunks.Count > 2)
            {
                returnChunks.Add(GetTerrainchunkWithCoords(new Vector2(returnChunks[1].coord.x, returnChunks[2].coord.y)));
            }
        }
        return(returnChunks);
    }