问题描述
是否可以使纹理具有alpha透明度?
我有一个包含8位RGBA的png文件,但是由于某些原因,应该透明的部分只是黑色。
我这样分配材料:
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)
}
解决方法
.tintColor
是.baseColor
的乘数
如果您有一个带有预乘alpha(.png
* RGB
的{{1}}文件。您所需要做的就是另外使用一个A
实例属性,其alpha值等于tintColor
。
0.9999
这是真实代码中的样子:
material.tintColor = UIColor(white: 1.0,alpha: 0.9999)
PS
对我来说,这似乎是RealityKit中的错误。我不知道为什么方法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)
}
不能按预期工作。