forked from utkuali/utk_streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
211 lines (173 loc) · 7.2 KB
/
script.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
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
200
201
202
203
204
205
206
207
208
209
210
211
//! CONFIG
const uploadUrl = ""; //? default upload url
const uploadField = ""; //? default upload field
//! CONFIG
import {OrthographicCamera, Scene, WebGLRenderTarget, LinearFilter, NearestFilter, RGBAFormat, UnsignedByteType, CfxTexture, ShaderMaterial, PlaneBufferGeometry, Mesh, WebGLRenderer} from "/module/Three.js";
var isAnimated = false;
var MainRender;
var scId = 0;
// from https://stackoverflow.com/a/12300351
function dataURItoBlob(dataURI) {
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;
}
// citizenfx/screenshot-basic
class GameRender {
constructor() {
window.addEventListener('resize', this.resize);
const cameraRTT = new OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -10000, 10000);
cameraRTT.position.z = 0;
//const width = Math.floor(window.innerHeight * 10 / 23);
cameraRTT.setViewOffset(window.innerWidth, window.innerHeight, 0, 0, window.innerWidth, window.innerHeight);
const sceneRTT = new Scene();
const rtTexture = new WebGLRenderTarget(window.innerWidth, window.innerHeight, {minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat, type: UnsignedByteType});
const gameTexture = 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);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D tDiffuse;
void main() {
gl_FragColor = texture2D(tDiffuse, vUv);
}
`
});
this.material = material;
const plane = new PlaneBufferGeometry(window.innerWidth, window.innerHeight);
const quad = new Mesh(plane, material);
quad.position.z = -100;
sceneRTT.add(quad);
const renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.autoClear = false;
let appendArea = document.createElement("div");
appendArea.id = "three-game-render";
document.body.append(appendArea);
appendArea.appendChild(renderer.domElement);
appendArea.style.display = 'none';
this.renderer = renderer;
this.rtTexture = rtTexture;
this.sceneRTT = sceneRTT;
this.cameraRTT = cameraRTT;
this.gameTexture = gameTexture;
this.customCamRatio = false;
this.tempCanvas = false;
this.animate = this.animate.bind(this);
requestAnimationFrame(this.animate);
}
resize() {
const cameraRTT = new OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -10000, 10000);
cameraRTT.position.z = 100;
let width = window.innerWidth;
let screenLeft = 0;
if (this.customCamRatio) {
width = Math.floor(window.innerHeight * (this.customCamRatio));
screenLeft = this.screenLeft;
}
cameraRTT.setViewOffset(window.innerWidth, window.innerHeight, screenLeft, 0, width, window.innerHeight);
cameraRTT.zoom = 0.1;
this.cameraRTT = cameraRTT;
const sceneRTT = new Scene();
const plane = new PlaneBufferGeometry(window.innerWidth, window.innerHeight);
const quad = new Mesh(plane, this.material);
quad.position.z = -100;
sceneRTT.add(quad);
this.sceneRTT = sceneRTT;
this.rtTexture = new WebGLRenderTarget(window.innerWidth, window.innerHeight, {minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat, type: UnsignedByteType});
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
animate() {
requestAnimationFrame(this.animate);
if (isAnimated) {
this.renderer.clear();
this.renderer.render(this.sceneRTT, this.cameraRTT, this.rtTexture, true);
const read = new Uint8Array(window.innerWidth * window.innerHeight * 4);
this.renderer.readRenderTargetPixels(this.rtTexture, 0, 0, window.innerWidth, window.innerHeight, read);
this.canvas.style.display = 'inline';
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
const d = new Uint8ClampedArray(read.buffer);
const cxt = this.canvas.getContext('2d');
const imageData = new ImageData(d, window.innerWidth, window.innerHeight);
if (this.tempCanvas) {
const temp_cxt = this.tempCanvas.getContext('2d');
temp_cxt.putImageData(imageData, 0, 0);
}
cxt.putImageData(imageData, 0, 0);
}
}
createTempCanvas() {
this.tempCanvas = document.createElement("canvas");
this.tempCanvas.style.display = 'inline';
this.tempCanvas.width = window.innerWidth;
this.tempCanvas.height = window.innerHeight;
}
renderToTarget(element, ratio, left) {
if (ratio) {
this.customCamRatio = ratio;
this.screenLeft = left;
let width = Math.floor(window.innerHeight * (this.customCamRatio));
let screenLeft = this.screenLeft;
this.cameraRTT.setViewOffset(window.innerWidth, window.innerHeight, screenLeft, 0, width, window.innerHeight);
}
this.canvas = element;
isAnimated = true;
}
requestScreenshot = (url, field) => new Promise((res) => {
this.createTempCanvas();
if (!isAnimated) isAnimated = true;
url = url ? url : uploadUrl;
field = field ? field : uploadField;
let wasInAnim = isAnimated;
this.cameraRTT.setViewOffset(window.innerWidth, window.innerHeight, 0, 0, window.innerWidth, window.innerHeight);
const imageURL = this.tempCanvas.toDataURL("image/png", 1.0);
const getFormData = () => {
const formData = new FormData();
formData.append(field, dataURItoBlob(imageURL), `screenshot.png`);
return formData;
};
fetch(url, {
method: 'POST',
mode: 'cors',
body: (field) ? getFormData() : JSON.stringify({
data: imageURL,
id: scId
})
})
.then(response => response.text())
.then(text => {
res(text);
});
scId++;
isAnimated = wasInAnim;
this.tempCanvas.remove();
this.tempCanvas = false;
})
stop() {
isAnimated = false;
if (this.canvas) {
if (this.canvas.style.display != "none") {
this.canvas.style.display = "none";
}
}
}
}
setTimeout(() => {
MainRender = new GameRender();
window.MainRender = MainRender;
}, 1000);