Hi
I try to make a 360 stereo viewer, and I have made a ShaderGraphMaterial on Reality Composer Pro.
Im trying to use that material on a inverted sphere whitch is generated in Swift.
When I try to attach the material I get this error "Type of expression is ambiguous without a type annotation"
Here is the code (sorry im noob =) ):
import SwiftUI import RealityKit import RealityKitContent import PhotosUI
struct ImmersiveView: View {
@Environment(AppModel.self) var appModel var body: some View { RealityView { content in // Add the initial RealityKit content guard let skyBoxEntity = await createSkybox() else { return } content.add(skyBoxEntity) } }
}
private func createSkybox () async -> Entity? {
var matX = try? await ShaderGraphMaterial(named: "/Root/Mat_Stereo360", from: "360Stereo.usda", in: realityKitContentBundle) let sphere = await MeshResource.generateSphere(radius:1000) let entity = await Entity() entity.components.set(ModelComponent(mesh: sphere, materials: [matX])). //ERROR HERE: Type of expression is ambiguous without a type annotation //entity.scale *= .init(x:-1, y:1, z:1) return entity }
I hope someone can help me =)
Best regards, Kim
Hi @Kito7777
Thanks for the question!
Try initializing matX
like this:
guard let matX = try? await ShaderGraphMaterial(named: "/Root/Mat_Stereo360", from: "360Stereo.usda", in: realityKitContentBundle) else {return nil}
The error occurs because the compiler doesn't know if matX
is nil
or an instance of ShaderGraphMaterial
. One way to remedy this is to unwrap matX
.
Also consider changing:
let sphere = await MeshResource.generateSphere(radius:1000) let entity = await Entity()
to
let sphere = MeshResource.generateSphere(radius:1000) let entity = Entity()
since neither of the operations are asynchronous.