What I actually need ?
Overall I just need to place coins throughout the whole ARPlaneAnchor without overlapping them and updating there position all the time.
I just need to place AR entities (in my case it’s coins) within the whole plane area, which user scan.
I’m using RealityKit with ARView and I need to place AR entities (in my case it’s coins) within the whole plane area, which user scan.
I’m trying to get everything work with method from ARSessionDelegate
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
for anchor in acnhors where anchor is ARPlaneAnchor {
guard amountOfRenderedCoins(for anchor) <= 10 else {
continue
}
let pointsForCoins = calculatePoints(on anchor.boundaryVertices)
for point in pointsForCoins {
let worldPositionVec3 = anchor.transform * SIMD4<Float>(point, point.y, point.z, 1.0)
let anchorEntity = AnchorEntity(world: worldPositionVec3)
anchorEntity.addChild(makeCoin())
arView.scene.addAnchor(anchorEntity)
}
}
}
When anchor updated(I ignore didAdd
method , cause plane at that moment are not accurate), I take ARPlaneAnchor.geomary.boundaryVertices
and calculate the point to place my Coin entity.
I’m also converting this point to real world point by multiplying plane anchor transform and my calculated point in boundary vertices.
let worldPositionVec3 = planeAnchor.transform * SIMD4<Float>(point, point.y, point.z, 1.0)
Then I create an AnchorEntity(world: worldPositionVec3)
and add coin to the anchor entity. Then I add anchor entity to anchors of scene. I’m adding 10 coins for each ARPlaneAnchor
I have an array where I store how many coins were rendered on each ARPlaneAnchor
to prevent from showing too much coins.
However the things are not going as expected. Because
Sometimes coins are rendered on top of each other, cause ARPlaneAnchor
dynamically changes. The points which I calculate sometimes can overlap which leads to placing coins on top of each other.
I was trying to store already rendered coins and just updating there positions , when new loop of didUpdate anchors
method called , however because of that coins are always moving and changing it position.
Just if it’s needed here the calculatePoints
method for position of coins.
private func calculatePoints(for anchor: ARPlaneAnchor) -> [SIMD3<Float>] {
let cellSize: Float = coinWidth + coinSpacing
var minX = Float.greatestFiniteMagnitude
var minZ = Float.greatestFiniteMagnitude
var maxX = -Float.greatestFiniteMagnitude
var maxZ = -Float.greatestFiniteMagnitude
for boundaryVertex in anchor.geometry.boundaryVertices {
if boundaryVertex.x < minX { minX = boundaryVertex.x }
if boundaryVertex.z < minZ { minZ = boundaryVertex.z }
if boundaryVertex.x > maxX { maxX = boundaryVertex.x }
if boundaryVertex.z > maxZ { maxZ = boundaryVertex.z }
}
var coinCentres = [SIMD3<Float>]()
var currentX = minX
while currentX <= maxX {
var currentZ = minZ
while currentZ <= maxZ {
let center = SIMD3<Float>(x: currentX + coinWidth / 2, y: 0, z: currentZ + coinWidth / 2)
coinCentres.append(center)
currentZ += cellSize
}
currentX += cellSize
}
return coinCentres
}
Maybe I’m going in a wrong direction, ready to change my solution anyway cause I’m stuck.