Adding Gesture Control to Entities without RealityComposer

186 Views Asked by At

I wrote VisionOS code that pulls entities from a cloud bucket, which means that I can't use RealityComposer to hook components to them.

I want to use gestures to move, rotate and resize the entities like described in this Apple doc apple developer documentation

The component is loaded on app init

init(){
    GestureComponent.registerComponent()
}

and then I attempt to set the entity in a similar way like Apple documentation suggests

RealityView { content in

            guard let ent = try? await Entity(named: "Scene", in: realityKitContentBundle) else { return }

            ent.generateCollisionShapes(recursive: false)
            let inputTarget = InputTargetComponent()
            let gesture = GestureComponent()
            ent.components.set(inputTarget)
            ent.components.set(gesture)
            content.add(ent)

        }
        update: { content in

        }
        .installGestures()

The GestureComponent is the same as in the documentation and works if set using RealityComposer.

I tried setting the GestureComponent in different ways but none worked

1

There are 1 best solutions below

1
On BEST ANSWER

You have to have a InputTargetComponent, CollisionComponent and GestureComponent

    e.components.set(InputTargetComponent(allowedInputTypes: .indirect))
    
    let collision = CollisionComponent(shapes: [shape], isStatic: false)
    
    e.components.set(collision)

    e.components.set(HoverEffectComponent())
    
    let component = GestureComponent(canDrag: true, pivotOnDrag: true, preserveOrientationOnPivotDrag: false, canScale: false, canRotate: false)
    e.components.set(component)

You also have to add an init to the provided GestureComponent

public init(canDrag: Bool , pivotOnDrag: Bool , preserveOrientationOnPivotDrag: Bool , canScale: Bool , canRotate: Bool ) {
    self.canDrag = canDrag
    self.pivotOnDrag = pivotOnDrag
    self.preserveOrientationOnPivotDrag = preserveOrientationOnPivotDrag
    self.canScale = canScale
    self.canRotate = canRotate
}