Created
April 2, 2021 04:56
-
-
Save MasDennis/47b46c7056a09a1421b78ae006dca47b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import RealityKit | |
import Combine | |
class GameViewController: UIViewController { | |
var sceneEventsUpdateSubscription: Cancellable! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let arView = ARView(frame: view.frame, | |
cameraMode: .nonAR, | |
automaticallyConfigureSession: false) | |
view.addSubview(arView) | |
let skyboxName = "aerodynamics_workshop_2k" | |
let skyboxResource = try! EnvironmentResource.load(named: skyboxName) | |
arView.environment.lighting.resource = skyboxResource | |
arView.environment.background = .skybox(skyboxResource) | |
var sphereMaterial = SimpleMaterial() | |
sphereMaterial.metallic = MaterialScalarParameter(floatLiteral: 1) | |
sphereMaterial.roughness = MaterialScalarParameter(floatLiteral: 0) | |
let sphereEntity = ModelEntity(mesh: .generateSphere(radius: 1), materials: [sphereMaterial]) | |
let sphereAnchor = AnchorEntity(world: .zero) | |
sphereAnchor.addChild(sphereEntity) | |
arView.scene.anchors.append(sphereAnchor) | |
let camera = PerspectiveCamera() | |
camera.camera.fieldOfViewInDegrees = 60 | |
let cameraAnchor = AnchorEntity(world: .zero) | |
cameraAnchor.addChild(camera) | |
arView.scene.addAnchor(cameraAnchor) | |
let cameraDistance: Float = 3 | |
var currentCameraRotation: Float = 0 | |
let cameraRotationSpeed: Float = 0.01 | |
sceneEventsUpdateSubscription = arView.scene.subscribe(to: SceneEvents.Update.self) { _ in | |
let x = sin(currentCameraRotation) * cameraDistance | |
let z = cos(currentCameraRotation) * cameraDistance | |
let cameraTranslation = SIMD3<Float>(x, 0, z) | |
let cameraTransform = Transform(scale: .one, | |
rotation: simd_quatf(), | |
translation: cameraTranslation) | |
camera.transform = cameraTransform | |
camera.look(at: .zero, from: cameraTranslation, relativeTo: nil) | |
currentCameraRotation += cameraRotationSpeed | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just follow the tutorial strictly and all works well:
https://rozengain.medium.com/quick-realitykit-tutorial-programmatic-non-ar-setup-cafaf61e9884