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):
let badgeGeometry = SCNPlane(width: CGFloat(badgeWidth) + 0.1, top: CGFloat(badgeHeight))
let badgeMaterial = SCNMaterial()
badgeMaterial.diffuse.contents = picture
badgeMaterial.isDoubleSided = true
badgeGeometry.supplies = [badgeMaterial]
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]
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]
If there’s something that I can reply, please ask.





