New constants and use of Float32Arrays

* `ColorMatrix._matrix` and `_data` are now Float32Arrays.
* Calling the `ColorMatrix.set`, `reset` and `getData` methods all now use the built-in Float32 Array operations, making them considerably faster.
* `ColorMatrix.BLACK_WHITE` is a new constant used by blackwhite operations.
* `ColorMatrix.NEGATIVE` is a new constant used by negative operations.
* `ColorMatrix.DESATURATE_LUMINANCE` is a new constant used by desaturation operations.
* `ColorMatrix.SEPIA` is a new constant used by sepia operations.
* `ColorMatrix.LSD` is a new constant used by LSD operations.
* `ColorMatrix.BROWN` is a new constant used by brown operations.
* `ColorMatrix.VINTAGE` is a new constant used by vintage pinhole operations.
* `ColorMatrix.KODACHROME` is a new constant used by kodachrome operations.
* `ColorMatrix.TECHNICOLOR` is a new constant used by technicolor operations.
* `ColorMatrix.POLAROID` is a new constant used by polaroid operations.
* `ColorMatrix.SHIFT_BGR` is a new constant used by shift BGR operations.
This commit is contained in:
Richard Davey 2023-01-23 17:21:11 +00:00
parent 61278d0812
commit 89459642e2

View file

@ -6,6 +6,8 @@
var Class = require('../utils/Class');
var tempMatrix = new Float32Array(20);
/**
* @classdesc
* The ColorMatrix class creates a 5x4 matrix that can be used in shaders and graphics
@ -29,16 +31,11 @@ var ColorMatrix = new Class({
* Internal ColorMatrix array.
*
* @name Phaser.Display.ColorMatrix#_matrix
* @type {number[]}
* @type {Float32Array}
* @private
* @since 3.50.0
*/
this._matrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
this._matrix = new Float32Array(20);
/**
* The value that determines how much of the original color is used
@ -70,7 +67,9 @@ var ColorMatrix = new Class({
* @private
* @since 3.50.0
*/
this._data;
this._data = new Float32Array(20);
this.reset();
},
/**
@ -79,13 +78,13 @@ var ColorMatrix = new Class({
* @method Phaser.Display.ColorMatrix#set
* @since 3.50.0
*
* @param {number[]} value - The ColorMatrix values to set.
* @param {(number[]|Float32Array)} value - The ColorMatrix values to set. Must have 20 elements.
*
* @return {this} This ColorMatrix instance.
*/
set: function (value)
{
this._matrix = value;
this._matrix.set(value);
this._dirty = true;
@ -93,7 +92,8 @@ var ColorMatrix = new Class({
},
/**
* Resets the ColorMatrix.
* Resets the ColorMatrix to default values and also resets
* the `alpha` property back to 1.
*
* @method Phaser.Display.ColorMatrix#reset
* @since 3.50.0
@ -102,34 +102,16 @@ var ColorMatrix = new Class({
*/
reset: function ()
{
// Long-winded, but saves on gc, which happens a lot in Post FX Shaders
// that reset the ColorMatrix every frame.
var m = this._matrix;
m.fill(0);
m[0] = 1;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = 0;
m[6] = 1;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = 0;
m[11] = 0;
m[12] = 1;
m[13] = 0;
m[14] = 0;
m[15] = 0;
m[16] = 0;
m[17] = 0;
m[18] = 1;
m[19] = 0;
this.alpha = 1;
this._dirty = true;
@ -148,21 +130,21 @@ var ColorMatrix = new Class({
*/
getData: function ()
{
var data = this._data;
if (this._dirty)
{
var f32 = new Float32Array(this._matrix);
data.set(this._matrix);
f32[4] /= 255;
f32[9] /= 255;
f32[14] /= 255;
f32[19] /= 255;
this._data = f32;
data[4] /= 255;
data[9] /= 255;
data[14] /= 255;
data[19] /= 255;
this._dirty = false;
}
return this._data;
return data;
},
/**
@ -300,12 +282,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0.3, 0.6, 0.1, 0, 0,
0.3, 0.6, 0.1, 0, 0,
0.3, 0.6, 0.1, 0, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.BLACK_WHITE, multiply);
},
/**
@ -349,12 +326,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
-1, 0, 0, 1, 0,
0, -1, 0, 1, 0,
0, 0, -1, 1, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.NEGATIVE, multiply);
},
/**
@ -371,12 +343,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0.2764723, 0.9297080, 0.0938197, 0, -37.1,
0.2764723, 0.9297080, 0.0938197, 0, -37.1,
0.2764723, 0.9297080, 0.0938197, 0, -37.1,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.DESATURATE_LUMINANCE, multiply);
},
/**
@ -393,12 +360,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0.393, 0.7689999, 0.18899999, 0, 0,
0.349, 0.6859999, 0.16799999, 0, 0,
0.272, 0.5339999, 0.13099999, 0, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.SEPIA, multiply);
},
/**
@ -439,12 +401,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
2, -0.4, 0.5, 0, 0,
-0.5, 2, -0.4, 0, 0,
-0.4, -0.5, 3, 0, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.LSD, multiply);
},
/**
@ -461,12 +418,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0.5997023498159715, 0.34553243048391263, -0.2708298674538042, 0, 47.43192855600873,
-0.037703249837783157, 0.8609577587992641, 0.15059552388459913, 0, -36.96841498319127,
0.24113635128153335, -0.07441037908422492, 0.44972182064877153, 0, -7.562075277591283,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.BROWN, multiply);
},
/**
@ -483,12 +435,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0.6279345635605994, 0.3202183420819367, -0.03965408211312453, 0, 9.651285835294123,
0.02578397704808868, 0.6441188644374771, 0.03259127616149294, 0, 7.462829176470591,
0.0466055556782719, -0.0851232987247891, 0.5241648018700465, 0, 5.159190588235296,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.VINTAGE, multiply);
},
/**
@ -505,12 +452,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
1.1285582396593525, -0.3967382283601348, -0.03992559172921793, 0, 63.72958762196502,
-0.16404339962244616, 1.0835251566291304, -0.05498805115633132, 0, 24.732407896706203,
-0.16786010706155763, -0.5603416277695248, 1.6014850761964943, 0, 35.62982807460946,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.KODACHROME, multiply);
},
/**
@ -527,12 +469,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
1.9125277891456083, -0.8545344976951645, -0.09155508482755585, 0, 11.793603434377337,
-0.3087833385928097, 1.7658908555458428, -0.10601743074722245, 0, -70.35205161461398,
-0.231103377548616, -0.7501899197440212, 1.847597816108189, 0, 30.950940869491138,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.TECHNICOLOR, multiply);
},
/**
@ -549,12 +486,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
1.438, -0.062, -0.062, 0, 0,
-0.122, 1.378, -0.122, 0, 0,
-0.016, -0.016, 1.483, 0, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.POLAROID, multiply);
},
/**
@ -571,12 +503,7 @@ var ColorMatrix = new Class({
{
if (multiply === undefined) { multiply = false; }
return this.multiply([
0, 0, 1, 0, 0,
0, 1, 0, 0, 0,
1, 0, 0, 0, 0,
0, 0, 0, 1, 0
], multiply);
return this.multiply(ColorMatrix.SHIFT_BGR, multiply);
},
/**
@ -586,11 +513,14 @@ var ColorMatrix = new Class({
* @since 3.50.0
*
* @param {number[]} a - The 5x4 array to multiply with ColorMatrix._matrix.
* @param {boolean} [multiply=false] - Multiply the resulting ColorMatrix (`true`), or set it (`false`) ?
*
* @return {this} This ColorMatrix instance.
*/
multiply: function (a, multiply)
{
if (multiply === undefined) { multiply = false; }
// Duplicate _matrix into c
if (!multiply)
@ -599,40 +529,41 @@ var ColorMatrix = new Class({
}
var m = this._matrix;
var c = [];
var c = tempMatrix;
for (var i = 0; i < 20; i++)
{
c[i] = m[i];
}
// copy _matrix to tempMatrox
c.set(m);
// R
m[0] = (c[0] * a[0]) + (c[1] * a[5]) + (c[2] * a[10]) + (c[3] * a[15]);
m[1] = (c[0] * a[1]) + (c[1] * a[6]) + (c[2] * a[11]) + (c[3] * a[16]);
m[2] = (c[0] * a[2]) + (c[1] * a[7]) + (c[2] * a[12]) + (c[3] * a[17]);
m[3] = (c[0] * a[3]) + (c[1] * a[8]) + (c[2] * a[13]) + (c[3] * a[18]);
m[4] = (c[0] * a[4]) + (c[1] * a[9]) + (c[2] * a[14]) + (c[3] * a[19]) + c[4];
m.set([
// R
(c[0] * a[0]) + (c[1] * a[5]) + (c[2] * a[10]) + (c[3] * a[15]),
(c[0] * a[1]) + (c[1] * a[6]) + (c[2] * a[11]) + (c[3] * a[16]),
(c[0] * a[2]) + (c[1] * a[7]) + (c[2] * a[12]) + (c[3] * a[17]),
(c[0] * a[3]) + (c[1] * a[8]) + (c[2] * a[13]) + (c[3] * a[18]),
(c[0] * a[4]) + (c[1] * a[9]) + (c[2] * a[14]) + (c[3] * a[19]) + c[4],
// G
m[5] = (c[5] * a[0]) + (c[6] * a[5]) + (c[7] * a[10]) + (c[8] * a[15]);
m[6] = (c[5] * a[1]) + (c[6] * a[6]) + (c[7] * a[11]) + (c[8] * a[16]);
m[7] = (c[5] * a[2]) + (c[6] * a[7]) + (c[7] * a[12]) + (c[8] * a[17]);
m[8] = (c[5] * a[3]) + (c[6] * a[8]) + (c[7] * a[13]) + (c[8] * a[18]);
m[9] = (c[5] * a[4]) + (c[6] * a[9]) + (c[7] * a[14]) + (c[8] * a[19]) + c[9];
// G
(c[5] * a[0]) + (c[6] * a[5]) + (c[7] * a[10]) + (c[8] * a[15]),
(c[5] * a[1]) + (c[6] * a[6]) + (c[7] * a[11]) + (c[8] * a[16]),
(c[5] * a[2]) + (c[6] * a[7]) + (c[7] * a[12]) + (c[8] * a[17]),
(c[5] * a[3]) + (c[6] * a[8]) + (c[7] * a[13]) + (c[8] * a[18]),
(c[5] * a[4]) + (c[6] * a[9]) + (c[7] * a[14]) + (c[8] * a[19]) + c[9],
// B
m[10] = (c[10] * a[0]) + (c[11] * a[5]) + (c[12] * a[10]) + (c[13] * a[15]);
m[11] = (c[10] * a[1]) + (c[11] * a[6]) + (c[12] * a[11]) + (c[13] * a[16]);
m[12] = (c[10] * a[2]) + (c[11] * a[7]) + (c[12] * a[12]) + (c[13] * a[17]);
m[13] = (c[10] * a[3]) + (c[11] * a[8]) + (c[12] * a[13]) + (c[13] * a[18]);
m[14] = (c[10] * a[4]) + (c[11] * a[9]) + (c[12] * a[14]) + (c[13] * a[19]) + c[14];
// B
(c[10] * a[0]) + (c[11] * a[5]) + (c[12] * a[10]) + (c[13] * a[15]),
(c[10] * a[1]) + (c[11] * a[6]) + (c[12] * a[11]) + (c[13] * a[16]),
(c[10] * a[2]) + (c[11] * a[7]) + (c[12] * a[12]) + (c[13] * a[17]),
(c[10] * a[3]) + (c[11] * a[8]) + (c[12] * a[13]) + (c[13] * a[18]),
(c[10] * a[4]) + (c[11] * a[9]) + (c[12] * a[14]) + (c[13] * a[19]) + c[14],
// A
m[15] = (c[15] * a[0]) + (c[16] * a[5]) + (c[17] * a[10]) + (c[18] * a[15]);
m[16] = (c[15] * a[1]) + (c[16] * a[6]) + (c[17] * a[11]) + (c[18] * a[16]);
m[17] = (c[15] * a[2]) + (c[16] * a[7]) + (c[17] * a[12]) + (c[18] * a[17]);
m[18] = (c[15] * a[3]) + (c[16] * a[8]) + (c[17] * a[13]) + (c[18] * a[18]);
m[19] = (c[15] * a[4]) + (c[16] * a[9]) + (c[17] * a[14]) + (c[18] * a[19]) + c[19];
// A
(c[15] * a[0]) + (c[16] * a[5]) + (c[17] * a[10]) + (c[18] * a[15]),
(c[15] * a[1]) + (c[16] * a[6]) + (c[17] * a[11]) + (c[18] * a[16]),
(c[15] * a[2]) + (c[16] * a[7]) + (c[17] * a[12]) + (c[18] * a[17]),
(c[15] * a[3]) + (c[16] * a[8]) + (c[17] * a[13]) + (c[18] * a[18]),
(c[15] * a[4]) + (c[16] * a[9]) + (c[17] * a[14]) + (c[18] * a[19]) + c[19]
]);
this._dirty = true;
@ -641,4 +572,114 @@ var ColorMatrix = new Class({
});
/**
* A constant array used by the ColorMatrix class for black_white operations.
*
* @name Phaser.Display.ColorMatrix.BLACK_WHITE
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.BLACK_WHITE = [ 0.3, 0.6, 0.1, 0, 0, 0.3, 0.6, 0.1, 0, 0, 0.3, 0.6, 0.1, 0, 0, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for negative operations.
*
* @name Phaser.Display.ColorMatrix.NEGATIVE
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.NEGATIVE = [ -1, 0, 0, 1, 0, 0, -1, 0, 1, 0, 0, 0, -1, 1, 0, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for desatured luminance operations.
*
* @name Phaser.Display.ColorMatrix.DESATURATE_LUMINANCE
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.DESATURATE_LUMINANCE = [ 0.2764723, 0.9297080, 0.0938197, 0, -37.1, 0.2764723, 0.9297080, 0.0938197, 0, -37.1, 0.2764723, 0.9297080, 0.0938197, 0, -37.1, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for sepia operations.
*
* @name Phaser.Display.ColorMatrix.SEPIA
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.SEPIA = [ 0.393, 0.7689999, 0.18899999, 0, 0, 0.349, 0.6859999, 0.16799999, 0, 0, 0.272, 0.5339999, 0.13099999, 0, 0, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for lsd operations.
*
* @name Phaser.Display.ColorMatrix.LSD
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.LSD = [ 2, -0.4, 0.5, 0, 0, -0.5, 2, -0.4, 0, 0, -0.4, -0.5, 3, 0, 0, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for brown operations.
*
* @name Phaser.Display.ColorMatrix.BROWN
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.BROWN = [ 0.5997023498159715, 0.34553243048391263, -0.2708298674538042, 0, 47.43192855600873, -0.037703249837783157, 0.8609577587992641, 0.15059552388459913, 0, -36.96841498319127, 0.24113635128153335, -0.07441037908422492, 0.44972182064877153, 0, -7.562075277591283, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for vintage pinhole operations.
*
* @name Phaser.Display.ColorMatrix.VINTAGE
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.VINTAGE = [ 0.6279345635605994, 0.3202183420819367, -0.03965408211312453, 0, 9.651285835294123, 0.02578397704808868, 0.6441188644374771, 0.03259127616149294, 0, 7.462829176470591, 0.0466055556782719, -0.0851232987247891, 0.5241648018700465, 0, 5.159190588235296, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for kodachrome operations.
*
* @name Phaser.Display.ColorMatrix.KODACHROME
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.KODACHROME = [ 1.1285582396593525, -0.3967382283601348, -0.03992559172921793, 0, 63.72958762196502, -0.16404339962244616, 1.0835251566291304, -0.05498805115633132, 0, 24.732407896706203, -0.16786010706155763, -0.5603416277695248, 1.6014850761964943, 0, 35.62982807460946, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for technicolor operations.
*
* @name Phaser.Display.ColorMatrix.TECHNICOLOR
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.TECHNICOLOR = [ 1.9125277891456083, -0.8545344976951645, -0.09155508482755585, 0, 11.793603434377337, -0.3087833385928097, 1.7658908555458428, -0.10601743074722245, 0, -70.35205161461398, -0.231103377548616, -0.7501899197440212, 1.847597816108189, 0, 30.950940869491138, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for polaroid shift operations.
*
* @name Phaser.Display.ColorMatrix.POLAROID
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.POLAROID = [ 1.438, -0.062, -0.062, 0, 0, -0.122, 1.378, -0.122, 0, 0, -0.016, -0.016, 1.483, 0, 0, 0, 0, 0, 1, 0 ];
/**
* A constant array used by the ColorMatrix class for shift BGR operations.
*
* @name Phaser.Display.ColorMatrix.SHIFT_BGR
* @const
* @type {number[]}
* @since 3.60.0
*/
ColorMatrix.SHIFT_BGR = [ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ];
module.exports = ColorMatrix;