14.8 C
Canberra
Thursday, October 30, 2025

ios – The way to 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 at all times takes up the entire dimension of the container it’s in. I want to change that, by possibly including a bit padding throughout, or with the ability to dynamically change the dimensions of it. For testing functions, I’m utilizing a circle to check it, but it surely at all times 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
    }

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

        return scene
    }

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

        let screenWidth = UIScreen.fundamental.bounds.width
        let screenHeight = UIScreen.fundamental.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 non-public 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 vary the worth within the badgeGeometry = SCNPlane(width: CGFloat(badgeWidth), top: CGFloat(badgeHeight)) it solely appears to have an effect on the peak of the item within the SceneKit. If I try 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’s something that I can reply, please ask.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles