Ejemplo n.º 1
0
        public WorldCompositionManager(IStreamingResourceHandler streamingManager, WorldCompositionParam param)
        {
            _streamingManager = streamingManager;
            _param            = param;

            _sceneNameToScene   = new Dictionary <string, SceneNode>();
            _sceneLoadingStatus = new SceneNode[_param.TerrainDimension, _param.TerrainDimension];

            for (int i = 0; i < _param.TerrainDimension; i++)
            {
                for (int j = 0; j < param.TerrainDimension; j++)
                {
                    var node = new SceneNode
                    {
                        Center    = new Vector2(i + 0.5f, j + 0.5f),
                        Status    = SceneLoadingStatus.Unloaded,
                        SceneName = string.Format(_param.TerrainNamePattern, i, j)
                    };
                    node.Dimension = new Vector4(
                        node.Center.x * _param.TerrainSize + _param.TerrainMin.x,
                        _param.TerrainSize * 0.5f,
                        node.Center.y * _param.TerrainSize + _param.TerrainMin.z,
                        _param.TerrainSize);

                    _sceneLoadingStatus[i, j] = node;
                    _sceneNameToScene.Add(node.SceneName, node);
                }
            }

            // outside parameter measured in grid distance between player and border
            // since center is used, extra 0.5 need to be added
            _param.LoadRadiusInGrid   += 0.5f;
            _param.UnloadRadiusInGrid += 0.5f;
        }
Ejemplo n.º 2
0
 public void SetHandlerActivation(int index, bool value, IStreamingResourceHandler handler)
 {
     if (Status == StreamingGoStatus.Loaded)
     {
         var itor = Handlers[index];
         while (itor != null)
         {
             itor.StateChanged(value);
             itor = itor.Sibling;
         }
     }
 }
Ejemplo n.º 3
0
        public bool SetGoActivatiton(DistCullingCat cat, bool isActivated, IStreamingResourceHandler handler)
        {
            bool ret = false;

            if (cat == Cat)
            {
                switch (Status)
                {
                case StreamingGoStatus.NotLoaded:
                    if (isActivated)
                    {
                        handler.LoadGo(Key.SceneIndex, Key.GoIndex);
                        Status = StreamingGoStatus.Loading;
                    }

                    break;

                case StreamingGoStatus.Loading:
                    if (!isActivated)
                    {
                        Status = StreamingGoStatus.NotLoaded;
                    }

                    break;

                case StreamingGoStatus.Loaded:
                    if (!isActivated)
                    {
                        handler.UnloadGo(Go, Key.SceneIndex);

                        Status = StreamingGoStatus.NotLoaded;
                        RemoveHandlers();
                        Go  = null;
                        ret = true;
                    }

                    break;
                }
            }

            return(ret);
        }
Ejemplo n.º 4
0
        public void Delete(IStreamingResourceHandler handler)
        {
            var count = _streamingGoes.Count;

            for (int i = 0; i < count; i++)
            {
                var streamingGo = _streamingGoes[i];
                if (streamingGo.Go != null)
                {
                    handler.UnloadGo(streamingGo.Go, streamingGo.Key.SceneIndex);
                }
            }

            count = _children.Length;
            for (int i = 0; i < count; i++)
            {
                if (_children[i] != null)
                {
                    _children[i].Delete(handler);
                }
            }
        }
Ejemplo n.º 5
0
        public void AssignGo(UnityObject unityObj, CullingHandler handler, StreamingGoKey key,
                             Vector3 position, Vector3 size, IStreamingResourceHandler resHandler)
        {
            if (_height > 0 && FitInOneChild(size))
            {
                var index = GetChildIndex(position);
                _children[index].AssignGo(unityObj, handler, key, position, size, resHandler);
            }
            else
            {
                var streamingGo = _streamingGoMap[key];
                if (streamingGo.SetGo(unityObj))
                {
                    while (handler != null)
                    {
                        handler.StateChanged(_cullingState[(int)handler.Category] != CullState.Culled);
                        var sibling = handler.Sibling;
                        handler.Sibling = null;

                        streamingGo.AddHandler(handler);
                        ChangeCullingCount(handler.Category, 1);

                        handler = sibling;
                    }
                }
                else
                {
                    resHandler.UnloadGo(unityObj, key.SceneIndex);
                    while (handler != null)
                    {
                        var sibling = handler.Sibling;
                        handler.Free();
                        handler = sibling;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void SetActivation(int index, bool value, IStreamingResourceHandler handler)
        {
            if (_cullingCount[index] != 0)
            {
                var count = _streamingGoes.Count;
                for (int i = 0; i < count; i++)
                {
                    if (_streamingGoes[i].SetGoActivatiton((DistCullingCat)index, value, handler))
                    {
                        for (int j = 0; j < Constants.DistCullingCatCount; j++)
                        {
                            var itor = _streamingGoes[i].Handlers[j];
                            while (itor != null)
                            {
                                ChangeCullingCount((DistCullingCat)j, -1);
                                itor = itor.Sibling;
                            }
                        }
                    }

                    _streamingGoes[i].SetHandlerActivation(index, value, handler);
                }
            }
        }
Ejemplo n.º 7
0
        private bool DistanceCull(float nearestDistSquare, float farthestDistSquare, IStreamingResourceHandler handler)
        {
            bool allowExitAhead = true;

            // camera in this cell
            if (nearestDistSquare < 0)
            {
                for (int i = 0; i < Constants.DistCullingCatCount; i++)
                {
                    if (_cullingState[i] != CullState.NotCulled)
                    {
                        _cullingState[i] = CullState.NotCulled;

                        SetActivation(i, true, handler);
                    }
                }

                allowExitAhead = false;
            }
            else
            {
                // 分若干种距离判断是否需要提前退出
                for (int i = 0; i < Constants.DistCullingCatCount; i++)
                {
                    if (_cullingCountOverall[i] != 0)
                    {
                        var standard     = Constants.DistCullingCats[i].Dist;
                        var cullStandard = _cullingState[i] == CullState.NotSure
                            ? Constants.DistCullingCats[i].RedundantDist
                            : standard;

                        CullState state = CullState.NotSure;
                        if (nearestDistSquare >= cullStandard)
                        {
                            state = CullState.Culled;
                        }
                        else if (farthestDistSquare < standard)
                        {
                            state = CullState.NotCulled;
                        }

                        // 结果为剔除,上次也为剔除,提前退出
                        // 结果为必可见,上次也为必可见,提前退出
                        if (_cullingState[i] != state || state == CullState.NotSure)
                        {
                            allowExitAhead = false;
                        }

                        // 状态变化时,处理可见性
                        if (_cullingState[i] != state)
                        {
                            _cullingState[i] = state;

                            SetActivation(i, _cullingState[i] != CullState.Culled, handler);
                        }
                    }
                }
            }

            return(allowExitAhead);
        }
Ejemplo n.º 8
0
        public void OnCameraMovement(Vector3 cameraPos, float scale, IStreamingResourceHandler handler)
        {
            var index = -1;

            _recursiveStack[++index] = this;

            while (index >= 0)
            {
                var node = _recursiveStack[index];
                node._childTraverseIndex = 0;

                float nearestDist  = -1f;
                float farthestDist = -1f;

                var toCenterX = cameraPos.x - node._center.x;
                var toCenterY = cameraPos.y - node._center.y;
                var toCenterZ = cameraPos.z - node._center.z;

                if (Math.Abs(toCenterX) > node._extremeDir[7].x || Math.Abs(toCenterY) > node._extremeDir[7].y ||
                    Math.Abs(toCenterZ) > node._extremeDir[7].z)
                {
                    var childIndex       = node.GetChildIndex(cameraPos);
                    var remoteChildIndex = node.GetRemoteChildIndex(cameraPos);

                    var distX = toCenterX - node._extremeDir[childIndex].x;
                    var distY = toCenterY - node._extremeDir[childIndex].y;
                    var distZ = toCenterZ - node._extremeDir[childIndex].z;

                    var scaleSquare = scale * scale;

                    nearestDist = (distX * distX + distY * distY + distZ * distZ) * scaleSquare;

                    distX = toCenterX - node._extremeDir[remoteChildIndex].x;
                    distY = toCenterY - node._extremeDir[remoteChildIndex].y;
                    distZ = toCenterZ - node._extremeDir[remoteChildIndex].z;

                    farthestDist = (distX * distX + distY * distY + distZ * distZ) * scaleSquare;
                }

                if (node.DistanceCull(nearestDist, farthestDist, handler))
                {
                    node._childTraverseIndex = node._children.Length;
                }

                bool findNotTraversedNode = false;

                while (!findNotTraversedNode)
                {
                    while (node._childTraverseIndex < node._children.Length)
                    {
                        if (node._children[node._childTraverseIndex] != null)
                        {
                            _recursiveStack[++index] = node._children[node._childTraverseIndex];
                            ++node._childTraverseIndex;
                            findNotTraversedNode = true;
                            break;
                        }

                        ++node._childTraverseIndex;
                    }

                    if (!findNotTraversedNode)
                    {
                        if (index == 0)
                        {
                            return;
                        }

                        node = _recursiveStack[--index];
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public void SetResourceHandler(IStreamingResourceHandler handler)
 {
     _streamingResourceHandler = handler;
 }