V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Game Engines
Unreal Engine
MyCryENGINE
zuosiruan
V2EX  ›  游戏开发

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

  •  
  •   zuosiruan · 2023-05-05 19:53:52 +08:00 · 1660 次点击
    这是一个创建于 552 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要是从合图中切取小图,如果要对切取的图做旋转应该如何做? 要看懂 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;
        }
    
    4 条回复    2023-05-06 11:51:06 +08:00
    zhanlanhuizhang
        1
    zhanlanhuizhang  
       2023-05-06 09:24:28 +08:00
    zhanlanhuizhang
        2
    zhanlanhuizhang  
       2023-05-06 09:24:58 +08:00
    zuosiruan
        3
    zuosiruan  
    OP
       2023-05-06 10:19:33 +08:00
    @zhanlanhuizhang #1 多谢多谢
    codehz
        4
    codehz  
       2023-05-06 11:51:06 +08:00   ❤️ 1
    迷惑回复之 yuv vs uv
    这俩玩意都不是一个东西啊,texture 里的 uv ,就是指纹理坐标系里的坐标啊(你把 u 看成 x ,v 看成 y 不就好了),用 uv 这俩只是为了防止名字冲突
    https://en.wikipedia.org/wiki/UV_mapping
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2871 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 11:13 · PVG 19:13 · LAX 03:13 · JFK 06:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.