Example #1
0
        private void CollectCollidersCPU(Collector3.ShapeCollector collector)
        {
            if (profiler != null)
            {
                profiler.AddLog("Start shape collecting by CPU");
            }
            //Debuger_K.ClearLabels();

            //Debug.Log(shapeDataGenericCPU.Count);
            //int c = 0;
            foreach (var shape in shapeDataGenericCPU)
            {
                collector.Append(shape);

                //Debuger_K.AddLabel(shape.bounds.center, c++);
                //try {
                //    collector.Append(shape);
                //}
                //catch (System.Exception) {
                //    Debuger_K.AddLine(shape.bounds.center, shape.bounds.center + Vector3.up, Color.red);
                //    Debug.LogError(c + " : " + shape.name);
                //    throw;
                //}

                if (profiler != null)
                {
                    profiler.AddLogFormat("Collected {0} of type {1}", shape.name, shape.GetType().Name);
                }
            }

            if (profiler != null)
            {
                profiler.AddLog("Finish shape collecting by CPU");
            }
        }
Example #2
0
        private void CollectTerrainCPU(Collector3.ShapeCollector shape)
        {
            foreach (TerrainColliderInfoMesh terrainInfo in terrainsInfoForCPU)
            {
                Vector3[] verts;
                int[]     tris;
                GetTerrainMesh(terrainInfo, out verts, out tris);

                if (terrainInfo.alphaMap == null)
                {
                    shape.AppendTerrain(verts, tris, PathFinder.getDefaultArea);
                }
                else
                {
                    byte[] areaSplatMap = Pool.GenericPoolArray <byte> .Take(shape.flattenSize);

                    GenerateTerrainAreaMapFromSplatMap(terrainInfo, shape, ref areaSplatMap);
                    shape.AppendTerrain(verts, tris, areaSplatMap);
                    Pool.GenericPoolArray <byte> .ReturnToPool(ref areaSplatMap);
                }

                Pool.GenericPoolArray <Vector3> .ReturnToPool(ref verts);

                Pool.GenericPoolArray <int> .ReturnToPool(ref tris);

                foreach (var tree in terrainInfo.trees)
                {
                    shape.Append(tree);
                }
            }
        }
        public void Collect()
        {
            shapeCollectorResult = ShapeCollector.GetFromPool(template.lengthX_extra, template.lengthZ_extra, template);

            if (profiler != null)
            {
                profiler.AddLog("Start Collecting Terrain");
            }
            CollectTerrainCPU(shapeCollectorResult);
            CollectTerrainGPU(shapeCollectorResult);
            if (profiler != null)
            {
                profiler.AddLog("End Collecting Terrain");
            }

            if (profiler != null)
            {
                profiler.AddLog("Start Collecting shapes");
            }
            CollectCollidersCPU(shapeCollectorResult);
            CollectCollidersGPU(shapeCollectorResult);
            if (profiler != null)
            {
                profiler.AddLog("End Collecting shapes");
            }


            if (profiler != null)
            {
                profiler.AddLog("Start Collecting shapes");
            }
            modsChangeArea.Sort(Comparer);
            modsMakeHole.Sort(Comparer);

            for (int i = 0; i < modsChangeArea.Count; i++)
            {
                shapeCollectorResult.ChangeArea(modsChangeArea[i].shapes, modsChangeArea[i].area);
            }

            for (int i = 0; i < modsMakeHole.Count; i++)
            {
                if (modsMakeHole[i].mode == ColliderInfoMode.MakeHoleApplyArea)
                {
                    shapeCollectorResult.MakeHole(modsMakeHole[i].shapes, modsMakeHole[i].area);
                }
                if (modsMakeHole[i].mode == ColliderInfoMode.MakeHoleRetainArea)
                {
                    shapeCollectorResult.MakeHole(modsMakeHole[i].shapes, null);
                }
            }

            if (profiler != null)
            {
                profiler.AddLog("End Collecting shapes");
            }
            //shapeCollectorGeneric.DebugMe();
        }
        public static void ReturnToPool(ref ShapeCollector shape)
        {
            GenericPoolArray <Data> .ReturnToPool(ref shape.arrayData);

            GenericPoolArray <int> .ReturnToPool(ref shape.freeIndexStack);

            shape.template = null;
            lock (poolDictionary) {
                poolDictionary[shape.size].Push(shape);
            }
            shape = null;
        }
        private void CollectCollidersGPU(Collector3.ShapeCollector collector)
        {
            if (profiler != null)
            {
                profiler.AddLog("Start shape collecting by GPU");
            }
            int collectedSolidShapes = 0;

            foreach (var resultHolder in collectedComputeShaderData)
            {
                if (resultHolder.infoMode == ColliderInfoMode.Solid)
                {
                    collector.AppendComputeShaderResult(resultHolder.result, resultHolder.area);
                    collectedSolidShapes++;
                }
            }
            if (profiler != null)
            {
                profiler.AddLogFormat("End shape collecting by GPU. Added {0} shapes", collectedSolidShapes);
            }
        }
Example #6
0
        private void CollectTerrainGPU(Collector3.ShapeCollector shape)
        {
            foreach (var terrainGPU in terrainGPUResuts)
            {
                if (terrainGPU.info.alphaMap != null)
                {
                    byte[] areaSplatMap = Pool.GenericPoolArray <byte> .Take(shape.flattenSize);

                    GenerateTerrainAreaMapFromSplatMap(terrainGPU.info, shape, ref areaSplatMap);
                    shape.AppendComputeShaderResult(terrainGPU.result, areaSplatMap);
                    Pool.GenericPoolArray <byte> .ReturnToPool(ref areaSplatMap);
                }
                else
                {
                    shape.AppendComputeShaderResult(terrainGPU.result, 0);
                }

                foreach (var tree in terrainGPU.info.trees)
                {
                    shape.Append(tree);
                }
            }
        }
        public static ShapeCollector GetFromPool(VectorInt.Vector2Int size, NavMeshTemplateCreation template)
        {
            ShapeCollector result = null;

            lock (poolDictionary) {
                Stack <ShapeCollector> stack;
                if (poolDictionary.TryGetValue(size, out stack) == false)
                {
                    stack = new Stack <ShapeCollector>();
                    poolDictionary.Add(size, stack);

                    for (int i = 0; i < INITIAL_POOL_SIZE; i++)
                    {
                        stack.Push(new ShapeCollector(size));
                    }
                }

                result = stack.Count > 0 ? stack.Pop() : new ShapeCollector(size);
            }

            result.Init(template);
            return(result);
        }
Example #8
0
        /// <summary>
        /// ref cause it should be pooled and returned outside this function
        /// </summary>
        private void GenerateTerrainAreaMapFromSplatMap(TerrainColliderInfoMesh terrainInfo, Collector3.ShapeCollector shape, ref byte[] result)
        {
            int size = shape.flattenSize;

            //var areaLibrary = PathFinder.settings.areaLibrary;

            float[,,] alpha = terrainInfo.alphaMap;
            int alphaCount = alpha.GetLength(2);

            int[] alphaToArea = terrainInfo.settings.data;

            //shome stored values
            int startXClamp = terrainInfo.startXClamp;
            int startZClamp = terrainInfo.startZClamp;

            int endXClamp = terrainInfo.endXClamp;
            int endZClamp = terrainInfo.endZClamp;

            int terrainStartX = terrainInfo.terrainStartX;
            int terrainStartZ = terrainInfo.terrainStartZ;

            float terrainSizeX = terrainInfo.terrainSizeX;
            float terrainSizeZ = terrainInfo.terrainSizeZ;

            int alphaWidth  = terrainInfo.alphaWidth;
            int alphaHeight = terrainInfo.alphaHeight;

            int alphaStartX = terrainInfo.alphaStartX;
            int alphaStartZ = terrainInfo.alphaStartZ;

            int alphaSizeXIndexLimit = terrainInfo.alphaSizeX - 1;
            int alphaSizeZIndexLimit = terrainInfo.alphaSizeZ - 1;

            int shapeSizeX = shape.sizeX;

            //this done this way to take into account that alpha map not aligned with chunk start
            for (int x = startXClamp; x < endXClamp; x++)
            {
                int curX = Mathf.Clamp(Mathf.RoundToInt((x - terrainStartX) / terrainSizeX * alphaWidth) - alphaStartX, 0, alphaSizeXIndexLimit);

                for (int z = startZClamp; z < endZClamp; z++)
                {
                    int curZ = Mathf.Clamp(Mathf.RoundToInt((z - terrainStartZ) / terrainSizeZ * alphaHeight) - alphaStartZ, 0, alphaSizeZIndexLimit);

                    int highest = 0;
                    for (int i = 0; i < alphaCount; i++)
                    {
                        if (alpha[curZ, curX, i] > alpha[curZ, curX, highest])
                        {
                            highest = i;
                        }
                    }

                    //int targetX = x - template.startX_extra;
                    //int targetZ = z - template.startZ_extra;
                    //areaMap[shape.GetIndex(targetX, targetZ)] = (byte)alphaToArea[highest];

                    result[((z - template.startZ_extra) * shapeSizeX) + (x - template.startX_extra)] = (byte)alphaToArea[highest];
                }
            }
        }
 public TerrainShape(Collector3.ShapeCollector terrain, Collector3.ShapeCollector trees)
 {
     this.terrain = terrain;
     this.trees   = trees;
 }