private void IntersectTest()
    {
#if UNITY_EDITOR
        GameObject go;

        // -- for debug
        Stopwatch   stopwatch      = Stopwatch.StartNew();
        int         objIndex       = 0;
        int         intersectCount = 0;
        List <Task> tasks          = new List <Task>();
        foreach (KeyValuePair <GameObject, TriangleInfo[]> pair in objDic)
        {
            if (pair.Value == null)
            {
                continue;
            }
            // 每个物体多线程相交测试
            if (enableMultiThreading)
            {
                tasks.Add(Task.Factory.StartNew(MultiThreadingIntersectTest, new ObjInfo(pair)));
                continue;
            }
            // 单线程相交
            objIndex++;
            intersectCount = 0;
            go             = pair.Key;
            Bounds       bound  = go.GetComponent <Renderer>().bounds;
            AABBBoundBox objBox = new AABBBoundBox(bound.min, bound.max);
            var          l      = octree.GetIntersections(objBox);
            for (int i = 0; i < l.Count; i++)
            {
                foreach (TriangleInfo tInfo in pair.Value)
                {
                    if (l[i].overlapWithTriangle)
                    {
                        continue;
                    }
                    if (IntersectionTest.AABBTriangle(l[i].boundBox, tInfo))
                    {
                        l[i].overlapWithTriangle = true;
                    }
                }
                intersectCount++;
                bool cancel = EditorUtility.DisplayCancelableProgressBar("Voxelize", $"No.{objIndex} Object (of {objDic.Count}), Intersect Count {intersectCount}/{l.Count}",
                                                                         (float)intersectCount / l.Count);
                if (cancel)
                {
                    break;
                }
            }
        }
        Task.WaitAll(tasks.ToArray());
        stopwatch.Stop();
        EditorUtility.ClearProgressBar();
        Debug.Log($"体素化完成,耗时:{stopwatch.ElapsedMilliseconds}ms");
#endif
    }
Example #2
0
    private void IntersectTestDivide(object obj)
    {
        Tuple <int, int, int> indexTuple = (Tuple <int, int, int>)obj;
        int startX = indexTuple.Item1;
        int startY = indexTuple.Item2;
        int startZ = indexTuple.Item3;
        int xRange = startX + divide;
        int yRange = startY + divide;
        int zRange = startZ + divide;

        xRange = xRange < _xMax ? xRange : _xMax;
        yRange = yRange < _yMax ? yRange : _yMax;
        zRange = zRange < _zMax ? zRange : _zMax;

        #region Naive Simple Violent Loop...
        for (int x = startX; x < xRange; x++)
        {
            for (int y = startY; y < yRange; y++)
            {
                for (int z = startZ; z < zRange; z++)
                {
                    if (_voxels[x, y, z])
                    {
                        continue;
                    }
                    AABBBoundBox         boundBox          = new AABBBoundBox(GetVoxelBoundMin(x, y, z), GetVoxelBoundMax(x, y, z));
                    List <TriangleBound> triangleBoundList = _octree.GetIntersections(boundBox);

                    for (int i = 0; i < triangleBoundList.Count; i++)
                    {
                        for (int j = 0; j < triangleBoundList[i].triangles.Length; j++)
                        {
                            if (IntersectionTest.AABBTriangle(boundBox, triangleBoundList[i].triangles[j]))
                            {
                                _voxels[x, y, z] = true;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        lock (_locker)
        {
            _completeCount++;
        }
    }