ios – add padding to an object in a SceneKit in Swift

0
18
ios –  add padding to an object in a SceneKit in Swift


So I’m attempting to make one thing in SceneKit, however no matter I do it all the time takes up the entire dimension of the container it’s in. I want to change that, by perhaps including a bit padding throughout, or having the ability to dynamically change the dimensions of it. For testing functions, I’m utilizing a circle to check it, but it surely all the time appears to evolve to the dimensions of the SceneKit.

right here is the SceneKit that I created:

struct SceneKitView: UIViewRepresentable {
    let imageName: String

    func makeUIView(context: Context) -> SCNView {
        let sceneView = SCNView()
        sceneView.scene = makeScene()
        sceneView.allowsCameraControl = true
        sceneView.backgroundColor = UIColor.black
        return sceneView
    }

    func updateUIView(_ uiView: SCNView, context: Context) {
        // No dynamic updates wanted but
    }

    personal func makeScene() -> SCNScene {
        let scene = SCNScene()
        let badgeNode = createBadgeNode(from: imageName)
        scene.rootNode.addChildNode(badgeNode)

        return scene
    }

    personal func createBadgeNode(from imageName: String) -> SCNNode {
        guard let picture = UIImage(named: imageName) else {
            fatalError("Picture (imageName) not present in property.")
        }

        let screenWidth = UIScreen.foremost.bounds.width
        let screenHeight = UIScreen.foremost.bounds.top
        let horizontalPadding: CGFloat = 40 // <-- appears to do nothing
        let maxBadgeWidth = screenWidth - horizontalPadding
        let aspectRatio = picture.dimension.width / picture.dimension.top

        let badgeWidth = Float(min(maxBadgeWidth, screenHeight * aspectRatio)) / 1000
        let badgeHeight = badgeWidth / Float(aspectRatio)

        let badgeGeometry = SCNPlane(width: CGFloat(badgeWidth), top: CGFloat(badgeHeight))
        let badgeMaterial = SCNMaterial()
        badgeMaterial.diffuse.contents = picture
        badgeMaterial.isDoubleSided = true
        badgeGeometry.supplies = [badgeMaterial]

        let badgeNode = SCNNode(geometry: badgeGeometry)
        badgeNode.place = SCNVector3(0, 0, 0)

        return badgeNode
    }
}

And right here is the contentView, I simply have these two views:

struct ContentView: View {
    @State personal var selectedImage: UIImage?
    var physique: some View {
        VStack {
            SceneKitView(imageName: "Test1")
                .body(width: 300, top: 300)
                //.edgesIgnoringSafeArea(.all)
        }
    }
}

I’ve tried to regulate the values within the createBadgeNode operate, but when I attempted to alter the worth within the badgeGeometry = SCNPlane(width: CGFloat(badgeWidth), top: CGFloat(badgeHeight)) it solely appears to have an effect on the peak of the thing within the SceneKit. If I attempt to change it in each of them it adjustments the form an excessive amount of. I’ll present examples:

Regular (what seems in above operate):

enter image description here
enter image description here

let badgeGeometry = SCNPlane(width: CGFloat(badgeWidth) + 0.1, top: CGFloat(badgeHeight))
        let badgeMaterial = SCNMaterial()
        badgeMaterial.diffuse.contents = picture
        badgeMaterial.isDoubleSided = true
        badgeGeometry.supplies = [badgeMaterial]

enter image description here

let badgeGeometry = SCNPlane(width: CGFloat(badgeWidth) + 0.1, top: CGFloat(badgeHeight) + 0.1)
        let badgeMaterial = SCNMaterial()
        badgeMaterial.diffuse.contents = picture
        badgeMaterial.isDoubleSided = true
        badgeGeometry.supplies = [badgeMaterial]

enter image description here

let badgeGeometry = SCNPlane(width: CGFloat(badgeWidth) + 0.1, top: CGFloat(badgeHeight) + 0.5)
        let badgeMaterial = SCNMaterial()
        badgeMaterial.diffuse.contents = picture
        badgeMaterial.isDoubleSided = true
        badgeGeometry.supplies = [badgeMaterial]

enter image description here

If there may be something that I can reply, please ask.

LEAVE A REPLY

Please enter your comment!
Please enter your name here