-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
render-texture.ts
153 lines (125 loc) · 3.73 KB
/
render-texture.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
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
/**
* @category asset
*/
import { ccclass, property } from '../data/class-decorator';
import { GFXFormat } from '../gfx';
import { GFXDevice } from '../gfx/device';
import { GFXWindow } from '../gfx/window';
import { DepthStencilFormat, PixelFormat } from './asset-enum';
import { TextureBase } from './texture-base';
import { ccenum } from '../value-types/enum';
export interface IRenderTextureCreateInfo {
name?: string;
width: number;
height: number;
colorFormat: PixelFormat;
depthStencilFormat: DepthStencilFormat;
}
ccenum(DepthStencilFormat);
@ccclass('cc.RenderTexture')
export class RenderTexture extends TextureBase {
public static DepthStencilFormat = DepthStencilFormat;
private _window: GFXWindow | null = null;
@property
private _depthStencilFormat: DepthStencilFormat = DepthStencilFormat.NONE;
@property
get width () {
return this._width;
}
set width (value) {
this._width = value;
this.reset();
}
@property
get height () {
return this._height;
}
set height (value) {
this._height = value;
this.reset();
}
@property
get depthStencilFormat () {
return this._depthStencilFormat;
}
set depthStencilFormat (value) {
this._depthStencilFormat = value;
this.reset();
}
public getGFXWindow () {
return this._window;
}
public getGFXTextureView () {
return this._window ? this._window.colorTexView : null;
}
public getGFXStencilTexture (){
return this._window ? this._window.depthStencilTexView : null;
}
public reset (info?: IRenderTextureCreateInfo) {
if (info) {
this._width = info.width;
this._height = info.height;
this._format = info.colorFormat;
this._depthStencilFormat = info.depthStencilFormat;
this._tryResetWindow();
this.emit('resize', this);
}
}
public destroy () {
if (this._window) {
cc.director.root!.destroyWindow(this._window);
this._window = null;
}
return super.destroy();
}
public onLoaded (){
this._tryResetWindow();
this.loaded = true;
this.emit('load');
}
public _serialize (exporting?: any): any {
return {
base: super._serialize(),
name: this._name,
width: this._width,
height: this._height,
colorFormat: this._format,
depthStencilFormat: this._depthStencilFormat,
};
}
public _deserialize (serializeData: any, handle: any) {
super._deserialize(serializeData.base, handle);
const data = serializeData as IRenderTextureCreateInfo;
this.name = data.name || '';
this._width = data.width;
this._height = data.height;
this._format = data.colorFormat;
this._depthStencilFormat = data.depthStencilFormat;
}
protected _tryResetWindow () {
const device = this._getGFXDevice();
if (!device) {
return;
}
if (this._window) {
this._window.destroy();
}
this._createWindow(device);
}
protected _createWindow (device: GFXDevice) {
const config = {
title: this.name,
isOffscreen: true,
width: this._width,
height: this._height,
colorFmt: this._format,
depthStencilFmt: this._depthStencilFormat as unknown as GFXFormat,
};
if(this._window){
this._window.initialize(config);
return this._window;
}
this._window = cc.director.root!.createWindow(config);
}
}
cc.RenderTexture = RenderTexture;