SwiftUI view as contents of SCNMaterialProperty

In our app, we have an AR part where we set a SwiftUI view as the diffuse contents on a material of a node with plane geometry. The node's position is based on the device's location at the moment a button to create it is tapped.

The view shows up fine and interaction is possible. However, after moving the device to positive values along the X and Z axes the touches seem to be offset. This makes the subviews on the rightmost side of the view unreachable. The view is scaled down because otherwise this issue happens from any camera position. Debugging view hierarchy shows the subviews' wireframe as out of bounds, but that's due to the whole view being scaled down.

What's interesting is that some views' wireframes were missing from the debug on larger devices (iPhone 11 Pro) as opposed to smaller devices (iPhone 7), where it takes getting closer to the node for this issue to happen, and I don't know if that's related to the problem or not.

I know views aren't recommended as diffuse contents, but that property is of type Any and so far it seems to work with views. Creating an image out of the view, as recommended, removes the interaction we need.

Is hit-testing / raycasting useful in this scenario to know where it landed on the view? Or is there another way to fix the touch offset?

Answered by DTS Engineer in 711272022

Hello,

The only types that are supported as material property contents are documented in SCNMaterialProperty.h.

UIView is not one of those types, and so is not supported as the contents of a material property.

I know views aren't recommended as diffuse contents, but that property is of type Any and so far it seems to work with views. 

While it may work to some degree for certain views, it is not supported, and so should not be used. Some views do not work at all as material property contents, some views don't work completely (as you are experiencing).

Accepted Answer

Hello,

The only types that are supported as material property contents are documented in SCNMaterialProperty.h.

UIView is not one of those types, and so is not supported as the contents of a material property.

I know views aren't recommended as diffuse contents, but that property is of type Any and so far it seems to work with views. 

While it may work to some degree for certain views, it is not supported, and so should not be used. Some views do not work at all as material property contents, some views don't work completely (as you are experiencing).

SwiftUI view as contents of SCNMaterialProperty
 
 
Q