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);
    }
  }
相关文章
|
编解码 程序员 atlas
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
图集只是当所有给低昂的纹理需要相同的着色器时采用的一种方法,如果一些纹理需要通过着色器应用独立的图形效果,它们就必须分离到自己的材质中,并在单独的组中打图集。
1531 0
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
|
JSON atlas 图形学
unity之spine骨骼动画使用
unity实现spine骨骼动画使用
unity之spine骨骼动画使用
|
9月前
|
JSON 缓存 前端开发
CocosCreator3.8研究笔记(二十五)CocosCreator 动画系统-2d骨骼动画spine
CocosCreator3.8研究笔记(二十五)CocosCreator 动画系统-2d骨骼动画spine
474 0
|
12月前
|
前端开发
DSP开发软件css(10)使用基础(汉化、工程导入、设置目标配置文件、选择仿真器和芯片型号、添加文件|库路径、编译下载等操作)
DSP开发软件css(10)使用基础(汉化、工程导入、设置目标配置文件、选择仿真器和芯片型号、添加文件|库路径、编译下载等操作)
132 0
Revit 二次开发—使用相对路径显示图片的问题
Revit 二次开发—使用相对路径显示图片的问题
Revit 二次开发—使用相对路径显示图片的问题
|
缓存 机器人 Android开发
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(2)
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(2)
143 0
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(2)
|
Java Shell Android开发
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(1)
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(1)
537 0
Android插件化开发之用DexClassLoader加载未安装的APK资源文件来实现app切换背景皮肤(1)
Py之wxPython:利用wxPython设计GUI界面(图片背景+简单按钮)
Py之wxPython:利用wxPython设计GUI界面(图片背景+简单按钮)
Py之wxPython:利用wxPython设计GUI界面(图片背景+简单按钮)
全网找不到jpeg2000的代码范例和图片,怎么可能流行开来?
全网找不到jpeg2000的代码范例和图片,怎么可能流行开来?
108 0
|
前端开发 API
SVG Path路径在网页开发的作用
SVG是矢量图形表示,它的一个强大之处在于path标签可以表示任意的矢量形状,利用好这个path可以做出很多传统html/css做不出来的效果。下面举几个例子。
1915 0