File size: 2,830 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { EventDispatcher } from '../core/EventDispatcher.js';
import { Texture } from '../textures/Texture.js';
import { LinearFilter } from '../constants.js';
import { Vector4 } from '../math/Vector4.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 WebGLRenderTarget extends EventDispatcher {
	constructor(width, height, options = {}) {
		super();

		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);

		this.texture = new Texture(
			undefined,
			options.mapping,
			options.wrapS,
			options.wrapT,
			options.magFilter,
			options.minFilter,
			options.format,
			options.type,
			options.anisotropy,
			options.encoding
		);
		this.texture.isRenderTargetTexture = true;

		this.texture.image = { width: width, height: height, depth: 1 };

		this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
		this.texture.internalFormat = options.internalFormat !== undefined ? options.internalFormat : null;
		this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;

		this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
		this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : false;
		this.depthTexture = options.depthTexture !== undefined ? options.depthTexture : null;
	}

	setTexture(texture) {
		texture.image = {
			width: this.width,
			height: this.height,
			depth: this.depth,
		};

		this.texture = texture;
	}

	setSize(width, height, depth = 1) {
		if (this.width !== width || this.height !== height || this.depth !== depth) {
			this.width = width;
			this.height = height;
			this.depth = depth;

			this.texture.image.width = width;
			this.texture.image.height = height;
			this.texture.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.viewport.copy(source.viewport);

		this.texture = source.texture.clone();

		// ensure image object is not shared, see #20328

		this.texture.image = Object.assign({}, source.texture.image);

		this.depthBuffer = source.depthBuffer;
		this.stencilBuffer = source.stencilBuffer;
		this.depthTexture = source.depthTexture;

		return this;
	}

	dispose() {
		this.dispatchEvent({ type: 'dispose' });
	}
}

WebGLRenderTarget.prototype.isWebGLRenderTarget = true;

export { WebGLRenderTarget };