6

I'm trying to load a UIView into SceneKit with a translucent background, but currently, it just fades to white as I decrease the opacity. I have a very simple UIView layout in a .xib file that I want to load into SceneKit. So far I can display the UIView in the SCNMaterial, change any text fields, images, etc inside the view without a problem. However, I cannot let it have transparency. If I change the alpha of the view it just fades to white.

most of the code is the below:

if let cardView = Bundle.main.loadNibNamed("OverlayCard", owner: self, options: nil)?.first as? OverlayCard {
  cardView.backgroundColor = UIColor(displayP3Red: 1.0, green: 0.4, blue: 0.9, alpha: 0.2)
  let newplane = SCNPlane()
  let newMaterial = SCNMaterial()
  cardView.alpha = 0.2
  cardView.isOpaque = false
  newMaterial.diffuse.contents = cardView
  newMaterial.blendMode = .add
  newplane.materials = [newMaterial]
  let viewNode = SCNNode(geometry: newplane)
  self.addChildNode(viewNode)
}

I've left in various things like assigning the blendMode, backgroundColor with 0.2 opacity as well as the entire view because these are all things I've tried but still get a white background with some of the view's elements on top of the white very faded out. Have also tried blendMode = .alpha to find no difference. 'self' here is a subclass of SCNNode.

Does anyone know how I can make this view's background fade to transparent, rather than fade to white? Or, another way to load a view into SceneKit.

Yash Rathod
  • 141
  • 1
  • 7
maxxfrazer
  • 1,173
  • 6
  • 15
  • The diffuse property only defines the color of the material, not the transparency. What happens if you set `cardView` as the `transparent` content of the material? – EmilioPelaez Nov 21 '17 at 19:48
  • Doing that just gives me back a white square where the view should be – maxxfrazer Nov 21 '17 at 20:05
  • I've tried to accomplish the same thing with mixed results. Setting *both* the diffuse and transparent colors seems like it should work, but it doesn't. However, setting the diffuse to the view and the transparent to *an image generated from the view* seems to work, though it may not be performant/scale well – Dfowj Feb 14 '18 at 21:37
  • @maxxfrazer I have the same problem, did you solve it or did you apply any workaround it would help me a lot, thanks – user1241006 Aug 31 '18 at 11:22
  • The secret is you can't do **views** you can only do **layers**. – Fattie Nov 21 '22 at 13:27

1 Answers1

2

Try setting the layer of the view as diffuse.contents instead of the view itself.

newMaterial.diffuse.contents = cardView.layer