- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
import { Geometry } from '../geometry/Geometry';
import { Buffer } from '../geometry/Buffer';
import type { Rectangle } from '@pixi/math';
/**
* Helper class to create a quad with uvs like in v4
* @memberof PIXI
*/
export class QuadUv extends Geometry
{
vertexBuffer: Buffer;
uvBuffer: Buffer;
/** An array of vertices. */
vertices: Float32Array;
/** The Uvs of the quad. */
uvs: Float32Array;
constructor()
{
super();
this.vertices = new Float32Array([
-1, -1,
1, -1,
1, 1,
-1, 1,
]);
this.uvs = new Float32Array([
0, 0,
1, 0,
1, 1,
0, 1,
]);
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]);
}
/**
* Maps two Rectangle to the quad.
* @param targetTextureFrame - The first rectangle
* @param destinationFrame - The second rectangle
* @returns - Returns itself.
*/
map(targetTextureFrame: Rectangle, destinationFrame: Rectangle): this
{
let x = 0; // destinationFrame.x / targetTextureFrame.width;
let y = 0; // destinationFrame.y / targetTextureFrame.height;
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();
return this;
}
/**
* Legacy upload method, just marks buffers dirty.
* @returns - Returns itself.
*/
invalidate(): this
{
this.vertexBuffer._updateID++;
this.uvBuffer._updateID++;
return this;
}
}