Skip to content

Instantly share code, notes, and snippets.

@Matt54
Created September 5, 2024 00:59
Show Gist options
  • Save Matt54/8f015d32926f7c424f1390c0bc06dadb to your computer and use it in GitHub Desktop.
Save Matt54/8f015d32926f7c424f1390c0bc06dadb to your computer and use it in GitHub Desktop.
RealityView creating floor tile pattern using scale of textureCoordinateTransform
import SwiftUI
import RealityKit
#Preview("TileFloorSinglePlaneRealityView") {
TileFloorSinglePlaneRealityView()
}
struct TileFloorSinglePlaneRealityView: View {
var body: some View {
let gridSize = 1
GeometryReader3D { proxy in
RealityView { content in
let size = content.convert(proxy.frame(in: .local), from: .local, to: .scene).extents
let tileSize = Float(min(size.x, size.y))
let mesh = MeshResource.generatePlane(width: tileSize, height: tileSize)
let textureResource = try! await loadTileTextureResource()
var material = UnlitMaterial(texture: textureResource)
material.textureCoordinateTransform.scale = SIMD2<Float>(x: Float(gridSize), y:Float(gridSize))
let modelEntity = ModelEntity(mesh: mesh, materials: [material])
content.add(modelEntity)
}
}
}
}
fileprivate func loadTileTextureResource(url: URL = URL(string: "https://matt54.github.io/Resources/floor_tile_1.png")!) async throws -> TextureResource {
let (data, _) = try await URLSession.shared.data(from: url)
let image = UIImage(data: data)!
let cgImage = image.cgImage!
return try await TextureResource(image: cgImage, options: .init(semantic: nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment