-
Notifications
You must be signed in to change notification settings - Fork 4
/
RenderTargetTexture.ts
199 lines (178 loc) · 5.96 KB
/
RenderTargetTexture.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { AbstractTexture } from './AbstractTexture';
import {
TextureParameter,
TextureParameterEnum,
} from '../definitions/TextureParameter';
import {PixelFormat, PixelFormatEnum} from '../definitions/PixelFormat';
import {ComponentTypeEnum, ComponentType} from '../definitions/ComponentType';
import { IRenderable } from './IRenderable';
import { CGAPIResourceRepository } from '../renderer/CGAPIResourceRepository';
import {Size, Index} from '../../types/CommonTypes';
import { FrameBuffer } from '../renderer/FrameBuffer';
import { Vector4 } from '../math/Vector4';
export class RenderTargetTexture
extends AbstractTexture
implements IRenderable
{
private __fbo?: FrameBuffer;
constructor() {
super();
}
create({
width,
height,
level = 0,
internalFormat = TextureParameter.RGB8,
format = PixelFormat.RGBA,
type = ComponentType.UnsignedByte,
magFilter = TextureParameter.Linear,
minFilter = TextureParameter.Linear,
wrapS = TextureParameter.ClampToEdge,
wrapT = TextureParameter.ClampToEdge,
}: {
width: Size;
height: Size;
level: number;
internalFormat: TextureParameterEnum;
format: PixelFormatEnum;
type: ComponentTypeEnum;
magFilter: TextureParameterEnum;
minFilter: TextureParameterEnum;
wrapS: TextureParameterEnum;
wrapT: TextureParameterEnum;
}) {
this.__width = width;
this.__height = height;
this.__level = level;
this.__internalFormat = internalFormat;
this.__format = format;
this.__type = type;
this.__magFilter = magFilter;
this.__minFilter = minFilter;
this.__wrapS = wrapS;
this.__wrapT = wrapT;
this.__createRenderTargetTexture();
}
set _fbo(fbo: FrameBuffer) {
this.__fbo = fbo;
}
get fbo() {
return this.__fbo;
}
private __createRenderTargetTexture() {
const webGLResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
const texture = webGLResourceRepository.createRenderTargetTexture({
width: this.__width,
height: this.__height,
level: this.__level,
internalFormat: this.__internalFormat,
format: this.__format,
type: this.__type,
magFilter: this.__magFilter,
minFilter: this.__minFilter,
wrapS: this.__wrapS,
wrapT: this.__wrapT,
});
this.cgApiResourceUid = texture;
AbstractTexture.__textureMap.set(texture, this);
}
resize(width: Size, height: Size) {
this.destroy3DAPIResources();
this.__width = width;
this.__height = height;
this.__createRenderTargetTexture();
}
destroy3DAPIResources() {
AbstractTexture.__textureMap.delete(this.cgApiResourceUid);
const webGLResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
webGLResourceRepository.deleteTexture(this.cgApiResourceUid);
this.cgApiResourceUid = CGAPIResourceRepository.InvalidCGAPIResourceUid;
return true;
}
getTexturePixelData() {
const webGLResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
const glw = webGLResourceRepository.currentWebGLContextWrapper;
const gl = glw!.getRawContext() as WebGLRenderingContext;
// Create a framebuffer backed by the texture
const fbo = webGLResourceRepository.getWebGLResource(
this.__fbo!.framebufferUID
) as WebGLFramebuffer;
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
// const texture = webGLResourceRepository.getWebGLResource(this.cgApiResourceUid!) as WebGLTexture;
// gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
// Read the contents of the framebuffer (data stores the pixel data)
const data = new Uint8Array(this.width * this.height * 4);
if ((gl as WebGL2RenderingContext).readBuffer != null) {
(gl as WebGL2RenderingContext).readBuffer(
36064 + this.__fbo!.whichColorAttachment(this)
); // 36064 means gl.COLOR_ATTACHMENT0
}
gl.readPixels(
0,
0,
this.width,
this.height,
gl.RGBA,
gl.UNSIGNED_BYTE,
data
);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return data;
}
downloadTexturePixelData() {
const data = this.getTexturePixelData();
const canvas = document.createElement('canvas');
canvas.width = this.__width;
canvas.height = this.__height;
const ctx = canvas.getContext('2d')!;
const imageData = new ImageData(
new Uint8ClampedArray(data.buffer),
this.__width,
this.__height
);
ctx.putImageData(imageData, this.__width, this.__height);
const dataUri = canvas.toDataURL('image/png');
const a = document.createElement('a');
const e = document.createEvent('MouseEvent');
a.href = dataUri;
a.download = 'texture.png';
e.initEvent('click', true, true); //, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e);
}
/**
* Origin is left bottom
*
* @param x horizontal pixel position (0 is left)
* @param y vertical pixel position (0 is bottom)
* @param argByteArray Pixel Data as Uint8Array
* @returns Pixel Value in Vector4
*/
getPixelValueAt(x: Index, y: Index, argByteArray?: Uint8Array): Vector4 {
let byteArray = argByteArray;
if (!byteArray) {
byteArray = this.getTexturePixelData();
}
const color = Vector4.fromCopyArray([
byteArray[(y * this.width + x) * 4 + 0],
byteArray[(y * this.width + x) * 4 + 1],
byteArray[(y * this.width + x) * 4 + 2],
byteArray[(y * this.width + x) * 4 + 3],
]);
return color;
}
generateMipmap() {
const webGLResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
const glw = webGLResourceRepository.currentWebGLContextWrapper;
const gl = glw!.getRawContext() as WebGLRenderingContext;
const texture = webGLResourceRepository.getWebGLResource(
this.cgApiResourceUid
) as WebGLTexture;
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}
}