cocoscreator实现spine用外部图片进行换皮

简介: cocoscreator实现spine用外部图片进行换皮

我希望可以做到外部图片替换spine一个部位上面的图片

网上有看到2dx的做法是新建sp.spine.RegionAttachment,这个时候问题就来了:

如何通过我们的Texture2D来设置sp.spine.TextureAtlasRegion和sp.spine.TextureAtlasPage


笔者研究一个礼拜,自己写了个实现函数,大家可以参考下

void SkeletonRenderer::createRegion(const std::string& slotName,cocos2d::middleware::Texture2D * texture){
    Slot *slot = _skeleton->findSlot(slotName.c_str());
    Texture* texture2D  = texture->getNativeTexture();
    RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
    float width = texture2D->getWidth();
    float height = texture2D->getHeight();
    float wide = texture->getPixelsWide();
    float high = texture->getPixelsHigh();
    attachment->setUVs(0, 0, 1,1, false);
    attachment->setWidth(wide);
    attachment->setHeight(high);
    attachment->setRegionWidth(wide);
    attachment->setRegionHeight(high);
    attachment->setRegionOriginalWidth(wide);
    attachment->setRegionOriginalHeight(high);
    attachment->setRegionOffsetX(0);
    attachment->setRegionOffsetY(15);
    texture->setPixelsWide(width);
    texture->setPixelsHigh(height);
    texture->setRealTextureIndex(1);
    AttachmentVertices * attachV = (AttachmentVertices *)attachment->getRendererObject();
    if (attachV->_texture == texture) return;
    CC_SAFE_RELEASE(attachV->_texture);
    attachV->_texture = texture;
    CC_SAFE_RETAIN(texture);
    V2F_T2F_C4B*  vertices = attachV->_triangles->verts;
    for (int i = 0, ii = 0; i < 4; ++i, ii += 2) {
        vertices[i].texCoord.u = attachment->getUVs()[ii];
        vertices[i].texCoord.v = attachment->getUVs()[ii+1];
    }
    attachment->updateOffset();
}
public changeSpineTexture(slotname: string, texture2d: cc.Texture2D, size: cc.Size = null): void {
    if (this._spine == null || texture2d == null) {
      return;
    }
    let slot = this._spine.findSlot(slotname);
    if (slot) {
      let att = slot.getAttachment();
      if (cc.sys.isNative) {
        let jsbTexture = new middleware.Texture2D();
        let texture = texture2d.getImpl();
        if (size == null) {
          jsbTexture.setPixelsWide(texture2d.width);
          jsbTexture.setPixelsHigh(texture2d.height);
        } else {
          jsbTexture.setPixelsWide(size.width);
          jsbTexture.setPixelsHigh(size.height);
        }
        jsbTexture.setNativeTexture(texture);
        this._spine._nativeSkeleton.createRegion(slotname, jsbTexture);
      } else {
        texture2d.setPremultiplyAlpha(true);
        let skeletonTexture = new sp.SkeletonTexture();
        skeletonTexture.setRealTexture(texture2d);
        let page = new sp.spine.TextureAtlasPage();
        page.name = texture2d.name
        page.uWrap = sp.spine.TextureWrap.ClampToEdge;
        page.vWrap = sp.spine.TextureWrap.ClampToEdge;
        page.texture = skeletonTexture;
        page.texture.setWraps(page.uWrap, page.vWrap);
        page.width = texture2d.width;
        page.height = texture2d.height;
        let region = new sp.spine.TextureAtlasRegion();
        region.page = page;
        region.width = texture2d.width;
        region.height = texture2d.height;
        region.originalWidth = texture2d.width;
        region.originalHeight = texture2d.height;
        region.rotate = false;
        region.u = 0;
        region.v = 0;
        region.u2 = 1;
        region.v2 = 1;
        region.texture = skeletonTexture;
        att.region = region;
        if (size != null) {
          att.width = size.width;
          att.height = size.height;
        }
        att.setRegion(region);
        att.updateOffset();
      }
    } else {
      cc.error('!!!no slot name!!=', slotname);
    }
  }
相关文章
|
前端开发 JavaScript
【JavaScript原型链prototype详解】
在JavaScript中,每个对象都有一个原型(prototype)属性,它指向另一个对象。这个被指向的对象也有自己的原型,以此类推,最终形成了一个原型链。原型链的顶端是Object.prototype,它是所有对象的根原型。 当我们访问一个对象的属性时,如果该对象自身没有这个属性,JavaScript会沿着原型链向上查找,直到找到匹配的属性或者到达原型链的末端。
421 0
【JavaScript原型链prototype详解】
CocosCreator3.8研究笔记(十三)CocosCreator 音频资源理解(2)
CocosCreator3.8研究笔记(十三)CocosCreator 音频资源理解
745 0
|
6月前
|
自然语言处理 前端开发 JavaScript
js异步
js异步
698 108
|
3月前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
3272 43
|
12月前
|
供应链 搜索推荐 API
亚马逊商品列表数据接口(亚马逊 API 系列)
亚马逊的商品列表数据接口为电商从业者、数据分析人员和开发者提供了宝贵的市场洞察。通过该接口,用户可以批量获取商品的关键信息,包括基本信息、价格、销售排名和库存状态等,助力市场分析、竞品研究和商品推荐。开发者需在亚马逊开发者中心注册并申请API权限,使用安全凭证进行认证,支持HTTP/HTTPS协议的GET和POST请求。Python示例展示了如何调用接口获取商品列表,并解析响应数据。应用场景涵盖市场趋势分析、竞品对比、个性化推荐及库存管理,帮助商家优化策略,提升竞争力。
663 13
|
JSON 缓存 前端开发
CocosCreator3.8研究笔记(二十五)CocosCreator 动画系统-2d骨骼动画spine
CocosCreator3.8研究笔记(二十五)CocosCreator 动画系统-2d骨骼动画spine
2965 0
|
JSON JavaScript API
JS【详解】Map (含Map 和 Object 的区别,Map 的常用 API,Map与Object 的性能对比,Map 的应用场景和不适合的使用场景)
JS【详解】Map (含Map 和 Object 的区别,Map 的常用 API,Map与Object 的性能对比,Map 的应用场景和不适合的使用场景)
1114 0
|
编解码 图形学 开发者
【unity小技巧】使用三种方式实现瞄准瞄具放大变焦效果
【unity小技巧】使用三种方式实现瞄准瞄具放大变焦效果
1034 0
|
编解码 前端开发 UED
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
987 0
|
算法 开发工具 索引
三消游戏查找算法的原理和实现
三消游戏查找算法的原理和实现
524 1