Esempio n. 1
0
        /// <inheritdoc />
        public void GetIndexiesVoxel(T item, NativeList <int3> results)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckReadAndThrow(_safety);
#endif
            var bounds = new Bounds(item.GetCenter(), item.GetSize());

            bounds.Clamp(_data->WorldBounds);

            CalculStartEndIterationInternal(_data, bounds, out var start, out var end);

            var hashPosition = new int3(0F);

            for (int x = start.x; x < end.x; ++x)
            {
                hashPosition.x = x;

                for (int y = start.y; y < end.y; ++y)
                {
                    hashPosition.y = y;

                    for (int z = start.z; z < end.z; ++z)
                    {
                        hashPosition.z = z;

                        results.Add(hashPosition);
                    }
                }
            }
        }
Esempio n. 2
0
        public void Query(Bounds bounds, NativeList <int3> voxelIndexes)
        {
            Assert.IsTrue(voxelIndexes.IsCreated);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckReadAndThrow(_safety);
#endif
            bounds.Clamp(_data->WorldBounds);

            CalculStartEndIterationInternal(_data, bounds, out var start, out var end);

            var hashPosition = new int3(0F);

            for (int x = start.x; x < end.x; ++x)
            {
                hashPosition.x = x;

                for (int y = start.y; y < end.y; ++y)
                {
                    hashPosition.y = y;

                    for (int z = start.z; z < end.z; ++z)
                    {
                        hashPosition.z = z;

                        voxelIndexes.Add(hashPosition);
                    }
                }
            }
        }
Esempio n. 3
0
        public void AddFast(ref T item)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckWriteAndThrow(_safety);
#endif

            var itemID = item.SpatialHashingIndex;
            var bounds = new Bounds(item.GetCenter(), item.GetSize());

            bounds.Clamp(_data->WorldBounds);

            _itemIDToBounds[itemID] = bounds;
            _itemIDToItem[itemID]   = item;

            CalculStartEndIterationInternal(_data, bounds, out var start, out var end);

            var hashPosition = new int3(0F);

            for (int x = start.x; x < end.x; ++x)
            {
                hashPosition.x = x;

                for (int y = start.y; y < end.y; ++y)
                {
                    hashPosition.y = y;

                    for (int z = start.z; z < end.z; ++z)
                    {
                        hashPosition.z = z;

                        AddInternal(hashPosition, itemID);
                    }
                }
            }
        }
Esempio n. 4
0
            public bool TryAdd(ref T item)
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                AtomicSafetyHandle.CheckWriteAndThrow(_safety);
#endif

                var bounds = new Bounds(item.GetCenter(), item.GetSize());

                bounds.Clamp(_data->WorldBounds);

                var itemID = Interlocked.Increment(ref _data->Counter);
                item.SpatialHashingIndex = itemID;

                if (_itemIDToBounds.TryAdd(itemID, bounds) == false || _itemIDToItem.TryAdd(itemID, item) == false)
                {
                    return(false);
                }

                CalculStartEndIterationInternal(_data, bounds, out var start, out var end);

                var hashPosition = new int3(0F);

                for (int x = start.x; x < end.x; ++x)
                {
                    hashPosition.x = x;

                    for (int y = start.y; y < end.y; ++y)
                    {
                        hashPosition.y = y;

                        for (int z = start.z; z < end.z; ++z)
                        {
                            hashPosition.z = z;

                            var hash = Hash(hashPosition);
                            _buckets.Add(hash, itemID);
                        }
                    }
                }

                return(true);
            }
Esempio n. 5
0
        /// <summary>
        /// Query system to find object in <paramref name="bounds"/>.
        /// </summary>
        public void Query(Bounds bounds, NativeList <T> resultList)
        {
            Assert.IsTrue(resultList.IsCreated);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckReadAndThrow(_safety);
#endif
            bounds.Clamp(_data->WorldBounds);

            CalculStartEndIterationInternal(_data, bounds, out var start, out var end);

            var hashMapUnic  = new NativeHashMap <int, byte>(64, Allocator.Temp);
            var hashPosition = new int3(0F);

            for (int x = start.x; x < end.x; ++x)
            {
                hashPosition.x = x;

                for (int y = start.y; y < end.y; ++y)
                {
                    hashPosition.y = y;

                    for (int z = start.z; z < end.z; ++z)
                    {
                        hashPosition.z = z;

                        var hash = Hash(hashPosition);

                        if (_buckets.TryGetFirstValue(hash, out var item, out var it))
                        {
                            do
                            {
                                hashMapUnic.TryAdd(item, 0);
                            }while (_buckets.TryGetNextValue(out item, ref it));
                        }
                    }
                }
            }

            ExtractValueFromHashMap(hashMapUnic, bounds, resultList);
        }
Esempio n. 6
0
        public void Move(T item)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckWriteAndThrow(_safety);
#endif
            var itemID  = item.SpatialHashingIndex;
            var success = _itemIDToBounds.TryGetValue(itemID, out var oldBounds);
            Assert.IsTrue(success);

            var newBounds = new Bounds(item.GetCenter(), item.GetSize());
            newBounds.Clamp(_data->WorldBounds);
            _itemIDToBounds.Remove(itemID);
            _itemIDToBounds.TryAdd(itemID, newBounds);
            _itemIDToItem.Remove(itemID);
            _itemIDToItem.TryAdd(itemID, item);

            _helpMoveHashMapOld.Clear();
            SetVoxelIndexForBounds(_data, oldBounds, _helpMoveHashMapOld);
            _helpMoveHashMapNew.Clear();
            SetVoxelIndexForBounds(_data, newBounds, _helpMoveHashMapNew);

            var oldVoxel = _helpMoveHashMapOld.GetKeyArray(Allocator.Temp);

            for (int i = 0; i < oldVoxel.Length; i++)
            {
                if (_helpMoveHashMapNew.TryGetValue(oldVoxel[i], out _) == false)
                {
                    RemoveInternal(oldVoxel[i], itemID);
                }
            }

            var newVoxel = _helpMoveHashMapNew.GetKeyArray(Allocator.Temp);

            for (int i = 0; i < newVoxel.Length; i++)
            {
                if (_helpMoveHashMapOld.TryGetValue(newVoxel[i], out _) == false)
                {
                    AddInternal(newVoxel[i], itemID);
                }
            }
        }