-
Notifications
You must be signed in to change notification settings - Fork 1
/
RenderTarget2DArray.js
86 lines (64 loc) · 2.17 KB
/
RenderTarget2DArray.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
import {
WebGLArrayRenderTarget,
RGBAFormat,
UnsignedByteType,
MeshBasicMaterial,
Color,
RepeatWrapping,
LinearFilter,
NoToneMapping,
DataTexture,
ByteType,
} from 'three';
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
const whiteTex = new DataTexture( new Uint8Array( [ 255, 255, 255, 255 ] ), 1, 1, RGBAFormat, ByteType );
const prevColor = new Color();
export class RenderTarget2DArray extends WebGLArrayRenderTarget {
constructor( renderer, ...args ) {
super( ...args );
this._renderer = renderer;
const tex = this.texture;
tex.format = RGBAFormat;
tex.type = UnsignedByteType;
tex.minFilter = LinearFilter;
tex.magFilter = LinearFilter;
tex.wrapS = RepeatWrapping;
tex.wrapT = RepeatWrapping;
const fsQuad = new FullScreenQuad( new MeshBasicMaterial() );
this.fsQuad = fsQuad;
}
setTextureAt( id, texture ) {
texture = texture || whiteTex;
// save previous renderer state
const renderer = this._renderer;
const prevRenderTarget = renderer.getRenderTarget();
const prevToneMapping = renderer.toneMapping;
const prevAlpha = renderer.getClearAlpha();
renderer.getClearColor( prevColor );
// resize the render target and ensure we don't have an empty texture
// render target depth must be >= 1 to avoid unbound texture error on android devices
renderer.setClearColor( 0, 0 );
renderer.toneMapping = NoToneMapping;
// render each texture into each layer of the target
const fsQuad = this.fsQuad;
// revert to default texture transform before rendering
texture.matrixAutoUpdate = false;
texture.matrix.identity();
fsQuad.material.map = texture;
fsQuad.material.transparent = true;
renderer.setRenderTarget( this, id );
fsQuad.render( renderer );
// restore custom texture transform
texture.updateMatrix();
texture.matrixAutoUpdate = true;
// reset the renderer
fsQuad.material.map = null;
renderer.setClearColor( prevColor, prevAlpha );
renderer.setRenderTarget( prevRenderTarget );
renderer.toneMapping = prevToneMapping;
}
dispose() {
super.dispose();
this.fsQuad.dispose();
}
}