UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SceneKit View Inside SwiftUI Project

Forums > SwiftUI

Hi All,

I've written some lists and menu's of buttons in SwiftUI that I would like to appear ontop of a SceneKit view.

I'm yet to understand how to merge SceneKit views with SwiftUI views.

Is this possible, and might it be possible for someone to point me to an example of a basic SwiftUI view ontop of a very simple SceneKit view.

My overall goal is to have SwiftUI buttons interacting with Scenekit elements.

Many thanks.

3      

Hey , Heres a sample code for the same.

import SwiftUI import SceneKit

struct ContentView: View {
var body: some View { SceneView(scene: SCNScene(named: "Earth.isdz"), options: [.allowsCameraControl,.autoenablesDefaultLighting]) } }

3      

How does one do hit testing with the new SceneView?

3      

Ok, so you should use a ZStack to layer your controls over the top of the SceneView. Here is an example:

ZStack {
    SceneView(
        scene: scene,
        pointOfView: cameraNode,
        options: [ .autoenablesDefaultLighting, .temporalAntialiasingEnabled ]
    )
    .border(Color.black, width: 3)

    VStack {
        HStack(alignment: .top, spacing: 0, content: {
            Button("Boop") {
                print("Boop!")
            }
            .padding()
            Spacer()
        })
        Spacer()
    }

}

Your 'scene' can be any SCNScene you create, even an empty one such as:

  @State var scene = SCNScene()

3      

I don't suppose anyone knows how to do "in-scene" hit-testing, like we used to with SCNView's (or rather the SCNSceneRenderer protocol's) hitTest method? The SceneView seems to have an "allowsHitTesting" modifier, but I have yet to figure out what it does and how to use the resulting view...

3      

Sadly I came here with the same question, but this is one of the things where SwiftUI does not fit into the existing world, so I took a backward approach: I have a renderDelegate, so for every frame I am told the renderer, so either you store the info hit test info for the next frame or you rember the renderer from the last frame.

    @StateObject var cameraController: CameraController

    var dragGesture: some Gesture {
        DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
            .onChanged(cameraController.onDragChange(value:))
            .onEnded(cameraController.onDragEnded(value:))
    }

    var body: some View {
        ZStack(alignment: .bottom) {
            SceneView(scene: scene, pointOfView: camera, options: [], delegate: cameraController)
                .gesture(dragGesture)
        }
    }

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.