Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprite Blending Revisited #174

Open
jusforfun opened this issue Jun 7, 2018 · 0 comments
Open

Sprite Blending Revisited #174

jusforfun opened this issue Jun 7, 2018 · 0 comments

Comments

@jusforfun
Copy link

jusforfun commented Jun 7, 2018

Hi all,
In continuation to #138 , After #138 was resolved, in my recent usage of the plugin, have discovered additional issue regarding the sprite blend;

-Opacity Animation Which due to the removal the blend func; it reads as src blend as one which will give off the slight Additive Blend feel during the animation instead of normal fade-in/out,

//Opacity Animation test(0 ~ 255 alpha) Before Modification
opacityanimationtest

Additive Blend Mode Issue , Which resulted in a slightly duller colour;

//Additive Blend Mode test Before Modification
additivecolorblend

So i have tried to troubleshoot and have did a slight modification of creatorReader to resolve the issue of my above mentioned use case,
It may not be the best or good way;
But i would at least like to highlight possible issue that the community would encounter since i have encounter it myself;

Below is the modified Snippet of the CreatorReader.cpp

 void CreatorReader::parseSprite(cocos2d::Sprite* sprite, const buffers::Sprite* spriteBuffer) const
{

    /* As Per before*/

    // Creator doesn't premultiply alpha, so its blend function can not work in cocos2d-x.
    // const auto& srcBlend = spriteBuffer->srcBlend();
    // const auto& dstBlend = spriteBuffer->dstBlend();
    // cocos2d::BlendFunc blendFunc;
    // blendFunc.src = srcBlend;
    // blendFunc.dst = dstBlend;
    // sprite->setBlendFunc(blendFunc);

    const auto& srcBlend = spriteBuffer->srcBlend();
    const auto& dstBlend = spriteBuffer->dstBlend();

    //MODIFICATION START - If the blend function declared in creator is additive; 
    //Without setting the blend function, the color would get dull;
    if (srcBlend == GL_SRC_ALPHA && dstBlend == GL_ONE)
    {
	    cocos2d::BlendFunc blendFunc;
	    blendFunc.src = srcBlend;
	    blendFunc.dst = dstBlend;
	    sprite->setBlendFunc(blendFunc);
    } 
    //For Animation that plays around with opacity
    const AnimationRef *animRef = nodeBuffer->anim();
    if (animRef) 
    {
        const auto& animationClips = animRef->clips();
        for (const auto& fbAnimationClip : *animationClips)
        {
	        const auto& curveDatas = fbAnimationClip->curveData();
	        for (const auto& fbCurveData : *curveDatas) 
	        {
		        if (fbCurveData) 
		        {
				const AnimProps* fbAnimProps = fbCurveData->props();
				//Retrieve opacity curveData
				const auto& opacityValue = fbAnimProps->opacity();

				//If there is an opacity data, set the blend mode as per creator;
				//As without setting blend from creator, 
				//the Blend func read would be src = GL_ONE; dst = GL_ONE_MINUS_ALPHA
				//It will give off an additive effect instead of normal opacity animation;
				if (opacityValue->size() != 0)
				{
				    cocos2d::BlendFunc blendFunc;
				    blendFunc.src = srcBlend;
				    blendFunc.dst = dstBlend;
				    sprite->setBlendFunc(blendFunc);
				}
		        }
	        }
        }
    }
     //MODIFICATION END 
    /* As Per before*/
}

End result of the modification

//OpacityAnimation (0 ~ 255 alpha)
opacityanimationtest_ok

//Additive Blend
additivecolorblend_ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant