我是SceneKit的新手。我试图设置一个纹理,该视图是由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)
}
}
视图如下所示:
要将此视图设置为SCNSphere
的纹理,我会使用layer
并将其设置为contents
的{{1}}。
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)
}
}
,那么我会将整个球体覆盖为白色。
请告诉我这里我做错了什么。提前谢谢!