-
Notifications
You must be signed in to change notification settings - Fork 46
/
post-processing-sprite.ts
107 lines (96 loc) · 3.24 KB
/
post-processing-sprite.ts
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
import { RenderTexture, Renderer } from "@pixi/core"
import { DisplayObject, IDestroyOptions } from "@pixi/display"
import { Sprite } from "@pixi/sprite"
import { Ticker } from "@pixi/ticker"
import { Compatibility } from "../compatibility/compatibility"
export interface PostProcessingSpriteOptions {
/**
* The width of the texture for the sprite.
*/
width?: number,
/**
* The height of the texture for the sprite.
*/
height?: number,
/**
* The object to render. When set, it will automatically be rendered to the
* sprite's texture each frame.
*/
objectToRender?: DisplayObject,
/**
* The resolution of the texture for the sprite.
*/
resolution?: number
}
/**
* Represents a sprite which can have post processing effects. Can be used for
* rendering 3D objects as 2D sprites.
*/
export class PostProcessingSprite extends Sprite {
private _tickerRender = () => { }
private _renderTexture: RenderTexture
/** The render texture. */
get renderTexture() {
return this._renderTexture
}
/** The depth texture. */
get depthTexture() {
if (this._renderTexture) {
return this._renderTexture.baseTexture.framebuffer.depthTexture
}
}
/**
* Creates a new post processing sprite using the specified options.
* @param renderer The renderer to use.
* @param options The options for the render texture. If both width and height
* has not been set, it will automatically be resized to the renderer size.
*/
constructor(public renderer: Renderer, options?: PostProcessingSpriteOptions) {
super()
let {
width = 512, height = 512, objectToRender, resolution = 1
} = options || {}
this._renderTexture = RenderTexture.create({ width, height, resolution })
/* When rendering to a texture, it's flipped vertically for some reason.
This will flip it back to it's expected orientation. */
this._renderTexture.rotate = 8
this._renderTexture.baseTexture.framebuffer.addDepthTexture()
this._texture = this._renderTexture
if (!options || !options.width || !options.height) {
renderer.on("prerender", () => {
this._renderTexture.resize(renderer.screen.width, renderer.screen.height)
})
}
if (objectToRender) {
this._tickerRender = () => {
if (!renderer.gl) {
// The renderer was probably destroyed.
Ticker.shared.remove(this._tickerRender); return
}
if (this.worldVisible && this.worldAlpha > 0 && this.renderable) {
objectToRender && this.renderObject(objectToRender)
}
}
Ticker.shared.add(this._tickerRender)
}
}
/**
* Sets the resolution of the render texture.
* @param resolution The resolution to set.
*/
setResolution(resolution: number) {
this._renderTexture.setResolution(resolution)
this._renderTexture.resize(
this._renderTexture.width, this._renderTexture.height, true)
}
destroy(options?: boolean | IDestroyOptions) {
Ticker.shared.remove(this._tickerRender); super.destroy(options)
}
/**
* Updates the sprite's texture by rendering the specified object to it.
* @param object The object to render.
*/
renderObject(object: DisplayObject) {
Compatibility.render(this.renderer, object, this.renderTexture)
}
}