来自 2d 游戏引擎 Layabox 源码中的一段代码,看不懂,求解释

2023-05-05 19:53:52 +08:00
 zuosiruan

主要是从合图中切取小图,如果要对切取的图做旋转应该如何做? 要看懂 uv 相关的内容需要补充哪些知识?多谢。

    Texture.createWithRotation = (source: Laya.Texture2D | Laya.Texture | any, x: number, y: number, width: number, height: number, offsetX: number = 0, offsetY: number = 0, sourceWidth: number = 0, sourceHeight: number = 0, rotation: number = 90) => {
        (offsetX === void 0) && (offsetX = 0);
        (offsetY === void 0) && (offsetY = 0);
        (sourceWidth === void 0) && (sourceWidth = 0);
        (sourceHeight === void 0) && (sourceHeight = 0);
        var btex = (source instanceof laya.resource.Texture);
        var uv = btex ? source.uv : Texture.DEF_UV;
        var bitmap = btex ? source.bitmap : source;
        var bIsAtlas = Laya.RunDriver.isAtlas(bitmap);
        if (bIsAtlas) {
            var atlaser = bitmap._atlaser;
            var nAtlasID = (source)._atlasID;
            if (nAtlasID == -1) {
                throw new Error("create texture error");
            }
            bitmap = atlaser._inAtlasTextureBitmapValue[nAtlasID];
            uv = atlaser._inAtlasTextureOriUVValue[nAtlasID];
        };
        var tex = new Texture(bitmap, null);
        if (bitmap.width && (x + width) > bitmap.width) width = bitmap.width - x;
        if (bitmap.height && (y + height) > bitmap.height) height = bitmap.height - y;
        tex.width = width;
        tex.height = height;
        tex.offsetX = offsetX;
        tex.offsetY = offsetY;
        tex.sourceWidth = sourceWidth || width;
        tex.sourceHeight = sourceHeight || height;
        var dwidth = 1 / bitmap.width;
        var dheight = 1 / bitmap.height;
        x *= dwidth;
        y *= dheight;
        width *= dwidth;
        height *= dheight;
        var u1 = tex.uv[0], v1 = tex.uv[1], u2 = tex.uv[4], v2 = tex.uv[5];
        var inAltasUVWidth = (u2 - u1), inAltasUVHeight = (v2 - v1);
        var oriUV;
        oriUV = Texture.moveUV(uv[0], uv[1], [x, y, x + width, y, x + width, y + height, x, y + height]);

        tex.uv = [u1 + oriUV[0] * inAltasUVWidth, v1 + oriUV[1] * inAltasUVHeight, u2 - (1 - oriUV[2]) * inAltasUVWidth, v1 + oriUV[3] * inAltasUVHeight, u2 - (1 - oriUV[4]) * inAltasUVWidth, v2 - (1 - oriUV[5]) * inAltasUVHeight, u1 + oriUV[6] * inAltasUVWidth, v2 - (1 - oriUV[7]) * inAltasUVHeight];
        if (bIsAtlas) {
            tex.addTextureToAtlas();
        };
        var bitmapScale = bitmap.scaleRate;
        if (bitmapScale && bitmapScale != 1) {
            tex.sourceWidth /= bitmapScale;
            tex.sourceHeight /= bitmapScale;
            tex.width /= bitmapScale;
            tex.height /= bitmapScale;
            tex.scaleRate = bitmapScale;
            tex.offsetX /= bitmapScale;
            tex.offsetY /= bitmapScale;
        } else {
            tex.scaleRate = 1;
        }
        return tex;
    }
1660 次点击
所在节点    游戏开发
4 条回复
zhanlanhuizhang
2023-05-06 09:24:28 +08:00
zhanlanhuizhang
2023-05-06 09:24:58 +08:00
zuosiruan
2023-05-06 10:19:33 +08:00
@zhanlanhuizhang #1 多谢多谢
codehz
2023-05-06 11:51:06 +08:00
迷惑回复之 yuv vs uv
这俩玩意都不是一个东西啊,texture 里的 uv ,就是指纹理坐标系里的坐标啊(你把 u 看成 x ,v 看成 y 不就好了),用 uv 这俩只是为了防止名字冲突
https://en.wikipedia.org/wiki/UV_mapping

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/937643

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX