来自 2d 游戏引擎 Layabox 源码中的一段代码,看不懂,求解释 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Game Engines
Unreal Engine
MyCryENGINE
zuosiruan
V2EX    游戏开发

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

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

    主要是从合图中切取小图,如果要对切取的图做旋转应该如何做? 要看懂 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     Solana     857 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 21:41 PVG 05:41 LAX 14:41 JFK 17:41
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86