How to specify two-sided material for RealityKit / ARView?

Viewed 1536

I'm trying to load a model and texture in RealityKit (set up in an ARView instance), but I can't seem to figure out how to specify the material should be two-sided.

I have the model loaded up as a ModelEntity, the texture loaded up as a TextureResource. The model and texture are loading up, but are rending one-sided. Since the model is open (i.e., back faces are visible), there are gaps on how it is rendered.

So far, I have,

let entity: ModelEntity = try .loadModel(named: "model.obj")

var material = SimpleMaterial()
material.baseColor = try .texture(.load(named: "texture.png"))
entity.model?.materials = [material]

I was hoping to find a property such as

material.twoSided = true

but so far, I have not found the equivalent thing in RealityKit.

Anyone know how to set two-sided materials in RealityKit?

4 Answers

There doesn't seem to be any way to do this programmatically at the moment via the RealityKit APIs.

Can you change your model definition so it doesn't do back face culling? For example in a USDZ file I am importing it defines one part of the mesh as:

def Mesh "Plane_1"
  {
    uniform bool doubleSided = 1

You might be able to convert your obj file to a used file using usdzconvert first (https://developer.apple.com/download/more/?=USDPython) then edit the file manually, then import that in to your scene.

It might also be depending on how the model is setup that you can pass in more than one material to the materials array that are applied to different parts of the model, you can see how many materials the model expects:

entity.model?.mesh.expectedMaterialCount

As others already answered you can't do it programmatically. You can however do it manually for each model via the inspection panel. See the image below. Near the bottom, you have "Double Sided" checkbox.

enter image description here

I don't think there is a way to do this. RealityKit is still in early days. Material support in RealityKit is very limited right now. I think there are plans to change this in iOS 14 or beyond. There are comments in the documentation that describe features that don't yet exist such as Material protocol says "Describes a material (colors, shaders, textures, etc.) that define the look of a mesh part." There currently is no way to define custom shaders. If you look at the RealityKit framework bundle, there are shader graph definitions and new material features that are not yet exposed in the public API. I suspect there will be a shader graph editor, support for custom shaders, and double-sided materials coming.

What you describe is called culling. Check MTLCullMode for example. From there you can jump to various points where you can set culling mode (you are interested in no culling).

Related