当前位置: 首页 > news >正文

转:cocos2d-x里的TiledMap出现黑线和抖动的解决方案(不影响其他类使用)

本文转载自 cocos2d-x里的TiledMap出现黑线和抖动的解决方案(不影响其他类使用)

 

今天搞tiled map发现黑边,开始认为是反锯齿问题,但发现无论开启与否都有边,只是程度不同而已。简单网上搜了下发现有解决方案是 修改ccconfig.h让

#define CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL 1
同时开启tile图块texture的反锯齿避免抖动。
 
但简单看了下CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL, 发现对texture coordinates作了圈限制。
这是原始的注释:
If enabled, the texture coordinates will be calculated by using this formula:
- texCoord.left = (rect.origin.x*2+1) / (texture.wide*2);
- texCoord.right = texCoord.left + (rect.size.width*2-2)/(texture.wide*2);
 
The same for bottom and top.
 
This formula prevents artifacts by using 99% of the texture.
The "correct" way to prevent artifacts is by using the spritesheet-artifact-fixer.py or a similar tool.
 
Affected nodes:
- CCSprite / CCSpriteBatchNode and subclasses: CCLabelBMFont, CCTMXTiledMap
- CCLabelAtlas
- CCQuadParticleSystem
- CCTileMap
 
可见影响了全部CCSprite,事实也发现Sprite只要使用的贴图是紧贴边的,周围都有可能都被切掉了一部分。
其实你只要解决tilemap的显示问题,就没必要去动到别的类。简单看了下加载tiledmap相关源码,其实只需很小改动。
 
首先不要改CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL, 让它仍然是0.
知道的是tiledmap显示部分要用到CCSprite,但这个CCSprite要CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL开启时的作用, 所以这里要新建个CCSprite的子类CCSpriteTileMap。
 
class CCSpriteTileMap : public CCSprite 
{
protected:
virtual void setTextureCoords(CCRect rect);
};
 
void CCSpriteTileMap::setTextureCoords( CCRect rect )
{
rect = CC_RECT_POINTS_TO_PIXELS(rect);
 
CCTexture2D *tex = m_pobBatchNode ? m_pobTextureAtlas->getTexture() : m_pobTexture;
if (! tex)
{
return;
}
 
float atlasWidth = (float)tex->getPixelsWide();
float atlasHeight = (float)tex->getPixelsHigh();
 
float left, right, top, bottom;
 
if (m_bRectRotated)
{
#if 1//CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left    = (2*rect.origin.x+1)/(2*atlasWidth);
right    = left+(rect.size.height*2-2)/(2*atlasWidth);
top        = (2*rect.origin.y+1)/(2*atlasHeight);
bottom    = top+(rect.size.width*2-2)/(2*atlasHeight);
#else
left    = rect.origin.x/atlasWidth;
right    = (rect.origin.x+rect.size.height) / atlasWidth;
top        = rect.origin.y/atlasHeight;
bottom    = (rect.origin.y+rect.size.width) / atlasHeight;
#endif // CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
 
if (m_bFlipX)
{
CC_SWAP(top, bottom, float);
}
 
if (m_bFlipY)
{
CC_SWAP(left, right, float);
}
 
m_sQuad.bl.texCoords.u = left;
m_sQuad.bl.texCoords.v = top;
m_sQuad.br.texCoords.u = left;
m_sQuad.br.texCoords.v = bottom;
m_sQuad.tl.texCoords.u = right;
m_sQuad.tl.texCoords.v = top;
m_sQuad.tr.texCoords.u = right;
m_sQuad.tr.texCoords.v = bottom;
}
else
{
#if 1//CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left    = (2*rect.origin.x+1)/(2*atlasWidth);
right    = left + (rect.size.width*2-2)/(2*atlasWidth);
top        = (2*rect.origin.y+1)/(2*atlasHeight);
bottom    = top + (rect.size.height*2-2)/(2*atlasHeight);
#else
left    = rect.origin.x/atlasWidth;
right    = (rect.origin.x + rect.size.width) / atlasWidth;
top        = rect.origin.y/atlasHeight;
bottom    = (rect.origin.y + rect.size.height) / atlasHeight;
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
 
if(m_bFlipX)
{
CC_SWAP(left,right,float);
}
 
if(m_bFlipY)
{
CC_SWAP(top,bottom,float);
}
 
m_sQuad.bl.texCoords.u = left;
m_sQuad.bl.texCoords.v = bottom;
m_sQuad.br.texCoords.u = right;
m_sQuad.br.texCoords.v = bottom;
m_sQuad.tl.texCoords.u = left;
m_sQuad.tl.texCoords.v = top;
m_sQuad.tr.texCoords.u = right;
m_sQuad.tr.texCoords.v = top;
}
}
 
CCTileMapAtlas.cpp里
CCTileMapAtlas::updateAtlasValueAt 函数中将 CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL强行开启
#if 1//CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
    float left        = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
    float right       = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
    float top         = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
    float bottom      = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
#else
    float left        = (row * itemWidthInPixels) / textureWide;
    float right       = left + itemWidthInPixels / textureWide;
    float top         = (col * itemHeightInPixels) / textureHigh;
    float bottom      = top + itemHeightInPixels / textureHigh;
#endif
 
 
然后找到CCTMXLayer类里面申请new CCSprite的地方替换成new  CCSpriteTileMap就OK了。
发现有两处,在这两个函数内
CCTMXLayer::reusedTileWithRect
CCTMXLayer::tileAt
 
完工!重新BUILD,就看不见恶心的黑线了,同时也不影响别的类使用。
 
想想如果cocos2d出 CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL这个定义最初目的是为了解决tiledmap显示问题的话,为什么不独立出一个针对tiledmap显示问题的编译项呢?当然还有其他目的就另当别论了。
 
注明下笔者用的cocos2d-x版本是2.1.5,其实2.0.0后版本应该都适用

转载于:https://www.cnblogs.com/cg-Yun/articles/4380316.html

相关文章:

  • 结对开发——求环形二维数组最大子矩阵和的问题
  • linux 2.6 互斥锁的实现-源码分析
  • 构建基于Nginx的web服务器
  • iOS之网络数据下载和Json数据解析
  • SQL Server技术内幕笔记合集
  • windbg vmware配置
  • [数分提高]2014-2015-2第5教学周第2次课讲义 3.2 微分中值定理
  • Clr静态数据Table-Valued函数
  • Python判断字符串编码以及编码的转换
  • 转:一个基于互联网医疗的创业公司,三年是一个收获
  • LoadRunner的Socket脚本关联小技巧
  • linux 驱动cc1101
  • (Repost) Getting Genode with TrustZone on the i.MX
  • [数分提高]2014-2015-2第5教学周第1次课
  • 【每天进步一点】毒药和老鼠的研究
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • android图片蒙层
  • Java,console输出实时的转向GUI textbox
  • JavaScript服务器推送技术之 WebSocket
  • js中forEach回调同异步问题
  • mysql 5.6 原生Online DDL解析
  • Python十分钟制作属于你自己的个性logo
  • Service Worker
  • Vue.js 移动端适配之 vw 解决方案
  • 百度小程序遇到的问题
  • 大整数乘法-表格法
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 工作手记之html2canvas使用概述
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 漂亮刷新控件-iOS
  • 前端面试之CSS3新特性
  • 日剧·日综资源集合(建议收藏)
  • 如何解决微信端直接跳WAP端
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 树莓派 - 使用须知
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • Java性能优化之JVM GC(垃圾回收机制)
  • NLPIR智能语义技术让大数据挖掘更简单
  • ​VRRP 虚拟路由冗余协议(华为)
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #LLM入门|Prompt#3.3_存储_Memory
  • #QT(串口助手-界面)
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (python)数据结构---字典
  • (九)c52学习之旅-定时器
  • (九)信息融合方式简介
  • (算法)N皇后问题
  • . NET自动找可写目录
  • .“空心村”成因分析及解决对策122344
  • .NET Framework 服务实现监控可观测性最佳实践
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET MVC之AOP
  • .net反编译的九款神器
  • .NET文档生成工具ADB使用图文教程