protected override void Awake() { base.Awake(); landParcelHash.Initialize(); foreach (LandParcel landParcel in landParcelSet) { landParcelHash.Add(landParcel, landParcel.bounds); } }
private void Start() { Random.InitState(123456789); var copy = new Bounds(_worldBounds.Center, _worldBounds.Size * 4F); _spatialHashing = new SpatialHash <ItemTest>(copy, new float3(10F), Allocator.Persistent); for (int i = 0; i < _spawnCount; i++) { var g = GameObject.CreatePrimitive(PrimitiveType.Cube); if (_useRaycast == false) { g.AddComponent <HashTeleport>(); } Destroy(g.GetComponent <BoxCollider>()); g.transform.position = new Vector3(Random.Range(_worldBounds.Min.x + 1, _worldBounds.Max.x), Random.Range(_worldBounds.Min.y + 1, _worldBounds.Max.y), Random.Range(_worldBounds.Min.z + 1, _worldBounds.Max.z)); var item = new ItemTest() { ID = i, Position = g.transform.position }; Profiler.BeginSample("SpatialHasing Add"); _spatialHashing.Add(ref item); Profiler.EndSample(); _listItemGameobject.Add(g); _listItem.Add(item); } }
public void SpatialHashOverWorldQuerry() { SpatialHash <Item> sh = new SpatialHash <Item>(new Bounds(new float3(15F), new float3(30F)), new float3(1F), 15, Allocator.Temp); var item = new Item { Center = new float3(5.5F), Size = new float3(1.1F) }; sh.Add(ref item); Assert.AreEqual(1, sh.ItemCount); Assert.AreEqual(3 * 3 * 3, sh.BucketItemCount); var querryBound = new Bounds(15F, 50F); var results = new NativeList <Item>(5, Allocator.TempJob); sh.Query(querryBound, results); Assert.AreEqual(1, results.Length); Assert.AreEqual(item, results[0]); //check clear result results.Dispose(); sh.Dispose(); }
public void SpatialHashAdd() { SpatialHash <Item> sh = new SpatialHash <Item>(new Bounds(new float3(15F), new float3(30F)), new float3(1F), 15, Allocator.Temp); var item = new Item { Center = new float3(5.5F), Size = new float3(1.1F) }; sh.Add(ref item); Assert.AreEqual(1, sh.ItemCount); Assert.AreEqual(3 * 3 * 3, sh.BucketItemCount); sh.Dispose(); }
public void WeldVertices(float threshold) { var hash = new SpatialHash <Vertex>(threshold, 10000000); vertices.ForEach((v) => { hash.Add(v.position, v); }); var tocombine = new List <Vertex>(); var combined = new List <Vertex>(); foreach (var cell in hash.HashTable.Values) { // check if the vertices are actually colocated or are just hashed similarly while (cell.Count > 0) { var next = cell.First(); cell.Remove(next); tocombine.Clear(); for (int i = 0; i < cell.Count; i++) { if ((next.position - cell[i].position).magnitude < threshold) { tocombine.Add(cell[i]); } } // combine the vertices foreach (var vertex in tocombine) { foreach (var triangle in vertex.triangles) { triangle.vertices.Replace(vertex, next); } next.originalIndices.AddRange(vertex.originalIndices); cell.Remove(vertex); } combined.Add(next); } } vertices = combined; UpdateVertexReferences(); }
public void SpatialHashAddOverWorld() { var worldSize = new float3(30); SpatialHash <Item> sh = new SpatialHash <Item>(new Bounds(new float3(15F), worldSize), new float3(1F), 1, Allocator.Temp); var item = new Item { Center = new float3(15F), Size = worldSize + new float3(10F) }; sh.Add(ref item); Assert.AreEqual(1, sh.ItemCount); Assert.AreEqual(worldSize.x * worldSize.y * worldSize.z, sh.BucketItemCount); sh.Dispose(); }
public void SpatialHashQuerry() { var cellSize = new float3(1F); SpatialHash <Item> sh = new SpatialHash <Item>(new Bounds(new float3(15F), new float3(30F)), cellSize, 15, Allocator.Temp); var item = new Item { Center = new float3(5.5F), Size = new float3(1.1F) }; sh.Add(ref item); Assert.AreEqual(1, sh.ItemCount); Assert.AreEqual(3 * 3 * 3, sh.BucketItemCount); var results = new NativeList <Item>(5, Allocator.TempJob); var bounds = new Bounds(item.GetCenter(), item.GetSize()); sh.CalculStartEndIteration(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 querryBound = new Bounds(sh.GetPositionVoxel(hashPosition, true), cellSize * 0.95F); results.Clear(); sh.Query(querryBound, results); Assert.AreEqual(1, results.Length); Assert.AreEqual(item, results[0]); } } } //check clear result results.Dispose(); sh.Dispose(); }
void Add(Machine machine) { Assert.IsFalse(machineSpatialHash.Overlaps(machine.bounds)); machines.Add(machine); machineSpatialHash.Add(machine, machine.bounds); MachineInfo machineInfo = machine.machineInfo; bool machinesMetaDataExists = machinesMetaData.TryGetValue(machineInfo, out MachineMetaData machineMetaData); machineMetaData.numInstances++; if (machinesMetaDataExists) { machinesMetaData[machineInfo] = machineMetaData; } else { machinesMetaData.Add(machineInfo, machineMetaData); } }
public void CreationTopologicalListJobTest() { var b = new Bounds(new float3(9400F, 400F, 0.5F), new float3(9600F, 5200F, 0.5F) * 2F); var spatialHash = new SpatialHash <IsometricData>(b, new float3(192F, 144F, 1F), Allocator.TempJob); var isoObject = new IsometricData { IsoPosition = new float3(17F, -1F, 0F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(39.99997F, -144F), ScreenMax = new float2(71.99997F, -112F) }; //1 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(16F, -1F, 0F), IsoSize = new float3(3F, 1F, 1F), ScreenMin = new float2(-8.000031F, -136F), ScreenMax = new float2(55.99997F, -88F) }; //2 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -1F, 3F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00002F, -64F), ScreenMax = new float2(7.999985F, -32F) }; //3 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(17F, -2F, 0F), IsoSize = new float3(3F, 1F, 1F), ScreenMin = new float2(-8.000031F, -152F), ScreenMax = new float2(55.99997F, -104F) }; //4 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -4F, 2F), IsoSize = new float3(1F, 3F, 1F), ScreenMin = new float2(-72.00002F, -104F), ScreenMax = new float2(-8.000015F, -56F) }; //5 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(14F, -2F, 0F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00003F, -128F), ScreenMax = new float2(7.999969F, -96F) }; //6 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(14F, -5F, 0F), IsoSize = new float3(1F, 3F, 1F), ScreenMin = new float2(-72.00003F, -152F), ScreenMax = new float2(-8.000031F, -104F) }; //7 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(20F, -1F, 0F), IsoSize = new float3(3F, 1F, 1F), ScreenMin = new float2(55.99997F, -168F), ScreenMax = new float2(120F, -120F) }; //8 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(14F, -1F, 1F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-8.000031F, -104F), ScreenMax = new float2(23.99997F, -72F) }; //9 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -2F, 1F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-40.00002F, -104F), ScreenMax = new float2(-8.000031F, -72F) }; //10 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(17F, -1F, 1F), IsoSize = new float3(3F, 1F, 1F), ScreenMin = new float2(7.999969F, -128F), ScreenMax = new float2(71.99997F, -80F) }; //11 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -1F, 1F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00002F, -96F), ScreenMax = new float2(7.999969F, -64F) }; //12 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(14F, -2F, 1F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00003F, -112F), ScreenMax = new float2(7.999969F, -80F) }; //13 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(18F, -1F, 1F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(55.99997F, -136F), ScreenMax = new float2(87.99997F, -104F) }; //14 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(16F, -1F, 2F), IsoSize = new float3(3F, 1F, 1F), ScreenMin = new float2(-8.000015F, -104F), ScreenMax = new float2(55.99997F, -56F) }; //15 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -1F, 0F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00003F, -112F), ScreenMax = new float2(7.999969F, -80F) }; //16 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -5F, 0F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-88.00003F, -144F), ScreenMax = new float2(-56.00003F, -112F) }; //17 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -1F, 2F), IsoSize = new float3(1F, 1F, 1F), ScreenMin = new float2(-24.00002F, -80F), ScreenMax = new float2(7.999985F, -48F) }; //18 spatialHash.Add(ref isoObject); isoObject = new IsometricData { IsoPosition = new float3(13F, -4F, 0F), IsoSize = new float3(1F, 3F, 1F), ScreenMin = new float2(-72.00003F, -136F), ScreenMax = new float2(-8.000031F, -88F) }; //19 spatialHash.Add(ref isoObject); const int iTemCount = 19; var chunkIndicies = new NativeList <int3>(Allocator.TempJob); var cellCount = spatialHash.CellCount; for (int x = 0; x < cellCount.x; x++) { for (int y = 0; y < cellCount.y; y++) { for (int z = 0; z < cellCount.x; z++) { chunkIndicies.Add(new int3(x, y, z)); } } } var topologicalListFrontToBack = new NativeMultiHashMap <int, int>(60, Allocator.TempJob); var queue = new NativeQueue <int>(Allocator.TempJob); var job = new IsoSortingSystem.CreationTopologicalListJob { SpatialHashing = spatialHash, ChunkList = chunkIndicies, TopologicalListFrontToBack = topologicalListFrontToBack.ToConcurrent(), IsometricDataPresent = queue.ToConcurrent() }; job.Schedule(chunkIndicies, 1).Complete(); var hashset = new HashSet <int>(); while (queue.Count > 0) { hashset.Add(queue.Dequeue()); } Assert.AreEqual(iTemCount, hashset.Count); FindAllItem(topologicalListFrontToBack, 18, 3, 5, 3, 15); FindAllItem(topologicalListFrontToBack, 16, 5, 6, 7, 10, 12, 13, 2, 4, 6, 9, 12, 13, 15, 19); FindAllItem(topologicalListFrontToBack, 15); FindAllItem(topologicalListFrontToBack, 14); FindAllItem(topologicalListFrontToBack, 13); FindAllItem(topologicalListFrontToBack, 12, 9, 5, 13, 10, 13, 15, 18, 18); FindAllItem(topologicalListFrontToBack, 11, 14, 15); FindAllItem(topologicalListFrontToBack, 9, 11, 13, 15); FindAllItem(topologicalListFrontToBack, 8, 14); FindAllItem(topologicalListFrontToBack, 6, 4, 7, 13, 13); FindAllItem(topologicalListFrontToBack, 4); FindAllItem(topologicalListFrontToBack, 3); FindAllItem(topologicalListFrontToBack, 2, 1, 4, 6, 9, 11, 13, 15); FindAllItem(topologicalListFrontToBack, 1, 4, 8, 11, 14); FindAllItem(topologicalListFrontToBack, 19, 5, 6, 7, 10, 13, 17); FindAllItem(topologicalListFrontToBack, 17, 7); FindAllItem(topologicalListFrontToBack, 10, 5, 13); FindAllItem(topologicalListFrontToBack, 7); FindAllItem(topologicalListFrontToBack, 5); spatialHash.Dispose(); chunkIndicies.Dispose(); topologicalListFrontToBack.Dispose(); queue.Dispose(); }