cocos2dx图片精灵裁剪圆角矩形方法

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

在做项目中,需要对图片进行裁剪已统一所有图片的边角,在网上查找方法结合自己的项目,书写了一个专门做裁剪的函数,供大家参考;


本方法是对图片的边角进行像素点的裁剪,在使用中发现如果大料使用对图片的裁剪,对程序的性能还是有点影响的,会降低cocos2dx的帧率;
 ClippingNode* ccDrawRoundRect(cocos2d::Sprite *bgSprite, cocos2d::Vec2 origin, cocos2d::Vec2 destination, float radius, unsigned int segments){


Sprite *thisbgSprite = bgSprite;
ClippingNode* pClip = ClippingNode::create();


pClip->setInverted(false);//设置是否反向,将决定画出来的圆是透明的还是黑色的
//    this->addChild(pClip);
pClip->setAnchorPoint(Point(0, 0));
pClip->setPosition(-7, -7);


//算出1/4圆


const float coef = 0.5f * (float)M_PI / segments;
Point * vertices = new Point[segments + 1];
Point * thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisVertices)
{
float rads = (segments - i)*coef;
thisVertices->x = (int)(radius * sinf(rads));
thisVertices->y = (int)(radius * cosf(rads));
}
//
Point tagCenter;
float minX = MIN(origin.x, destination.x);
float maxX = MAX(origin.x, destination.x);
float minY = MIN(origin.y, destination.y);
float maxY = MAX(origin.y, destination.y);


unsigned int dwPolygonPtMax = (segments + 1) * 4;
Point * pPolygonPtArr = new Point[dwPolygonPtMax];
Point * thisPolygonPt = pPolygonPtArr;
int aa = 0;
//左上角
tagCenter.x = minX + radius;
tagCenter.y = maxY - radius;
thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
{
thisPolygonPt->x = tagCenter.x - thisVertices->x;
thisPolygonPt->y = tagCenter.y + thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//右上角
tagCenter.x = maxX - radius;
tagCenter.y = maxY - radius;
thisVertices = vertices + segments;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
{
thisPolygonPt->x = tagCenter.x + thisVertices->x;
thisPolygonPt->y = tagCenter.y + thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//右下角
tagCenter.x = maxX - radius;
tagCenter.y = minY + radius;
thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
{
thisPolygonPt->x = tagCenter.x + thisVertices->x;
thisPolygonPt->y = tagCenter.y - thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//左下角
tagCenter.x = minX + radius;
tagCenter.y = minY + radius;
thisVertices = vertices + segments;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
{
thisPolygonPt->x = tagCenter.x - thisVertices->x;
thisPolygonPt->y = tagCenter.y - thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}


//设置参数
static Color4F red(1, 0, 0, 1);//顶点颜色设置为红色,参数是R,G,B,透明度


//注意不要将pStencil addChild
DrawNode *pStencil = DrawNode::create();
pStencil->drawPolygon(pPolygonPtArr, dwPolygonPtMax, red, 0, red);//绘制这个多边形


pStencil->setPosition(Point(0, 0));


pClip->setStencil(pStencil);




pClip->addChild(thisbgSprite, 1);
pClip->setContentSize(thisbgSprite->getContentSize());


CC_SAFE_DELETE_ARRAY(vertices);
CC_SAFE_DELETE_ARRAY(pPolygonPtArr);
          return pClip;
 }
【包教包会】CocosCreator3.x——圆角矩形(支持原生、可合批、高性能) 1、将Demo中RoundBox.ts复制到自己项目assets目录下。4、增加线段数量,可以让圆角更平滑,但也会增加计算量,具体看项目需求。3、拖一张图片到组件的Sprite Frame属性。2、新建一个节点,将RoundBox组件挂上去。EMail地址:27185709@qq.com。完美适配Web、原生平台(其余平台没测过)。其余不明白的评论区沟通~加油。 阅读详情

相关推荐

cocosCreator任意图片圆角

【代码】cocosCreator任意图片圆角

weixin_42253927的博客 1307

cocos2d-x 2.0 绘制圆角矩形

分享个cocos2d-x 2.0.x后绘制圆角矩形方法,值得一看!

【包教包会】CocosCreator Assembler经典案例第二期——圆角矩形(支持原生、可合批、附Demo源码)

用Assembler实现的图片圆角效果,方便、高效、值得收藏

CocosCreator水煮肉片饭的博客 2239

Cocos_lua_shader-master.zip

基于cocos2dx lua的shader使用,包括图片圆角,文字颜色渐变等

Cocos2d 使用shader创建圆角图片,以及添加投影

最近在使用cocos creater编写一个应用,算是深入了解了下cocos creater,总体评估只能说令人遗憾,性能问题很是明显,想到现在市面上还有许多团队使用cocos2d-js开发游戏,确实前景令人比较担忧。 项目中用到一个图片列表的展示框,图片本身是矩形,显示的时候需要圆角,通常使用mask遮罩直接修出圆角,但是实际项目中发现,4列2行八张圆角,直接20帧(性能感人。。。),额,可能...

zhaoxiaofeng44的博客 5827

Cocos2d-x 3.9教程:4.精灵的创建和基本操作

1. 精灵的创建和基本操作 1.1. 精灵的创建 ①正常创建 Sprite *sprite = Sprite::create("man.png");//通过图片创建精灵 sprite->setPosition(CCPoint(500, 500));//设置位置 this->addChild(sprite);//精灵必须被Layer添加,才能显示出来 ②创建时剪裁 Sprite *

tiancao222的博客 1223

cocoscreator 利用Mask组件裁剪圆角矩形

//计算矩形角度常量 const KAPPA90 = 0.5522847493; /** * 裁剪不规则矩形 */ cc.Class({ extends: cc.Mask, properties: { type: { override: true, get: function () { return this._type; }, visible(

Lu1030017255的博客 7291

cocos creator 切圆角处理

1、利用mask组件切出来一个圆角 创建一个空的Node节点,在该节点上添加Mask组件,Type选择ELLIPSE 在该节点下加入一个新的Sprite精灵组件,就能显示如图所示的圆角图片 2、第一步操作出来的图片会有锯齿状的效果,如图: 这是Mask组件切圆角的一个弊端,当然也可以自己写脚本去操作,偶尔遇到大批量的圆角图片展示的时候,也许会出现卡顿的现象,先不考虑脚本操作,现在使用一种讨巧...

鹤九霄的博客 1万+

cocosCreator 之 Graphics绘制基础图形,五角星,线型图,柱形图

Grphics绘制五角星、柱形图等

鹤九日的博客 5457

Cocos Creator 绘制自定义大小圆角框体(3.6)

cocos creator 绘制自定义大小圆角框体

ZQSCJHS的博客 1695

RectMask.js

Cocos Creator 利用Mask组件裁剪圆角矩形

工序升级!在 Cocos Creator 里画一个完美的矩形

在认识了 WebGL 的基础工作原理,以及如何利用 WebGL 绘制后,接下来的几章就带大家了解一下这套流程如何应用到 Cocos Creator 3.x。之前我们利用 WebGL 绘制出了一个矩形,本章先来看看要如何在 Cocos Creator 3.x 绘制同样的矩形。流程概述Cocos Creator 3.x 为了方便用户使用和定制,对内部功能做了多层封装,用户根据需求自行组装即可。因此,我...

Cocos的博客 680

【包教包会】CocosCreator3.x——重写Sprite,圆角、3D翻转、纹理循环、可合批调色板、不影响子节点的位移旋转缩放透明度

Sprite、圆角、3D翻转、纹理循环、调色板、自变换

CocosCreator水煮肉片饭的博客 1614

【进击的Shader】【入门二】Cocos Shader Effect 实现Sprite 边缘光效果,圆角裁切抗锯齿效果,高斯模糊效果

cocos creator Effect sprite 边缘光效果,圆角裁切效果,高斯模糊效果

weixin_43677520的博客 852

小游戏开发--Cocos引擎

生命周期回调函数: 节点:指cc.Node;组件:指cc.Component。 onLoad:脚本组件绑定的节点所在场景加载时系统回调一次(或者节点active从false变为true时系统回调一次)。可在这里获取场景中其它节点,并可以初始化一些不常改变的属性。 start:只在第一次update前系统回调一次。这里可以初始化一些经常改变的属性。 update(dt):每一帧渲染前系统回调,主要...

qq_30856231的博客 3227

cocos2dx-js 实现圆形头像

cocos2dx-js 实现圆形头像, 代码如下:// 首先创建一个Nodevar stencil = new cc.Node();// 接下来使用一个圆形的图片作为头像框的大小var iconHole = new cc.Sprite("img_0.png");// 在刚刚的节点上添加这个精灵图stencil.addChild(iconHole);// 再依据刚刚的Node再创建一个用于裁切的No...

wpj0304的专栏 1186

CocosCreator项目实战(09):界面美化与结束界面

文章目录 一、界面重绘 二、结束界面 一、界面重绘 重新布局界面使其更为美观。Canvas节点下主要为bg和box,box分为titleBox、tipBox、gameBox、guideBox四块。 titleBox:包含titleLabel、scoreBox、bestBox; tipBox:包含welcomeLabel、tipLabel、restartButton、rankButton; gameBox:包含gameBg节点,添加了Graphics组件由于绘制深色圆角矩形背景。

胖虎的博客 3086

CocosCreator入门】CocosCreator组件 | Graphics(绘制)组件

Cocos Creator 是一款流行的游戏开发引擎,具有丰富的组件和工具,其中Graphics组件允许您在游戏中绘制2D图形和几何形状,并通过编写脚本来控制其外观和行为。

看世界,也找自己 4476

7.cc.Sprite组件的使用详解

1. cc.Sprite 游戏中显示一个图片,通常我们把这个叫做”精灵” sprite cocos creator如果需要显示一个图片,那么需要在节点上挂一个精灵组件,为这个组件指定要显示的图片(SpriteFrame) 显示一个图片的步骤: (1) 创建一个空节点; (2) 添加一个渲染组件-->sprite; (3) 要显示的图片(SpriteFrame)...

qaz540411484的专栏 949
上一篇: Android浏览器多窗口webview界面截屏心得
下一篇: cocos2dx上如何对图片和文字做倒影(镜像)效果
Anana_JP
博客等级 码龄17年 11粉丝 18原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值