如何将CALayer设置为SCNMaterial的内容

Tak*_*ano 8 calayer ios scenekit

我是SceneKit的新手.我正在尝试设置一个纹理,该视图由a上的代码绘制SCNSphere.自定义视图如下所示:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

该视图如下所示: 在此输入图像描述

要将此视图设置为a的纹理SCNSphere,我将其取出并将layer其设置contentsfirstMaterial.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望图层覆盖所有球体,但这就是我得到的:

在此输入图像描述

只绘制了球体的某些部分.如果我注释掉所有自定义绘图代码,然后设置customView.backgroundColor = UIColor.whiteColor(),那么我将整个球体覆盖为白色.

请告诉我这里我做错了什么.提前致谢!