Skip to content

Instantly share code, notes, and snippets.

@eurobob
Created December 2, 2024 13:47
Show Gist options
  • Select an option

  • Save eurobob/e8f34e5138b83d45c06b238b1d96e66b to your computer and use it in GitHub Desktop.

Select an option

Save eurobob/e8f34e5138b83d45c06b238b1d96e66b to your computer and use it in GitHub Desktop.
Basic reality kit volume with draggable sphere
//
// ContentView.swift
// SphereTest
//
//
import SwiftUI
import RealityKit
struct ContentView: View {
var body: some View {
RealityView { content in
// Create the sphere entity
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.1))
sphere.position = [0, 0, 0] // Position the sphere 0.5 meters in front of the user
// var material = try! await ShaderGraphMaterial
// Apply a material
sphere.model?.materials = [SimpleMaterial(color: .blue, isMetallic: false)]
// Enable interactions
sphere.components.set(HoverEffectComponent(.spotlight(HoverEffectComponent.SpotlightHoverEffectStyle(color: .green, strength: 2.0))))
sphere.components.set(InputTargetComponent())
sphere.components.set(CollisionComponent(shapes: [.generateSphere(radius: 0.1)]))
// Add the sphere to the RealityKit content
content.add(sphere)
}
.gesture(DragGesture()
.targetedToAnyEntity()
.onChanged { value in
let newLocation = value.convert(value.location3D, from: .local, to: value.entity.parent!)
value.entity.move(to: Transform(translation: newLocation), relativeTo: value.entity.parent!, duration: 0.5)
}
.onEnded { value in
value.entity.move(to: Transform(translation: [0, 0, 0]), relativeTo: value.entity.parent!, duration: 0.5)
}
)
}
}
#Preview(windowStyle: .volumetric) {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment