Esempio n. 1
0
        public void UpdateTerrainChunk()
        {
            if (!_heightMapReceived)
            {
                return;
            }

            var viewerDistanceFromNearestEdge = Mathf.Sqrt(_bounds.SqrDistance(viewerPosition));
            var wasVisible = IsVisible();
            var visible    = viewerDistanceFromNearestEdge <= _maxViewDistance;

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

                if (lodIndex != _prevLODIndex)
                {
                    var lodMesh = _lodMeshes[lodIndex];
                    if (lodMesh.hasMesh)
                    {
                        _prevLODIndex    = lodIndex;
                        _meshFilter.mesh = lodMesh.mesh;

                        if (!_droidsRequested)
                        {
                            CreateInitialDroids(lodMesh.meshVertices);
                        }
                        if (!_meshDatSentForTower)
                        {
                            BuildingAndTowerSpawner.instance
                            .AddTerrainData(lodMesh.meshVertices, _sampleCenter, _meshObject.transform, _bounds);
                            _meshDatSentForTower = true;
                        }
                    }
                    else if (!lodMesh.hasRequestedMesh)
                    {
                        lodMesh.RequestMesh(_heightMap, _meshSettings);
                    }

                    if (lodIndex == 0 && lodMesh.hasMesh)
                    {
                        LOD0ValidStateAvailable(lodMesh);
                    }
                    else if (lodIndex != 0)
                    {
                        _chunkTrees.ClearTrees();
                    }
                }
            }

            if (wasVisible != visible)
            {
                SetVisible(visible);
                OnVisibilityChanged?.Invoke(this, visible);
            }
        }
Esempio n. 2
0
    public void UpdateTerrainChunk()
    {
        if (!_heightMapReceived)
        {
            if (!_heightMapRequested)
            {
                Load();
            }
            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 != _prevLODIndex)
            {
                LODMesh lodMesh = _lodMeshes[lodIndex];
                if (lodMesh.hasMesh)
                {
                    _prevLODIndex    = lodIndex;
                    _meshFilter.mesh = lodMesh.mesh;
                }
                else if (!lodMesh.hasRequestedMesh)
                {
                    lodMesh.RequestMesh(_heightMap, _meshSettings);
                }

                if (lodIndex == 0 && lodMesh.hasMesh)
                {
                    if (!_chunkTrees.hasRequestedTreePoints)
                    {
                        _chunkTrees.RequestTreePoints(
                            lodMesh.meshVertices,
                            _meshSettings.chunkSizeIndex
                            );
                    }
                    else if (!_chunkTrees.hasPlacedTrees && _chunkTrees.hasReceivedTreePoints)
                    {
                        _chunkTrees.PlaceTreesOnPoints();
                    }
                }
                else if (lodIndex != 0)
                {
                    _chunkTrees.ClearTrees();
                }
            }
        }
        if (wasVisible != visible)
        {
            SetVisible(visible);
            onVisibilityChanged?.Invoke(this, visible);
        }
    }