@gchiste It works well!! Thanks! I was also able to move the entity from a window to an immersive view.
Now, I'm trying to merge this code with the example Work with RealityComposer Pro content in Xcode so that the attachments stay facing the user. If you have any pointers or suggestions, I'd welcome them.
I'm attaching a screenshot of what my initial app
Post
Replies
Boosts
Views
Activity
@gchiste
Thanks, that's very useful code.
I'm still confused about how to add attachments that rotate with the primary entity.
Do you have any examples?
Thanks
Michele
Hi,
Here is my code.
I attach it with scene.addChild(Left_Hemisphere)
Should I use a different rotation method? The rotation of the main entity works well.
But the attachment does not move.
I used this rotation code (got it from another post here https://developer.apple.com/forums/thread/748192).
It works well and it rotates the entity over its axes.
@State private var lastGestureValueX = CGFloat(0)
@State private var lastGestureValueY = CGFloat(0)
var body: some View {
RealityView { content, attachments in
// Add the initial RealityKit content
if let scene = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
content.add(scene)
scene.generateCollisionShapes (recursive: true)
scene.components.set(InputTargetComponent())
if let Left_Hemisphere = attachments.entity(for: "Left_Hemisphere") {
//4. Position the Attachment and add it to the RealityViewContent
Left_Hemisphere.position = [-0.5, 1, 0]
scene.addChild(Left_Hemisphere)
}
}
} attachments: {
Attachment(id: "Left_Hemisphere") {
//2. Define the SwiftUI View
Text("Left_Hemisphere")
.font(.extraLargeTitle)
.padding()
.glassBackgroundEffect()
}
}
.gesture(
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
let entity = value.entity
var orientation = Rotation3D(entity.orientation(relativeTo: nil))
var newOrientation: Rotation3D
if (value.location.x >= lastGestureValueX) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .y))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .y))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueX = value.location.x
orientation = Rotation3D(entity.orientation(relativeTo: nil))
if (value.location.y >= lastGestureValueY) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .x))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .x))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueY = value.location.y
}
)
}
}
Hi,
Yes, I did.
I checked with the debugger, and it goes through that line for each attachment.
Note that if I use my "old" gesture code, it rotates along some other axis (I'm not sure which one the attachment moves>
Here is the other code, which rotates the whole thing including the attachments
Baffling
.gesture(
DragGesture(minimumDistance: 0.0)
.targetedToAnyEntity()
.onChanged {value in
let location3d = value.convert(value.location3D, from: .local, to: .scene)
let startLocation = value.convert(value.startLocation3D, from: .local, to: .scene)
let delta = location3d - startLocation
rotateByX = Double(atan(delta.x * 0.1))
rotateByY = Double(atan(delta.y * 0.1))
currentrotateByX = currentrotateByX + rotateByX
currentrotateByY = currentrotateByY + rotateByY
}
)
@Technology Evangelist If you know how to rotate not just the entity but also the attachments connected to it, please let me know.
Thanks
Michele
I found the problem.
The orientation needed to be a var and reset to the current orientation between moving the second axis.
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
let entity = value.entity
var orientation = Rotation3D(entity.orientation(relativeTo: nil))
var newOrientation: Rotation3D
// let newOrientationY: Rotation3D
if (value.location.x >= lastGestureValueX) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(1.0), axis: .y))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-1.0), axis: .y))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueX = value.location.x
orientation = Rotation3D(entity.orientation(relativeTo: nil))
if (value.location.y >= lastGestureValueY) {
newOrientation = orientation.rotated(by: .init(angle: .degrees(1.0), axis: .x))
} else {
newOrientation = orientation.rotated(by: .init(angle: .degrees(-1.0), axis: .x))
}
entity.setOrientation(.init(newOrientation), relativeTo: nil)
lastGestureValueY = value.location.y
}
)
Thanks a lot!!!!!
Is it possible to rotate in both axes?
I tried to duplicate the movement for both axes (changing .x and .y where appropriate) but it only seems to rotate in the second axis
I'm sure I made a mistake but I cannot find it:
.gesture(
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
let entity = value.entity
let orientation = Rotation3D(entity.orientation(relativeTo: nil))
let newOrientationX: Rotation3D
let newOrientationY: Rotation3D
if (value.location.x >= lastGestureValueX) {
newOrientationX = orientation.rotated(by: .init(angle: .degrees(2.0), axis: .y))
} else {
newOrientationX = orientation.rotated(by: .init(angle: .degrees(-2.0), axis: .y))
}
entity.setOrientation(.init(newOrientationX), relativeTo: nil)
lastGestureValueX = value.location.x
if (value.location.y >= lastGestureValueY) {
newOrientationY = orientation.rotated(by: .init(angle: .degrees(2.0), axis: .x))
} else {
newOrientationY = orientation.rotated(by: .init(angle: .degrees(-2.0), axis: .x))
}
entity.setOrientation(.init(newOrientationY), relativeTo: nil)
lastGestureValueY = value.location.y
}
)