phaser/src/renderer/webgl/Utils.js

174 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2018-04-05 08:02:36 +00:00
* @author Felipe Alfonso <@bitnenfer>
* @author Matthew Groves <@doormat>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2018-02-12 22:16:18 +00:00
/**
* @namespace Phaser.Renderer.WebGL.Utils
* @since 3.0.0
*/
2018-01-09 21:00:56 +00:00
module.exports = {
2018-02-12 22:16:18 +00:00
/**
2018-04-25 16:17:33 +00:00
* Packs four floats on a range from 0.0 to 1.0 into a single Uint32
2018-02-12 22:16:18 +00:00
*
* @function Phaser.Renderer.WebGL.Utils.getTintFromFloats
* @since 3.0.0
*
* @param {number} r - Red component in a range from 0.0 to 1.0
2018-10-22 11:12:31 +00:00
* @param {number} g - Green component in a range from 0.0 to 1.0
* @param {number} b - Blue component in a range from 0.0 to 1.0
2018-04-25 16:17:33 +00:00
* @param {number} a - Alpha component in a range from 0.0 to 1.0
*
2020-02-04 17:30:27 +00:00
* @return {number} The packed RGBA values as a Uint32.
2018-02-12 22:16:18 +00:00
*/
getTintFromFloats: function (r, g, b, a)
{
2020-09-14 08:44:17 +00:00
var ur = ((r * 255) | 0) & 0xff;
var ug = ((g * 255) | 0) & 0xff;
var ub = ((b * 255) | 0) & 0xff;
var ua = ((a * 255) | 0) & 0xff;
2018-01-09 21:00:56 +00:00
2018-01-26 03:55:05 +00:00
return ((ua << 24) | (ur << 16) | (ug << 8) | ub) >>> 0;
},
2018-01-09 21:00:56 +00:00
2018-02-12 22:16:18 +00:00
/**
2018-04-25 16:17:33 +00:00
* Packs a Uint24, representing RGB components, with a Float32, representing
* the alpha component, with a range between 0.0 and 1.0 and return a Uint32
2018-02-12 22:16:18 +00:00
*
* @function Phaser.Renderer.WebGL.Utils.getTintAppendFloatAlpha
* @since 3.0.0
*
2018-04-25 16:17:33 +00:00
* @param {number} rgb - Uint24 representing RGB components
* @param {number} a - Float32 representing Alpha component
*
2018-04-25 16:17:33 +00:00
* @return {number} Packed RGBA as Uint32
2018-02-12 22:16:18 +00:00
*/
getTintAppendFloatAlpha: function (rgb, a)
2018-01-26 03:55:05 +00:00
{
2020-09-14 08:44:17 +00:00
var ua = ((a * 255) | 0) & 0xff;
2018-01-26 03:55:05 +00:00
return ((ua << 24) | rgb) >>> 0;
},
2018-02-12 22:16:18 +00:00
/**
2018-04-25 16:17:33 +00:00
* Packs a Uint24, representing RGB components, with a Float32, representing
* the alpha component, with a range between 0.0 and 1.0 and return a
2018-04-25 16:17:33 +00:00
* swizzled Uint32
2018-02-12 22:16:18 +00:00
*
* @function Phaser.Renderer.WebGL.Utils.getTintAppendFloatAlphaAndSwap
* @since 3.0.0
*
2018-04-25 16:17:33 +00:00
* @param {number} rgb - Uint24 representing RGB components
* @param {number} a - Float32 representing Alpha component
*
2018-04-25 16:17:33 +00:00
* @return {number} Packed RGBA as Uint32
2018-02-12 22:16:18 +00:00
*/
2018-01-26 03:55:05 +00:00
getTintAppendFloatAlphaAndSwap: function (rgb, a)
{
2020-09-14 08:44:17 +00:00
var ur = ((rgb >> 16) | 0) & 0xff;
var ug = ((rgb >> 8) | 0) & 0xff;
var ub = (rgb | 0) & 0xff;
var ua = ((a * 255) | 0) & 0xff;
return ((ua << 24) | (ub << 16) | (ug << 8) | ur) >>> 0;
2018-01-22 22:51:15 +00:00
},
2018-02-12 22:16:18 +00:00
/**
2018-04-25 16:17:33 +00:00
* Unpacks a Uint24 RGB into an array of floats of ranges of 0.0 and 1.0
2018-02-12 22:16:18 +00:00
*
* @function Phaser.Renderer.WebGL.Utils.getFloatsFromUintRGB
* @since 3.0.0
*
2018-04-25 16:17:33 +00:00
* @param {number} rgb - RGB packed as a Uint24
*
* @return {array} Array of floats representing each component as a float
2018-02-12 22:16:18 +00:00
*/
2018-01-30 22:46:43 +00:00
getFloatsFromUintRGB: function (rgb)
{
2020-09-14 08:44:17 +00:00
var ur = ((rgb >> 16) | 0) & 0xff;
var ug = ((rgb >> 8) | 0) & 0xff;
var ub = (rgb | 0) & 0xff;
2018-01-30 22:46:43 +00:00
2020-09-14 08:44:17 +00:00
return [ ur / 255, ug / 255, ub / 255 ];
2018-01-30 22:46:43 +00:00
},
/**
* Check to see how many texture units the GPU supports in a fragment shader
* and if the value specific in the game config is allowed.
*
2022-09-22 13:55:29 +00:00
* This value is hard-clamped to 16 for performance reasons on Android devices.
*
* @function Phaser.Renderer.WebGL.Utils.checkShaderMax
2020-07-31 12:41:29 +00:00
* @since 3.50.0
*
* @param {WebGLRenderingContext} gl - The WebGLContext used to create the shaders.
* @param {number} maxTextures - The Game Config maxTextures value.
*
* @return {number} The number of texture units that is supported by this browser and GPU.
*/
checkShaderMax: function (gl, maxTextures)
{
// Note: This is the maximum number of TIUs that a _fragment_ shader supports
// https://www.khronos.org/opengl/wiki/Common_Mistakes#Texture_Unit
// Hard-clamp this to 16 to avoid run-away texture counts such as on Android
var gpuMax = Math.min(16, gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS));
if (!maxTextures || maxTextures === -1)
{
return gpuMax;
}
else
{
return Math.min(gpuMax, maxTextures);
}
},
/**
2022-11-03 22:04:58 +00:00
* Checks the given Fragment Shader Source for `%count%` and `%forloop%` declarations and
* replaces those with GLSL code for setting `texture = texture2D(uMainSampler[i], outTexCoord)`.
*
* @function Phaser.Renderer.WebGL.Utils.parseFragmentShaderMaxTextures
* @since 3.50.0
*
* @param {string} fragmentShaderSource - The Fragment Shader source code to operate on.
* @param {number} maxTextures - The number of maxTextures value.
*
* @return {string} The modified Fragment Shader source.
*/
parseFragmentShaderMaxTextures: function (fragmentShaderSource, maxTextures)
{
2020-11-05 10:08:21 +00:00
if (!fragmentShaderSource)
{
return '';
}
2022-11-03 22:04:58 +00:00
var src = '';
for (var i = 0; i < maxTextures; i++)
{
if (i > 0)
{
src += '\n\telse ';
}
if (i < maxTextures - 1)
{
src += 'if (outTexId < ' + i + '.5)';
}
src += '\n\t{';
src += '\n\t\ttexture = texture2D(uMainSampler[' + i + '], outTexCoord);';
src += '\n\t}';
}
fragmentShaderSource = fragmentShaderSource.replace(/%count%/gi, maxTextures.toString());
return fragmentShaderSource.replace(/%forloop%/gi, src);
}
2018-02-12 22:16:18 +00:00
};