How to set CALayer as SCNMaterial content

I am new to SceneKit. I am trying to set a texture whose representation is drawn with SCNSphere code. The custom view is as follows:

 class CustomView : UIView { override func drawRect(rect: CGRect) { let ctx = UIGraphicsGetCurrentContext() let centerX = self.bounds.width / 2 let centerY = self.bounds.height / 2 // fill background CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor) CGContextFillRect(ctx, self.layer.frame) // draw axises CGContextSetLineWidth(ctx, 1) CGContextSetStrokeColorWithColor(ctx, UIColor.blackColor().CGColor) CGContextMoveToPoint(ctx, 0, centerY) CGContextAddLineToPoint(ctx, self.bounds.width, centerY) CGContextStrokePath(ctx) CGContextMoveToPoint(ctx, centerX, 0) CGContextAddLineToPoint(ctx, centerX, self.bounds.height) CGContextStrokePath(ctx) } } 

The view is as follows: enter image description here

To set this view as an SCNSphere texture, I took its layer and set it to contents firstMaterial .

 class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let sceneView = self.view as SCNView let scene = SCNScene() sceneView.scene = scene sceneView.backgroundColor = UIColor.blackColor() sceneView.autoenablesDefaultLighting = true let cameraNode = SCNNode() cameraNode.camera = SCNCamera() cameraNode.position = SCNVector3(x: 0, y: 0, z: 4) scene.rootNode.addChildNode(cameraNode) let sphere = SCNSphere(radius: 1) let customView = CustomView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) sphere.firstMaterial?.diffuse.contents = customView.layer let sphereNode = SCNNode(geometry: sphere) scene.rootNode.addChildNode(sphereNode) } } 

I expect the layer to cover the entire scope, but this is what I get:

enter image description here

Only part of the sphere is drawn. If I comment on the entire code of the user drawing and put customView.backgroundColor = UIColor.whiteColor() instead, then I get the whole sphere covered with white.

Please tell me what I'm doing wrong here. Thanks in advance!

+6
source share

Source: https://habr.com/ru/post/983412/


All Articles