Search code examples
ioscore-animationcalayerscenekitcashapelayer

SceneKit CALayer unsharp drawing


I'm trying to draw UIBezierPaths and display them on a SCNPlane, though I'm getting a very unsharp result (See in the image) How could I make it sharper?

let displayLayer = CAShapeLayer()
  displayLayer.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
  displayLayer.path = path.cgPath
  displayLayer.fillColor = UIColor.clear.cgColor
  displayLayer.strokeColor = stroke.cgColor
  displayLayer.lineWidth = lineWidth
  let plane = SCNPlane(width:size.width, height: size.height)
  let material = SCNMaterial()
  material.diffuse.contents = displayLayer
  plane.materials = [material]
  let node = SCNNode(geometry: plane)

I've tried to increase the displayLayer.frame's size but it only made things smaller.

Thanks for your answer! Andras


Solution

  • This has been driving me crazy, so hopefully this answer helps someone else who stumbles across this. I'm mapping a CALayer to a SCNMaterial texture for use in an ARKit scene, and everything was blurry/pixelated. Setting the contentsScale property of the CALayer to UIScreen.main.scale didn't do anything, nor playing with shouldRasterize and rasterizationScale.

    The solution is to set the layer's frame to the desired frame, multiplied by the screen's scale, otherwise the material is scale transforming the layer to fit the texture. In other words, the layer's size needs to be 3x its desired size.

    let layer = CALayer() layer.frame = CGRect(x: 0, y: 0, width: 500 * UIScreen.main.scale, height: 750 * UIScreen.main.scale)