赞
踩
_hasPremultipliedAlpha
保持为初始值false
, 引擎核心代码中的类基本都会根据该变量正确设置混合模式即{ backend::BlendFactor::SRC_ALPHA, backend::BlendFactor::ONE_MINUS_SRC_ALPHA}
,所以渲染没什么问题void SkeletonRenderer::initialize () {
_clipper = new (__FILE__, __LINE__) SkeletonClipping();
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
setTwoColorTint(false);
_skeleton->setToSetupPose();
_skeleton->updateWorldTransform();
}
void SkeletonRenderer::setupGLProgramState (bool twoColorTintEnabled) { Texture2D *texture = nullptr; for (int i = 0, n = _skeleton->getSlots().size(); i < n; i++) { Slot* slot = _skeleton->getDrawOrder()[i]; Attachment* const attachment = slot->getAttachment(); if (!attachment) continue; if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { RegionAttachment* regionAttachment = static_cast<RegionAttachment*>(attachment); texture = static_cast<AttachmentVertices*>(regionAttachment->getRendererObject())->_texture; } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { MeshAttachment* meshAttachment = static_cast<MeshAttachment*>(attachment); texture = static_cast<AttachmentVertices*>(meshAttachment->getRendererObject())->_texture; } else { continue; } if (texture != nullptr) { break; } } if (texture != nullptr && texture->hasPremultipliedAlpha()) { _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setOpacityModifyRGB(true); } else { _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; setOpacityModifyRGB(false); } }
这种方案,可以解决spine多数渲染问题,然而,当包含 BlendMode_Screen 模式的部件时,渲染依然不正常(和png对比),查看代码如下:
BlendFunc makeBlendFunc(BlendMode blendMode, bool premultipliedAlpha) { BlendFunc blendFunc; switch (blendMode) { case BlendMode_Additive: blendFunc.src = premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA; blendFunc.dst = backend::BlendFactor::ONE; break; case BlendMode_Multiply: blendFunc.src = backend::BlendFactor::DST_COLOR; blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA; break; case BlendMode_Screen: blendFunc.src = backend::BlendFactor::ONE; blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_COLOR; break; default: blendFunc.src = premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA; blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA; } return blendFunc; }
怀疑是BlendMode_Screen没有正确设置混合方式造成,待考究
void main()
{
vec4 texColor = texture2D(u_texture, v_texCoord);
texColor.rgb *= texColor.a; // Premultiply with Alpha channel
gl_FragColor = v_fragmentColor * texColor;
}
_hasPremultipliedAlpha
设置为true,这样做可以达到使用png,astc,etc2的spine渲染效果一致的目的,但是修改需要覆盖到引擎的其他内置shaderconst char* etc1_frag = R"( #ifdef GL_ES precision mediump float; #endif varying vec4 v_fragmentColor; varying vec2 v_texCoord; uniform sampler2D u_texture; uniform sampler2D u_texture1; void main() { vec4 texColor = vec4(texture2D(u_texture, v_texCoord).rgb, texture2D(u_texture1, v_texCoord).r); texColor.rgb *= texColor.a; // Premultiply with Alpha channel gl_FragColor = v_fragmentColor * texColor; } )";
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。