I have a SceneKit scene in which the camera looks down at a single sphere that rolls around atop a flat plane.
The camera has the default configuration, and is positioned at SCNVector3(0.0, 0.0, 100.0).
The sphere starts out in the center of the screen. At this point, the sphere looks normal, as seen in this screenshot.
But the farther away from the center it moves, the more warped/stretched it appears. As seen in this screenshot, the sphere appears warped (it looks like an egg) when it's near the edge of the screen.
The sphere is configured like this:
let ballNode = SCNNode()
let sphereGeometry = SCNSphere(radius: MainData.screenWidth*0.005)
let targetMaterial = SCNMaterial()
targetMaterial.diffuse.contents = UIColor.red
sphereGeometry.materials = [targetMaterial]
sphereGeometry.firstMaterial?.diffuse.contents = targetMaterial
ballNode.geometry = sphereGeometry
ballNode.geometry?.firstMaterial?.lightingModel = .phong
ballNode.position = SCNVector3(0.0,0.0,20.0)
ballNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic, shape: SCNPhysicsShape.init(node: ballNode))
ballNode.physicsBody?.categoryBitMask = CollisionTypes.dynamicObjects
ballNode.physicsBody?.collisionBitMask = CollisionTypes.staticObjects
ballNode.physicsBody?.contactTestBitMask = CollisionTypes.nothing
ballNode.physicsBody?.isAffectedByGravity = true
ballNode.physicsBody?.allowsResting = false
scnView.scene?.rootNode.addChildNode(ballNode)
The "plane" is actually just an SCNBox node, and is configured like this:
let platform = SCNNode()
let platformGeometry = SCNBox(width: MainData.screenWidth, height: MainData.screenHeight, length: 2.0, chamferRadius: 0.0)
platform.geometry = platformGeometry
platform.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.static, shape: SCNPhysicsShape.init(node: platform))
platform.physicsBody?.categoryBitMask = CollisionTypes.staticObjects
platform.physicsBody?.collisionBitMask = CollisionTypes.dynamicObjects
platform.physicsBody?.contactTestBitMask = CollisionTypes.nothing
platform.physicsBody?.isAffectedByGravity = false
platform.physicsBody?.allowsResting = true
scnView.scene?.rootNode.addChildNode(platform)
Question: Why does the sphere appear warped (like an egg) the farther it travels from the center of the scene/screen?
Thanks for your help!
I don't really understand why, but adding the following solved the problem:
cameraNode.camera?.fieldOfView = 20.0
Related
I have a SceneKit scene in which the camera is stationary, and is positioned like this: cameraNode.position = SCNVector3(0.0, 0.0, 100.0). Other than that, the camera has the default configuration.
In the scene is a single, spherical SCNNode with a physics body.
Below the sphere is a flat plane, with a physics body, on which the sphere rolls around. The plane is positioned in the center of the scene, at SCNVector3(0.0, 0.0, 0.0).
What I need is for the scene to be surrounded by invisible "walls" that are positioned exactly at the edges of the screen. The sphere should bounce off these static physics bodies so it never leaves the screen.
I've tried placing one of these "walls" (an SCNNode with an SCNBox geometry) using the actual screen dimensions, but the positioning is incorrect; the node is apparently off screen. This is presumably because SceneKit coordinates are in meters, not pixels or whatever.
Question: How can I figure out the positioning of the "walls" so that they are fixed to the edges of the screen?
Thanks for your help!
Use the SCNSceneRenderer's unprojectPoint(_:) method to convert left and right edges of the screen to 3D space coordinates and add two planes in those coordinates.
let leftPoint = SCNVector3(scnView.bounds.minX, scnView.bounds.midY, 1.0)
let righPoint = SCNVector3(scnView.bounds.maxX, scnView.bounds.midY, 1.0)
let leftPointCoords = scnView.unprojectPoint(leftPoint)
let rightPointCoords = scnView.unprojectPoint(righPoint)
let rightPlane = SCNPlane(width: 100.0, height: 100.0)
let leftPlane = SCNPlane(width: 100.0, height: 100.0)
let rightPlaneNode = SCNNode(geometry: rightPlane)
rightPlaneNode.eulerAngles = .init([0.0, .pi / 2, 0.0])
rightPlaneNode.physicsBody = .init(type: .static, shape: nil)
let leftPlaneNode = SCNNode(geometry: leftPlane)
leftPlaneNode.physicsBody = .init(type: .static, shape: nil)
leftPlaneNode.eulerAngles = .init([0.0, .pi / 2, 0.0])
rightPlaneNode.position = rightPointCoords
leftPlaneNode.position = leftPointCoords
scene.rootNode.addChildNode(rightPlaneNode)
So, obviously there's a mathematical solution to this problem, and that's the best way to do this. Unfortunately, I'm not very good at math, so I had to come up with another solution.
First, I create the wall to be located at the top edge of the screen. It will begin at the center of the scene:
let topEdge = SCNNode()
let topEdgeGeo = SCNBox(width: MainData.screenWidth, height: 5.0, length: 5.0, chamferRadius: 0.0)
topEdge.geometry = topEdgeGeo
topEdge.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.kinematic, shape: SCNPhysicsShape.init(node: topEdge))
topEdge.physicsBody?.categoryBitMask = CollisionTypes.kinematicObjects.rawValue
topEdge.physicsBody?.collisionBitMask = CollisionTypes.dynamicObjects.rawValue
topEdge.physicsBody?.isAffectedByGravity = false
topEdge.physicsBody?.allowsResting = true
topEdge.physicsBody?.friction = 0.0
topEdge.position = SCNVector3(0.0, 0.0, 2.5)
scnView.scene?.rootNode.addChildNode(topEdge)
I then repeatedly reposition the wall a little bit farther up the y axis until it's no longer within the camera's viewport:
var topWallIsOnScreen: Bool = scnView.isNode(topEdge, insideFrustumOf: scnView.pointOfView!)
while topWallIsOnScreen {
topEdge.position.y += 0.001
topWallIsOnScreen = scnView.isNode(topEdge, insideFrustumOf: scnView.pointOfView!)
}
The end result is that the wall is positioned at the top edge of the screen. I was concerned about performance, but it seems to work just fine.
I'm trying to wrap my head around how to use SCNTechnique with Scenekit.
The effect I'm trying to create can easily be achieved in Unity by adding a custom shader to two materials, making objects with the "filtered" shader to only be visible when it is seen through the other shader (the portal). I've followed this tutorial to create this in Unity: https://www.youtube.com/watch?v=b9xUO0zHNXw
I'm trying to achieve the same effect in SceneKit, but I'm not sure how to apply the passes. I'm looking at SCNTechnique because it has options for stencilStates, but I'm not sure if this will work.
There are very little resources and tutorials on SCNTechnique
As mentioned in the comments above, I am not sure how to achieve an occlusion effect using Stencil Tests which as we know are fairly (and I use that term very loosely) easy to do in Unity.
Having said this, we can achieve a similar effect in Swift using transparency, and rendering order.
Rendering Order simply refers to:
The order the node’s content is drawn in relative to that of other
nodes.
Whereby SCNNodes with a larger rendering order are rendered last and vice versa.
In order to make an object virtually invisible to the naked eye we would need to set the transparency value of an SCNMaterial to a value such as 0.0000001.
So how would we go about this?
In this very basic example which is simply a portal door, and two walls we can do something like this (which is easy enough to adapt into something more substantial and aesthetically pleasing):
The example is fully commented so it should be easy to understand what we are doing.
/// Generates A Portal Door And Walls Which Can Only Be Seen From Behind e.g. When Walking Through The Portsal
///
/// - Returns: SCNNode
func portalNode() -> SCNNode{
//1. Create An SCNNode To Hold Our Portal
let portal = SCNNode()
//2. Create The Portal Door
let doorFrame = SCNNode()
//a. Create The Top Of The Door Frame
let doorFrameTop = SCNNode()
let doorFrameTopGeometry = SCNPlane(width: 0.55, height: 0.1)
doorFrameTopGeometry.firstMaterial?.diffuse.contents = UIColor.black
doorFrameTopGeometry.firstMaterial?.isDoubleSided = true
doorFrameTop.geometry = doorFrameTopGeometry
doorFrame.addChildNode(doorFrameTop)
doorFrameTop.position = SCNVector3(0, 0.45, 0)
//b. Create The Left Side Of The Door Frame
let doorFrameLeft = SCNNode()
let doorFrameLeftGeometry = SCNPlane(width: 0.1, height: 1)
doorFrameLeftGeometry.firstMaterial?.diffuse.contents = UIColor.black
doorFrameLeftGeometry.firstMaterial?.isDoubleSided = true
doorFrameLeft.geometry = doorFrameLeftGeometry
doorFrame.addChildNode(doorFrameLeft)
doorFrameLeft.position = SCNVector3(-0.25, 0, 0)
//c. Create The Right Side Of The Door Frame
let doorFrameRight = SCNNode()
let doorFrameRightGeometry = SCNPlane(width: 0.1, height: 1)
doorFrameRightGeometry.firstMaterial?.diffuse.contents = UIColor.black
doorFrameRightGeometry.firstMaterial?.isDoubleSided = true
doorFrameRight.geometry = doorFrameRightGeometry
doorFrame.addChildNode(doorFrameRight)
doorFrameRight.position = SCNVector3(0.25, 0, 0)
//d. Add The Portal Door To The Portal And Set Its Rendering Order To 200 Meaning It Will Be Rendered After Our Masks
portal.addChildNode(doorFrame)
doorFrame.renderingOrder = 200
doorFrame.position = SCNVector3(0, 0, -1)
//3. Create The Left Wall Enclosure To Hold Our Wall And The Occlusion Node
let leftWallEnclosure = SCNNode()
//a. Create The Left Wall And Set Its Rendering Order To 200 Meaning It Will Be Rendered After Our Masks
let leftWallNode = SCNNode()
let leftWallMainGeometry = SCNPlane(width: 0.5, height: 1)
leftWallNode.geometry = leftWallMainGeometry
leftWallMainGeometry.firstMaterial?.diffuse.contents = UIColor.red
leftWallMainGeometry.firstMaterial?.isDoubleSided = true
leftWallNode.renderingOrder = 200
//b. Create The Left Wall Mask And Set Its Rendering Order To 10 Meaning It Will Be Rendered Before Our Walls
let leftWallMaskNode = SCNNode()
let leftWallMaskGeometry = SCNPlane(width: 0.5, height: 1)
leftWallMaskNode.geometry = leftWallMaskGeometry
leftWallMaskGeometry.firstMaterial?.diffuse.contents = UIColor.blue
leftWallMaskGeometry.firstMaterial?.isDoubleSided = true
leftWallMaskGeometry.firstMaterial?.transparency = 0.0000001
leftWallMaskNode.renderingOrder = 10
leftWallMaskNode.position = SCNVector3(0, 0, 0.001)
//c. Add Our Wall And Mask To The Wall Enclosure
leftWallEnclosure.addChildNode(leftWallMaskNode)
leftWallEnclosure.addChildNode(leftWallNode)
//4. Add The Left Wall Enclosure To Our Portal
portal.addChildNode(leftWallEnclosure)
leftWallEnclosure.position = SCNVector3(-0.55, 0, -1)
//5. Create The Left Wall Enclosure
let rightWallEnclosure = SCNNode()
//a. Create The Right Wall And Set Its Rendering Order To 200 Meaning It Will Be Rendered After Our Masks
let rightWallNode = SCNNode()
let rightWallMainGeometry = SCNPlane(width: 0.5, height: 1)
rightWallNode.geometry = rightWallMainGeometry
rightWallMainGeometry.firstMaterial?.diffuse.contents = UIColor.red
rightWallMainGeometry.firstMaterial?.isDoubleSided = true
rightWallNode.renderingOrder = 200
//b. Create The Right Wall Mask And Set Its Rendering Order To 10 Meaning It Will Be Rendered Before Our Walls
let rightWallMaskNode = SCNNode()
let rightWallMaskGeometry = SCNPlane(width: 0.5, height: 1)
rightWallMaskNode.geometry = rightWallMaskGeometry
rightWallMaskGeometry.firstMaterial?.diffuse.contents = UIColor.blue
rightWallMaskGeometry.firstMaterial?.isDoubleSided = true
rightWallMaskGeometry.firstMaterial?.transparency = 0.0000001
rightWallMaskNode.renderingOrder = 10
rightWallMaskNode.position = SCNVector3(0, 0, 0.001)
//c. Add Our Wall And Mask To The Wall Enclosure
rightWallEnclosure.addChildNode(rightWallMaskNode)
rightWallEnclosure.addChildNode(rightWallNode)
//6. Add The Left Wall Enclosure To Our Portal
portal.addChildNode(rightWallEnclosure)
rightWallEnclosure.position = SCNVector3(0.55, 0, -1)
return portal
}
Which can be tested like so:
let portal = portalNode()
portal.position = SCNVector3(0, 0, -1.5)
self.sceneView.scene.rootNode.addChildNode(portal)
Hope it points you in the right direction...
FYI there is a good tutorial here from Ray Wenderlich which will also be of use to you...
I am attempting to implement a first-person space shooter in Scenekit, and I am having the (familiar, I know) problem of getting the physics simulation match the actual position and transform of the SCNNodes the physics simulation is supposed to represent.
The enemy drone ship is created using this function, which places the node in a SCNnode called SectorObjectNode which contains all game objects external to the ship(enemies, stars, etc) and its torpedoes (both of which live in the scene's root node:
func spawnDrone(_ sender: UIButton) {
let humonshipScene = SCNScene(named: "Humon.scn")
let humonShip = humonshipScene?.rootNode.childNodes[0]
self.enemyDrone = humonShip
let droneShape = SCNBox(width: 10, height: 5, length: 5, chamferRadius: 0)
let dronePhysicsShape = SCNPhysicsShape(geometry: droneShape, options: nil)
self.enemyDrone?.physicsBody = SCNPhysicsBody(type: .dynamic, shape: dronePhysicsShape)
self.enemyDrone?.physicsBody?.isAffectedByGravity = false
self.enemyDrone?.physicsBody?.friction = 0
self.enemyDrone?.physicsBody?.categoryBitMask = 0b00000010
self.enemyDrone?.physicsBody?.contactTestBitMask = 0b00000010
self.enemyDrone?.name = "drone"
self.enemyDrone?.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5)
self.enemyDrone?.position = SCNVector3Make(0, 0, -30)
self.enemyDrone?.scale = SCNVector3Make(1, 1, 1)
let actualPosition = self.scene.rootNode.convertPosition((self.enemyDrone?.position)!, from: self.enemyDrone)
self.enemyDrone?.position = self.scene.rootNode.convertPosition(actualPosition, to: self.sectorObjectsNode)
self.sectorObjectsNode.addChildNode(self.enemyDrone!)
}
The sectorObjectsNode is rotated in reaction to onScreen joystick (thereby rotating the "universe" around the ship to simulate motion) using this code:
func turnShip() {
self.rotate(self.sectorObjectsNode, around: SCNVector3Make(1, 0, 0), by: CGFloat(self.yThrust))
self.rotate(self.sectorObjectsNode, around: SCNVector3Make(0, 1, 0), by: CGFloat(self.xThrust))
}
func rotate(_ node: SCNNode, around axis: SCNVector3, by angle: CGFloat) {
let rotation = SCNMatrix4MakeRotation(Float(angle), axis.x, axis.y, axis.z)
let newTransform = SCNMatrix4Mult(node.worldTransform, rotation)
// Set the new transform
if let parent = node.parent {
node.transform = parent.convertTransform(newTransform, from: nil)
} else {
node.transform = newTransform
}
}
But this code causes the physics simulation to reset ( The grey box in the center of the screen is the physics bounding box for the drone as depicted by the engine when sceneView.debugOptions is set to .showPhysicsShapes), with the following results:
I've tried capturing the drone's presentation position before rotation and then applying it after the two rotate functions, but this causes the ship to move down and to the left. I'm stymied as to how to get the physics simulation of the drone (which I'm using pretty exclusively for collision detection) to stick to the actual position of the enemyDrone node.
As per usual, the issue was RTFM. I set the physics body to be the wrong type:
self.enemyDrone?.physicsBody = SCNPhysicsBody(type: .dynamic, shape: dronePhysicsShape)
Needed to be changed to
self.enemyDrone?.physicsBody = SCNPhysicsBody(type: .kinematic, shape: dronePhysicsShape)
I have the following code that creates a SCNBox and shoots it on the screen. This works but as soon as I turn the phone in any other direction then the force impulse does not get updated and it always shoots the box in the same old position.
Here is the code:
#objc func tapped(recognizer :UIGestureRecognizer) {
guard let currentFrame = self.sceneView.session.currentFrame else {
return
}
/
let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
let material = SCNMaterial()
material.diffuse.contents = UIColor.red
material.lightingModel = .constant
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.01
let node = SCNNode()
node.geometry = box
node.geometry?.materials = [material]
print(currentFrame.camera.transform)
node.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)
node.physicsBody?.applyForce(SCNVector3(0,2,-10), asImpulse: true)
self.sceneView.scene.rootNode.addChildNode(node)
}
Line 26 is where I apply the force but it does not take into account the user's current phone orientation. How can I fix that?
On line 26 you're passing a constant vector to applyForce. That method takes a vector in world space, so passing a constant vector means you're always applying a force in the same direction — if you want a direction that's based on the direction the camera or something else is pointing, you'll need to calculate a vector based on that direction.
The (new) SCNNode property worldFront might prove helpful here — it gives you the direction a node is pointing, automatically converted to world space, so it's useful with physics methods. (Though you might want to scale it.)
I declare my camera like this at init:
defaultCameraNode.camera = SCNCamera()
defaultCameraNode.position = SCNVector3Make(0, 200, 500)
defaultCameraNode.camera?.zFar = 1000.0
defaultCameraNode.camera?.zNear = 10.0
defaultCameraNode.camera?.xFov = 30.0
defaultCameraNode.camera?.yFov = 30.0
scene.rootNode.addChildNode(defaultCameraNode)
sceneView.pointOfView = defaultCameraNode
defaultCameraNode.constraints = [SCNLookAtConstraint(target: rootNode)]
After this in a tapGesture block I do a hit test:
let hitResults = sceneView.hitTest(sender.locationInView(sceneView), options: nil)
This returns what I want, got the node.
After I add a new camera and change the scene's point of view
var cameraNode = SCNNode()
cameraNode.name = "cameraNode"
cameraNode.position = SCNVector3Make(position.x, position.y + 50.0, position.z + Float(radius * 3))
cameraNode.rotation = SCNVector4Make(1, 0, 0, -atan2f(20.0, 40.0))
var camera = SCNCamera()
camera.zNear = 0.0
camera.zFar = 1000.0
camera.xFov = 40.0
camera.yFov = 40.0
cameraNode.camera = camera
node.addChildNode(cameraNode)
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(animationDuration)
sceneView.pointOfView = cameraNode
SCNTransaction.commit()
When the camera position is changed the same hit test I used before returns a 0 length array and got this error on the console:
SceneKit: error, error in _C3DUnProjectPoints
Anyone can help me solving this?
thanks
I've started a new project and figured it out step by step when does the hittest go wrong. I didn't find it anywhere in the offical Apple documentation, but my experiences are the followings:
If you want to change the camera's position or any other property, you can do it by adding a new camera to a new node with new position, parameters, etc. then you set the SCNView's pointOfView property, you can do it animated like this:
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(2.0)
sceneView.pointOfView = cameraNode
SCNTransaction.commit()
One important point here: the node that holding the new SCNCamera has to be added to the SCNScene's rootView, otherwise (if you add it to the rootView's childNode) the hittest will give you an error instead the SCNNode that you touched.
It looks like you are setting another node (on that doesn't have a camera attached to it) to be the scenes point of view.
Look at your code. The node you are attaching the camera node to is cameraNode, and the node you are making the point of view is node (which you are adding the camera node to).