SceneKit – UV projection from Camera node

250 Views Asked by At

I am trying to project 360 image from camera to mesh like this.

If anyone has idea on how to do this will be helpful.

1

There are 1 best solutions below

1
On

Unlike Unity, Autodesk Maya or The Foundry NUKE, in SceneKit a camera cannot be a projector. Even if the SCNCamera could be used as a 360-projector, it would require a rig of 6 cameras with a square far clipping plane for each camera (like this), not a rectangular far clipping plane, like you're using.

Solution 1 – Gobo

To create a 360-projector, use 6 spot lights (with north-south-east-west-up-down directions) with gobo effect. Gobo is supposedly an acronym for "goes between optics".

Here's a sample code for a single spot (add each spot as a child node to your camera).

let projector = SCNNode()
projector.light = SCNLight()
projector.light?.type = .spot
projector.position.y = 10

projector.light?.gobo?.contents = UIImage(named: "imageForProjection.jpg")

projector.light?.gobo?.contentsTransform.m11 = 0.5    // texture's scale X
projector.light?.gobo?.contentsTransform.m22 = 0.5    // texture's scale Y

projector.light?.intensity = 1000
projector.eulerAngles.x = -.pi/2
camera.addChildNode(projector)

enter image description here

Also, there are three useful properties to control light's attenuation:

var attenuationStartDistance: CGFloat { get set }
var attenuationEndDistance: CGFloat { get set }
var attenuationFalloffExponent: CGFloat { get set }

Solution 2

Sometimes, you need a Procedural Ambience and an Environmental Lighting with a cubic image map.

Solution 3

To generate a spherical, cubic or cylindrical projection from scratch, use Apple Metal.