Scene Kit Performance with Cube Test

Building a 3D cube in iOS with two gestures

You can use UIGestureRecognizers to accomplish what you are looking for.

in your ViewDidLoad() add your recognizer:

    // Add Gesture recognizers
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(scaleSizeOfGeometry(sender:)))
scnView.addGestureRecognizer(pinch)

Incremental scaling in the XZ directions can now be accomplished by using:

func scaleSizeOfGeometry(sender: UIPinchGestureRecognizer) {
let scale = Float(sender.scale)
referenceToGeometry?.scale.x *= scale
referenceToGeometry?.scale.z *= scale
sender.scale = 1.0
}

You have to reference the geometry you want to scale somehow. For example with a variable referenceToGeometry : SCNNode? that is set in your spawnGeometry():

referenceToGeometry = geometryNode

The UIPanGestureRecognizer is a bit more tricky since you can use a velocity or a translation to scale in the Y direction. In your viewDidLoad() add the gesture recognizer:

let pan = UIPanGestureRecognizer(target: self, action: #selector(scaleHeightOfGeometry(sender:)))
scnView.addGestureRecognizer(pan)

And in your function use either the velocity or the translation:

func scaleHeightOfGeometry(sender: UIPanGestureRecognizer) {
// y : sender.translation(in: self.scnView).y
// v : sender.velocity(in: self.scnView).y
}

You will have to extract a multiplier to your own liking from either of these. For example by storing the y[i-1] and checking the difference between the two, than adding the difference to the height of your geometry and storing the newValue as the y[i-1]th value.

EDIT:

To add to the post, you can use UIGestureRecognizer states for more advanced/finetuned versions of this.

SceneKit - Movement + Gravity = Strange Movement

Dynamic physics bodies should only be moved using physics, e.g.applyForce(_:at:asImpulse:) You may be able to get it to work by calling resetTransform() after updating the position, but at a cost to performance.

https://developer.apple.com/documentation/scenekit/scnphysicsbody/1514782-resettransform

Tap / Select Node in SceneKit (Swift)

You can hit test the scene view (for example from the location of a tap gesture recognizer), which will give you a list of hit test results. From each result you can get the node (and other information):

let location: CGPoint = // for example from a tap gesture recognizer
let hits = self.sceneView.hitTest(location, options: nil)
if let tappedNode = hits?.first?.node {
// do something with the tapped node ...
}

SceneKit SCNGeometryElement TriangleStrip vs triangles

For a triangle strip the primitiveCount should be equal to indexCount - 2.

Alternatively you can use the Swift-friendly SCNGeometryElement(indices:primitiveType:) (see also this post).



Related Topics



Leave a reply



Submit