Created
March 22, 2024 11:18
-
-
Save arthurschiller/7b165ae04ee6875b8e526e3831c17d05 to your computer and use it in GitHub Desktop.
visionOS ModelSortGroupComponent Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import RealityKit | |
struct ImmersiveView: View { | |
var body: some View { | |
RealityView { content in | |
// .postPass was the only one that had any visible effect for me | |
let sortGroup = ModelSortGroup(depthPass: .postPass) | |
// this represents some UI and should always be drawn in front of everything else | |
let planeContainerEntity = Entity() | |
let plane = ModelEntity( | |
mesh: .generatePlane(width: 1, height: 1, cornerRadius: 0.1), | |
materials: [SimpleMaterial(color: .green, isMetallic: true)] | |
) | |
planeContainerEntity.addChild(plane) | |
planeContainerEntity.position = [0.5, 1.5, -1.5] | |
planeContainerEntity.visit { entity in | |
entity.components.set(ModelSortGroupComponent(group: sortGroup, order: 1)) | |
} | |
content.add(planeContainerEntity) | |
// this represents models in my scene and should be drawn as usual but still respect the UI (e.g appear behind it) | |
let boxContainerEntity = Entity() | |
let box = ModelEntity( | |
mesh: .generateBox(size: 0.5, cornerRadius: 0.05), | |
materials: [ | |
SimpleMaterial(color: .red, isMetallic: true) | |
// OcclusionMaterial() | |
] | |
) | |
boxContainerEntity.addChild(box) | |
boxContainerEntity.position = [0.05, 1.5, -1.5] | |
boxContainerEntity.visit { entity in | |
entity.components.set(ModelSortGroupComponent(group: sortGroup, order: 0)) | |
} | |
content.add(boxContainerEntity) | |
let sphereContainerEntity = Entity() | |
let sphere = ModelEntity( | |
mesh: .generateSphere(radius: 0.4), | |
materials: [ | |
SimpleMaterial(color: .blue, isMetallic: true) | |
// OcclusionMaterial() | |
] | |
) | |
sphereContainerEntity.addChild(sphere) | |
sphereContainerEntity.position = [0.1, 1.45, -2] | |
sphereContainerEntity.visit { entity in | |
entity.components.set(ModelSortGroupComponent(group: sortGroup, order: 0)) | |
} | |
content.add(sphereContainerEntity) | |
} | |
} | |
} | |
extension Entity { | |
func visit(using block: (Entity) -> Void) { | |
block(self) | |
for child in children { | |
child.visit(using: block) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment