RealityKit - ความโปร่งใสอัลฟ่าของวัสดุ

Aug 20 2020

เป็นไปได้ไหมที่จะมีความโปร่งใสของอัลฟาพร้อมพื้นผิว?

ฉันมีไฟล์ png ที่มี RGBA 8 บิต แต่ด้วยเหตุผลบางประการส่วนที่ควรโปร่งใสจึงเป็นสีดำ

ฉันกำหนดเนื้อหาดังนี้:

private func setupLightMeshes(_ scene: Entity) {
    let lightEntity = scene.findEntity(named: "LightWindow_Plane")!
    var lightMaterial = UnlitMaterial()
    
    lightMaterial.baseColor = try! MaterialColorParameter.texture(
    TextureResource.load(named: "light.png")) // this is 8bpc RGBA
    var modelComponent = lightEntity.components[ModelComponent] as! ModelComponent
    modelComponent = ModelComponent(mesh: modelComponent.mesh, materials: [lightMaterial])
    lightEntity.components.set(modelComponent)
}

คำตอบ

1 AndyFedoroff Aug 24 2020 at 20:18

.tintColor เป็นตัวคูณสำหรับ .baseColor

หากคุณมี.pngไฟล์ที่มีอัลฟาล่วงหน้า( RGB* A) ทั้งหมดที่คุณต้องทำคือการยังใช้คุณสมบัติเช่นแอลฟาเท่ากับtintColor0.9999

material.tintColor = UIColor(white: 1.0, alpha: 0.9999)


นี่คือลักษณะที่ปรากฏในโค้ดจริง:

fileprivate func material() -> UnlitMaterial {

    var material = UnlitMaterial()
    material.baseColor = try! .texture(.load(named: "transparent.png"))
    material.tintColor = UIColor(white: 1.0, alpha: 0.9999)
    return material
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    let sphere: MeshResource = .generateSphere(radius: 0.5)

    let entity = ModelEntity(mesh: sphere,
                        materials: [material()])

    let anchor = AnchorEntity()
    anchor.orientation = simd_quatf(angle: .pi, axis: [0, 1, 0])

    anchor.addChild(entity)
    arView.scene.anchors.append(anchor)
}

ปล

สำหรับฉันดูเหมือนว่าเป็นข้อบกพร่องใน RealityKit ฉันไม่รู้ว่าทำไมวิธีการ.load(named: "file.png")ไม่ได้ผลตามที่คาดไว้