コード例 #1
0
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));

                bool visible = viewerDstFromNearestEdge <= maxViewDst;

                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].visibleDstTreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            meshFilter.mesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    if (lodIndex == 0)
                    {
                        if (collisionLODMesh.hasMesh)
                        {
                            meshCollider.sharedMesh = collisionLODMesh.mesh;
                        }
                        else if (!collisionLODMesh.hasRequestedMesh)
                        {
                            collisionLODMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunksVisibleLastUpdate.Add(this);
                }
                SetVisible(visible);
            }
        }
コード例 #2
0
        public void UpdateChunk()
        {
            if (mapDataRecieved)
            {
                float playerDistFromEdge = Mathf.Sqrt(bounds.SqrDistance(playerPos));
                bool  visible            = playerDistFromEdge <= maxViewDist;
                bool  wasVisible         = isVisible();

                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (playerDistFromEdge > detailLevels[i].distThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != prevLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            prevLODIndex    = lodIndex;
                            meshFilter.mesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                }
                if (wasVisible != visible)
                {
                    if (visible)
                    {
                        oldChunks.Add(this);
                    }
                    else
                    {
                        oldChunks.Remove(this);
                    }
                    SetVisible(visible);
                }
            }
        }
コード例 #3
0
    public void UpdateTerrainChunk()
    {
        if (heightMapReceived)
        {
            float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));

            bool wasVisible = IsVisible();
            bool visible    = viewerDstFromNearestEdge <= maxViewDst;

            if (visible)
            {
                int lodIndex = 0;

                for (int i = 0; i < detailLevels.Length - 1; i++)
                {
                    if (viewerDstFromNearestEdge > detailLevels [i].visibleDstThreshold)
                    {
                        lodIndex = i + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodIndex != previousLODIndex)
                {
                    LODMesh lodMesh = lodMeshes [lodIndex];
                    if (lodMesh.hasMesh)
                    {
                        previousLODIndex        = lodIndex;
                        meshFilter.mesh         = lodMesh.mesh;
                        meshCollider.sharedMesh = lodMesh.mesh;                         // THIS
                    }
                    else if (!lodMesh.hasRequestedMesh)
                    {
                        lodMesh.RequestMesh(heightMap, meshSettings);
                    }
                }
            }

            if (wasVisible != visible)
            {
                SetVisible(visible);
                if (onVisibilityChanged != null)
                {
                    onVisibilityChanged(this, visible);
                }
            }
        }
    }
コード例 #4
0
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float viewerDistance = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible        = viewerDistance <= maxViews;

                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDistance > detailLevels[i].visibilityThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.meshReceived)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;

                            if (lodIndex == 0)
                            {
                                UpdateResources();
                                GenerateCities();
                            }

                            UpdateForests(lodIndex);
                        }
                        else if (!lodMesh.meshRequested)
                        {
                            lodMesh.RequestMesh(heightMap);
                        }
                    }

                    terrainChunksVisibleLast.Add(this);
                }

                SetVisible(visible);
            }
        }
コード例 #5
0
    public void UpdateTerrainChunk()
    {
        if (!hasMapData)
        {
            return;
        }

        float viewerDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));

        bool wasVisible = isVisible();
        bool visible    = viewerDistanceFromNearestEdge <= maxViewDistance;

        if (visible)
        {
            int lodIndex = 0;

            for (int i = 0; i < detailLevels.Length - 1; i++)
            {
                if (viewerDistanceFromNearestEdge > detailLevels[i].visibleDistanceThreshold)
                {
                    lodIndex = i + 1;
                }
                else
                {
                    break;
                }
            }

            if (lodIndex != previousLODIndex)
            {
                LODMesh lodMesh = lodMeshes[lodIndex];
                if (lodMesh.hasMesh)
                {
                    previousLODIndex = lodIndex;
                    meshFilter.mesh  = lodMesh.mesh;
                }
                else if (!lodMesh.hasRequestedMesh)
                {
                    lodMesh.RequestMesh(heightMap, meshSettings);
                }
            }
        }

        if (wasVisible != visible)
        {
            SetVisible(visible);
            onVisibilityChanged?.Invoke(this, visible);
        }
    }
コード例 #6
0
 public void UpdateChunk()
 {
     if (mapDataReceived)
     {
         float dist    = Mathf.Sqrt(bounds.SqrDistance(viewerPos));
         bool  visible = dist <= maxViewDist;
         if (visible)
         {
             int lodIndex = 0;
             for (int i = 0; i < detailLevels.Length - 1; i++)
             {
                 if (dist > detailLevels[i].lodThreshold)
                 {
                     lodIndex = i + 1;
                 }
                 else
                 {
                     break;
                 }
             }
             if (lodIndex != prevLODIndex)
             {
                 LODMesh lodMesh = lodMeshes[lodIndex];
                 if (lodMesh.hasMesh)
                 {
                     prevLODIndex    = lodIndex;
                     meshFilter.mesh = lodMesh.mesh;
                 }
                 else if (!lodMesh.hasReqMesh)
                 {
                     lodMesh.RequestMesh(mapData);
                 }
             }
             if (lodIndex == 0)
             {
                 if (collisionLODMesh.hasMesh)
                 {
                     meshCollider.sharedMesh = collisionLODMesh.mesh;
                 }
                 else if (!collisionLODMesh.hasReqMesh)
                 {
                     collisionLODMesh.RequestMesh(mapData);
                 }
             }
             chunksVisited.Add(this);
         }
         SetVisible(visible);
     }
 }
コード例 #7
0
        public void UpdateTerrainChunk()
        {
            if (!_mapDataReceived)
            {
                return;
            }

            float viewerDistanceFromNearestEdge = Mathf.Sqrt(_bounds.SqrDistance(viewerPosition));

            bool isVisible = viewerDistanceFromNearestEdge <= maxViewDistance;

            if (isVisible)
            {
                int levelOfDetailIndex = GetLevelOfDetailIndex(viewerDistanceFromNearestEdge);

                if (levelOfDetailIndex != _previousLevelOfDetailIndex)
                {
                    LODMesh levelOfDetailMesh = _levelOfDetailMeshes[levelOfDetailIndex];
                    if (levelOfDetailMesh.hasMesh)
                    {
                        _previousLevelOfDetailIndex = levelOfDetailIndex;
                        _meshFilter.mesh            = levelOfDetailMesh.mesh;
                    }
                    else if (!levelOfDetailMesh.hasRequestedMesh)
                    {
                        levelOfDetailMesh.RequestMesh(_mapData);
                    }
                }

                if (levelOfDetailIndex == 0)
                {
                    if (_collisionLevelOfDetailMesh.hasMesh)
                    {
                        _meshCollider.sharedMesh = _collisionLevelOfDetailMesh.mesh;
                    }

                    else if (!_collisionLevelOfDetailMesh.hasRequestedMesh)
                    {
                        _collisionLevelOfDetailMesh.RequestMesh(_mapData);
                    }
                }


                _previouslyVisibleTerrainChunks.Add(this);
            }

            SetVisibility(isVisible);
        }
コード例 #8
0
    public void UpdateChunk()
    {
        if (heightMapDataReceived)
        {
            float viewerDistToNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
            bool  wasVisible = IsVisible();
            bool  visible    = viewerDistToNearestEdge <= maxViewDist;

            if (visible)
            {
                int lodDetailIndex = 0;
                for (int i = 0; i < detailLevels.Length - 1; i++) // don't have to check the last one, because visible will be false there
                {
                    if (viewerDistToNearestEdge > detailLevels[i].distanceThreshold)
                    {
                        lodDetailIndex = i + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodDetailIndex != previousLodIndex)
                {
                    LODMesh lod = lodMeshes[lodDetailIndex];
                    if (lod.hasMesh)
                    {
                        previousLodIndex = lodDetailIndex;
                        meshFilter.mesh  = lod.mesh;
                    }
                    else if (!lod.hasRequestedMesh)
                    {
                        lod.RequestMesh(heightMapData, meshSettings);
                    }
                }
            }

            if (wasVisible != visible)
            {
                SetVisible(visible);
                if (OnVisibilityChanged != null)
                {
                    OnVisibilityChanged(this, visible);
                }
            }
        }
    }
コード例 #9
0
    public void UpdateArea()
    {
        if (received)
        {
            float distanceToPlayer = Mathf.Sqrt(bounds.SqrDistance(playerPosition));

            bool wasVisible = IsVisible();
            bool visible    = distanceToPlayer <= viewRange;

            if (visible)
            {
                int lodIndex = 0;

                for (int index = 0; index < lodDetails.Length - 1; index++)
                {
                    if (distanceToPlayer > lodDetails[index].distance)
                    {
                        lodIndex = index + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodIndex != previousLODIndex)
                {
                    LODMesh lodMesh = lodMeshes[lodIndex];

                    if (lodMesh.received)
                    {
                        meshFilter.mesh  = lodMesh.mesh;
                        previousLODIndex = lodIndex;
                    }
                    else if (!lodMesh.requested)
                    {
                        lodMesh.RequestMesh(areaNoise, areaDetails);
                    }
                }
            }

            if (wasVisible != visible)
            {
                SetVisible(visible);
                onVisibilityChange(this, visible);
            }
        }
    }
コード例 #10
0
        protected override void OnUpdateStatus()
        {
            bool visible = TerrianColliderMgr.IsVisibleFormViewer(coord);

            if (visible)
            {
                if (colliderMesh.hasMesh)
                {
                    meshCollider.sharedMesh = colliderMesh.mesh;
                }
                else if (!colliderMesh.hasRequestedMesh)
                {
                    colliderMesh.RequestMesh(mapData);
                }
            }
        }
コード例 #11
0
    public void UpdateTerrainChunk()
    {
        if (m_heightMapReceived)
        {
            float viewerDstFromNearestEdge = Mathf.Sqrt(m_bounds.SqrDistance(viewerPosition));

            bool wasVisible = IsVisible();
            bool visible    = viewerDstFromNearestEdge <= m_maxViewDst;

            if (visible)
            {
                int lodIndex = 0;

                for (int i = 0; i < m_detailLevels.Length - 1; i++)
                {
                    if (viewerDstFromNearestEdge > m_detailLevels [i].visibleDstThreshold)
                    {
                        lodIndex = i + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodIndex != m_previousLODIndex)
                {
                    LODMesh lodMesh = m_lodMeshes [lodIndex];
                    if (lodMesh.hasMesh)
                    {
                        m_previousLODIndex = lodIndex;
                        m_meshFilter.mesh  = lodMesh.mesh;
                    }
                    else if (!lodMesh.hasRequestedMesh)
                    {
                        lodMesh.RequestMesh(m_heightMap, m_meshSettings);
                    }
                }
            }

            if (wasVisible != visible)
            {
                SetVisible(visible);
                onVisibilityChanged?.Invoke(this, visible);
            }
        }
    }
コード例 #12
0
        public void updateTerrainChunk()
        {
            if (MapDataRecieved)
            {
                float viewerDistanceFromNearestEdge = Mathf.Sqrt(terrainBound.SqrDistance(viewerPosition));

                bool visible = viewerDistanceFromNearestEdge <= maxViewDistance;


                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < levelOfDetailInfo.Length - 1; i++)
                    {
                        if (viewerDistanceFromNearestEdge > levelOfDetailInfo[i].visibleDistanceThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != prevLevelOfDetailINdex)
                    {
                        //update which meshData to use.
                        LODMesh _LODMesh = lodMeshes[lodIndex];
                        if (_LODMesh.hasMesh)
                        {
                            terrainMeshFilter.mesh = _LODMesh.meshObject;
                            prevLevelOfDetailINdex = lodIndex;
                        }
                        else if (!_LODMesh.hasRequestedmesh)
                        {
                            _LODMesh.RequestMesh(mapData);
                        }
                    }
                    visibleLastUpdate.Add(this);
                }



                SetVisible(visible);
            }
        }
コード例 #13
0
        // Check visible distance to enable/disable meshObject
        public void UpdateTerrainChunk()
        {
            // Make sure we've received mapData before checking visible LOD distance & set meshObject visible
            if (mapDataReceived)
            {
                float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDstFromNearestEdge <= maxViewDst;

                if (visible)
                {
                    int lodIndex = 0;

                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)   // When viewer distance is greater than the threshold, move to the next level of detail
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Keep track of the previous LOD index
                    // Make sure OnMeshDataReceived is completed (hasMesh == true)
                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this);
                }

                SetVisible(visible);
            }
        }
コード例 #14
0
        public void UpdateTerrainChunk()
        {
            // find the point on perimeter closest to viewers pos
            // find distance between point and viewer
            // if distance is <= maxViewDist -> enable meshObject
            // if distance is > maxViewDist -> disable meshObject

            if (mapDataReceived)
            {
                float viewerDistFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDistFromNearestEdge <= maxViewDist;

                if (visible)
                {
                    int lodIndex = 0;

                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDistFromNearestEdge > detailLevels[i].visibleDistanceThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != prevLODInd)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            meshFilter.mesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.requestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                }

                SetVisible(visible);
            }
        }
コード例 #15
0
        public void UpdateTerrainChunk()
        {
            if (mapDataRecieved)
            {
                float viewerDstfromNrstEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                //Debug.LogError(viewerDstfromNrstEdge);
                bool visible = viewerDstfromNrstEdge <= MaxViewDistance;

                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstfromNrstEdge >= detailLevels[i].visibleDstThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex        = lodIndex;
                            meshFilter.mesh         = lodMesh.mesh;
                            meshCollider.sharedMesh = lodMesh.mesh;
                            //Debug.Log(bounds);
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this);
                }

                SetVisible(visible);
            }
        }
コード例 #16
0
        public void UpdateTerrainChunk()
        {
            if (!m_heightDataReceived)
            {
                return;
            }
            float sqrViewerDstFromNearestEdge = m_bounds.SqrDistance(s_viewerPosition);
            bool  visible = sqrViewerDstFromNearestEdge <= (s_maxViewDst * s_maxViewDst);

            if (!visible)
            {
                s_visibleTerrainChunks.Remove(this);
                s_terrainChunkDictionary.Remove(m_coord);
                Destroy(m_meshObject);
                return;
            }

            int lodIndex = 0;

            for (int i = 0; i < m_detailLevels.Length - 1; i++)
            {
                if (sqrViewerDstFromNearestEdge > m_detailLevels[i].SqrViewDistanceThreshold)
                {
                    lodIndex = i + 1;
                }
                else
                {
                    break;
                }
            }

            if (lodIndex != m_prevLODIndex)
            {
                LODMesh lodMesh = m_lodMeshes[lodIndex];
                if (lodMesh.HasMesh)
                {
                    m_prevLODIndex    = lodIndex;
                    m_meshFilter.mesh = lodMesh.Mesh;
                }
                else if (!lodMesh.HasRequestedMesh)
                {
                    lodMesh.RequestMesh(m_heightMap);
                }
            }
        }
コード例 #17
0
ファイル: TerrainChunk.cs プロジェクト: rivony/synthesis
    //tell the terrain chunk  to update itself: find the point on its perimeters that is the
    //closest to the viewer's position, and find the distance between that point and the viewer
    //if the distance is less than the maximum value distance then make sure that the meshObject is enabled
    //otherwise if that distance exceed the max, then it will disable the mesh object
    public void UpdateTerrainChunk()
    {
        if (!MapDataReceived)
        {
            return;
        }

        Vector2 viewerPosition = EndlessTerrainBehaviour.ViewerPosition;
        float   viewerDistanceFromNearestEdge = Mathf.Sqrt(
            bounds.SqrDistance(viewerPosition));
        bool chunkIsVisible = viewerDistanceFromNearestEdge <= EndlessTerrainBehaviour.MaxViewDist;

        if (chunkIsVisible)
        {
            int lodIndex = 0;
            for (int i = 0; i < DetailsLevels.Length - 1; i++)              //
            {
                if (viewerDistanceFromNearestEdge > DetailsLevels[i].visibleDistanceThreshold)
                {
                    lodIndex = i + 1;
                }
                else
                {
                    break;
                }
            }
            if (lodIndex != previousLodIndex)
            {
                LODMesh lodMesh = LodMeshes [lodIndex];
                if (lodMesh.HasMesh)
                {
                    previousLodIndex = lodIndex;
                    meshFilter.mesh  = lodMesh.MeshToFetch;
                }
                else if (!lodMesh.HasRequestedMesh)
                {
                    lodMesh.RequestMesh(mapData);
                }
            }
            //if it is visible, it will add itself in the chunkList
            EndlessTerrainBehaviour.TerrainChunkVisibleLastUpdate.Add(this);
        }

        SetVisible(chunkIsVisible);
    }
コード例 #18
0
        public void UpdateTerrainChunk()
        {
            if (!mapDataReceived)
            {
                return;
            }

            float viewerDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
            bool  visible = viewerDistanceFromNearestEdge <= maxViewDist;

            if (visible)
            {
                int lodIndex = 0;

                for (int i = 0; i < detailLevels.Length - 1; i++)
                {
                    if (viewerDistanceFromNearestEdge > detailLevels[i].visibleDistanceThreshold)
                    {
                        lodIndex = i + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodIndex != previousLODIndex)
                {
                    LODMesh lodMesh = lodMeshes[lodIndex];
                    if (lodMesh.hasMesh)
                    {
                        previousLODIndex = lodIndex;
                        meshFilter.mesh  = lodMesh.mesh;
                    }
                    else if (!lodMesh.hasRequestedMesh)
                    {
                        lodMesh.RequestMesh(mapData);
                    }
                }

                terrainChunksPreviouslyVisible.Add(this);
            }
            SetVisible(visible);
        }
コード例 #19
0
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float brushDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(brushPos)); //SqrDistance returns the smallest given point between the given point and this bounding box. sqrt reverts sqr
                bool  visible = brushDstFromNearestEdge <= maxViewDist;                   //viewerdistance: when its visible

                //look at the distance of the brush from the nearest edge and compares it to the distance threshold of each of the detaillevels to determine which one to display
                if (visible)
                {
                    int lodIndex = 0;

                    for (int i = 0; i < levelOfDetailsInfo.Length - 1; i++)
                    {
                        if (brushDstFromNearestEdge > levelOfDetailsInfo[i].dstThreshold) //its not needed to look at the last one, because bool visible = false, because viewerDstFromNearestEdge > maxViewDist
                        {
                            lodIndex = i + 1;                                             //lod is the next one
                        }
                        else
                        {
                            break; //if not greater and its the correct one, break out of loop
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];  //the lodMesh to work with
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequMesh) //it lodMesh hat NOT yet requested, it will requested and past in the mapData
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this); //if its visible, it adds it self to the list
                }

                SetVisible(visible);
            }
        }
コード例 #20
0
        /// <summary>
        /// Find the point on the perimeter that is the closest to viewer position, find distance between that point and viewer
        ///   if that distance is less than max view dist, make sure mesh is enabled
        ///   if that distance is greater than max view, disable the mesh
        /// </summary>
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float viewerDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDistanceFromNearestEdge <= maxViewDistance;

                if (visible)
                {
                    int lodIndex = 0;

                    // We don't need to look at the last level of detail because visible won't be true at furthest range
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDistanceFromNearestEdge > detailLevels[i].lodRange)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            // Change the mesh to be of different quality
                            meshFilter.mesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunksVisibleLastUpdate.Add(this);
                }

                SetVisible(visible);
            }
        }
コード例 #21
0
ファイル: EndlessTerrain.cs プロジェクト: nmauthes/neuroflite
        // Enables mesh object if dist between viewer and chunk is less than max view dist
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)              // Don't update if map data hasn't been received
            {
                float viewDistFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewDistFromNearestEdge <= maxViewDist;
                SetVisible(visible);

                if (visible)
                {
                    int lodIndex = 0;

                    // Look at the terrain chunk and choose LOD based on dist from viewer
                    for (int i = 0; i < detailLevels.Length - 1; i++)                       // Don't have to look at last index because visible is false
                    {
                        if (viewDistFromNearestEdge > detailLevels [i].visibleDistThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes [lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this);                      // Adds self to list
                }
            }
        }
コード例 #22
0
        /*Ya no es necesario void OnMeshDataReceived(MeshData meshdata){
         *  meshFilter.mesh = meshdata.CrearMesh();
         * }*/

        //Comprobacion para ver si el chunk es visible o no
        public void UpdateTerrainChunk()
        {
            //Solo hace falta si recivimos el mapData
            if (mapDataReceived)
            {
                //Distancia entre el punto dado y esta bounding box
                float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDstFromNearestEdge <= maxViewDistance;

                if (visible)
                {
                    int lodIndex = 0;
                    //Conseguimos el nivel de detalle de este chunk
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].visibleDistanceThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    //Si el chunk es visible, lo metemos en la lista de chunks visibles que recorremos para ver cuales hay que dejar de renderizar
                    terrainChunksVisibleLastUpdate.Add(this);
                }
                SetVisible(visible);
            }
        }
コード例 #23
0
        public void UpdateTerrainChunk()
        {
            if (this.mapDataReceived)
            {
                // See if player is within render range, if not, deactivate this meshObject
                // oh cool, nested classes can use its parent classes' stuff
                float viewerDistFromNearestEdge = Mathf.Sqrt(this.bounds.SqrDistance(viewerPos));
                bool  visible = viewerDistFromNearestEdge <= maxViewDist;

                if (visible)
                {
                    int lodIndex = 0;
                    for (int c = 0; c < this.detailLevels.Length - 1; c++)
                    {
                        if (viewerDistFromNearestEdge > this.detailLevels[c].visibleDistThreshold)
                        {
                            lodIndex = c + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodIndex != this.previousLODIndex)
                    {
                        LODMesh lodMesh = this.lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            this.previousLODIndex = lodIndex;
                            this.meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(this.mapData);
                        }
                    }

                    terrainChunksVisibleLastUpdate.Add(this);
                }

                this.SetVisible(visible);
            }
        }
コード例 #24
0
            void SwapMesh(int lodIndex)
            {
                if (previousLODIndex == lodIndex)
                {
                    return;
                }

                LODMesh lodMesh = lodMeshes[lodIndex];

                if (lodMesh.hasMesh)
                {
                    previousLODIndex = lodIndex;
                    meshFilter.mesh  = lodMesh.mesh;
                }
                else if (!lodMesh.hasRequested)
                {
                    lodMesh.RequestMesh(mapData);
                }
            }
コード例 #25
0
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  visible = viewerDstFromNearestEdge <= maxViewDst;


                if (visible)
                {
                    int lodIndex = 0;
                    // we do not need to look at the last one becuse then the bool "visable" will be false
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].visableDstThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex        = lodIndex;
                            meshFilter.mesh         = lodMesh.mesh;
                            meshCollider.sharedMesh = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequstedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunksVisibleLastUpdate.Add(this);
                }
                SetVisible(visible);
            }
        }
コード例 #26
0
        //überprüft ob der Punkt innehralb des aktuellen Chunks der am nähesten zum Spieler liegt innerhalb der maximalen View Distance liegt. Tut er das wird der Chunk angezeigt. Tut er das nicht ausgeblendet.
        public void UpdateTerrainChunk()
        {
            if (mapDataRecieved)
            { //nur wenn die MapData bereits vorhanden ist macht es Sinn diesen teil auszuführen. vorher können keine Meshes generiert werden.
                float viewerDstFromNearestEdge = bounds.SqrDistance(viewerPosition);
                bool  visible = viewerDstFromNearestEdge <= maxViewDst * maxViewDst;

                //setzt den LOD der Meshes auf basis der Distanz zum Spieler und die eingestellten LOD im Editor;
                if (visible)
                {
                    int lodindex = 0;//gibt an welcher LOD aufgrund der aktuellen Distanz zum Spieler dargestellt werden soll.
                    for (int i = 0; i < detailLevels.Length - 1; i++)
                    {
                        if (viewerDstFromNearestEdge > detailLevels[i].distanceToNextLOD * detailLevels[i].distanceToNextLOD)
                        {
                            lodindex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodindex != prevLOD)
                    {
                        LODMesh lodMesh = lodMeshes[lodindex]; // wenn die beiden Werte nicht überinstimmen ist das neue Mesh das lvlOfDetailMesh mit dem aktuell benötigten LOD;
                        if (lodMesh.hasMesh)
                        {
                            prevLOD         = lodindex;
                            meshFilter.mesh = lodMesh.mesh; //das "alte" Mesh wird mit dem neuen Mesh mit richtigen LOD ersetzt.
                        }
                        else if (!lodMesh.hasRequestedMesh) //wenn noch kein Mesh requested also generiert wurde wird heir ein neues erzeugt.
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunksVisibleLastUpdate.Add(this); //fügt sich selbst der liste hinzu um später ausgeblendet zu werden wenn der viewer nicht mehr in Reichweite ist.
                }

                SetVisible(visible);
            }
        }
コード例 #27
0
        public void UpdateTerrianChunk()
        {
            if (mapDataRecieved)
            {
            }
            float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
            bool  visible = viewerDstFromNearestEdge <= maxViewDistance;

            if (visible)
            {
                int lodindex = 0;

                for (int i = 0; i < detailLevels.Length - 1; i++)
                {
                    if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
                    {
                        lodindex = i + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (lodindex != previousLODindex)
                {
                    LODMesh lodMesh = lODMeshes[lodindex];
                    if (lodMesh.hasMesh)
                    {
                        previousLODindex = lodindex;
                        meshFilter.mesh  = lodMesh.mesh;
                    }
                    else if (!lodMesh.hasRequestedMesh)
                    {
                        lodMesh.RequestMesh(mapData);
                    }
                }
            }

            SetVisible(visible);
        }
コード例 #28
0
        public void UpdateTerrainChunk()
        {
            if (mapDataRecived)
            {
                float viewerDstDrontNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
                bool  isVisible = viewerDstDrontNearestEdge <= maxViewDst;

                if (isVisible)
                {
                    int lodIndex = 0;

                    for (int i = 0; i < detailsLevel.Length - 1; i++)
                    {
                        if (viewerDstDrontNearestEdge > detailsLevel[i].visibleDistThreshHold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshs[lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunkVisibleLastUpdate.Add(this);
                }

                SetVisible(isVisible);
            }
        }
コード例 #29
0
        public void UpdateTerrainChunk()
        {
            if (mapDataReceived)
            {
                float viewerDistFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPos));
                bool  visible = viewerDistFromNearestEdge <= maxViewDist;

                if (visible)
                {
                    int lodIndex = 0;
                    for (int i = 0; i < detailLevels.Length - 1; i++)                       // dont have to look at last one, because bool visible would then be false
                    {
                        if (viewerDistFromNearestEdge > detailLevels [i].visibleDistThreshold)
                        {
                            lodIndex = i + 1;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (lodIndex != previousLODIndex)
                    {
                        LODMesh lodMesh = lodMeshes [lodIndex];
                        if (lodMesh.hasMesh)
                        {
                            previousLODIndex = lodIndex;
                            meshFilter.mesh  = lodMesh.mesh;
                        }
                        else if (!lodMesh.hasRequestedMesh)
                        {
                            lodMesh.RequestMesh(mapData);
                        }
                    }
                    terrainChunksVisibleLastUpdate.Add(this);
                }
                SetVisible(visible);
            }
        }
コード例 #30
0
    public void UpdateChunk()
    {
        if (!heightMapReceived)
        {
            return;
        }
        float viewDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
        bool  wasVisible = IsVisible();
        bool  visible    = viewDistanceFromNearestEdge <= maxViewDistance;

        if (visible)
        {
            int terrainLODIndex = 0;
            for (; terrainLODIndex < detailLevels.Length - 1; terrainLODIndex++)
            {
                if (detailLevels[terrainLODIndex].visibleDistanceThreshold >= viewDistanceFromNearestEdge)
                {
                    break;
                }
            }
            if (terrainLODIndex != previousLODIndex)
            {
                LODMesh lodMesh = lodMeshes[terrainLODIndex];
                if (lodMesh.hasMesh)
                {
                    previousLODIndex = terrainLODIndex;
                    meshFilter.mesh  = lodMesh.mesh;
                }
                else if (!lodMesh.hasRequestedMesh)
                {
                    lodMesh.RequestMesh(heightMap, meshSettings);
                }
            }
        }
        if (wasVisible != visible)
        {
            SetVisible(visible);
            OnVisibilityChanged?.Invoke(this, visible);
        }
    }