Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIView (from xib) with transparency to SceneKit

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.

like image 829
maxxfrazer Avatar asked Nov 21 '17 19:11

maxxfrazer


Video Answer


1 Answers

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

newMaterial.diffuse.contents = cardView.layer
like image 196
matejmolnar Avatar answered Oct 22 '22 19:10

matejmolnar