arcade-physics
Version:
Use Arcade Physics without Phaser.
263 lines • 8.49 kB
JavaScript
"use strict";
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Class_1 = __importDefault(require("../../utils/Class"));
const Utils_1 = __importDefault(require("../../renderer/webgl/Utils"));
const Vector3_1 = __importDefault(require("../../math/Vector3"));
/**
* @classdesc
* A Vertex Geometry Object.
*
* This class consists of all the information required for a single vertex within a Face Geometry Object.
*
* Faces, and thus Vertex objects, are used by the Mesh Game Object in order to render objects in WebGL.
*
* @class Vertex
* @memberof Phaser.Geom.Mesh
* @constructor
* @extends Phaser.Math.Vector3
* @since 3.50.0
*
* @param {number} x - The x position of the vertex.
* @param {number} y - The y position of the vertex.
* @param {number} z - The z position of the vertex.
* @param {number} u - The UV u coordinate of the vertex.
* @param {number} v - The UV v coordinate of the vertex.
* @param {number} [color=0xffffff] - The color value of the vertex.
* @param {number} [alpha=1] - The alpha value of the vertex.
* @param {number} [nx=0] - The x normal value of the vertex.
* @param {number} [ny=0] - The y normal value of the vertex.
* @param {number} [nz=0] - The z normal value of the vertex.
*/
const Vertex = new Class_1.default({
Extends: Vector3_1.default,
initialize: function Vertex(x, y, z, u, v, color, alpha, nx, ny, nz) {
if (color === undefined) {
color = 0xffffff;
}
if (alpha === undefined) {
alpha = 1;
}
if (nx === undefined) {
nx = 0;
}
if (ny === undefined) {
ny = 0;
}
if (nz === undefined) {
nz = 0;
}
Vector3_1.default.call(this, x, y, z);
/**
* The projected x coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#vx
* @type {number}
* @since 3.50.0
*/
this.vx = 0;
/**
* The projected y coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#vy
* @type {number}
* @since 3.50.0
*/
this.vy = 0;
/**
* The projected z coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#vz
* @type {number}
* @since 3.50.0
*/
this.vz = 0;
/**
* The projected x coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#nx
* @type {number}
* @since 3.50.0
*/
this.nx = nx;
/**
* The projected y coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#ny
* @type {number}
* @since 3.50.0
*/
this.ny = ny;
/**
* The projected z coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#nz
* @type {number}
* @since 3.50.0
*/
this.nz = nz;
/**
* UV u coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#u
* @type {number}
* @since 3.50.0
*/
this.u = u;
/**
* UV v coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#v
* @type {number}
* @since 3.50.0
*/
this.v = v;
/**
* The color value of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#color
* @type {number}
* @since 3.50.0
*/
this.color = color;
/**
* The alpha value of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#alpha
* @type {number}
* @since 3.50.0
*/
this.alpha = alpha;
/**
* The translated x coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#tx
* @type {number}
* @since 3.50.0
*/
this.tx = 0;
/**
* The translated y coordinate of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#ty
* @type {number}
* @since 3.50.0
*/
this.ty = 0;
/**
* The translated alpha value of this vertex.
*
* @name Phaser.Geom.Mesh.Vertex#ta
* @type {number}
* @since 3.50.0
*/
this.ta = 0;
},
/**
* Sets the U and V properties.
*
* @method Phaser.Geom.Mesh.Vertex#setUVs
* @since 3.50.0
*
* @param {number} u - The UV u coordinate of the vertex.
* @param {number} v - The UV v coordinate of the vertex.
*
* @return {this} This Vertex.
*/
setUVs: function (u, v) {
this.u = u;
this.v = v;
return this;
},
/**
* Transforms this vertex by the given matrix, storing the results in `vx`, `vy` and `vz`.
*
* @method Phaser.Geom.Mesh.Vertex#transformCoordinatesLocal
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} transformMatrix - The transform matrix to apply to this vertex.
* @param {number} width - The width of the parent Mesh.
* @param {number} height - The height of the parent Mesh.
* @param {number} cameraZ - The z position of the MeshCamera.
*/
transformCoordinatesLocal: function (transformMatrix, width, height, cameraZ) {
const x = this.x;
const y = this.y;
const z = this.z;
const m = transformMatrix.val;
const tx = x * m[0] + y * m[4] + z * m[8] + m[12];
const ty = x * m[1] + y * m[5] + z * m[9] + m[13];
const tz = x * m[2] + y * m[6] + z * m[10] + m[14];
const tw = x * m[3] + y * m[7] + z * m[11] + m[15];
this.vx = (tx / tw) * width;
this.vy = -(ty / tw) * height;
if (cameraZ <= 0) {
this.vz = tz / tw;
}
else {
this.vz = -(tz / tw);
}
},
/**
* Updates this Vertex based on the given transform.
*
* @method Phaser.Geom.Mesh.Vertex#update
* @since 3.50.0
*
* @param {number} a - The parent transform matrix data a component.
* @param {number} b - The parent transform matrix data b component.
* @param {number} c - The parent transform matrix data c component.
* @param {number} d - The parent transform matrix data d component.
* @param {number} e - The parent transform matrix data e component.
* @param {number} f - The parent transform matrix data f component.
* @param {boolean} roundPixels - Round the vertex position or not?
* @param {number} alpha - The alpha of the parent object.
*
* @return {this} This Vertex.
*/
update: function (a, b, c, d, e, f, roundPixels, alpha) {
let tx = this.vx * a + this.vy * c + e;
let ty = this.vx * b + this.vy * d + f;
if (roundPixels) {
tx = Math.round(tx);
ty = Math.round(ty);
}
this.tx = tx;
this.ty = ty;
this.ta = this.alpha * alpha;
return this;
},
/**
* Loads the data from this Vertex into the given Typed Arrays.
*
* @method Phaser.Geom.Mesh.Vertex#load
* @since 3.50.0
*
* @param {Float32Array} F32 - A Float32 Array to insert the position, UV and unit data in to.
* @param {Uint32Array} U32 - A Uint32 Array to insert the color and alpha data in to.
* @param {number} offset - The index of the array to insert this Vertex to.
* @param {number} textureUnit - The texture unit currently in use.
* @param {number} tintEffect - The tint effect to use.
*
* @return {number} The new array offset.
*/
load: function (F32, U32, offset, textureUnit, tintEffect) {
F32[++offset] = this.tx;
F32[++offset] = this.ty;
F32[++offset] = this.u;
F32[++offset] = this.v;
F32[++offset] = textureUnit;
F32[++offset] = tintEffect;
U32[++offset] = Utils_1.default.getTintAppendFloatAlpha(this.color, this.ta);
return offset;
}
});
exports.default = Vertex;
//# sourceMappingURL=Vertex.js.map