cocoscreator 使用内置自带的资源和生成单色图片

1: 加载内置的effect和material的资源

cc.assetManager.builtins.getBuiltin("material", "builtin-" + name)
cc.assetManager.builtins.getBuiltin("effect", "builtin-" + name)

2:因为有些内置的资源打包的时候是不打到包里的,不能通过cc.assetManager.internal.get的方式获取,这种的可以通过预制体的方式解决或者直接复制一分图片在自己的resources目录里

3:动态生成单色的图片, 4个255对应的是rgba的值,需要注意的是,rgba一般调成白色255,不然,node节点设置颜色时会无效

let node = new cc.Node();
let texture = new cc.Texture2D();
let spriteFrame = new cc.SpriteFrame();

let imgWidth = 1;
let imgHeight = 1;
let count = imgWidth * imgHeight * 4;
let imgData =new Uint8Array(count);
for (var j = 0; j < count; j += 4) {
    imgData[j] = 255 // r
    imgData[j + 1] = 255 // g
    imgData[j + 2] = 255 // b
    imgData[j + 3] = 255 // a
}

texture.initWithData(imgData, cc.Texture2D.PixelFormat.RGBA8888, imgWidth, imgHeight);
// texture.initWithData(new Uint8Array([255,255,255,255]), cc.Texture2D.PixelFormat.RGBA8888, 1, 1);
spriteFrame.setTexture(texture);
spriteFrame.setRect(cc.rect(0, 0, 100, 100));
// spriteFrame.setRect(cc.rect(0, 0, width, height));

node.addComponent(cc.Sprite).spriteFrame = spriteFrame;
return node

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,实现将一张单色位图图像数据加上抗锯齿算法并生成图片: ```c++ // 创建一个单色位图 CBitmap bmp; bmp.CreateBitmap(100, 100, 1, 1, NULL); // 获取位图信息 BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); // 获取位图数据 BYTE* pData = new BYTE[bmpInfo.bmWidthBytes * bmpInfo.bmHeight]; bmp.GetBitmapBits(bmpInfo.bmWidthBytes * bmpInfo.bmHeight, pData); // 定义抗锯齿算法 int offset = 1; int weight = 4; int threshold = 128; // 遍历每个像素点 for (int y = 0; y < bmpInfo.bmHeight; y++) { for (int x = 0; x < bmpInfo.bmWidth; x++) { // 获取当前像素值 BYTE val = pData[y * bmpInfo.bmWidthBytes + x / 8] & (0x80 >> (x % 8)); // 计算抗锯齿后的像素值 BYTE newVal = 0; int count = 0; for (int i = -offset; i <= offset; i++) { for (int j = -offset; j <= offset; j++) { int nx = x + j; int ny = y + i; if (nx >= 0 && nx < bmpInfo.bmWidth && ny >= 0 && ny < bmpInfo.bmHeight) { BYTE nv = pData[ny * bmpInfo.bmWidthBytes + nx / 8] & (0x80 >> (nx % 8)); if (nv >= threshold) { newVal += nv * weight; count += weight; } else { newVal += nv; count++; } } } } newVal /= count; // 设置抗锯齿后的像素值 if (newVal >= threshold) { pData[y * bmpInfo.bmWidthBytes + x / 8] |= (0x80 >> (x % 8)); } else { pData[y * bmpInfo.bmWidthBytes + x / 8] &= ~(0x80 >> (x % 8)); } } } // 生成新的位图 CBitmap newBmp; newBmp.CreateBitmap(bmpInfo.bmWidth, bmpInfo.bmHeight, 1, 1, pData); // 释放内存 delete[] pData; ``` 上述代码,抗锯齿算法采用的是简单的平均值滤波,权重为4的像素点被认为是边缘,权重为1的像素点被认为是平滑区域,阈值为128。可以根据实际需求调整算法参数。最终生成的新位图可以通过 CDC 对象的 DrawBitmap 方法绘制到屏幕上,或保存为文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值