/
main.ts
196 lines (156 loc) · 5.49 KB
/
main.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
import {
OrthographicCamera,
Scene,
WebGLRenderTarget,
LinearFilter,
NearestFilter,
RGBAFormat,
UnsignedByteType,
CfxTexture,
ShaderMaterial,
PlaneBufferGeometry,
Mesh,
WebGLRenderer
} from '@citizenfx/three';
class ScreenshotRequest {
encoding: 'jpg' | 'png' | 'webp';
quality: number;
correlation: string;
resultURL: string;
targetURL: string;
targetField: string;
}
// from https://stackoverflow.com/a/12300351
function dataURItoBlob(dataURI: string) {
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
const blob = new Blob([ab], {type: mimeString});
return blob;
}
class ScreenshotUI {
renderer: any;
rtTexture: any;
sceneRTT: any;
cameraRTT: any;
request: ScreenshotRequest;
initialize() {
window.addEventListener('message', event => {
this.request = event.data.request;
})
const cameraRTT: any = new OrthographicCamera( window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -10000, 10000 );
cameraRTT.position.z = 100;
const sceneRTT: any = new Scene();
const rtTexture = new WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat, type: UnsignedByteType } );
const gameTexture: any = new CfxTexture( );
gameTexture.needsUpdate = true;
const material = new ShaderMaterial( {
uniforms: { "tDiffuse": { value: gameTexture } },
vertexShader: `
varying vec2 vUv;
void main() {
vUv = vec2(uv.x, 1.0-uv.y); // fuck gl uv coords
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D tDiffuse;
void main() {
gl_FragColor = texture2D( tDiffuse, vUv );
}
`
} );
const plane = new PlaneBufferGeometry( window.innerWidth, window.innerHeight );
const quad: any = new Mesh( plane, material );
quad.position.z = -100;
sceneRTT.add( quad );
const renderer = new WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false;
document.getElementById('app').appendChild(renderer.domElement);
document.getElementById('app').style.display = 'none';
this.renderer = renderer;
this.rtTexture = rtTexture;
this.sceneRTT = sceneRTT;
this.cameraRTT = cameraRTT;
this.animate = this.animate.bind(this);
requestAnimationFrame(this.animate);
}
animate() {
requestAnimationFrame(this.animate);
this.renderer.clear();
this.renderer.render(this.sceneRTT, this.cameraRTT, this.rtTexture, true);
if (this.request) {
const request = this.request;
this.request = null;
this.handleRequest(request);
}
}
handleRequest(request: ScreenshotRequest) {
// read the screenshot
const read = new Uint8Array(window.innerWidth * window.innerHeight * 4);
this.renderer.readRenderTargetPixels(this.rtTexture, 0, 0, window.innerWidth, window.innerHeight, read);
// create a temporary canvas to compress the image
const canvas = document.createElement('canvas');
canvas.style.display = 'inline';
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// draw the image on the canvas
const d = new Uint8ClampedArray(read.buffer);
const cxt = canvas.getContext('2d');
cxt.putImageData(new ImageData(d, window.innerWidth, window.innerHeight), 0, 0);
// encode the image
let type = 'image/png';
switch (request.encoding) {
case 'jpg':
type = 'image/jpeg';
break;
case 'png':
type = 'image/png';
break;
case 'webp':
type = 'image/webp';
break;
}
if (!request.quality) {
request.quality = 0.92;
}
// actual encoding
const imageURL = canvas.toDataURL(type, request.quality);
const getFormData = () => {
const formData = new FormData();
formData.append(request.targetField, dataURItoBlob(imageURL), `screenshot.${request.encoding}`);
return formData;
};
// upload the image somewhere
fetch(request.targetURL, {
method: 'POST',
mode: 'cors',
body: (request.targetField) ? getFormData() : JSON.stringify({
data: imageURL,
id: request.correlation
})
})
.then(response => response.text())
.then(text => {
if (request.resultURL) {
fetch(request.resultURL, {
method: 'POST',
mode: 'cors',
body: JSON.stringify({
data: text,
id: request.correlation
})
});
}
});
}
}
const ui = new ScreenshotUI();
ui.initialize();