最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-09 08:31:50
文档

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

Cocos2d-x3.1中ClippingNode的使用实例:#include 2d/CCNode.h//用到节点头文件和OPENGL深度缓冲定义头文件#include CCGL.h#include renderer/CCGroupCommand.h//用到GroupCommand和CustomCommand渲染#include renderer/CCCustomCommand.hNS_CC_BEG
推荐度:
导读Cocos2d-x3.1中ClippingNode的使用实例:#include 2d/CCNode.h//用到节点头文件和OPENGL深度缓冲定义头文件#include CCGL.h#include renderer/CCGroupCommand.h//用到GroupCommand和CustomCommand渲染#include renderer/CCCustomCommand.hNS_CC_BEG


#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


3、使用实例

class NewClippingNode : public Layer
{
public:
 CREATE_FUNC(NewClippingNode);
 virtual bool init();
};

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;

}




文档

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

Cocos2d-x3.1中ClippingNode的使用实例:#include 2d/CCNode.h//用到节点头文件和OPENGL深度缓冲定义头文件#include CCGL.h#include renderer/CCGroupCommand.h//用到GroupCommand和CustomCommand渲染#include renderer/CCCustomCommand.hNS_CC_BEG
推荐度:
标签: 使用 实例 node
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top