IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> CocosCreator Label优化 -> 正文阅读

[游戏开发]CocosCreator Label优化

1.需要修改引擎,参阅引擎定制工作流程

2.BITMAP缓存优化原理,参阅【乐府】突破Label的缓存模式之(1) BITMAP - Creator - Cocos中文社区

3.查看动态合图的api

运行时在console中输入

开启

cc.dynamicAtlasManager.showDebug(true);

关闭

cc.dynamicAtlasManager.showDebug(false);

4.修改记录

cocos2d/core/renderer/utils/label/ttf.js


_calDynamicAtlas (comp) {

????????if(comp.cacheMode !== Label.CacheMode.BITMAP) return;

????????let frame = comp._frame;

????????// Delete cache in atlas.

????????// mrlizs

????????// deleteFromDynamicAtlas(comp, frame);

????????if (!frame._original) {

????????????frame.setRect(cc.rect(0, 0, _canvas.width, _canvas.height));

????????}

????????// mrlizs

????????// 给frame的texture计算一个新的_uuid值

????????frame._texture._uuid = comp.string + "_" + comp.node.color + "_" + comp.fontSize + comp.fontFamily;

????????this.packToDynamicAtlas(comp, frame);

????}

cocos2d/core/renderer/utils/utils.js

deleteFromDynamicAtlas (comp, frame) {

????????if (frame && !CC_TEST) {

????????????if (frame._original && dynamicAtlasManager) {

????????????????// mrlizs

????????????????// dynamicAtlasManager.deleteAtlasSpriteFrame(frame);

????????????????frame._resetDynamicAtlasFrame();

????????????}

????????}

????}

cocos2d/core/renderer/utils/dynamic-atlas/atlas.js

insertSpriteFrame (spriteFrame) {

????????let rect = spriteFrame._rect,

????????????texture = spriteFrame._texture;

????????    // mrlizs

????????????// info = this._innerTextureInfos[texture._id];

????????// 在这里通过_uuid来判断重用性

????????// this._dynamicTextureRect 增加的一个字典用来记录uuid与texture的映射关系

????????let info = this._dynamicTextureRect[texture._uuid];

????????let sx = rect.x, sy = rect.y;

????????if (info) {

????????????sx += info.x;

????????????sy += info.y;

????????}

????????else {

????????????let width = texture.width, height = texture.height; ???????

????????????if ((this._x + width + space) > this._width) {

????????????????this._x = space;

????????????????this._y = this._nexty;

????????????}

????????????if ((this._y + height + space) > this._nexty) {

????????????????this._nexty = this._y + height + space;

????????????}

????????????if (this._nexty > this._height) {

????????????????return null;

????????????}

????????????// texture bleeding

????????????if (cc.dynamicAtlasManager.textureBleeding) {

????????????????// Smaller frame is more likely to be affected by linear filter

????????????????if (width <= 8 || height <= 8) {

????????????????????this._texture.drawTextureAt(texture, this._x-1, this._y-1);

????????????????????this._texture.drawTextureAt(texture, this._x-1, this._y+1);

????????????????????this._texture.drawTextureAt(texture, this._x+1, this._y-1);

????????????????????this._texture.drawTextureAt(texture, this._x+1, this._y+1);

????????????????}

????????????????this._texture.drawTextureAt(texture, this._x-1, this._y);

????????????????this._texture.drawTextureAt(texture, this._x+1, this._y);

????????????????this._texture.drawTextureAt(texture, this._x, this._y-1);

????????????????this._texture.drawTextureAt(texture, this._x, this._y+1);

????????????}

????????????this._texture.drawTextureAt(texture, this._x, this._y);

????????????this._innerTextureInfos[texture._id] = {

????????????????x: this._x,

????????????????y: this._y,

????????????????texture: texture

????????????};

????????????// mrlizs

????????????this._dynamicTextureRect[texture._uuid] = {

????????????????x: this._x,

????????????????y: this._y

????????????};

????????????this._count++;

????????????sx += this._x;

????????????sy += this._y;

????????????this._x += width + space;

????????????this._dirty = true;

????????}

????????let frame = {

????????????x: sx,

????????????y: sy,

????????????texture: this._texture

????????}

????????

????????this._innerSpriteFrames.push(spriteFrame);

????????return frame;

????},

????// mrlizs

????fetchSpriteFrame(spriteFrame) {

????????let texture = spriteFrame._texture;

????????let info = this._dynamicTextureRect[texture._uuid];

????????if (!info) {

????????????return null;

????????} ???

????????let rect = spriteFrame._rect;

????????let sx = rect.x + info.x, sy = rect.y + info.y;

????????let frame = {

????????????????x: sx,

????????????????y: sy,

????????????????texture: this._texture

????????????}

????????if (!this._innerSpriteFrames.includes(spriteFrame)) {

????????????this._innerSpriteFrames.push(spriteFrame);

????????}

????????return frame;

????},

????deleteInnerTexture (texture) {

????????if (texture && this._innerTextureInfos[texture._id]) {

????????????delete this._innerTextureInfos[texture._id];

????????????// mrlizs

????????????delete this._dynamicTextureRect[texture._uuid];

????????????for(let i = 0, n = this._innerSpriteFrames.length; i < n; i++){

????????????????if (this._innerSpriteFrames[i]._texture._uuid == texture._uuid){

????????????????????this._innerSpriteFrames.splice(i,1);

????????????????????break;

????????????????}

????????????}

????????????this._count--;

????????}

????}

cocos2d/core/renderer/utils/dynamic-atlas/manager.js

insertSpriteFrame(spriteFrame) {

????????if (CC_EDITOR) return null;

????????if (!_enabled || _atlasIndex === _maxAtlasCount ||

????????????!spriteFrame || spriteFrame._original) return null;

????????if (!spriteFrame._texture.packable) return null;

????????// mrlizs

????????for (let i = 0, n = _atlases.length; i < n; i++) {

????????????let atlas = _atlases[i];

????????????// 如果能从当前图集中找到相同_uuid的图集块,则重用

????????????let frame = atlas.fetchSpriteFrame(spriteFrame);

????????????if (frame != null) return frame;

????????}

????????let atlas = _atlases[_atlasIndex];

????????if (!atlas) {

????????????atlas = newAtlas();

????????}

????????let frame = atlas.insertSpriteFrame(spriteFrame);

????????if (!frame && _atlasIndex !== _maxAtlasCount) {

????????????atlas = newAtlas();

????????????return atlas.insertSpriteFrame(spriteFrame);

????????}

????????return frame;

????}

5.效果

修改前,原引擎对label的缓存是在onEnable时都会把上一次已缓存的纹理清掉然后重新创建,而实际上动态合图里有大量重复的label纹理,运行cc.dynamicAtlasManager.showDebug(true);

时可以看到合图内容。

修改后,首先不再清掉动态合图里的缓存纹理,然后label的纹理会先去动态合图中找已经存在的图块,如果存在则复用,不存在再创建。

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-18 10:33:04  更:2021-09-18 10:34:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 19:24:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码