- 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
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
import { BaseRenderTexture } from './BaseRenderTexture';
import { Texture } from '../textures/Texture';
import type { Rectangle } from '@pixi/math';
import type { Framebuffer } from '../framebuffer/Framebuffer';
import type { IBaseTextureOptions } from '../textures/BaseTexture';
import type { MSAA_QUALITY, SCALE_MODES } from '@pixi/constants';
import { deprecation } from '@pixi/utils';
/**
* A RenderTexture is a special texture that allows any PixiJS display object to be rendered to it.
*
* __Hint__: All DisplayObjects (i.e. Sprites) that render to a RenderTexture should be preloaded
* otherwise black rectangles will be drawn instead.
*
* __Hint-2__: The actual memory allocation will happen on first render.
* You shouldn't create renderTextures each frame just to delete them after, try to reuse them.
*
* A RenderTexture takes a snapshot of any Display Object given to its render method. For example:
*
* ```js
* let renderer = PIXI.autoDetectRenderer();
* let renderTexture = PIXI.RenderTexture.create({ width: 800, height: 600 });
* let sprite = PIXI.Sprite.from("spinObj_01.png");
*
* sprite.position.x = 800/2;
* sprite.position.y = 600/2;
* sprite.anchor.x = 0.5;
* sprite.anchor.y = 0.5;
*
* renderer.render(sprite, {renderTexture});
* ```
* Note that you should not create a new renderer, but reuse the same one as the rest of the application.
*
* The Sprite in this case will be rendered using its local transform. To render this sprite at 0,0
* you can clear the transform
*
* ```js
*
* sprite.setTransform()
*
* let renderTexture = new PIXI.RenderTexture.create({ width: 100, height: 100 });
*
* renderer.render(sprite, {renderTexture}); // Renders to center of RenderTexture
* ```
* @memberof PIXI
*/
export class RenderTexture extends Texture
{
public baseTexture: BaseRenderTexture;
/**
* Stores `sourceFrame` when this texture is inside current filter stack.
*
* You can read it inside filters.
* @readonly
*/
public filterFrame: Rectangle | null;
/**
* The key for pooled texture of FilterSystem.
* @see PIXI.RenderTexturePool
*/
public filterPoolKey: string | number | null;
/**
* @param baseRenderTexture - The base texture object that this texture uses.
* @param frame - The rectangle frame of the texture to show.
*/
constructor(baseRenderTexture: BaseRenderTexture, frame?: Rectangle)
{
super(baseRenderTexture, frame);
this.valid = true;
this.filterFrame = null;
this.filterPoolKey = null;
this.updateUvs();
}
/**
* Shortcut to `this.baseTexture.framebuffer`, saves baseTexture cast.
* @readonly
*/
get framebuffer(): Framebuffer
{
return this.baseTexture.framebuffer;
}
/**
* Shortcut to `this.framebuffer.multisample`.
* @default PIXI.MSAA_QUALITY.NONE
*/
get multisample(): MSAA_QUALITY
{
return this.framebuffer.multisample;
}
set multisample(value: MSAA_QUALITY)
{
this.framebuffer.multisample = value;
}
/**
* Resizes the RenderTexture.
* @param desiredWidth - The desired width to resize to.
* @param desiredHeight - The desired height to resize to.
* @param resizeBaseTexture - Should the baseTexture.width and height values be resized as well?
*/
resize(desiredWidth: number, desiredHeight: number, resizeBaseTexture = true): void
{
const resolution = this.baseTexture.resolution;
const width = Math.round(desiredWidth * resolution) / resolution;
const height = Math.round(desiredHeight * resolution) / resolution;
// TODO - could be not required..
this.valid = (width > 0 && height > 0);
this._frame.width = this.orig.width = width;
this._frame.height = this.orig.height = height;
if (resizeBaseTexture)
{
this.baseTexture.resize(width, height);
}
this.updateUvs();
}
/**
* Changes the resolution of baseTexture, but does not change framebuffer size.
* @param resolution - The new resolution to apply to RenderTexture
*/
setResolution(resolution: number): void
{
const { baseTexture } = this;
if (baseTexture.resolution === resolution)
{
return;
}
baseTexture.setResolution(resolution);
this.resize(baseTexture.width, baseTexture.height, false);
}
/**
* Use the object-based construction instead.
* @deprecated since 6.0.0
* @param {number} [width]
* @param {number} [height]
* @param {PIXI.SCALE_MODES} [scaleMode=PIXI.settings.SCALE_MODE]
* @param {number} [resolution=PIXI.settings.FILTER_RESOLUTION]
*/
static create(width: number, height: number, scaleMode?: SCALE_MODES, resolution?: number): RenderTexture;
/**
* A short hand way of creating a render texture.
* @param options - Options
* @param {number} [options.width=100] - The width of the render texture
* @param {number} [options.height=100] - The height of the render texture
* @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.settings.SCALE_MODE] - See {@link PIXI.SCALE_MODES}
* for possible values
* @param {number} [options.resolution=PIXI.settings.RESOLUTION] - The resolution / device pixel ratio of the texture
* being generated
* @param {PIXI.MSAA_QUALITY} [options.multisample=PIXI.MSAA_QUALITY.NONE] - The number of samples of the frame buffer
* @returns The new render texture
*/
static create(options?: IBaseTextureOptions): RenderTexture;
static create(options?: IBaseTextureOptions | number, ...rest: any[]): RenderTexture
{
// @deprecated fallback, old-style: create(width, height, scaleMode, resolution)
if (typeof options === 'number')
{
// #if _DEBUG
deprecation('6.0.0', 'Arguments (width, height, scaleMode, resolution) have been deprecated.');
// #endif
/* eslint-disable prefer-rest-params */
options = {
width: options,
height: rest[0],
scaleMode: rest[1],
resolution: rest[2],
};
/* eslint-enable prefer-rest-params */
}
return new RenderTexture(new BaseRenderTexture(options));
}
}