-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathforge.html
449 lines (449 loc) · 23.4 KB
/
forge.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FPS5 Forge</title>
<style>
html, body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
overflow: hidden !important;
user-select: none !important;
transition: top .2s;
background: #eeeeee;
}
* {
cursor: url(./cursors/cursor2.png), auto !important;
}
.sidebar {
position: absolute;
text-align: center;
width: 300px;
padding: 0;
margin: 0;
background: #fff;
left: 100%;
margin-left: -300px;
bottom: 200px;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
iframe {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: 0;
z-index: 99999;
}
button, input {
color: #333;
border: 2px solid #333;
background: #fff;
padding: 5px;
margin: 5px;
}
canvas {
outline: 20px solid #fff;
outline-offset: -20px;
}
.swal-button {
color: #333 !important;
border: 2px solid #333 !important;
background: #fff !important;
padding: 5px !important;
margin: 5px !important;
border-radius: none !important;
box-shadow: none !important;
}
label[for="file"][data-filehider] {
border: 2px solid #333;
padding: 5px;
background: #fff;
}
</style>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-2ZQ6TMGVXJ"></script>
<script>
window.dataLayer=window.dataLayer||[];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "G-2ZQ6TMGVXJ");
</script>
<body style="margin:0!important;">
<div class="pause-menu" style="display:none">
<h1 style="color:#25750a;font-weight:bold;font-family:monospace;position:absolute;left:20px;top:20px;font-size:75px;margin:0">PAUSED</h1>
<div class="pause-tab" onclick="unpause()">Continue</div><br>
<div class="pause-tab" onclick="confirm('Are you sure? All player will be disconnected.')&&cancelSearch()">Leave game</div><br>
<div class="pause-tab" onclick="openSettings()">Settings</div><br>
</div>
<audio id="grenade_explosion" src="sounds/grenade_explosion.mp3" volume="1" current-time=".1"></audio>
<audio id="ambience" src="sounds/ambience.mp3" autoplay loop volume="1"></audio>
<audio id="click" src="sounds/click.mp3"></audio>
<script src="https://parking-master.github.io/FPS3/js/three.min.js"></script>
<script src="https://threejs.org/examples/js/loaders/OBJLoader.js"></script>
<script src="https://threejs.org/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://threejs.org/examples/js/exporters/GLTFExporter.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js/examples/js/libs/fflate.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/alvaromontoro/gamecontroller.js@latest/dist/gamecontroller.min.js"></script>
<div class="sidebar">
<button onclick="add()">Add</button>
<br>
<button onclick="confirm('Are you sure?')&&env.traverse(x=>x!=sky&&x.name!='skyobj'&&env.remove(x))">Reset</button>
<hr>
<div id="objectRotation">
<span id="x">0</span>, <span id="y">0</span>, <span id="z">0</span>
</div>
<br>
<div id="objectPosition">
<span id="x">0</span>, <span id="y">0</span>, <span id="z">0</span>
</div>
<label for="preferredRotation">Object Rotation</label><br>
<input name="preferredRotation" id="preferredRotation" value="0, 0, 0" onkeydown="this.onkeyup()" onkeyup="currentItem&&(currentItem.preferredRotation.x=this.value.split(',')[0]-0,currentItem.preferredRotation.y=this.value.split(',')[1]-0,currentItem.preferredRotation.z=this.value.split(',')[2]-0)">
<hr>
<i>Drop: [ESC]</i>
<br>
<i>Pickup: [Click]</i>
<br>
<br>
<i>Ground level is -0.7</i>
<hr>
<button onclick="save()">Save</button>
<br>
<button onclick="test()">Test</button>
</div>
<script>
const scene=new THREE.Scene();
env = new THREE.Group();
const camera=new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,.1,1e3),renderer=new THREE.WebGLRenderer({antialias:true});renderer.setSize(window.innerWidth,window.innerHeight),document.body.appendChild(renderer.domElement),renderer.setPixelRatio(1.5);
renderer.setClearColor("#eee");
function isTouchDevice(){return!!window.matchMedia("(pointer: coarse)").matches}
isTouchDevice()?(()=>{const{Euler:e,EventDispatcher:t,Vector3:n}=THREE;var o;o=!1;var i=/iPhone|iPad|iPod|Android/i.test(navigator.userAgent),c=document.body.clientWidth/2,r=(document.body.clientHeight,window.innerHeight),s=function(t,s){o=!!i,void 0===s&&(console.warn('THREE.PointerLockControls: The second parameter "domElement" is now mandatory.'),s=document.body),this.domElement=s,this.isLocked=!1;var m,u,d,a=this,h={type:"change"},v={type:"lock"},l={type:"unlock"},E=new e(0,0,0,"YXZ"),p=Math.PI/2,k=Math.PI/3.8,L=Math.PI/9,f=new n,y=0,M=0;function x(){}function P(e){u=e.touches[0].clientY,m=e.touches[0].clientX,y=m-c,M=u-(r-100),E.setFromQuaternion(t.quaternion),E.y=.005*y,E.x=.005*M,E.x=Math.max(-p,Math.min(L,E.x)),E.y=Math.max(-k,Math.min(k,E.y)),t.quaternion.setFromEuler(E),a.dispatchEvent(h),console.log(M)}function w(e){}function g(e){if(!1!==a.isLocked){var n=e.movementX||e.mozMovementX||e.webkitMovementX||0,o=e.movementY||e.mozMovementY||e.webkitMovementY||0;E.setFromQuaternion(t.quaternion),E.y-=.002*n,E.x-=.002*o,E.x=Math.max(-p,Math.min(p,E.x)),E.y=Math.max(-k,Math.min(k,E.y)),t.quaternion.setFromEuler(E),a.dispatchEvent(h)}}function b(){document.pointerLockElement===a.domElement?(a.dispatchEvent(v),a.isLocked=!0):(a.dispatchEvent(l),a.isLocked=!1)}function F(){console.error("THREE.PointerLockControls: Unable to use Pointer Lock API")}this.connect=function(){document.addEventListener("mousemove",g,!1),document.addEventListener("touchstart",x,!1),document.addEventListener("touchmove",P,!1),document.addEventListener("pointerlockchange",b,!1),document.addEventListener("pointerlockerror",F,!1)},this.disconnect=function(){document.removeEventListener("mousemove",g,!1),document.removeEventListener("touchend",w,!1),document.removeEventListener("touchmove",onTouchmove,!1),document.removeEventListener("pointerlockchange",b,!1),document.removeEventListener("pointerlockerror",F,!1)},this.dispose=function(){this.disconnect()},this.getObject=function(){return t},this.getDirection=(d=new n(0,0,-1),function(e){return e.copy(d).applyQuaternion(t.quaternion)}),this.moveForward=function(e){f.setFromMatrixColumn(t.matrix,0),f.crossVectors(t.up,f),t.position.addScaledVector(f,e)},this.moveRight=function(e){f.setFromMatrixColumn(t.matrix,0),t.position.addScaledVector(f,e)},this.lock=1==o?function(){a.isLocked=!0,this.connect()}:function(){this.domElement.requestPointerLock()},this.unlock=function(){document.exitPointerLock()},this.connect()};(s.prototype=Object.create(t.prototype)).constructor=s,window.PointerLockControls=s})():(()=>{const _euler=new THREE.Euler(0,0,0,"YXZ"),_vector=new THREE.Vector3,_changeEvent={type:"change"},_lockEvent={type:"lock"},_unlockEvent={type:"unlock"},_PI_2=Math.PI/2;class PointerLockControls extends THREE.EventDispatcher{constructor(e,t){super(),void 0===t&&(console.warn('THREE.PointerLockControls: The second parameter "domElement" is now mandatory.'),t=document.body),this.domElement=t,this.isLocked=!1,this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.pointerSpeed=1;const o=this;function n(t){if(!1===o.isLocked)return;const n=t.movementX||t.mozMovementX||t.webkitMovementX||0,r=t.movementY||t.mozMovementY||t.webkitMovementY||0;_euler.setFromQuaternion(e.quaternion),_euler.y-=.002*n*o.pointerSpeed,_euler.x-=.002*r*o.pointerSpeed,_euler.x=Math.max(_PI_2-o.maxPolarAngle,Math.min(_PI_2-o.minPolarAngle,_euler.x)),e.quaternion.setFromEuler(_euler),o.dispatchEvent(_changeEvent)}function r(){o.domElement.ownerDocument.pointerLockElement===o.domElement?(o.dispatchEvent(_lockEvent),o.isLocked=!0):(o.dispatchEvent(_unlockEvent),o.isLocked=!1)}function c(){console.error("THREE.PointerLockControls: Unable to use Pointer Lock API")}this.connect=function(){o.domElement.ownerDocument.addEventListener("mousemove",n),o.domElement.ownerDocument.addEventListener("pointerlockchange",r),o.domElement.ownerDocument.addEventListener("pointerlockerror",c)},this.disconnect=function(){o.domElement.ownerDocument.removeEventListener("mousemove",n),o.domElement.ownerDocument.removeEventListener("pointerlockchange",r),o.domElement.ownerDocument.removeEventListener("pointerlockerror",c)},this.dispose=function(){this.disconnect()},this.getObject=function(){return e},this.getDirection=function(){const t=new THREE.Vector3(0,0,-1);return function(o){return o.copy(t).applyQuaternion(e.quaternion)}}(),this.moveForward=function(t){_vector.setFromMatrixColumn(e.matrix,0),_vector.crossVectors(e.up,_vector),e.position.addScaledVector(_vector,t)},this.moveRight=function(t){_vector.setFromMatrixColumn(e.matrix,0),e.position.addScaledVector(_vector,t)},this.lock=function(){this.domElement.requestPointerLock()},this.unlock=function(){o.domElement.ownerDocument.exitPointerLock()},this.connect()}}window.PointerLockControls=PointerLockControls})();
scene.add(camera);
map = new THREE.Mesh();
light=new THREE.DirectionalLight(0xf7f7ea,2),light.position.set(-10,20,-10),env.add(light),light.position.set(180,130,-144),light.rotation.set(Math.PI/2,0,0);
setInterval(()=>{renderer.shadowMap.enabled=!0,env.traverse((a=>{a.isObject3D&&(a.castShadow=!0,((a.name=="skyobj"||a==light)&&(a.receiveShadow=!0)),a.material&&a.material.map&&(a.material.map.anisotropy=16))})),light.shadow.bias=-1e-4,light.shadow.mapSize.width=1024*4,light.shadow.mapSize.height=1024*4,light.shadow.radius=3},1000);
sky=new THREE.Mesh(new THREE.SphereGeometry(100,100,100),new THREE.MeshBasicMaterial({color:0xffffff,map:(new THREE.TextureLoader).load("images/clouds3.png"),side:THREE.DoubleSide}));
sky.name="skyobj";
env.add(sky);
isWalkingForward = false;
isWalkingBackward = false;
isWalkingLeft = false;
isWalkingRight = false;
scene.add(env);
function test() {
(new THREE.GLTFExporter).parse(env,(function(e){const n=JSON.stringify(e),t=new Blob([n],{type:"application/json"});let o=URL.createObjectURL(t);
let button = document.createElement("button");
let frame = document.createElement("iframe");
button.onclick = () => {
renderer.domElement.style.display = "block";
button.remove();
frame.remove();
}
button.style = "padding:5px;background:#fff;color:#333;position:absolute;left:20px;top:20px;border:2px solid #333;z-index:999999";
button.textContent = "Close";
frame.src = ("src.html?character=mark-vi&mode=Slayer&blob=true&lobby="+(Math.random()*Math.random()).toString()+"&map="+o);
document.body.appendChild(button);
document.body.appendChild(frame);
renderer.domElement.style.display = "none";
}),{binary:!0});
}
function save(storage) {
(new THREE.GLTFExporter).parse(env,(function(e){const n=JSON.stringify(e),t=new Blob([n],{type:"application/json"});let o=URL.createObjectURL(t),c=document.createElement("a");c.download="scene.glb",c.href=o,storage?localStorage["autosaveForgeBuild"]=btoa(encodeURIComponent(o)):c.click()}),{binary:!0})
}
setInterval(() => {
if (isWalkingForward) {
camera.translateZ(-.1);
}
if (isWalkingBackward) {
camera.translateZ(.1);
}
if (isWalkingLeft) {
camera.translateX(-.1);
}
if (isWalkingRight) {
camera.translateX(.1);
}
});
document.addEventListener("keydown", (e) => {
e.key == "Escape" && (currentItem = null);
if (currentItem) {
if (e.key == "ArrowUp") {
currentItem.translateY(.1);
}
if (e.key == "ArrowDown") {
currentItem.translateY(-.1);
}
if (e.key == "ArrowLeft") {
(e.altKey)?currentItem.translateZ(-.1):currentItem.translateX(-.1);
}
if (e.key == "ArrowRight") {
(e.altKey)?currentItem.translateZ(.1):currentItem.translateX(.1);
}
return;
}
if (e.key == "ArrowUp") {
isWalkingForward = true;
}
if (e.key == "ArrowDown") {
isWalkingBackward = true;
}
if (e.key == "ArrowLeft") {
isWalkingLeft = true;
}
if (e.key == "ArrowRight") {
isWalkingRight = true;
}
});
document.addEventListener("keyup", (e) => {
if (e.key == "ArrowUp") {
isWalkingForward = false;
}
if (e.key == "ArrowDown") {
isWalkingBackward = false;
}
if (e.key == "ArrowLeft") {
isWalkingLeft = false;
}
if (e.key == "ArrowRight") {
isWalkingRight = false;
}
});
currentItem = null;
function animate() {
if (currentItem) {
// currentItem.position.copy(camera.position);
// currentItem.rotation.copy(camera.rotation);
// currentItem.updateMatrix();
currentItem.rotation.x=(currentItem.preferredRotation.x);
currentItem.rotation.y=(currentItem.preferredRotation.y);
currentItem.rotation.z=(currentItem.preferredRotation.z);
document.querySelector("#objectRotation").querySelector("#x").textContent=currentItem.rotation.x;
document.querySelector("#objectRotation").querySelector("#y").textContent=currentItem.rotation.y;
document.querySelector("#objectRotation").querySelector("#z").textContent=currentItem.rotation.z;
document.querySelector("#objectPosition").querySelector("#x").textContent=currentItem.position.x;
document.querySelector("#objectPosition").querySelector("#y").textContent=currentItem.position.y;
document.querySelector("#objectPosition").querySelector("#z").textContent=currentItem.position.z;
controls.target.copy(currentItem.position);
}
// sky.position.copy(camera.position);
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
typeof controls != "undefined" && controls.update();
animate();
controls=new THREE.OrbitControls(camera,renderer.domElement);
camera.translateZ(10);
controls.isLocked=false;
function add() {
let object = new THREE.Mesh();
let map, color = "#ffffff", geometry, finalGeometry;
object.material = new THREE.MeshPhongMaterial();
window.dataURL = function(files) {
let file = files[0];
let reader = new FileReader();
reader.addEventListener("load", () => {
fetch(reader.result).then(function(response){return(response.blob())}).then(function(blob){
map = new THREE.TextureLoader().load(URL.createObjectURL(blob));
});
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
window.changeColor = function(value) {
color = value;
}
window.changeGeometry = function(value) {
console.log(value);
geometry = value == "Cube" ? THREE.BoxGeometry : value == "Sphere" ? THREE.SphereGeometry : value == "Cylinder" && THREE.CylinderGeometry;
}
window.changeSize = function(value) {
value = value.split(",");
console.log(value[0]-0,value[1]-0,value[2]-0)
finalGeometry = new geometry(value[0]-0,value[1]-0,value[2]-0);
}
changeGeometry("Cube");
changeSize("1, 1, 1");
let e = document.createElement("div");
e.innerHTML = `
<h1>Geometry</h1>
<label for="shape">Shape:</label>
<select value="Cube" name="shape" onchange="changeGeometry(this.value)">
<option value="Cube">Cube</option>
<option value="Sphere">Sphere</option>
<option value="Cylinder">Cylinder</option>
</select>
<br>
<h1>Material</h1>
<label for="color">Color:</label>
<input name="color" value="#FFFFFF" type="color" onchange="changeColor(this.value)">
<br>
<label for="file">Texture:</label>
<label for="file" data-filehider onclick="document.querySelector('input[type=\\'file\\']').click()">Upload</label><input hidden name="file" type="file" accept=".jpg,.jpeg,.png" onchange="dataURL(this.files)">
<br>
<h1>Size</h1>
<label for="size">Scale:</label>
<input name="size" value="1, 1, 1" type="text" onchange="changeSize(this.value)">
`;
swal({
content: e,
buttons: ["Cancel", "Add Object"]
}).then((e) => {
if (!e) {
return swal.close();
}
if (map) {
object.material.map = map;
}
object.geometry = finalGeometry;
object.material.color.r = new THREE.Color(color).r;
object.material.color.g = new THREE.Color(color).g;
object.material.color.b = new THREE.Color(color).b;
env.add(object);
object.preferredRotation = {};
object.preferredRotation.x = 0;
object.preferredRotation.y = 0;
object.preferredRotation.z = 0;
currentItem = object;
});
}
document.addEventListener("mousedown", (e) => {
document.onmousemove = (e) => {
isOrbiting = true;
}
});
document.addEventListener("mouseup", (e) => {
e.preventDefault();
document.onmousemove = null;
let wasOrbiting = isOrbiting;
isOrbiting = false;
if (e.target != renderer.domElement || wasOrbiting) {
return;
}
scene.traverse((x) => {
let raycaster = new THREE.Raycaster();
let mouse = new THREE.Vector2();
mouse.x=(e.clientX/renderer.domElement.clientWidth)*2-1;
mouse.y=-(e.clientY/renderer.domElement.clientHeight)*2+1;
raycaster.setFromCamera(mouse,camera);
let intersects = raycaster.intersectObjects([x]);
let intersection=null;
intersects[0]&&(intersection=intersects[0]);
if (intersection) {
let object = intersection.object;
let map, color = "#ffffff", geometry, finalGeometry;
window.dataURL = function(files) {
let file = files[0];
let reader = new FileReader();
reader.addEventListener("load", () => {
fetch(reader.result).then(function(response){return(response.blob())}).then(function(blob){
map = new THREE.TextureLoader().load(URL.createObjectURL(blob));
console.log(map);
});
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
window.changeColor = function(value) {
color = value;
}
window.changeGeometry = function(value) {
console.log(value);
geometry = value == "Cube" ? THREE.BoxGeometry : value == "Sphere" ? THREE.SphereGeometry : value == "Cylinder" && THREE.CylinderGeometry;
}
window.removeObject = function() {
scene.remove(object);
}
window.addObject = function() {
env.add(object);
}
window.duplicateObject = function() {
let r = object.clone();
env.add(r);
currentItem = r;
swal.close();
}
window.changeSize = function(value) {
value = value.split(",");
console.log(value[0]-0,value[1]-0,value[2]-0)
finalGeometry = new geometry(value[0]-0,value[1]-0,value[2]-0);
}
if (intersection.object == sky) {
let e = document.createElement("div");
e.innerHTML = `
<h1>Material</h1>
<label for="color">Color:</label>
<input name="color" value="#FFFFFF" type="color" onchange="changeColor(this.value)">
<br>
<label for="file">Background:</label>
<label for="file" data-filehider onclick="document.querySelector('input[type=\\'file\\']').click()">Upload</label><input hidden name="file" type="file" accept=".jpg,.jpeg,.png" onchange="dataURL(this.files)">
<br>
<br>
<button onclick="env.remove(sky)">Remove</button>
`;
swal({
title: "Sky Properties",
content: e
}).then(() => {
if (map) {
sky.material.map = map;
}
sky.material.color.r = new THREE.Color(color).r;
sky.material.color.g = new THREE.Color(color).g;
sky.material.color.b = new THREE.Color(color).b;
});
changeGeometry("Sphere");
changeSize("100, 100, 100");
} else {
let e = document.createElement("div");
e.innerHTML = `
<h1>Material</h1>
<label for="color">Color:</label>
<input name="color" value="${("#"+object.material.color.getHexString()).toUpperCase()}" type="color" onchange="changeColor(this.value)">
<br>
<label for="file">Texture:</label>
<label for="file" data-filehider onclick="document.querySelector('input[type=\\'file\\']').click()">Upload</label><input hidden name="file" type="file" accept=".jpg,.jpeg,.png" onchange="dataURL(this.files)">
<br>
<h1>Size</h1>
<label for="size">Scale:</label>
<input readonly name="size" value="${object.geometry.parameters.width}, ${object.geometry.parameters.height}, ${object.geometry.parameters.depth}" type="text">
<br>
<br>
<button onclick="removeObject()">Remove Object</button>
<br>
<button onclick="addObject()">Add Object</button>
<br>
<button onclick="duplicateObject()">Duplicate Object</button>
`;
swal({
title: "Object Properties",
content: e,
buttons: ["Pickup", "Save"]
}).then((e) => {
if (e) {
if (map) {
object.material.map = map;
}
object.material.color.r = new THREE.Color(color).r;
object.material.color.g = new THREE.Color(color).g;
object.material.color.b = new THREE.Color(color).b;
return camera.translateZ(10);
}
currentItem = object;
});
changeSize("1, 1, 1");
}
}
});
});
// setInterval(() => { localStorage["autosaveForgeBuild"]=JSON.stringify(env) }, 5000);
</script>
</body>
</html>