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);
    }
  }
相关文章
|
XML 存储 JSON
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
688 0
|
iOS开发 开发者
【教程】苹果 iOS 证书制作教程
【教程】苹果 iOS 证书制作教程
|
NoSQL 关系型数据库 MySQL
如何向mongoDB中添加新的字段附代码(全)
关于MongoDB更多的知识点可看我之前这篇文章: MongoDB框架零基础入门本身MongoDB的连接就和Mysql的数据库一样 Mysql连接方式:mysql -u -root -p(标准模式下) MongoDB类似:mongo -u root -p之所以要增加字段值 一般都是python web框架中,在form表单内增加了一个字段值写入数据库(只有最新的数据才有这个字段值) 之前数据没有的字段值只能通过数据库添加 具体添加方式可以通过数据库内或者脚本一键添加(两种方式都差不多)在数据库内增加字段值
1021 0
|
自然语言处理 前端开发 JavaScript
js异步
js异步
884 108
|
10月前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
5406 43
|
人工智能 自然语言处理 程序员
【重磅升级】通义灵码内置 DeepSeek-V3 和 R1 满血版,免费不限量,免部署!
近期通义灵码能力再升级全新上线模型选择功能,目前已经支持 Qwen2.5、DeepSeek-V3 和 R1系列模型,用户可以在 VSCode 和 JetBrains 里搜索并下载最新通义灵码插件,在输入框里选择模型,即可轻松切换模型。
|
安全 Linux 网络安全
SSH远程登陆配置sshd_config文件详解
ssh是linux远程登录的安全协议,是 C/S 模式的架构,配置文件分为服务器端配置文件 [/etc/ssh/sshd_config] 与客户端配置文件默认配置文件[/etc/ssh/ssh_config] 用户配置文件[~/.ssh/config] sshd_config 是服务端主配置文件。
|
编解码 前端开发 UED
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
1526 0
|
前端开发 安全 开发者
前端开发中的跨域资源共享(CORS)问题及解决方案探讨
在前端开发中,跨域资源共享(CORS)是一个常见且重要的问题。本文将深入探讨CORS的原理、影响以及解决方案,帮助开发者更好地应对跨域请求问题。