UNPKG

1.83 kBJavaScriptView Raw
1import { Buffer } from "../geometry/Buffer.mjs";
2import { Geometry } from "../geometry/Geometry.mjs";
3class QuadUv extends Geometry {
4 constructor() {
5 super(), this.vertices = new Float32Array([
6 -1,
7 -1,
8 1,
9 -1,
10 1,
11 1,
12 -1,
13 1
14 ]), this.uvs = new Float32Array([
15 0,
16 0,
17 1,
18 0,
19 1,
20 1,
21 0,
22 1
23 ]), this.vertexBuffer = new Buffer(this.vertices), this.uvBuffer = new Buffer(this.uvs), this.addAttribute("aVertexPosition", this.vertexBuffer).addAttribute("aTextureCoord", this.uvBuffer).addIndex([0, 1, 2, 0, 2, 3]);
24 }
25 /**
26 * Maps two Rectangle to the quad.
27 * @param targetTextureFrame - The first rectangle
28 * @param destinationFrame - The second rectangle
29 * @returns - Returns itself.
30 */
31 map(targetTextureFrame, destinationFrame) {
32 let x = 0, y = 0;
33 return this.uvs[0] = x, this.uvs[1] = y, this.uvs[2] = x + destinationFrame.width / targetTextureFrame.width, this.uvs[3] = y, this.uvs[4] = x + destinationFrame.width / targetTextureFrame.width, this.uvs[5] = y + destinationFrame.height / targetTextureFrame.height, this.uvs[6] = x, this.uvs[7] = y + destinationFrame.height / targetTextureFrame.height, x = destinationFrame.x, y = destinationFrame.y, this.vertices[0] = x, this.vertices[1] = y, this.vertices[2] = x + destinationFrame.width, this.vertices[3] = y, this.vertices[4] = x + destinationFrame.width, this.vertices[5] = y + destinationFrame.height, this.vertices[6] = x, this.vertices[7] = y + destinationFrame.height, this.invalidate(), this;
34 }
35 /**
36 * Legacy upload method, just marks buffers dirty.
37 * @returns - Returns itself.
38 */
39 invalidate() {
40 return this.vertexBuffer._updateID++, this.uvBuffer._updateID++, this;
41 }
42}
43export {
44 QuadUv
45};
46//# sourceMappingURL=QuadUv.mjs.map