I want to tile the ground with ARKit using custom polygon that creates using selected positions on horizontal plan by user, but tiles are stretched and wont show properly, Maybe problem is from texture coordinates, What's wrong with this code?
let vertices: [SCNVector3] = ... //Selected positions by user
var indices: [Int32] = [Int32(vertices.count)]
var index: Int32 = 0
for _ in vertices {
indices.append(index)
index += 1
}
let textureCoordinates = [ CGPoint(x: 0, y: 0),
CGPoint(x: 1, y: 0),
CGPoint(x: 0, y: 1),
CGPoint(x: 1, y: 1)
]
let vertexSource = SCNGeometrySource(vertices: vertices)
let uvSource = SCNGeometrySource(textureCoordinates: textureCoordinates)
let indexData = Data(bytes: indices,
count: indices.count * MemoryLayout<Int32>.size)
let element = SCNGeometryElement(data: indexData,
primitiveType: .polygon,
primitiveCount: 1,
bytesPerIndex: MemoryLayout<Int32>.size)
let geometry = SCNPlane(sources: [vertexSource, uvSource],
elements: [element]) //Creating geometry
//Tile material creation
let material = SCNMaterial()
material.isDoubleSided = true
material.diffuse.wrapS = .repeat
material.diffuse.wrapT = .repeat
//Tile image to tile polygon
material.diffuse.contents = UIImage(named: "tile")!
material.diffuse.contentsTransform = SCNMatrix4MakeScale(32, 32, 0)
geometry.firstMaterial = material
//Tiled Plan to put on the ground
let plane = SCNNode(geometry: geometry)
//Add custom polygon to sceneView
sceneView.scene.rootNode.addChildNode(plane)
Edit:
Thanks to Andy I changed the contentsTransform with small change on transform scale, but still have the same issue:
.init(
m11: 5, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 5, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0.5, m42: 0, m43: 0, m44: 1)
What I expect:
What happens:



