-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
483 lines (404 loc) · 16.5 KB
/
index.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MapEditorJS</title>
<!--<link rel="stylesheet" type="text/css" href="node_modules/bulma/css/bulma.css" />-->
<link rel="stylesheet" type="text/css" href="css/style.css" />
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<!-- Viewport -->
<!-- <div>
<div id="canvasview"></div>
</div>-->
<nav class="navbar has-background-dark" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<!-- navbar items, navbar burger... -->
</div>
</nav>
<div class="columns is-fullheight">
<div class="menu column is-2 is-sidebar-menu is-hidden-mobile">
<p class="menu-label">
Room List
</p>
<ul class="menu-list">
<li><a>Room 0</a></li>
<li><a>Room 1</a></li>
</ul>
<p class="menu-label">
Placeholder
</p>
<ul class="menu-list">
<li><a>Team Placeholder</a></li>
<li>
<a class="is-active">Placeholder</a>
<ul>
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
</ul>
</li>
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
</ul>
<p class="menu-label">
Placeholder
</p>
<ul class="menu-list">
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
<li><a>Placeholder</a></li>
</ul>
</div>
<div class="column">
<div class="main-viewport" id="editor-viewport"></div>
</div>
</div>
<script src="node_modules/three/examples/js/libs/inflate.min.js"></script>
<script type="x-shader/x-vertex" id="vertexShader">
varying vec2 vUv;
varying vec3 vColor;
void main()
{
//vColor = pow(color.rgb, vec3(1.0/2.2));
vColor = color;
// Convert UV to 256pixel range
//int pxU = int(uv.x * 255.0);
//int pxV = int(uv.y * 255.0);
//vUv = vec2(float(pxU)/255.0+(0.5/255.0),float(pxV)/255.0+(0.5/255.0));
vUv = uv;
//int posX = int(position.x * 256.0);
//int posY = int(position.y * 256.0);
//int posZ = int(position.z * 256.0);
//vec4 modelViewPosition = modelViewMatrix * vec4(float(posX)/256.0,float(posY)/256.0,float(posZ)/256.0, 1.0);
vec4 modelViewPosition = modelViewMatrix * vec4(position.xyz, 1.0);
gl_Position = projectionMatrix * modelViewPosition;
}
</script>
<script type="x-shader/x-fragment" id="fragmentShader">
uniform sampler2D texture;
uniform float selected;
uniform vec3 selectedColor;
varying vec2 vUv;
varying vec3 vColor;
void main()
{
vec4 textureIn = texture2D(texture, vUv);
int tex_r = (int(textureIn.r * 255.0) * int(vColor.r * 255.0))/128;
int tex_g = (int(textureIn.g * 255.0) * int(vColor.g * 255.0))/128;
int tex_b = (int(textureIn.b * 255.0) * int(vColor.b * 255.0))/128;
float texture_a = 1.0;
if(textureIn.r == 0.0 && textureIn.g == 0.0 && textureIn.b == 0.0){
texture_a = 0.0;
}
vec3 normalColor = vec3(float(tex_r)/255.0,float(tex_g)/255.0, float(tex_b)/255.0);
vec3 finalColor = mix(normalColor, selectedColor, selected);
gl_FragColor = vec4(finalColor.rgb, texture_a);
}
</script>
<script type="x-shader/x-vertex" id="vertexShaderNoTex">
varying vec3 vColor;
varying vec2 vUv;
void main()
{
vColor = color;
vUv = uv;
vec4 modelViewPosition = modelViewMatrix * vec4(position.xyz, 1.0);
gl_Position = projectionMatrix * modelViewPosition;
}
</script>
<script type="x-shader/x-fragment" id="fragmentShaderNoTex">
uniform float selected;
uniform vec3 selectedColor;
varying vec2 vUv;
varying vec3 vColor;
void main()
{
int tex_r = int(vColor.r * 255.0);
int tex_g = int(vColor.g * 255.0);
int tex_b = int(vColor.b * 255.0);
float texture_a = 1.0;
vec3 normalColor = vec3(float(tex_r)/255.0,float(tex_g)/255.0, float(tex_b)/255.0);
vec3 finalColor = mix(normalColor, selectedColor, selected);
gl_FragColor = vec4(finalColor.rgb, texture_a);
}
</script>
<!--<script src="./loader/PSXJSONLoader.js"></script>-->
<script>
const { ipcRenderer } = require('electron');
const THREE = require('three');
require('three/examples/js/controls/OrbitControls');
require('three/examples/js/controls/TransformControls');
require('./loader/PSXJSONLoader');
var viewport_element = document.getElementById("editor-viewport");
//var positionInfo = viewport_element.getBoundingClientRect();
//var height = positionInfo.height;
//var width = positionInfo.width;
var textures = [];
var materials = [];
var loaded_obj;
var world_subdiv = 256; // int 256 = float 1.0
// Internal Stuff
// Create a Texture simulating the PlayStation VRAM
// The PlayStation VRAM is 1024x512 pixels wide.
// When 4bit textures are used, the total size in pixels is actually
// 4096x512. Since PCs don't support 4bit paletted textures, we simulate
// the different textures by stretching them on the virtual VRAM buffer
// so a 4bit textures keeps it's original pixel count, while a
// 8bit texture will have double width, and 16bit 4x width.
// We repeat pixels to fit.
var vbuffer_width = 1024;
var vbuffer_vwidth = vbuffer_width * 4;
var vbuffer_height = 512;
//var vbuffer_data = new Uint8Array(vbuffer_vwidth * vbuffer_height * 4); // RGBA buffer
//var vbuffer_texture = new THREE.DataTexture(vbuffer_data, vbuffer_vwidth, vbuffer_height, THREE.RGBAFormat);
var vbuffer_canvas = document.createElement('canvas');
var vbuffer_ctx = vbuffer_canvas.getContext('2d');
vbuffer_canvas.width = vbuffer_vwidth;
vbuffer_canvas.height = vbuffer_height;
vbuffer_ctx.imageSmoothingEnabled = false;
vbuffer_ctx.fillStyle = '#008080';
vbuffer_ctx.fillRect(0, 0, vbuffer_canvas.width, vbuffer_canvas.height);
vbuffer_ctx.fillStyle = '#008000';
vbuffer_ctx.fillRect(2048, 0, 2048, 240);
vbuffer_ctx.fillStyle = '#808000';
vbuffer_ctx.fillRect(2048, 256, 2048, 240);
var vbuffer_texture = new THREE.CanvasTexture(vbuffer_canvas);
vbuffer_texture.magFilter = THREE.NearestFilter;
vbuffer_texture.minFilter = THREE.NearestFilter;
//document.getElementById('canvasview').appendChild(vbuffer_canvas);
var vramContext = {
width: vbuffer_width,
vwidth: vbuffer_vwidth,
height: vbuffer_height,
canvas: vbuffer_canvas,
context: vbuffer_ctx,
texture: vbuffer_texture,
update: (image, x, y, w, h) => {
this.context.drawImage(texture.image, x, y, w, h);
this.vramContext.texture.needsUpdate = true;
ipcRenderer.send('update-vram-contents', 'test');
}
};
var selectedObject = null;
//console.log(vbuffer_texture.);
//var vram_texture = {
// width: vbuffer_width,
// height: vbuffer_height,
// vwidth: vbuffer_vwidth,
// rawdata: vbuffer_data,
//};
var renderer = new THREE.WebGLRenderer();
const canvas = renderer.domElement;
canvas.className = 'main-viewport-canvas';
document.getElementById("editor-viewport").appendChild( canvas );
const width = canvas.clientWidth;
const height = canvas.clientHeight;
renderer.setSize(width, height, false);
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
camera.aspect = width / height;
camera.updateProjectionMatrix();
//renderer.setSize( width, height, false );
renderer.outputEncoding = THREE.LinearEncoding;
renderer.toneMapping = THREE.LinearToneMapping;
camera.position.z = 5;
controls = new THREE.OrbitControls( camera, canvas );
controls.screenSpacePanning = true;
//controls.mouseButtons = {LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.PAN, RIGHT: THREE.MOUSE.DOLLY };
controls.target.set( 0, 0, 0 );
controls.update();
function generateShader(texture) {
var psxUniforms = {
texture: { type: 't', value: texture },
selected: { value: 0.0 },
selectedColor: { value: new THREE.Color(rgb(255, 180, 38)) }
};
var psxUniformsNoTex = {
selected: { value: 0.0 },
selectedColor: { value: new THREE.Color(rgb(255, 180, 38)) }
};
var psxMaterial = new THREE.ShaderMaterial({
transparent: true,
alphaTest: 0.5,
uniforms: psxUniforms,
vertexShader: document.getElementById('vertexShader').textContent,
vertexColors: THREE.VertexColors,
fragmentShader: document.getElementById('fragmentShader').textContent,
});
//var psxMaterialNoTex = new THREE.ShaderMaterial({
// transparent: true,
// alphaTest: 0.5,
// uniforms: psxUniformsNoTex,
// vertexShader: document.getElementById('vertexShaderGouraud').textContent,
// vertexColors: THREE.VertexColors,
// fragmentShader: document.getElementById('fragmentShaderGouraud').textContent,
//});
psxMaterial.needsUpdate = true;
//psxMaterialNoTex.needsUpdate = true;
return psxMaterial;
}
function getTextureMap(material) {
var texmap;
//console.log(material);
if(material.map !== null) {
texmap = material.map;
} else if(material.emissiveMap !== null) {
texmap = material.emissiveMap;
} else {
texmap = null;
}
//console.log(texmap);
//LinearEncoding
if(texmap !== null){
texmap.encoding = THREE.LinearEncoding;
texmap.needUpdate = true;
}
return texmap;
}
var gridHelper = new THREE.GridHelper( 254, 254 );
scene.add( gridHelper );
var boundingBox = new THREE.Box3();
var lockSelection = false;
var selectionBox = new THREE.BoxHelper();
selectionBox.material.depthTest = false;
selectionBox.material.transparent = true;
selectionBox.visible = true;
selectionBox.position.set( 0, 0, 0 );
scene.add( selectionBox );
transformControl = new THREE.TransformControls( camera, canvas );
//transformControl.addEventListener( 'change', renderer );
transformControl.addEventListener( 'dragging-changed', function (event) {
controls.enabled = !event.value;
lockSelection = event.value;
console.log(lockSelection);
if(!event.value){
// Scale Position to integer precision
selectedObject.position.x = parseInt(selectedObject.position.x*world_subdiv) / world_subdiv;
selectedObject.position.y = parseInt(selectedObject.position.y*world_subdiv) / world_subdiv;
selectedObject.position.z = parseInt(selectedObject.position.z*world_subdiv) / world_subdiv;
}
selectionBox.setFromObject( selectedObject );
});
scene.add(transformControl);
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
animate();
click_drag = false;
var shaderData = {
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent,
vertexShaderNoTex: document.getElementById('vertexShaderNoTex').textContent,
fragmentShaderNoTex: document.getElementById('fragmentShaderNoTex').textContent,
}
var loader = new THREE.PSXJSONLoader();
loader.load('test_data/test_map.json', function ( object ) {
console.log("Load Function: ");
console.log(object);
loaded_obj = object;
scene.add(object.collection);
selectionBox.setFromObject(loaded_obj.collection.children[1]);
selectedObject = loaded_obj.collection.children[1];
}, null, null, shaderData, vramContext );
window.addEventListener( 'resize', onWindowResize, false );
window.addEventListener( 'mousedown', () => click_drag = false );
window.addEventListener( 'mousemove', () => click_drag = true );
window.addEventListener( 'mouseup', onDocumentMouseClick, false );
//function onDocumentMouseMove( event ) {
function onDocumentMouseClick( event ) {
event.preventDefault();
if(click_drag){
click_drag = false;
return;
}
if(event.button !== 0) return;
if(lockSelection) return;
if ( selectedObject ) {
setObjectSelectedState( selectedObject, 0.0 );
selectedObject = null;
}
var intersects = getIntersects( event.layerX, event.layerY );
if ( intersects.length > 0 ) {
var res = intersects.filter( function ( res ) {
return res && res.object;
} )[ 0 ];
if ( res && res.object ) {
selectedObject = res.object;
transformControl.attach(selectedObject);
//selectedObject.material.uniforms.selected.value = 1.0;
setObjectSelectedState( selectedObject, 0.5 );
selectionBox.setFromObject( selectedObject );
selectionBox.update();
}
}
}
function onWindowResize(){
//positionInfo.width positionInfo.height
//camera.aspect = window.innerWidth / window.innerHeight;
//let posinfo = document.getElementById('editor-viewport').getBoundingClientRect();
//const canvas = renderer.domElement;
//console.log('new w/h:',posinfo.width, posinfo.height);
//camera.aspect = posinfo.width / posinfo.height;
//camera.updateProjectionMatrix();
//renderer.setSize( posinfo.width, posinfo.height, false );
//renderer.setSize( window.innerWidth, window.innerHeight );
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
if (canvas.width !== width || canvas.height !== height) {
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
}
ipcRenderer.on('view-commands', (event, message) => {
console.log(message);
if(message === 'toggle-grid-on'){
gridHelper.visible = true;
}
if(message === 'toggle-grid-off'){
gridHelper.visible = false;
}
});
ipcRenderer.on('get-vram-contents', (event, message) => {
console.log('inside index.html', message);
let VramData = {
width: vramContext.vwidth,
height: vramContext.height,
data: vramContext.context.getImageData(0,0,vramContext.vwidth,vramContext.height).data
};
ipcRenderer.send('update-vram-contents', VramData);
});
var raycaster = new THREE.Raycaster();
var mouseVector = new THREE.Vector3();
function setObjectSelectedState( obj, val ) {
//console.log(obj.material);
if(obj.material){
//obj.material.wireframe = (val > 0.0) ? true : false;
if(obj.material.uniforms.selected){
obj.material.uniforms.selected.value = val;
}
}
}
function getIntersects( x, y ) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
x = ( x / width ) * 2 - 1;
y = - ( y / height ) * 2 + 1;
mouseVector.set( x, y, 0.5 );
raycaster.setFromCamera( mouseVector, camera );
if(loaded_obj){
return raycaster.intersectObject( loaded_obj.collection, true );
} else {
return;
}
}
</script>
</body>
</html>