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); } }
/// <inheritdoc /> public void Execute(int index) { var item = ItemList[index]; SpatialHash.TryAdd(ref item); ItemList[index] = 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(); }
/// <inheritdoc /> public void Execute() { for (int i = 0; i < ItemList.Length; i++) { SpatialHash.Move(ItemList[i]); } }
static Bounds3Int[] PlaceAddonRadially(float radianAngle, Bounds3Int[] addon, SpatialHash <LandParcel> occupied, out Vector2Int position) { Vector2 direction = Mathx.RadianToVector2(radianAngle); PixelTraversal tracer = new PixelTraversal(new Vector2(), direction); Bounds3Int[] placed = new Bounds3Int[addon.Length]; bool overlaps; do { tracer.Step(); overlaps = false; for (int i = 0, len = addon.Length; i < len; ++i) { Bounds3Int translated = addon[i].Translate(tracer.position.ToVector3XZ()); if (occupied.Overlaps(translated)) { overlaps = true; break; } placed[i] = translated; } } while (overlaps); position = tracer.position; return(placed); }
/// <inheritdoc /> public void Execute() { for (int i = 0; i < ItemList.Length; i++) { SpatialHash.Remove(ItemList[i].SpatialHashingIndex); } }
void Start() { hash = new SpatialHash<Transform>(cellSize); clones = new Transform[numberOfPoints]; wireSize = Mathf.CeilToInt(sphereRadius / cellSize) * cellSize + cellSize / 2; for (var i = 0; i < numberOfPoints; i++) { clone = Instantiate(point, Random.insideUnitSphere * sphereRadius, Quaternion.identity) as Transform; if (clone != null) { hash.Insert(clone.position, clone); clone.parent = transform; clones[i] = clone; } } InvokeRepeating("MovePoint", 0, 0.5f); InvokeRepeating("ClearHash", 10, 10); for (var x = -wireSize; x <= wireSize; x++) { for (var y = -wireSize; y <= wireSize; y++) { for (var z = -wireSize; z <= wireSize; z++) { yellowCubes.Add(Instantiate(yellowCube, new Vector3(x, y, z), Quaternion.identity) as Transform); blueCubes.Add(Instantiate(blueCube, new Vector3(x, y, z), Quaternion.identity) as Transform); } } } }
private void Start() { Random.InitState(123456789); var copy = new Bounds(_worldBounds.Center, _worldBounds.Size); _spatialHashing = new SpatialHash <HashMapVisualDebug.ItemTest>(copy, new float3(10F), Allocator.Persistent); }
static Bounds3Int[] PlaceAddon(Bounds3Int[] addon, SpatialHash <LandParcel> occupied) { float angle = UnityEngine.Random.value * Mathf.PI * 2; Bounds3Int[] placed = PlaceAddonRadially(angle, addon, occupied, out Vector2Int position); ShiftTowardsZero(addon, placed, occupied, ref position); return(placed); }
/// <inheritdoc /> public void Execute(int index) { var e = SpatialHash.GetObject(IsometricIndexForThisFrame[index]); IsometricDepthCDFE[e.Entity] = new IsometricDepthData { Depth = index }; }
static bool ShiftTowardsZero(Bounds3Int[] addon, Bounds3Int[] placed, SpatialHash <LandParcel> occupied, int positionIndex, ref Vector2Int placedPosition) { Vector2Int position = placedPosition; if (position[positionIndex] == 0) { return(false); } int step = position[positionIndex] > 0 ? -1 : 1; bool overlaps; const int maxIterations = 20; int iter = 0; do { overlaps = false; position[positionIndex] += step; if (position[positionIndex] == step) { break; } if (iter >= maxIterations) { // Failsafe in case we miss the geometry and shift into oblivion for (int i = 0, len = addon.Length; i < len; ++i) { placed[i] = addon[i].Translate(placedPosition.ToVector3XZ()); } return(false); } ++iter; for (int i = 0, len = addon.Length; i < len; ++i) { Bounds3Int translated = addon[i].Translate(position.ToVector3XZ()); if (occupied.Overlaps(translated)) { overlaps = true; } placed[i] = translated; } } while (!overlaps); position[positionIndex] -= step; for (int i = 0, len = addon.Length; i < len; ++i) { placed[i] = addon[i].Translate(position.ToVector3XZ()); //Shouldn't overlap } bool moved = position[positionIndex] != placedPosition[positionIndex]; placedPosition = position; return(moved); }
void Awake() { hash = GameObject.Find(hashName).GetComponent <SpatialHash>(); if (hash == null) { Debug.LogError("BoidVision cannot find SpatialHash by name " + hashName + "!"); } }
void Start() { rb = GetComponent <Rigidbody>(); hash = GameObject.Find(hashName).GetComponent <SpatialHash>(); if (hash == null) { Debug.LogError("BoidVision cannot find SpatialHash by name " + hashName + "!"); } }
static void ShiftTowardsZero(Bounds3Int[] addon, Bounds3Int[] placed, SpatialHash <LandParcel> occupied, ref Vector2Int position) { bool movedX; bool movedY; do { movedX = ShiftTowardsZero(addon, placed, occupied, positionIndex: 0, ref position); movedY = ShiftTowardsZero(addon, placed, occupied, positionIndex: 1, ref position); } while (movedX || movedY); }
void Start() { spatialHash = new SpatialHash(2); vertices = GetComponentsInChildren <vertex> (); for (int i = 0; i < vertices.Length; i++) { spatialHash.Insert(vertices[i].transform.position, vertices[i]); } triangles = new List <vertex>(); constraints = new List <Constraint>(); GenerateConstraints(); }
private void Start() { roiParticles = new List <RoiParticle>(); simulationBounds = Utils.getBounds(Camera.main); spatialHash = new SpatialHash(simulationBounds, particleRadius); objectPooler = new ObjectPooler <RoiParticle>(maxParticles); setUpEmitters(); setUpParticleSystem(); setUpShurikenParticles(); setUpNearParticlesArray(); }
private SpatialHash hash; //hash in which to store this object void Start() { hash = GameObject.Find(hashName).GetComponent <SpatialHash>(); if (hash == null) { Debug.LogError("HashObject cannot find SpatialHash by name " + hashName + "!"); } else { hash.Include(this.gameObject); } }
void Start() { hash = GetComponent <SpatialHash>(); if (hash == null) { Debug.LogError("SpatialHashDebug cannot find SpatialHash script to debug! (GetComponent<SpatialHash> == null)"); cellSize = Vector3.zero; } else { cellSize = new Vector3(hash.cellSizeX, hash.cellSizeY, hash.cellSizeZ); } }
void Start() { hash = GetComponent <SpatialHash>(); if (hash == null) { Debug.LogError("SpatialHashDebug cannot find SpatialHash script to debug! (GetComponent<SpatialHash> == null)"); } cellSizeX = hash.cellSizeX; cellSizeY = hash.cellSizeY; cellSizeZ = hash.cellSizeZ; cellSize = new Vector3(cellSizeX, cellSizeY, cellSizeZ); }
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 InsertAndRetrieve() { var spatialHash = new SpatialHash <int>(16); var rectA = new Rectangle(-2, -2, 2, 2); var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectB = new Rectangle(-2, -2, 2, 2); var rectBTransform = new Transform2D(new Vector2(8, 8)); var rectC = new Rectangle(-2, -2, 2, 2); var rectCTransform = new Transform2D(new Vector2(24, -4)); var rectD = new Rectangle(-2, -2, 2, 2); var rectDTransform = new Transform2D(new Vector2(24, 24)); var circleA = new Circle(2); var circleATransform = new Transform2D(new Vector2(24, -8)); var circleB = new Circle(8); var circleBTransform = new Transform2D(new Vector2(16, 16)); var line = new Line(new Position2D(20, -4), new Position2D(22, -12)); var lineTransform = new Transform2D(new Vector2(0, 0)); var point = new Point(); var pointTransform = new Transform2D(new Position2D(8, 8)); spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(1, rectB, rectBTransform); spatialHash.Insert(2, rectC, rectCTransform); spatialHash.Insert(3, rectD, rectDTransform); spatialHash.Insert(4, circleA, circleATransform); spatialHash.Insert(1, circleB, circleBTransform); spatialHash.Insert(6, line, lineTransform); spatialHash.Insert(7, point, pointTransform); spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty(); spatialHash.Retrieve(1, rectB, rectBTransform).Should().NotContain((1, circleB, circleBTransform)); spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((7, point, pointTransform)); spatialHash.Retrieve(2, rectC, rectCTransform).Should().Contain((6, line, lineTransform)).And.Contain((4, circleA, circleATransform)); spatialHash.Retrieve(3, rectD, rectDTransform).Should().Contain((1, circleB, circleBTransform)); spatialHash.Retrieve(4, circleA, circleATransform).Should().Contain((6, line, lineTransform)).And.Contain((2, rectC, rectCTransform)); spatialHash.Retrieve(1, circleB, circleBTransform).Should().NotContain((1, rectB, rectBTransform)).And.Contain((3, rectD, rectDTransform)); spatialHash.Retrieve(6, line, lineTransform).Should().Contain((4, circleA, circleATransform)).And.Contain((2, rectC, rectCTransform)); }
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(); }
public void Execute(int i) { // Query the spatial hash int x0 = SpatialHash.intCoord(particles[i].x - particleDiameter, spacing); int y0 = SpatialHash.intCoord(particles[i].y - particleDiameter, spacing); int z0 = SpatialHash.intCoord(particles[i].z - particleDiameter, spacing); int x1 = SpatialHash.intCoord(particles[i].x + particleDiameter, spacing); int y1 = SpatialHash.intCoord(particles[i].y + particleDiameter, spacing); int z1 = SpatialHash.intCoord(particles[i].z + particleDiameter, spacing); offsets[i] = Vector3.zero; int samples = 0; for (int xi = x0; xi <= x1; xi++) { for (int yi = y0; yi <= y1; yi++) { for (int zi = z0; zi <= z1; zi++) { int h = SpatialHash.hashCoords(xi, yi, zi, tableSize); int start = cellStart[h]; int end = cellStart[h + 1]; for (int q = start; q < end; q++) { int j = cellEntries[q]; // ------ Ideally this code could be wrapped inside of the hash's query iterator ------ if (i != j) { // Collide these two particles together Vector3 offset = particles[i] - particles[j]; float offsetMagnitude = offset.magnitude; if (offsetMagnitude < particleDiameter) { Vector3 normalizedOffset = offset / offsetMagnitude; offsets[i] += normalizedOffset * (particleDiameter - offsetMagnitude) * 0.5f; samples++; } } // ------------------------------------------------------------------------------------ } } } } if (samples > 0) { offsets[i] /= samples; } }
public void Clear() { var spatialHash = new SpatialHash <int>(16); var rectA = new Rectangle(-2, -2, 2, 2); var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectB = new Rectangle(-2, -2, 2, 2); var rectBTransform = new Transform2D(new Vector2(8, 8)); spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(1, rectB, rectBTransform); spatialHash.Clear(); spatialHash.Retrieve(0, rectA, rectATransform).Should().HaveCount(0); }
public SpatialHashData(int numParticles, float floorHeight, float particleDiameter) { particles = new NativeArray <Vector3>(numParticles, Allocator.Persistent); prevParticles = new NativeArray <Vector3>(numParticles, Allocator.Persistent); offsets = new NativeArray <Vector3>(numParticles, Allocator.Persistent); particleMatrices = new NativeArray <Matrix4x4>(numParticles, Allocator.Persistent); hash = new SpatialHash(particleDiameter, numParticles); for (int i = 0; i < particles.Length; i++) { particles[i] = UnityEngine.Random.insideUnitSphere * 5f; particles[i] = new Vector3(particles[i].x, Mathf.Abs(particles[i].y - floorHeight) + floorHeight, particles[i].z); prevParticles[i] = particles[i]; offsets[i] = Vector3.zero; particleMatrices[i] = Matrix4x4.identity; } hash.create(particles, particleDiameter); }
public void InsertAndRetrieveSameValues() { var spatialHash = new SpatialHash <int>(16); var rectA = new Rectangle(-2, -2, 2, 2); var rectATransform = new Transform2D(new Vector2(-8, -8)); var rectB = new Rectangle(-2, -2, 2, 2); var rectBTransform = new Transform2D(new Vector2(-8, -8)); var rectC = new Rectangle(-1, -1, 1, 1); var rectCTransform = new Transform2D(new Vector2(-8, -8)); spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(1, rectB, rectBTransform); spatialHash.Insert(2, rectC, rectCTransform); spatialHash.Retrieve(2, rectC, rectCTransform).Should().HaveCount(2); }
public GameplayScreen() { // TODO: customize levels, possibly parse textfiles specify level content, scrollVelocity, etc... this.scrollVelocity = 0.2f; this.TransitionOffTime = TimeSpan.FromSeconds(1.5f); this.TransitionOnTime = TimeSpan.FromSeconds(0.5f); this.enemies = new List<Enemy>(); this.entities = new List<Entity>(); this.items = new List<Item>(); this.players = new List<Player>(); this.projectiles = new List<Projectile>(); this.enchantments = new List<Enchantment>(); this.enemyMap = new SpatialHash<Enemy>(cellSize); this.itemMap = new SpatialHash<Item>(cellSize); this.enemyFactory = new Factory(); }
private void InitSPH() { particles = new SPHParticle[amount]; hash = new SpatialHash <SPHParticle>(cellSize); for (int i = 0; i < amount; i++) { float jitter = (Random.value * 2f - 1f) * parameters[parameterID].particleRadius * 0.1f; float x = worldStartOffsetX + (i % rowSize) + Random.Range(-0.1f, 0.1f); float y = 2 + (float)((i / rowSize) / rowSize) * 1.1f; float z = worldStartOffsetY + ((i / rowSize) % rowSize) + Random.Range(-0.1f, 0.1f); GameObject go = Instantiate(Prefab); go.transform.localScale = Vector3.one * parameters[parameterID].particleRadius; go.transform.position = new Vector3(x + jitter, y, z + jitter); go.name = "Particle" + i.ToString(); particles[i].Init(new Vector3(x, y, z), parameterID, go); particles[i].velocity = Vector3.zero; hash.Insert(particles[i].position, particles[i]); } }
/// <inheritdoc /> protected override void InitSpatialHashing() { var matrix = GetSingleton <IsometricMatrix>(); var size = new float3(600, 600, 50); var pivot = new float3(size.x, 0F, 0F); var bottomPosition = matrix.IsoToScreen(pivot); var topPosition = matrix.IsoToScreen(new float3(pivot.x - size.x, pivot.yz + size.yz)); var isoBottom = pivot; isoBottom.y += size.y; var rightPosition = matrix.IsoToScreen(isoBottom); var leftPosition = matrix.IsoToScreen(new float3(pivot.x - size.x, pivot.yz)); var screenMin = new float2(leftPosition.x, bottomPosition.y); var screenMax = new float2(rightPosition.x, topPosition.y); var bounds = new Bounds(); bounds.SetMinMax(new float3(screenMin, 0F), new float3(screenMax, 1F)); size = new float3(6, 6, 3); pivot = new float3(size.x, 0F, 0F); bottomPosition = matrix.IsoToScreen(pivot); topPosition = matrix.IsoToScreen(new float3(pivot.x - size.x, pivot.yz + size.yz)); isoBottom = pivot; isoBottom.y += size.y; rightPosition = matrix.IsoToScreen(isoBottom); leftPosition = matrix.IsoToScreen(new float3(pivot.x - size.x, pivot.yz)); screenMin = new float2(leftPosition.x, bottomPosition.y); screenMax = new float2(rightPosition.x, topPosition.y); Debug.Log($"SpatialHashing Bounds {bounds} CellSize {new float3(screenMax - screenMin, 1F)}"); _spatialHash = new SpatialHash <IsometricData>(bounds, new float3(screenMax - screenMin, 1F), Allocator.Persistent); }
private void OnDrawGizmos() { if (Application.isPlaying == false) { return; } var targetBounds = new Bounds(transform.position, _boundSize); var bounds2 = SpatialHash <HashMapVisualDebug.ItemTest> .TransformBounds(targetBounds, transform.rotation); bounds2.Clamp(_spatialHashing.WorldBounds); //********************** ligne raycast Ray r = new Ray(start.transform.position, end.transform.position - start.transform.position); var rr = new Ray(math.mul(math.inverse(transform.rotation), (start.transform.position - transform.position)) + (float3)transform.position, math.mul(math.inverse(transform.rotation), r.direction)); if (targetBounds.RayCastOBB(r.origin, r.direction, transform.rotation, out var pp, math.length(end.transform.position - start.transform.position))) { Gizmos.color = Color.yellow; }