0

I have 3 questions:

  1. I know when I want to set shader for Sprite object by using spr1->setShaderProgram(glProgram). However, I want to set shader for an Armature object. How can I do that?

  2. In CCSprite, I can use setBlendFunc, what about in Armature.

  3. I read this article http://blog.muditjaju.infiniteeurekas.in/?p=1 and I see the idea to detect collision between 2 sprites. But I want to write a extended function which can detect collision between different object like sprite vs sprite, sprite vs Armature, Amature vs Armature. How can I do that?

Thanks

5
  • what the heck is an Armature?
    – CodeSmile
    Mar 5, 2014 at 7:57
  • @LearnCocos2D: Read this cocos2d-x.org/reference/native-cpp/V2.2.2/d1/db8/…
    – lolyoshi
    Mar 5, 2014 at 9:58
  • Ooooh-kay. Gotta love a class reference that doesn't even mention what the class is or does. Something to do with armor? Skeletal animation?
    – CodeSmile
    Mar 5, 2014 at 10:34
  • @LearnCocos2D: So do you have any ideas for my 3 questions?
    – lolyoshi
    Mar 5, 2014 at 10:47
  • Not really, I'm not a -x user. I guess 1 & 2 may not be possible because it looks like this class is performing custom drawing. 3 is probably a polygon intersection issue, depends on whether and how you can get the armature's polygon shape.
    – CodeSmile
    Mar 5, 2014 at 11:11

1 Answer 1

0

Today I met the same situation like you. And luckily I found a solution from someone else. The main point is to write a subclass of Armature, say ShaderArmature, and rendering every bone's every child with a shader ---- in my case, the shader is for graying the armature.

You can create a ShaderArmature object and call its setgrayState() to enable graying and setUsuaState() to remove graying effect.

1, Add the ShaderArmature.h & ShaderArmature.cpp in the same directory of Armature.h.

ShaderArmature.h

//
//  Created by Wangyq on 2017/8/13.
//
//

#ifndef __SHADERARMATURE_H__
#define __SHADERARMATURE_H__

//
//---------shaderArmature.h
///
#include "cocos2d.h"
//#include "extensions/cocos-ext.h"
#include "cocostudio/CocoStudio.h"
//USING_NS_CC_EXT;
USING_NS_CC;
namespace cocostudio{
class CC_STUDIO_DLL ShaderArmature : public Armature
{
public:
    ShaderArmature();
    virtual ~ShaderArmature();

    bool init(const std::string& name) override;

    static ShaderArmature *create(const std::string& name) ;

    virtual void initShader(bool shaderState);
    //    void setBackgroundNotification();

//    draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
    virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t transformUpdated) override;
    //    void listenBackToForeground(Ref *obj);
    void setIceState();
    void setUsuaState();

    void setblurState();
    void setbanishState();
    void setfrozenState();
    void setgrayState();
    void setinvisState();
    void setmirrorState();
    void setpoisonState();
    void setstoneState();
protected:
    std::string _fragSourceFile;
    std::string _vertSourceFile;
    //GLchar * fragSource;
    //GLchar * vertSource;
    GLchar fragSource[2048];
    GLchar vertSource[2048];
    bool bSetShader;
};

}
#endif /* __SHADERARMATURE_H__ */

ShaderArmature.cpp

//
//  ShaderArmature.cpp
//  cocos2d_libs
//
//  Created by Wangyq on 2017/8/13.
//
//

#include "ShaderArmature.h"

using namespace cocos2d;


namespace cocostudio {
    ShaderArmature::ShaderArmature(){

    }
    ShaderArmature::~ShaderArmature(){

    }
    bool ShaderArmature::init(const std::string& name)
    {
        return Armature::init(name);
    }
    ShaderArmature *ShaderArmature::create(const std::string& name)
    {
        ShaderArmature *armature = new ShaderArmature;
        if (armature && armature->init(name))
        {
            armature->autorelease();
            return armature;
        }
        else{
            CC_SAFE_DELETE(armature);
            return nullptr;
        }
    }


    void ShaderArmature::initShader(bool shaderState){
        //Traverse every bone to set shader
        for (auto& object : _boneDic)
        {
            if (Bone *bone = dynamic_cast<Bone *>(object.second))
            {
                if (bone == nullptr)
                    continue;

                //-----------------!! IMPORTANT !!-----------------
                //Each bone may have more than one child, like several frames, also need to assign shader to them
                //Without this step, only the first frame will have the shader effect.
                const Vector<DecorativeDisplay*> list = bone->getDisplayManager()->getDecorativeDisplayList();
                for (auto& display : list)
                {
                    Node* node = display->getDisplay();
                    if (node == nullptr)
                        continue;

                    if (shaderState){
                        auto program = new GLProgram();
                        program->initWithByteArrays(vertSource, fragSource);


                        node->setGLProgram(program);

                        program->autorelease();

                        CHECK_GL_ERROR_DEBUG();

                        program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
                        program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
                        program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);

                        CHECK_GL_ERROR_DEBUG();

                        program->link();

                        CHECK_GL_ERROR_DEBUG();

                        program->updateUniforms();

                        CHECK_GL_ERROR_DEBUG();

                    }
                    else{//addNormal shader
                        node->setGLProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
                    }
                }
            }

        }
    }
    void ShaderArmature::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
    {
        Armature::draw(renderer, transform, flags);
    }

    void ShaderArmature::setIceState(){
        _fragSourceFile = "shader/IceShader.fsh";
        _vertSourceFile = "shader/IceShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }

    void ShaderArmature::setUsuaState(){
        initShader(false);
    }

    void ShaderArmature::setblurState(){
        _fragSourceFile = "shader/Blur.fsh";
        _vertSourceFile = "shader/Blur.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }

    void ShaderArmature::setbanishState(){
        _fragSourceFile = "shader/BanishShader.fsh";
        _vertSourceFile = "shader/BanishShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }

    void ShaderArmature::setfrozenState(){
        _fragSourceFile = "shader/FrozenShader.fsh";
        _vertSourceFile = "shader/FrozenShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }
    void ShaderArmature::setgrayState(){
        _fragSourceFile = "shader/GrayScalingShader.fsh";
        _vertSourceFile = "shader/GrayScalingShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }
    void ShaderArmature::setinvisState(){
        _fragSourceFile = "shader/InvisibleShader.fsh";
        _vertSourceFile = "shader/InvisibleShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }
    void ShaderArmature::setmirrorState(){
        _fragSourceFile = "shader/MirrorShader.fsh";
        _vertSourceFile = "shader/MirrorShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }
    void ShaderArmature::setpoisonState(){
        _fragSourceFile = "shader/PoisonShader.fsh";
        _vertSourceFile = "shader/PoisonShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }

    void ShaderArmature::setstoneState(){
        _fragSourceFile = "shader/StoneShader.fsh";
        _vertSourceFile = "shader/StoneShader.vsh";
        fragSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();

        vertSource = (GLchar*)String::createWithContentsOfFile(
            FileUtils::getInstance()->fullPathForFilename(_vertSourceFile).c_str())->getCString();
        initShader(true);
    }
}

2, For shader files, I put them in the folder "res/shader". Every shader consists of 2 files -- a .vsh file & a .fsh file. Take graying for example:

GrayScalingShader.fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;

void main()
{
    vec4 normalColor = v_fragmentColor * texture2D(u_texture, v_texCoord);

    //float gray = 0.299*0.5*normalColor.r + 0.587*0.5*normalColor.g + 0.114*0.5*normalColor.b;
    //gl_FragColor = vec4(gray*0.8 + normalColor.r*0.2, gray*0.8 + normalColor.g*0.2, gray*0.8 + normalColor.b*0.2, normalColor.a*0.3);
    float gray = dot(normalColor.rgb, vec3(0.299 * 0.5, 0.587 * 0.5, 0.114 * 0.5));
    gl_FragColor = vec4(gray, gray, gray, normalColor.a * 1);
}

GrayScalingShader.vsh

attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;

#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif

void main()
{
    //gl_Position = CC_MVPMatrix * a_position;
    gl_Position = CC_PMatrix * a_position;
    v_fragmentColor = a_color;
    v_texCoord = a_texCoord;
}

Hope this would be helpful.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.