主要是从合图中切取小图,如果要对切取的图做旋转应该如何做? 要看懂 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; }
1 zhanlanhuizhang 2023-05-06 09:24:28 +08:00 |
2 zhanlanhuizhang 2023-05-06 09:24:58 +08:00 |
3 zuosiruan OP @zhanlanhuizhang #1 多谢多谢 |
![]() | 4 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 |