Home > Database > Mysql Tutorial > Cocos2d-x3.1中ClippingNode的使用实例

Cocos2d-x3.1中ClippingNode的使用实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:01:11
Original
1144 people have browsed it

#include 2d/CCNode.h//用到节点头文件和OPENGL深度缓冲定义头文件#include CCGL.h#include renderer/CCGroupCommand.h//用到GroupCommand和CustomCommand渲染#include renderer/CCCustomCommand.hNS_CC_BEGIN/** ClippingNode is a subclass of Node. It dr

#include "2d/CCNode.h"//用到节点头文件和OPENGL深度缓冲定义头文件
#include "CCGL.h"
#include "renderer/CCGroupCommand.h"//用到GroupCommand和CustomCommand渲染
#include "renderer/CCCustomCommand.h"

NS_CC_BEGIN

/** ClippingNode is a subclass of Node.
 It draws its content (childs) clipped using a stencil.
 The stencil is an other Node that will not be drawn.
 The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold).
 */
class CC_DLL ClippingNode : public Node
{
public:
    /** Creates and initializes a clipping node without a stencil.
     */
    static ClippingNode* create();//静态创建对象
    
    /** Creates and initializes a clipping node with an other node as its stencil.
     The stencil node will be retained.
     */
    static ClippingNode* create(Node *stencil);//用模板创建裁剪节点

    /** The Node to use as a stencil to do the clipping.
     The stencil node will be retained.
     This default to nil.
     */
    Node* getStencil() const;//获取节点模板
    void setStencil(Node *stencil);//设置节点模板
    
    /** The alpha threshold.
     The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold.
     Should be a float between 0 and 1.
     This default to 1 (so alpha test is disabled).
     *///ALPHA测试参考值,用于进行ALPHA测试比较,一般比较算法为小于此值的像素会直接被舍弃,这样就实现了图像的镂空
    GLfloat getAlphaThreshold() const;//获取ALPHT的测试参考值
    void setAlphaThreshold(GLfloat alphaThreshold);//设置ALPHA的测试参考值
    
    /** Inverted. If this is set to true,
     the stencil is inverted, so the content is drawn where the stencil is NOT drawn.
     This default to false.
     */
    bool isInverted() const;//获取遮罩运算是否取反设置
    void setInverted(bool inverted);//设置遮罩运算取反

    // Overrides//继承的函数
    /**
     * @js NA
     * @lua NA
     */
    virtual void onEnter() override;
    /**
     * @js NA
     * @lua NA
     */
    virtual void onEnterTransitionDidFinish() override;
    /**
     * @js NA
     * @lua NA
     */
    virtual void onExitTransitionDidStart() override;
    /**
     * @js NA
     * @lua NA
     */
    virtual void onExit() override;
    virtual void visit(Renderer *renderer, const Mat4 &parentTransform, bool parentTransformUpdated) override;
    
CC_CONSTRUCTOR_ACCESS:
    ClippingNode();
    
    /**
     * @js NA
     * @lua NA
     */
    virtual ~ClippingNode();

    /** Initializes a clipping node without a stencil.
     */
    virtual bool init();
    
    /** Initializes a clipping node with an other node as its stencil.
     The stencil node will be retained, and its parent will be set to this clipping node.
     */
    virtual bool init(Node *stencil);

protected:
    /**draw fullscreen quad to clear stencil bits
    */
    void drawFullScreenQuadClearStencil();

    Node* _stencil;
    GLfloat _alphaThreshold;
    bool    _inverted;

    //renderData and callback
    void onBeforeVisit();
    void onAfterDrawStencil();
    void onAfterVisit();

    GLboolean _currentStencilEnabled;
    GLuint _currentStencilWriteMask;
    GLenum _currentStencilFunc;
    GLint _currentStencilRef;
    GLuint _currentStencilValueMask;
    GLenum _currentStencilFail;
    GLenum _currentStencilPassDepthFail;
    GLenum _currentStencilPassDepthPass;
    GLboolean _currentDepthWriteMask;

    GLboolean _currentAlphaTestEnabled;
    GLenum _currentAlphaTestFunc;
    GLclampf _currentAlphaTestRef;

    GLint _mask_layer_le;
    
    GroupCommand _groupCommand;
    CustomCommand _beforeVisitCmd;
    CustomCommand _afterDrawStencilCmd;
    CustomCommand _afterVisitCmd;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(ClippingNode);
};

NS_CC_END
Copy after login


3、使用实例

<pre name="code" class="cpp">class NewClippingNode : public Layer
{
public:
    CREATE_FUNC(NewClippingNode);
    virtual bool init();
};
Copy after login

boolNewClippingNode::init()

{

    auto s =Director::getInstance()->getWinSize();

   bool bRet = false;

   do{

       CC_BREAK_IF(!Layer::init());

        

       auto clipper = ClippingNode::create();

        clipper->setTag(kTagClipperNode);

        clipper->setContentSize(Size(200,200));//设置剪裁区域大小

        clipper->ignoreAnchorPointForPosition(false);

        clipper->setAnchorPoint(Vec2(0.5,0.5));//设置锚点

        clipper->setPosition(Vec2(s.width/2, s.height/2));

       this->addChild(clipper);

        

        clipper->setAlphaThreshold(0.05f);//设置透明度的阈值ALPHA值

        //设置剪裁模板

       auto stencil = Sprite::create("grossini.png");

        stencil->setScale(2);

        stencil->setPosition(Vec2(s.width/2, s.height/2));

        clipper->setStencil(stencil);//设置剪裁模板


    //TODO Fix draw node as clip node

//    auto stencil = NewDrawNode::create();

//    Vec2 rectangle[4];

//    rectangle[0] = Vec2(0, 0);

//    rectangle[1] = Vec2(clipper->getContentSize().width, 0);

//    rectangle[2] = Vec2(clipper->getContentSize().width, clipper->getContentSize().height);

//    rectangle[3] = Vec2(0, clipper->getContentSize().height);

//

//    Color4F white(1, 1, 1, 1);

//    stencil->drawPolygon(rectangle, 4, white, 1, white);

//    clipper->setStencil(stencil);




        //设置剪裁节点内容

       auto content = Sprite::create("background2.png");

        content->setTag(kTagContentNode);

        content->ignoreAnchorPointForPosition(false);

        content->setAnchorPoint(Vec2(0.5,0.5));

        content->setPosition(Vec2(s.width/2,200));

        clipper->addChild(content);

        

        bRet =true;

    }while(0);

   return bRet;

}




Copy after login
Related labels:
use
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template