18 lines
847 B
TypeScript
18 lines
847 B
TypeScript
import type { Rect, Size } from "@core/geometry";
|
|
|
|
const fullCoordinates = [0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1] as const;
|
|
|
|
export function textureCoordinatesForCrop(size: Size, crop?: Rect): Float32Array {
|
|
if (!crop) return new Float32Array(fullCoordinates);
|
|
return textureCoordinatesForRect(crop, { x: 0, y: 0, w: size.w, h: size.h });
|
|
}
|
|
|
|
/** Maps a drawn sub-rectangle onto the texture represented by sourceRect. */
|
|
export function textureCoordinatesForRect(drawRect: Rect, sourceRect: Rect): Float32Array {
|
|
const x1 = (drawRect.x - sourceRect.x) / sourceRect.w;
|
|
const x2 = (drawRect.x + drawRect.w - sourceRect.x) / sourceRect.w;
|
|
const y1 = (drawRect.y - sourceRect.y) / sourceRect.h;
|
|
const y2 = (drawRect.y + drawRect.h - sourceRect.y) / sourceRect.h;
|
|
return new Float32Array([x1, y1, x2, y1, x1, y2, x1, y2, x2, y1, x2, y2]);
|
|
}
|