Exemplo n.º 1
0
        public StreamData Process(StreamMode mode, StreamData data)
        {
            try
            {
                OCProfiler.Start();
                if (mode == StreamMode.Compress)
                {
                    return(Compress(data));
                }
                else
                {
                    return(Decompress(data));
                }
            }
            finally
            {
                var elasped = (int)OCProfiler.Stop();

                if (mode == StreamMode.Compress)
                {
                    _stats.CompressTime += elasped;
                }
                else
                {
                    _stats.DecompressTime += elasped;
                }
            }
        }
Exemplo n.º 2
0
        public bool GetRenderableModels(string progressTitle, Func <string, string, float, bool> progress = null)
        {
            bool cancelled = false;

            if (_curBakeCell >= cellList.Count)
            {
                return(false);
            }

            int count = 0;

            //for (int i = cellList.Count-1; i>= 0; i--)
            for (int i = _curBakeCell; i < cellList.Count; i++)
            {
                if (Config.ComputePerframe)
                {
                    if (count > Config.PerframeExecCount)
                    {
                        Debug.Log("batch mode Scene name:" + owner.Name + " current bake Cell count:" + _curBakeCell);
                        cancelled = true;
                        break;
                    }
                }



                Cell cell = cellList[i];

                OCProfiler.Start();
                cell.GetRenderableModels();
                var oneCellTime = OCProfiler.Stop();

                if (progress != null)
                {
                    if (progress(progressTitle, string.Format("Cell {0}/{1} 正在生成PVS数据, OneCellTime:{2}", i, cellList.Count, oneCellTime), ((float)i) / cellList.Count))
                    {
                        cancelled      = true;
                        owner.IsFinish = true;
                        break;
                    }
                }

                _curBakeCell++;
                count++;
            }

            return(!cancelled);
        }
Exemplo n.º 3
0
 // Update is called once per frame
 void Update()
 {
     if (OC)
     {
         OCProfiler.Start();
         for (int i = 0; i < 1; i++)
         {
             streamScene.DoCulling(Camera.main.transform.position);
         }
         var time = OCProfiler.Stop();
         //Debug.Log(time);
     }
     else
     {
         streamScene.UndoCulling();
     }
 }
Exemplo n.º 4
0
        // Fractions of PrecomputedVisibilitySettings.PlayAreaHeight to guarantee have cell coverage
        //private static readonly float[] _testHeights = new float[]{ .4f, .6f, .8f };
        public IList <VolumeCell> ComputeVolumeCells(IEnumerable <Collider> allColliders, Func <string, string, float, bool> progress = null)
        {
            if (_volumes.GetRasterBoundCount() <= 0)
            {
                return(null);
            }

            GC.Collect();

            _stat.Reset();
            OCProfiler.Start();
            var startMemory = GC.GetTotalMemory(false);

            var allMeshes = new LinkedList <IRasterMesh>();

            foreach (var collider in allColliders)
            {
                allMeshes.AddLast(RasterMeshFactory.CreateRasterMesh(collider));
            }

            var bounds          = _volumes.GetVolumeBounds(_settings.CellSize);
            var boundsCenter    = bounds.Center;
            var boundHalfExtent = bounds.HalfExtent;
            var volumeSizes     = bounds.HalfExtent * 2.0f / _settings.CellSize;
            int sizeX           = (int)(volumeSizes.x + DELTA) + 1;
            int sizeY           = (int)(volumeSizes.z + DELTA) + 1;


            var heightsMap   = new CellToHeightsMap(sizeX, sizeY);
            var rasterPolicy = new CellPlacementRasterPolicy(_settings.CellSize, heightsMap, _volumes);
            TriangleRasterizer rasterizer = new TriangleRasterizer(rasterPolicy);

            bool cancelled = false;

            OCProfiler.Start();
            long nextTriangleIndex = 1;
            // Rasterize the scene to determine potential cell heights
            int meshCount = 0;

            Debug.LogFormat("Rasterize Mesh Total Mesh {0}", meshCount);
            foreach (var mesh in allMeshes)
            {
                meshCount++;
                if (!Config.IsBatchMode && progress != null)
                {
                    if (progress("体素化", String.Format("当前正在处理Mesh {0}/{1} ...", meshCount, allMeshes.Count),
                                 ((float)meshCount) / allMeshes.Count))
                    {
                        cancelled = true;
                        break;
                    }
                }

                var meshBounds = mesh.WorldBounds;
                // Only process meshes whose bounding box intersects a PVS volume
                if (_volumes.Intersect(meshBounds))
                {
                    var triCount = mesh.TriangleCount;

                    for (int triIndex = 0; triIndex < triCount; ++triIndex)
                    {
                        mesh.GetTriangle(triIndex, _triVertices);

                        var normal = TriangleNormal(_triVertices[0], _triVertices[1], _triVertices[2]);

                        const float EdgePullback = .1f;
                        // Only rasterize upward facing triangles
                        if (normal.y > 0.0f)
                        {
                            for (int vertIndex = 0; vertIndex < 3; vertIndex++)
                            {
                                // Transform world space positions from [PrecomputedVisibilityBounds.Origin - PrecomputedVisibilityBounds.BoxExtent, PrecomputedVisibilityBounds.Origin + PrecomputedVisibilityBounds.BoxExtent] into [0,1]
                                Vector3 transformedPosition;
                                var     v = _triVertices[vertIndex];
                                transformedPosition.x =
                                    (v.x - boundsCenter.x + boundHalfExtent.x) / (2.0f * boundHalfExtent.x);
                                //transformedPosition.y =
                                //   (v.y - boundsCenter.y + boundHalfExtent.y) / (2.0f * boundHalfExtent.y);
                                transformedPosition.z =
                                    (v.z - boundsCenter.z + boundHalfExtent.z) / (2.0f * boundHalfExtent.z);

                                // Project positions onto the XY plane
                                _XYPositions[vertIndex] = new Vector2(transformedPosition.x * (sizeX - 1), transformedPosition.z * (sizeY - 1));
                            }

                            rasterizer.TriangleIndex = nextTriangleIndex;

                            for (int sampleIndex = 0; sampleIndex < 9; sampleIndex++)
                            {
                                var samplePosition = RasterVectorUtils.Add(RasterVectorUtils.Scale(_subsamplePositions[sampleIndex], (1 - 2 * EdgePullback)), new Vector2(EdgePullback, EdgePullback));

                                rasterizer.DrawTriangle(
                                    _triVertices[0],
                                    _triVertices[1],
                                    _triVertices[2],
                                    RasterVectorUtils.Substract(_XYPositions[0], samplePosition),
                                    RasterVectorUtils.Substract(_XYPositions[1], samplePosition),
                                    RasterVectorUtils.Substract(_XYPositions[2], samplePosition)
                                    );
                            }

                            nextTriangleIndex++;
                        }
                    }
                }
            }
            _stat.RasterizeTime = OCProfiler.Stop();

            if (cancelled)
            {
                return(null);
            }

            var cells = new List <VolumeCell>(sizeX * sizeY * 2);
            var placedHeightRanges = new List <Vector2>();

            Debug.LogFormat("Calculate Cell Total Cells {0}", sizeX * sizeY);
            for (int y = 0; y < sizeY; ++y)
            {
                if (cancelled)
                {
                    break;
                }

                for (int x = 0; x < sizeX; ++x)
                {
                    if (!Config.IsBatchMode && progress != null)
                    {
                        if (progress("计算Cell区域",
                                     String.Format("当前正在处理Cell {0}/{1} ...", x + y * sizeX + 1, sizeX * sizeY),
                                     ((float)(x + y * sizeX + 1)) / (sizeX * sizeY)))
                        {
                            cancelled = true;
                            break;
                        }
                    }

                    var cell            = heightsMap[x, y];
                    var currentPosition = cell.Position;

                    var   sortedHitTriangles = cell.HitTriangles.OrderByDescending(span => - span.Range.y).ToList();
                    float lastSampleHeight   = float.NegativeInfinity;
                    placedHeightRanges.Clear();

                    OCProfiler.Start();
                    int count = sortedHitTriangles.Count();
                    // Pass 1 - only place cells in the largest holes which are most likely to be where the play area is
                    // Place the bottom slightly above the surface, since cells that clip through the floor often have poor occlusion culling
                    for (int heightIndex = 0; heightIndex < count; heightIndex++)
                    {
                        float currentMaxHeight = sortedHitTriangles[heightIndex].Range.y;

                        // Place a new cell if this is the highest height
                        if (heightIndex + 1 == count
                            // Or if there's a gap above this height of size MinPlayAreaHeight
                            || ((sortedHitTriangles[heightIndex + 1].Range.y - currentMaxHeight) > _settings.MinPlayAreaHeight
                                // And this height is not within a cell that was just placed
                                && currentMaxHeight - lastSampleHeight > _settings.MinPlayAreaHeight))
                        {
                            var boundsMin = new Vector3(
                                currentPosition.x - _settings.CellSize * 0.5f,
                                currentMaxHeight,
                                currentPosition.y - _settings.CellSize * 0.5f);

                            var boundsMax = new Vector3(currentPosition.x + _settings.CellSize * 0.5f,
                                                        currentMaxHeight + _settings.MaxPlayAreaHeight,
                                                        currentPosition.y + _settings.CellSize * 0.5f);

                            cells.Add(new VolumeCell(boundsMin, boundsMax));
                            lastSampleHeight = currentMaxHeight;
                            placedHeightRanges.Add(new Vector2(boundsMin.y, boundsMax.y));
                        }
                    }

                    _stat.Pass1Time += OCProfiler.Stop();

                    OCProfiler.Start();
                    // Pass 2 - make sure the space above every triangle is covered by precomputed visibility cells, even if the cells are placed poorly (intersecting the floor)

                    /*for (int heightIndex = 0; heightIndex < count - 1; heightIndex++)
                     * {
                     *  for (int extremaIndex = 0; extremaIndex < 2; extremaIndex++)
                     *  {
                     *      var currentMaxHeight = extremaIndex == 0 ? sortedHitTriangles[heightIndex].Range.x : sortedHitTriangles[heightIndex].Range.y;
                     *      var compareHeight = currentMaxHeight + .5f * _settings.MaxPlayAreaHeight;
                     *
                     *      for (int testIndex = 0; testIndex < 3; testIndex++)
                     *      {
                     *          var testHeight = currentMaxHeight + _testHeights[testIndex] * _settings.MaxPlayAreaHeight;
                     *
                     *          int closestCellInZIndex = -1;
                     *          float closestCellInZDistance = float.MaxValue;
                     *          bool bInsideCell = false;
                     *
                     *          for (int placedHeightIndex = 0; placedHeightIndex < placedHeightRanges.Count; placedHeightIndex++)
                     *          {
                     *              var cellHeightRange = placedHeightRanges[placedHeightIndex];
                     *
                     *              if (testHeight > cellHeightRange.x && testHeight < cellHeightRange.y)
                     *              {
                     *                  bInsideCell = true;
                     *                  break;
                     *              }
                     *
                     *              float absDistance = Mathf.Min(Mathf.Abs(compareHeight - cellHeightRange.x), Mathf.Abs(compareHeight - cellHeightRange.y));
                     *
                     *              if (absDistance < closestCellInZDistance)
                     *              {
                     *                  closestCellInZDistance = absDistance;
                     *                  closestCellInZIndex = placedHeightIndex;
                     *              }
                     *          }
                     *
                     *          // Place a cell if TestHeight was not inside any existing cells
                     *          if (!bInsideCell)
                     *          {
                     *              float desiredCellBottom = currentMaxHeight;
                     *
                     *              if (closestCellInZIndex >= 0)
                     *              {
                     *                  var nearestCellHeightRange = placedHeightRanges[closestCellInZIndex];
                     *                  var nearestCellCompareHeight = (nearestCellHeightRange.x + nearestCellHeightRange.y) / 2;
                     *
                     *                  // Move the bottom of the cell to be placed such that it doesn't overlap the nearest cell
                     *                  // This makes use of the cell's full height to cover space
                     *                  if (compareHeight < nearestCellCompareHeight)
                     *                  {
                     *                      desiredCellBottom = Mathf.Min(desiredCellBottom, nearestCellHeightRange.x - _settings.MaxPlayAreaHeight);
                     *                  }
                     *                  else if (compareHeight > nearestCellCompareHeight)
                     *                  {
                     *                      desiredCellBottom = Mathf.Max(desiredCellBottom, nearestCellHeightRange.y);
                     *                  }
                     *              }
                     *
                     *              var boundsMin = new Vector3(
                     *                  currentPosition.x - _settings.CellSize * 0.5f,
                     *                  desiredCellBottom,
                     *                  currentPosition.y - _settings.CellSize * 0.5f);
                     *              var boundsMax = new Vector3(
                     *                  currentPosition.x + _settings.CellSize * 0.5f,
                     *                  desiredCellBottom + _settings.MaxPlayAreaHeight,
                     *                  currentPosition.y + _settings.CellSize * 0.5f);
                     *
                     *              cells.Add(new VolumeCell(boundsMin, boundsMax));
                     *              placedHeightRanges.Add(new Vector2(boundsMin.y, boundsMax.y));
                     *          }
                     *      }
                     *  }
                     * }*/

                    _stat.Pass2Time += OCProfiler.Stop();
                }
            }
            _stat.TotalTime = OCProfiler.Stop();

            if (cancelled)
            {
                return(null);
            }

            GC.Collect();
            _stat.TotalMemory = GC.GetTotalMemory(false) - startMemory;
            return(cells);
        }
Exemplo n.º 5
0
        private void RotateCameraDir(Vector3 forward, HashSet <RenderableObj> ret)
        {
            Camera.main.transform.forward = forward.normalized;

            OCProfiler.Start();

            HashSet <MeshRenderer> visList = null;

            if (Config.SoftRenderer)
            {
                if (owner.owner.Owner == null)
                {
                    var renders = owner.owner.treeMesh.GetWithinFrustum(Camera.main);
                    visList = owner.owner._renderer.GetVisibleModels(renders);
                }
                else
                {
                    var renders = owner.owner.Owner.treeMesh.GetWithinFrustum(Camera.main);
                    visList = owner.owner._renderer.GetVisibleModels(renders);
                }
            }
            else
            {
                visList = owner.owner._renderer.GetVisibleModels();
            }
            var calcVisTime = OCProfiler.Stop();

            OCProfiler.Start();
            foreach (var mr in visList)
            {
                RenderableObj renderObj = null;
                if (owner.owner.Owner == null)
                {
                    renderObj = owner.owner.GetRenderableObjectByMeshRenderer(mr);
                }
                else
                {
                    MultiScene world = owner.owner.Owner as MultiScene;
                    renderObj = world.GetRenderableObjectByMeshRenderer(mr);
                }

                if (renderObj == null)
                {
                    string log = string.Format("batch mode renderObj name:{0}, scene name: {1} is null!", mr.gameObject.name, mr.gameObject.scene.name);
                    Debug.LogError(log);
                    continue;
                }

                if (!visibleModelList.Contains(renderObj))
                {
                    visibleModelList.Add(renderObj);
                }

                if (ret != null && !ret.Contains(renderObj))
                {
                    ret.Add(renderObj);
                }
            }

            var updateVisTime = OCProfiler.Stop();
        }