[ios] Fix blending, see #2555

This commit is contained in:
Mario Zechner 2024-06-28 15:39:13 +02:00
parent 35d76de559
commit 7d6f0f9490

View File

@ -190,7 +190,7 @@ internal final class SpineRenderer: NSObject, MTKViewDelegate {
lastDraw = CACurrentMediaTime() lastDraw = CACurrentMediaTime()
} }
let delta = CACurrentMediaTime() - lastDraw let delta = CACurrentMediaTime() - lastDraw
delegate?.spineRendererWillUpdate(self) delegate?.spineRendererWillUpdate(self)
delegate?.spineRenderer(self, needsUpdate: delta) delegate?.spineRenderer(self, needsUpdate: delta)
lastDraw = CACurrentMediaTime() lastDraw = CACurrentMediaTime()
delegate?.spineRendererDidUpdate(self) delegate?.spineRendererDidUpdate(self)
@ -283,40 +283,75 @@ internal final class SpineRenderer: NSObject, MTKViewDelegate {
} }
} }
fileprivate extension MTLRenderPipelineColorAttachmentDescriptor { fileprivate extension BlendMode {
func sourceRGBBlendFactor(premultipliedAlpha: Bool) -> MTLBlendFactor {
func apply(blendMode: BlendMode, with premultipliedAlpha: Bool) { switch self {
isBlendingEnabled = true case SPINE_BLEND_MODE_NORMAL:
sourceRGBBlendFactor = blendMode.sourceRGBBlendFactor(premultipliedAlpha: premultipliedAlpha) return premultipliedAlpha ? .one : .sourceAlpha
destinationRGBBlendFactor = blendMode.destinationRGBBlendFactor case SPINE_BLEND_MODE_ADDITIVE:
destinationAlphaBlendFactor = .oneMinusSourceAlpha return .sourceAlpha
} case SPINE_BLEND_MODE_MULTIPLY:
return .destinationColor
case SPINE_BLEND_MODE_SCREEN:
return .one
default:
return .one // Should never be called
}
}
func sourceAlphaBlendFactor(premultipliedAlpha: Bool) -> MTLBlendFactor {
switch self {
case SPINE_BLEND_MODE_NORMAL:
return premultipliedAlpha ? .one : .sourceAlpha
case SPINE_BLEND_MODE_ADDITIVE:
return .sourceAlpha
case SPINE_BLEND_MODE_MULTIPLY:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_SCREEN:
return .oneMinusSourceColor
default:
return .one // Should never be called
}
}
var destinationRGBBlendFactor: MTLBlendFactor {
switch self {
case SPINE_BLEND_MODE_NORMAL:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_ADDITIVE:
return .one
case SPINE_BLEND_MODE_MULTIPLY:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_SCREEN:
return .oneMinusSourceColor
default:
return .one // Should never be called
}
}
var destinationAlphaBlendFactor: MTLBlendFactor {
switch self {
case SPINE_BLEND_MODE_NORMAL:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_ADDITIVE:
return .one
case SPINE_BLEND_MODE_MULTIPLY:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_SCREEN:
return .oneMinusSourceColor
default:
return .one // Should never be called
}
}
} }
fileprivate extension BlendMode { fileprivate extension MTLRenderPipelineColorAttachmentDescriptor {
func sourceRGBBlendFactor(premultipliedAlpha: Bool) -> MTLBlendFactor {
switch self { func apply(blendMode: BlendMode, with premultipliedAlpha: Bool) {
case SPINE_BLEND_MODE_NORMAL, SPINE_BLEND_MODE_ADDITIVE: isBlendingEnabled = true
return premultipliedAlpha ? .one : .sourceAlpha sourceRGBBlendFactor = blendMode.sourceRGBBlendFactor(premultipliedAlpha: premultipliedAlpha)
case SPINE_BLEND_MODE_MULTIPLY: sourceAlphaBlendFactor = blendMode.sourceAlphaBlendFactor(premultipliedAlpha: premultipliedAlpha)
return .destinationColor destinationRGBBlendFactor = blendMode.destinationRGBBlendFactor
case SPINE_BLEND_MODE_SCREEN: destinationAlphaBlendFactor = blendMode.destinationAlphaBlendFactor
return .one }
default:
return .one // Should never be called
}
}
var destinationRGBBlendFactor: MTLBlendFactor {
switch self {
case SPINE_BLEND_MODE_NORMAL, SPINE_BLEND_MODE_ADDITIVE:
return .oneMinusSourceAlpha
case SPINE_BLEND_MODE_MULTIPLY:
return .one
case SPINE_BLEND_MODE_SCREEN:
return .oneMinusSourceColor
default:
return .one // Should never be called
}
}
} }