是否可以使用纹理实现 alpha 透明度?
我有包含 8 位 RGBA 的 png 文件,但由于某种原因,本应透明的部分只是黑色。
我这样分配 Material :
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)
}
最佳答案
RealityKit 1.0
.tintColor
是 .baseColor
如果您有一个带有预乘 alpha (RGB
*A
) 的 .png
文件。您需要做的就是另外使用 alpha 等于 0.9999
的 tintColor
实例属性。
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 1.0 中的一个错误。我不知道为什么方法 .load(named: "file.png")
没有按预期工作。
RealityKit 2.0
关于部分透明纹理的相同故事出现在 RealityKit 2.0 中:
var material = SimpleMaterial()
material.color = try! .init(tint: .white.withAlphaComponent(0.9999),
texture: .init(.load(named: "semi.png", in: nil)))
tint
参数也是 texture
的乘数。
关于swift - RealityKit – Material 的 Alpha 透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63505133/