【问题标题】:cocos2d-x v3 c++ Drop shadow cocos2d::Spritecocos2d-x v3 c++ 投影 cocos2d::Sprite
【发布时间】:2016-10-11 11:56:14
【问题描述】:

据我所知,cocos 不像 AS3 那样提供简单的过滤器处理。

我的情况: 我想为 cocos2d::Sprite 添加实时阴影。

例如我想做这样的事情(类似于AS3):

auto mySprite = Sprite::createWithSpriteFrameName("myCharacter.png");

DropShadowFilter* dropShadow = new DropShadowFilter();
dropShadow->distance = 0;
dropShadow->angle = 45;
dropShadow->color = 0x333333;
dropShadow->alpha = 1;
dropShadow->blurX = 10;
dropShadow->blurY = 10;
dropShadow->strength = 1;
dropShadow->quality = 15;

mySprite->addFilter(dropShadow);

这应该为我的 Sprite 添加一个阴影,以实现如下效果: Adobe Drop Shadow Example

你能帮帮我吗?

【问题讨论】:

    标签: c++ cocos2d-x shadow


    【解决方案1】:

    Cocos2D-X 中的Sprites 没有任何内置的阴影支持。

    就性能而言,最好的选择是将阴影放置在精灵图像中,而不是在代码中计算和绘制它们。

    另一种选择是继承Sprite 并覆盖draw 方法,以便复制精灵并应用效果并将其绘制在原始下方。

    实现这一目标的一种可能方法是使用来自this thread on the Cocos forum 的这个 sn-p。我不能说我完全遵循这段代码对 GL 转换所做的事情,但您可以以此为起点进行实验。

    void CMySprite::draw()
    {
      // is_shadow is true if this sprite is to be considered like a shadow sprite, false otherwise.@
      if (is_shadow)
      {
        ccBlendFunc blend;
        // Change the default blending factors to this one.
        blend.src = GL_SRC_ALPHA;
        blend.dst = GL_ONE;
        setBlendFunc( blend );
        // Change the blending equation to thi in order to subtract from the values already written in the frame buffer
        // the ones of the sprite.
        glBlendEquationOES(GL_FUNC_REVERSE_SUBTRACT_OES);
      }
    
      CCSprite::draw();
    
      if (is_shadow)
      {
         // The default blending function of cocos2d-x is GL_FUNC_ADD.
        glBlendEquationOES(GL_FUNC_ADD_OES);        
      }
    }
    

    【讨论】:

    • 谢谢。我去看看。
    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    相关资源
    最近更新 更多