Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created January 6, 2021 13:17
Show Gist options
  • Save MickaelCruzDB/19a63e72915f5848df734fe90acc69fa to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/19a63e72915f5848df734fe90acc69fa to your computer and use it in GitHub Desktop.
func loadModel(named: String, result: ARRaycastResult) {
var usdzToLoad: String = ""
switch named {
case "DYNO":
usdzToLoad = "toy_robot_vintage.usdz"
default:
break;
}
DispatchQueue.main.async {
let modelToLoad = try! ModelEntity.load(named: usdzToLoad)
switch named {
case "DYNO":
modelToLoad.name = "DYNO"
default:
break;
}
let anchor = AnchorEntity(plane: .horizontal, classification: .any, minimumBounds: [0.1, 0.1])
anchor.position.y = 0.01
anchor.addChild(modelToLoad)
// Create a "Physics" model of the toy in order to add physics mode
guard let modelEntity = modelToLoad as? HasPhysics else { return }
self.arView.installGestures([.rotation], for: modelEntity)
modelEntity.generateCollisionShapes(recursive: true)
modelEntity.physicsBody = PhysicsBodyComponent(shapes: [.generateBox(size: .one)],
mass: 1.0,
material: .default,
mode: .kinematic)
self.currentEntity = modelEntity
self.anchorsEntities.append(anchor)
self.arView.scene.addAnchor(anchor)
let robotAnimationResource = self.currentEntity?.availableAnimations.first
self.currentEntity!.playAnimation(robotAnimationResource!.repeat(duration: .infinity),
transitionDuration: 1.25,
startsPaused: false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment