0

I'd like to simulate the shift of a tilt-shift/perspective-control lens in Scene Kit on MacOS.

Imagine the user has the camera facing a tall building at ground level, I'd like to be able to shift the 'lens' so that the projective distortion shifts (see e.g. Wikipedia).

Apple provides lots of physically-based parameters for SCNCamera (sensor height, aperture blade count), but I can't see anything obvious for this. It seems to exist in Unity.

Crucially I'd like to shift the lens so that the object stays in the same position relative to the camera. Obviously I could move the camera to get the effect, but the object needs to stay centred in the viewport (and I can't see a way to modify the viewport either). I've tried to modify the .projectionTransform matrix directly, but it was unsuccessful.

Thanks!

2 Answers 2

0

There's is no API on SCNCamera that does that out of the box. As you guessed one has to create a custom projection matrix and set it to the projectionTransform property.

1
  • Figured as much. Any chance you have an example of how to modify the projection matrix to achieve this effect? I’ve tried to run through the maths myself, but so far only have managed to get a skew effect. Jul 5, 2021 at 21:05
0

I finally worked out the correct adjustment to the projection matrix – it's quite confusing to follow the maths, because it is a 4x4 matrix rather than 3x4 or 4x3 as you'd use for a plain camera projection matrix, which additionally makes it especially confusing to work out whether it is expecting row vectors or column vectors.

Anyway, the correct element is .m32 for the y axis

let camera = SCNNode()
camera.camera = SCNCamera()

let yShift: CGFloat = 1.0

camera.camera!.projectionTransform.m32 = yShift

Presumably .m31 will shift in the x axis, but I have to admit I haven't tested this.

When I thought about it a bit more, I also realised that the effect I actually wanted involves moving the camera too. Adjusting .m32 simulates moving the sensor, which will appear to move the subject relative to the camera, as if you had a wide angle lens and you were moving the crop. To keep the subject centred in frame, you need to move the camera's position too.

With a bit (a lot) of help from this blog post and in particular this code, I implemented this too:

let distance: CGFloat = 1.0 // calculate distance from subject here
let fovRadians = camera.camera!.fieldOfView * CGFloat.pi / 180.0
let yAdjust = tan(fovRadians / 2) * distance * yShift
camera.position = camera.position - camera.worldUp * yAdjust

(any interested readers could presumably work out the x axis shift from the source above)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.