- 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
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
import { vertex } from '@tools/fragments';
import fragment from './simpleLightmap.frag';
import { Filter } from '@pixi/core';
import type { Texture, FilterSystem, RenderTexture } from '@pixi/core';
import type { CLEAR_MODES } from '@pixi/constants';
import { hex2rgb, rgb2hex } from '@pixi/utils';
type Color = number | number[];
/**
* SimpleLightmap, originally by Oza94
* http://www.html5gamedevs.com/topic/20027-pixijs-simple-lightmapping/
* http://codepen.io/Oza94/pen/EPoRxj
*
* You have to specify filterArea, or suffer consequences.
* You may have to use it with `filter.dontFit = true`,
* until we rewrite this using same approach as for DisplacementFilter.
*
* 
* @class
* @extends PIXI.Filter
* @memberof PIXI.filters
* @see @pixi/filter-simple-lightmap
* @see pixi-filters
*
* @example
* displayObject.filters = [new SimpleLightmapFilter(texture, 0x666666)];
*/
class SimpleLightmapFilter extends Filter
{
private _color = 0x0;
/**
* @param {PIXI.Texture} texture - a texture where your lightmap is rendered
* @param {Array<number>|number} [color=0x000000] - An RGBA array of the ambient color
* @param {number} [alpha=1] - Default alpha set independent of color (if it's a number, not array).
*/
constructor(texture: Texture, color: Color = 0x000000, alpha = 1)
{
super(vertex, fragment);
this.uniforms.dimensions = new Float32Array(2);
this.uniforms.ambientColor = new Float32Array([0, 0, 0, alpha]);
this.texture = texture;
this.color = color;
}
/**
* Applies the filter.
* @private
* @param {PIXI.FilterManager} filterManager - The manager.
* @param {PIXI.RenderTarget} input - The input target.
* @param {PIXI.RenderTarget} output - The output target.
*/
apply(filterManager: FilterSystem, input: RenderTexture, output: RenderTexture, clear: CLEAR_MODES): void
{
this.uniforms.dimensions[0] = input.filterFrame?.width;
this.uniforms.dimensions[1] = input.filterFrame?.height;
// draw the filter...
filterManager.applyFilter(this, input, output, clear);
}
/**
* a texture where your lightmap is rendered
* @member {PIXI.Texture}
*/
get texture(): Texture
{
return this.uniforms.uLightmap;
}
set texture(value: Texture)
{
this.uniforms.uLightmap = value;
}
/**
* An RGBA array of the ambient color or a hex color without alpha
* @member {Array<number>|number}
*/
set color(value: Color)
{
const arr = this.uniforms.ambientColor;
if (typeof value === 'number')
{
hex2rgb(value, arr);
this._color = value;
}
else
{
arr[0] = value[0];
arr[1] = value[1];
arr[2] = value[2];
arr[3] = value[3];
this._color = rgb2hex(arr);
}
}
get color(): Color
{
return this._color;
}
/**
* When setting `color` as hex, this can be used to set alpha independently.
*/
get alpha(): number
{
return this.uniforms.ambientColor[3];
}
set alpha(value: number)
{
this.uniforms.ambientColor[3] = value;
}
}
export { SimpleLightmapFilter };