-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
Copy pathRenderTarget.js
159 lines (99 loc) · 3.43 KB
/
RenderTarget.js
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
import { EventDispatcher } from './EventDispatcher.js';
import { Texture } from '../textures/Texture.js';
import { LinearFilter } from '../constants.js';
import { Vector4 } from '../math/Vector4.js';
import { Source } from '../textures/Source.js';
/*
In options, we can specify:
* Texture parameters for an auto-generated target texture
* depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
*/
class RenderTarget extends EventDispatcher {
constructor( width = 1, height = 1, options = {} ) {
super();
this.isRenderTarget = true;
this.width = width;
this.height = height;
this.depth = 1;
this.scissor = new Vector4( 0, 0, width, height );
this.scissorTest = false;
this.viewport = new Vector4( 0, 0, width, height );
const image = { width: width, height: height, depth: 1 };
options = Object.assign( {
generateMipmaps: false,
internalFormat: null,
minFilter: LinearFilter,
depthBuffer: true,
stencilBuffer: false,
resolveStencilBuffer: true,
depthTexture: null,
samples: 0,
count: 1
}, options );
const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
texture.flipY = false;
texture.generateMipmaps = options.generateMipmaps;
texture.internalFormat = options.internalFormat;
this.textures = [];
const count = options.count;
for ( let i = 0; i < count; i ++ ) {
this.textures[ i ] = texture.clone();
this.textures[ i ].isRenderTargetTexture = true;
}
this.depthBuffer = options.depthBuffer;
this.stencilBuffer = options.stencilBuffer;
this.resolveStencilBuffer = options.resolveStencilBuffer;
this.depthTexture = options.depthTexture;
this.samples = options.samples;
}
get texture() {
return this.textures[ 0 ];
}
set texture( value ) {
this.textures[ 0 ] = value;
}
setSize( width, height, depth = 1 ) {
if ( this.width !== width || this.height !== height || this.depth !== depth ) {
this.width = width;
this.height = height;
this.depth = depth;
for ( let i = 0, il = this.textures.length; i < il; i ++ ) {
this.textures[ i ].image.width = width;
this.textures[ i ].image.height = height;
this.textures[ i ].image.depth = depth;
}
this.dispose();
}
this.viewport.set( 0, 0, width, height );
this.scissor.set( 0, 0, width, height );
}
clone() {
return new this.constructor().copy( this );
}
copy( source ) {
this.width = source.width;
this.height = source.height;
this.depth = source.depth;
this.scissor.copy( source.scissor );
this.scissorTest = source.scissorTest;
this.viewport.copy( source.viewport );
this.textures.length = 0;
for ( let i = 0, il = source.textures.length; i < il; i ++ ) {
this.textures[ i ] = source.textures[ i ].clone();
this.textures[ i ].isRenderTargetTexture = true;
}
// ensure image object is not shared, see #20328
const image = Object.assign( {}, source.texture.image );
this.texture.source = new Source( image );
this.depthBuffer = source.depthBuffer;
this.stencilBuffer = source.stencilBuffer;
this.resolveStencilBuffer = source.resolveStencilBuffer;
if ( source.depthTexture !== null ) this.depthTexture = source.depthTexture.clone();
this.samples = source.samples;
return this;
}
dispose() {
this.dispatchEvent( { type: 'dispose' } );
}
}
export { RenderTarget };