主要是从合图中切取小图,如果要对切取的图做旋转应该如何做? 要看懂 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;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.