Skip to content

Instantly share code, notes, and snippets.

@MasDennis
Created April 2, 2021 04:56
Show Gist options
  • Save MasDennis/47b46c7056a09a1421b78ae006dca47b to your computer and use it in GitHub Desktop.
Save MasDennis/47b46c7056a09a1421b78ae006dca47b to your computer and use it in GitHub Desktop.
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
}
}
}
@jluo5364
Copy link

where should I place "aerodynamics_workshop_2k" in Xcode project bundle? Thanks

@nguyenpham
Copy link

nguyenpham commented Nov 1, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment