Source
factory RenderTextureQuad.slice(
RenderTextureQuad renderTextureQuad,
Rectangle<int> sourceRectangle,
Rectangle<int> offsetRectangle,
[int rotation = 0]) {
var renderTexture = renderTextureQuad.renderTexture;
var pixelRatio = renderTextureQuad.pixelRatio;
var oldRotation = renderTextureQuad.rotation;
var oldSourceL = renderTextureQuad.sourceRectangle.left;
var oldSourceT = renderTextureQuad.sourceRectangle.top;
var oldSourceR = renderTextureQuad.sourceRectangle.right;
var oldSourceB = renderTextureQuad.sourceRectangle.bottom;
var oldOffsetL = renderTextureQuad.offsetRectangle.left;
var oldOffsetT = renderTextureQuad.offsetRectangle.top;
var newRotation = (renderTextureQuad.rotation + rotation) % 4;
var newSourceL = sourceRectangle.left;
var newSourceT = sourceRectangle.top;
var newSourceR = sourceRectangle.right;
var newSourceB = sourceRectangle.bottom;
var newOffsetL = offsetRectangle.left;
var newOffsetT = offsetRectangle.top;
var newOffsetW = offsetRectangle.width;
var newOffsetH = offsetRectangle.height;
var tmpSourceL = 0;
var tmpSourceT = 0;
var tmpSourceR = 0;
var tmpSourceB = 0;
if (oldRotation == 0) {
tmpSourceL = oldSourceL + oldOffsetL + newSourceL;
tmpSourceT = oldSourceT + oldOffsetT + newSourceT;
tmpSourceR = oldSourceL + oldOffsetL + newSourceR;
tmpSourceB = oldSourceT + oldOffsetT + newSourceB;
} else if (oldRotation == 1) {
tmpSourceL = oldSourceR - oldOffsetT - newSourceB;
tmpSourceT = oldSourceT + oldOffsetL + newSourceL;
tmpSourceR = oldSourceR - oldOffsetT - newSourceT;
tmpSourceB = oldSourceT + oldOffsetL + newSourceR;
} else if (oldRotation == 2) {
tmpSourceL = oldSourceR - oldOffsetL - newSourceR;
tmpSourceT = oldSourceB - oldOffsetT - newSourceB;
tmpSourceR = oldSourceR - oldOffsetL - newSourceL;
tmpSourceB = oldSourceB - oldOffsetT - newSourceT;
} else if (oldRotation == 3) {
tmpSourceL = oldSourceL + oldOffsetT + newSourceT;
tmpSourceT = oldSourceB - oldOffsetL - newSourceR;
tmpSourceR = oldSourceL + oldOffsetT + newSourceB;
tmpSourceB = oldSourceB - oldOffsetL - newSourceL;
}
newSourceL = clampInt(tmpSourceL, oldSourceL, oldSourceR);
newSourceT = clampInt(tmpSourceT, oldSourceT, oldSourceB);
newSourceR = clampInt(tmpSourceR, oldSourceL, oldSourceR);
newSourceB = clampInt(tmpSourceB, oldSourceT, oldSourceB);
if (newRotation == 0) {
newOffsetL += tmpSourceL - newSourceL;
newOffsetT += tmpSourceT - newSourceT;
} else if (newRotation == 1) {
newOffsetL += tmpSourceT - newSourceT;
newOffsetT += newSourceR - tmpSourceR;
} else if (newRotation == 2) {
newOffsetL += newSourceR - tmpSourceR;
newOffsetT += tmpSourceB - newSourceB;
} else if (newRotation == 3) {
newOffsetL += newSourceB - tmpSourceB;
newOffsetT += newSourceL - tmpSourceL;
}
var newSourceW = newSourceR - newSourceL;
var newSourceH = newSourceB - newSourceT;
return new RenderTextureQuad(renderTexture,
new Rectangle<int>(newSourceL, newSourceT, newSourceW, newSourceH),
new Rectangle<int>(newOffsetL, newOffsetT, newOffsetW, newOffsetH),
newRotation, pixelRatio);
}