-
Notifications
You must be signed in to change notification settings - Fork 2
/
cube_editor.html
1685 lines (1417 loc) · 59.5 KB
/
cube_editor.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebGL Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>
/* materials (cube selection state), have ctrl + d, ctrl + z, ctrl + shift + z show up onscreen when they become possible */
const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl2', {antialias: true});
if (!gl) { alert('Failed to initialize WebGL'); }
(window.onresize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* account for e.g. high-retina macbook screens */
if (window.devicePixelRatio > 1) {
canvas.style.width = `${canvas.width}px`;
canvas.style.height = `${canvas.height}px`;
canvas.width *= window.devicePixelRatio;
canvas.height *= window.devicePixelRatio;
}
gl.viewport(
0,
0,
canvas.width,
canvas.height
);
})();
let shaders;
/* compile shaders */
{
const vs_geo = `
attribute vec3 a_pos;
attribute vec4 a_color;
uniform mat4 u_matrix;
uniform vec4 u_color;
varying vec4 v_color;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_color = u_color*0.85 + 0.15*a_color;
}`;
const fs_geo = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}`;
/* one notable difference in gizmo's shaders (compared to geo's) is that
* gizmo passes in the 4th coordinate from CPU-side. */
const vs_gizmo = `
attribute vec4 a_pos;
attribute vec4 a_color;
varying vec4 v_color;
void main() {
gl_Position = a_pos;
v_color = a_color;
}`;
const fs_gizmo = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}`;
const vs_grid = `#version 300 es
in vec3 a_pos;
in vec2 a_uv;
uniform mat4 u_matrix;
out vec2 v_uv;
void main() {
gl_Position = u_matrix * vec4(a_pos.xyz, 1);
v_uv = (a_uv - 0.5)*abs(a_pos.xy);
}`;
const fs_grid = `#version 300 es
precision mediump float;
in vec2 v_uv;
/* https://iquilezles.org/articles/filterableprocedurals/ */
const float N = 30.0; // grid ratio
float gridTextureGradBox(in vec2 p, in vec2 ddx, in vec2 ddy) {
// filter kernel
vec2 w = max(abs(ddx), abs(ddy)) + 0.01;
// analytic (box) filtering
vec2 a = p + 0.5*w;
vec2 b = p - 0.5*w;
vec2 i = (floor(a)+min(fract(a)*N,1.0)-
floor(b)-min(fract(b)*N,1.0))/(N*w);
//pattern
return (1.0-i.x)*(1.0-i.y);
}
out vec4 frag_color;
void main() {
// vec2 d = 1.0 - step(0.01, fract(v_uv*10.0));
// gl_FragColor = vec4(max(d.x, d.y));
vec2 uv = (v_uv - 0.5) + 0.5/N;
float grid = gridTextureGradBox(uv, dFdx(uv), dFdy(uv));
frag_color = vec4(1.0 - grid);
frag_color *= 0.25;
frag_color *= 0.3 + 0.6*smoothstep(0.0, 0.1, 1.0 - length(v_uv)/5.5);
}`;
function createProgram(gl, vertexSource, fragmentSource) {
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader));
}
return shader;
}
const program = gl.createProgram();
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(program));
}
const wrapper = {program};
const numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i = 0; i < numAttributes; i++) {
const attribute = gl.getActiveAttrib(program, i);
wrapper[attribute.name] = gl.getAttribLocation(program, attribute.name);
}
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < numUniforms; i++) {
const uniform = gl.getActiveUniform(program, i);
wrapper[uniform.name] = gl.getUniformLocation(program, uniform.name);
}
return wrapper;
}
shaders = {
geo: createProgram(gl, vs_geo , fs_geo ),
grid: createProgram(gl, vs_grid , fs_grid ),
gizmo: createProgram(gl, vs_gizmo, fs_gizmo),
}
}
const buf = {
geo_v_pos: gl.createBuffer(),
geo_v_color: gl.createBuffer(),
geo_i: gl.createBuffer(),
gizmo_v_pos: gl.createBuffer(),
gizmo_v_color: gl.createBuffer(),
gizmo_i: gl.createBuffer(),
grid_v_pos: gl.createBuffer(),
grid_v_uv: gl.createBuffer(),
grid_i: gl.createBuffer(),
};
const palette = [
/* lamp */
[
[0.148438, 0.273438, 0.324219, 1],
[0.164063, 0.613281, 0.558594, 1],
[0.910156, 0.765625, 0.414063, 1],
[0.953125, 0.632813, 0.378906, 1],
[0.902344, 0.433594, 0.316406, 1]
],
/* forest */
[
[0.937500, 0.914063, 0.820313, 1],
[0.863281, 0.894531, 0.710938, 1],
[0.675781, 0.753906, 0.468750, 1],
[0.660156, 0.515625, 0.402344, 1],
[0.421875, 0.343750, 0.296875, 1]
],
/* truck */
[
[0.468750, 0.000000, 0.000000, 1],
[0.753906, 0.070313, 0.121094, 1],
[0.988281, 0.937500, 0.832031, 1],
[0.000000, 0.187500, 0.285156, 1],
[0.398438, 0.605469, 0.734375, 1]
]
][0];
const ACTION_TRANSFORM = 0;
const ACTION_SPAWN = 1;
const ACTION_DELETE = 2;
const ACTION_COLOR = 3;
const ACT_DO = 1 << 0;
const ACT_UNDO = 1 << 1;
const ACT_KEEP_UNDONE = 1 << 2;
/* TODO: look into refactoring ACTION_TRANSFORM (usage of, i.e. outside of this function)
* so that a save.entities[i].transform isn't modified until the action is complete,
* to prevent leaving the world in invalid states where actions are half-applied?
*
* maybe only do this if related bugs are found. */
function action_apply(dir, action) {
if (dir & ACT_DO ) save.actions.push(action);
if (dir & ACT_UNDO) save.actions_undone.push(action = save.actions.pop());
if ((dir & ACT_DO) && !(dir & ACT_KEEP_UNDONE)) save.actions_undone = [];
if (action.kind == ACTION_TRANSFORM) {
if (dir & ACT_DO ) action.removed_transform = mat4_create(),
action.removed_transform.set(save.entities[action.index].transform);
if (dir & ACT_DO ) save.entities[action.index].transform.set(action.transform);
if (dir & ACT_UNDO) save.entities[action.index].transform.set(action.removed_transform);
}
if (action.kind == ACTION_COLOR) {
if (dir & ACT_DO ) action.removed_color = save.entities[action.index].color;
if (dir & ACT_DO ) save.entities[action.index].color = action.color;
if (dir & ACT_UNDO) save.entities[action.index].color = action.removed_color;
}
if (action.kind == ACTION_SPAWN) {
if (dir & ACT_DO ) save.entities.push(action.ent);
if (dir & ACT_UNDO) save.entities.pop();
}
if (action.kind == ACTION_DELETE) {
if (dir & ACT_DO ) action.removed_ent = save.entities.splice(action.index, 1)[0];
if (dir & ACT_UNDO) save.entities.push(action.removed_ent);
}
}
const save = {
actions: [],
actions_undone: [],
entities: [],
};
action_apply(ACT_DO, { kind: ACTION_SPAWN , ent: { color: palette[0], transform: mat4_from_translation(mat4_create(), 1, 1, 1) } });
action_apply(ACT_DO, { kind: ACTION_SPAWN , ent: { color: palette[1], transform: mat4_from_translation(mat4_create(), 1, -2, 1) } });
action_apply(ACT_DO, { kind: ACTION_SPAWN , ent: { color: palette[2], transform: mat4_from_translation(mat4_create(), 2, 0, 1) } });
action_apply(ACT_UNDO);
let input = {
/* camera/controls: */
/* { */
pitch: Math.PI*0.25,
yaw: -Math.PI*0.25,
eye: [0, 0, 0, 1],
cam_pivot_x: 0,
cam_pivot_y: 0,
cam_pivot_z: 0,
/* picking */
last_u_vp_inv: mat4_create(),
last_u_vp: mat4_create(),
/* } */
/* globalized input */
/* { */
lmb_down: false,
lmb_released: false, /* true for a frame after up */
lmb_down_x: 0, /* for when we need to calculate our own "movementX" in the context of an action */
lmb_down_y: 0,
rmb_down: false,
mouse_x: 0,
mouse_y: 0,
damped_event: { button: 0, movementX: 0, movementY: 0 },
zoom: 10,
scroll: 0,
/* } */
/* global selection state */
/* { */
entity_hovered: -1, /* this is an index into save.entities */
entity_selected: -1, /* this is an index into save.entities */
/* } */
/* gizmo */
/* { */
lmb_down_transform: mat4_create(),
captured_mouse: -1, /* this is a "gizmo ID" as defined in the context of "function gizmo" */
/* } */
}
window.onkeydown = e => {
if (e.code == "Delete") {
action_apply(ACT_DO, { kind: ACTION_DELETE, index: input.entity_selected });
input.entity_selected = -1;
input.entity_hovered = -1;
input.captured_mouse = -1;
}
if (e.ctrlKey && e.code == "KeyZ") {
if (!e.shiftKey) {
action_apply(ACT_UNDO);
} else {
/* TODO: message here explaining actions_undone stack is empty */
if (save.actions_undone.length > 0) {
action_apply(ACT_DO | ACT_KEEP_UNDONE, save.actions_undone.pop());
/* undo/redoing should probably clear your active hover-state */
input.captured_mouse = -1;
}
}
input.entity_hovered = -1;
input.entity_selected = -1;
/* undo/redoing should probably clear your active hover-state */
input.captured_mouse = -1;
}
if (e.ctrlKey && e.code == "KeyD") {
const selected = save.entities[input.entity_selected]
let ent = {
transform: new Float32Array(selected.transform),
color: selected.color
};
action_apply(ACT_DO, { kind: ACTION_SPAWN, ent });
e.preventDefault();
}
}
let wheelTimeout;
window.addEventListener("wheel", e => {
e.preventDefault();
if (input.lmb_down) return;
input.scroll = Math.sign(e.deltaY);
clearTimeout(wheelTimeout);
wheelTimeout = setTimeout(() => input.scroll = 0, 100)
}, { passive: false });
window.ondblclick = () => {
input.entity_selected = input.entity_hovered;
}
window.onmousedown = ev => {
ev.preventDefault();
if (ev.button == 0 && ev.shiftKey) {
input.rmb_down = true;
return;
}
if (ev.button == 2 || (ev.button == 0 && ev.shiftKey))
input.rmb_down = true;
if (ev.button == 0) {
input.lmb_released = true;
input.lmb_down = true;
input.lmb_down_x = ev.offsetX;
input.lmb_down_y = ev.offsetY;
if (input.entity_selected >= 0) {
input.lmb_down_transform.set(save.entities[input.entity_selected].transform);
}
}
};
window.onmousemove = ev => {
input.damped_event.button = ev.button;
input.damped_event.movementX = ev.movementX;
input.damped_event.movementY = ev.movementY;
input.mouse_x = ev.offsetX;
input.mouse_y = ev.offsetY;
};
window.oncontextmenu = ev => ev.preventDefault();
window.onmouseup = ev => {
ev.preventDefault();
if (ev.button == 0 && ev.shiftKey) {
input.rmb_down = false;
return;
}
if (ev.button == 2) input.rmb_down = false;
if (ev.button == 0) input.lmb_down = false;
/* record undo/redo for the action you just released */
if (ev.button == 0 && input.entity_selected >= 0) {
const tmp = mat4_create();
tmp.set(save.entities[input.entity_selected].transform);
save.entities[input.entity_selected].transform.set(input.lmb_down_transform);
action_apply(ACT_DO, {
kind: ACTION_TRANSFORM,
index: input.entity_selected,
transform: tmp
});
}
};
requestAnimationFrame(function frame(timestamp) {
requestAnimationFrame(frame);
/* we could apply these changes directly in the event handlers, but the result feels "low fps"
* because we don't get input events at 60fps. so instead, we apply the changes every frame
* and simply "damp them" so that they get weaker every frame.
*
* (this should probably use delta time rather than simply *= 0.8) */
{
const ev = input.damped_event;
if (input.captured_mouse == -1) {
/* based on the assumption that if you're zoomed in more,
* you're doing finer-detailed work and want more precise movements. */
const zoom_fudge = Math.sqrt(input.zoom/10.0)*2.0;
if (input.lmb_down) {
input.pitch -= ev.movementX * 0.0035 * zoom_fudge;
input.yaw -= ev.movementY * 0.0035 * zoom_fudge;
}
if (input.rmb_down) {
const unit = [0, -ev.movementX*0.0125*zoom_fudge, ev.movementY*0.0125*zoom_fudge, 1];
{
const view = mat4_create();
const scratch = mat4_create();
mat4_from_z_rotation(scratch, input.pitch);
mat4_mul(view, view, scratch);
mat4_from_y_rotation(scratch, input.yaw);
mat4_mul(view, view, scratch);
mat4_transform_vec4(unit, unit, view);
}
input.cam_pivot_x += unit[0];
input.cam_pivot_y += unit[1];
input.cam_pivot_z += unit[2];
}
if (!(input.lmb_down || input.rmb_down)) do {
/* instead of ray vs. box intersection, for the last selected, since their gizmos
* are being displayed/interacted with, we simply do a screenspace distance check
* (since the gizmos have constant screenspace size)
*
* UPDATE: not sure if this is necessary anymore, disabled for now */
if (input.entity_selected > -1, 0) {
const p = [0, 0, 0, 1];
mat4_transform_vec4(p, p, save.entities[input.entity_selected].transform);
mat4_transform_vec4(p, p, input.last_u_vp);
p[0] /= p[3];
p[1] /= p[3];
const normalized_mouse_x = -1 + (input.mouse_x / window.innerWidth )*2;
const normalized_mouse_y = +1 - (input.mouse_y / window.innerHeight)*2;
const dx = normalized_mouse_x - p[0];
const dy = normalized_mouse_y - p[1];
const dist = Math.sqrt(dx*dx + dy*dy);
/* unit here: fraction of the screen */
if (dist < 0.075) {
continue;
}
}
/* select new entity */
{
let ray_normal_x, ray_normal_y, ray_normal_z;
let ray_origin_x, ray_origin_y, ray_origin_z;
{
const eye = input.eye;
ray_origin_x = eye[0];
ray_origin_y = eye[1];
ray_origin_z = eye[2];
/* screen to world projection */
const vec = [input.mouse_x, input.mouse_y, 0, 1];
{
vec[0] = -1 + (vec[0] / window.innerWidth )*2;
vec[1] = +1 - (vec[1] / window.innerHeight)*2;
vec[2] = 1;
vec[3] = 1;
mat4_transform_vec4(vec, vec, input.last_u_vp_inv);
vec[0] /= vec[3];
vec[1] /= vec[3];
vec[2] /= vec[3];
}
ray_normal_x = vec[0] - eye[0];
ray_normal_y = vec[1] - eye[1];
ray_normal_z = vec[2] - eye[2];
/* normalize */
const nlen = Math.sqrt(ray_normal_x*ray_normal_x +
ray_normal_y*ray_normal_y +
ray_normal_z*ray_normal_z);
ray_normal_x /= nlen;
ray_normal_y /= nlen;
ray_normal_z /= nlen;
}
input.entity_hovered = -1;
let selected_tmin = Infinity;
for (let entity_i = 0; entity_i < save.entities.length; entity_i++) {
const b = {
min: [-1, -1, -1, 1],
max: [ 1, 1, 1, 1],
};
mat4_transform_vec4(b.min, b.min, save.entities[entity_i].transform);
mat4_transform_vec4(b.max, b.max, save.entities[entity_i].transform);
/* ray vs. box intersection */
let tmin = -Infinity, tmax = Infinity;
if (ray_normal_x != 0.0) {
const tx1 = (b.min[0] - ray_origin_x)/ray_normal_x;
const tx2 = (b.max[0] - ray_origin_x)/ray_normal_x;
tmin = Math.max(tmin, Math.min(tx1, tx2));
tmax = Math.min(tmax, Math.max(tx1, tx2));
}
if (ray_normal_y != 0.0) {
const tx1 = (b.min[1] - ray_origin_y)/ray_normal_y;
const tx2 = (b.max[1] - ray_origin_y)/ray_normal_y;
tmin = Math.max(tmin, Math.min(tx1, tx2));
tmax = Math.min(tmax, Math.max(tx1, tx2));
}
if (ray_normal_z != 0.0) {
const tx1 = (b.min[2] - ray_origin_z)/ray_normal_z;
const tx2 = (b.max[2] - ray_origin_z)/ray_normal_z;
tmin = Math.max(tmin, Math.min(tx1, tx2));
tmax = Math.min(tmax, Math.max(tx1, tx2));
}
if (tmax >= tmin && tmin < selected_tmin) {
selected_tmin = tmin;
input.entity_hovered = entity_i;
}
}
}
} while (false);
}
ev.button *= 0.8;
ev.movementX *= 0.8;
ev.movementY *= 0.8;
}
input.zoom += 0.025*input.scroll*input.zoom;
const ORTHO = false;
const u_vp = input.last_u_vp;
const u_vp_inv = input.last_u_vp_inv;
const u_view = mat4_create();
const scratch = mat4_create();
{
const model = save.transform;
if (ORTHO) {
const ar = window.innerWidth / window.innerHeight;
const projection = mat4_ortho(mat4_create(), -ar, ar, -1, 1, -2, 2);
mat4_mul(u_vp, projection, model);
} else {
const FIELD_OF_VIEW = 70 / 180 * Math.PI;
const ar = window.innerWidth / window.innerHeight;
const projection = mat4_perspective(mat4_create(), FIELD_OF_VIEW, ar, 0.01, 100.0);
{
input.eye[0] = input.zoom;
input.eye[1] = 0;
input.eye[2] = 0;
input.eye[3] = 1;
{
mat4_from_z_rotation(scratch, input.pitch);
mat4_mul(u_view, u_view, scratch);
mat4_from_y_rotation(scratch, input.yaw);
mat4_mul(u_view, u_view, scratch);
mat4_transform_vec4(input.eye, input.eye, u_view);
}
input.eye[0] += input.cam_pivot_x;
input.eye[1] += input.cam_pivot_y;
input.eye[2] += input.cam_pivot_z;
mat4_target_to(
u_view,
input.eye,
[input.cam_pivot_x, input.cam_pivot_y, input.cam_pivot_z],
[ 0, 0, 1]
);
mat4_invert(u_view, u_view);
}
mat4_mul(u_vp, projection, u_view);
mat4_invert(u_vp_inv, u_vp);
}
}
const geo_idx = [];
const geo_pos = [];
const geo_color = [];
{
const corners = [
-1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, /* Top face */
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, /* Bottom face */
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, /* Front face */
-1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, /* Back face */
1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, /* Right face */
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, /* Left face */
];
for (let corner_i = 0; corner_i < corners.length; corner_i += 12) {
geo_color.push(
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
255, 0, 255, 255
)
const vbuf_i = geo_pos.length / 3;
geo_pos.push(corners[corner_i + 0], corners[corner_i + 1], corners[corner_i + 2]);
geo_pos.push(corners[corner_i + 3], corners[corner_i + 4], corners[corner_i + 5]);
geo_pos.push(corners[corner_i + 6], corners[corner_i + 7], corners[corner_i + 8]);
geo_pos.push(corners[corner_i + 9], corners[corner_i + 10], corners[corner_i + 11]);
geo_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 3,
vbuf_i + 0
);
}
}
const grid_idx = [];
const grid_pos = [];
const grid_uv = [];
{
const corners = [
-999, -999, -0, -999, 999, -0, 999, 999, -0, 999, -999, -0, /* Bottom face */
];
for (let corner_i = 0; corner_i < corners.length; corner_i += 3)
corners[corner_i+2] -= 0.015;
for (let corner_i = 0; corner_i < corners.length; corner_i += 12) {
grid_uv.push(
255, 0,
0, 0,
0, 255,
255, 255,
);
const vbuf_i = grid_pos.length / 3;
grid_pos.push(corners[corner_i + 0], corners[corner_i + 1], corners[corner_i + 2]);
grid_pos.push(corners[corner_i + 3], corners[corner_i + 4], corners[corner_i + 5]);
grid_pos.push(corners[corner_i + 6], corners[corner_i + 7], corners[corner_i + 8]);
grid_pos.push(corners[corner_i + 9], corners[corner_i + 10], corners[corner_i + 11]);
grid_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 3,
vbuf_i + 0
);
}
}
const gizmo_idx = [];
const gizmo_pos = [];
const gizmo_color = [];
{
const gizmo_mvp = mat4_create();
if (input.entity_selected > -1) {
gizmo_mvp[12] = save.entities[input.entity_selected].transform[12];
gizmo_mvp[13] = save.entities[input.entity_selected].transform[13];
gizmo_mvp[14] = save.entities[input.entity_selected].transform[14];
}
/* flip gizmo as appropriate so it's lookin at you
* (think mona lisa, but more discretized rather than continuous) */
{
const face_user = mat4_from_scaling(
scratch,
Math.sign(input.eye[0] - gizmo_mvp[12]),
Math.sign(input.eye[1] - gizmo_mvp[13]),
Math.sign(input.eye[2] - gizmo_mvp[14])
);
mat4_mul(gizmo_mvp, gizmo_mvp, face_user);
}
mat4_mul(gizmo_mvp, u_vp, gizmo_mvp);
function line(a, b, a_thick, b_thick, color, z_push=0) {
const aspect_ratio = window.innerWidth / window.innerHeight;
mat4_transform_vec4(a, a, gizmo_mvp);
mat4_transform_vec4(b, b, gizmo_mvp);
const dx = b[0] - a[0];
const dy = b[1] - a[1];
const nx = -dy; /* perp */
const ny = dx*aspect_ratio;
const tlen = Math.sqrt(nx*nx + ny*ny);
if (tlen <= 0) return;
let a_tx = nx/tlen * a_thick / aspect_ratio;
let a_ty = ny/tlen * a_thick;
let b_tx = nx/tlen * b_thick / aspect_ratio;
let b_ty = ny/tlen * b_thick;
const vbuf_i = gizmo_pos.length / 4;
gizmo_pos.push(
a[0] + a_tx, a[1] + a_ty, a[2] + z_push, a[3],
a[0] - a_tx, a[1] - a_ty, a[2] + z_push, a[3],
b[0] + b_tx, b[1] + b_ty, b[2] + z_push, b[3],
b[0] - b_tx, b[1] - b_ty, b[2] + z_push, b[3]
);
gizmo_color.push(
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
color[0], color[1], color[2], color[3],
);
gizmo_idx.push(
vbuf_i + 0,
vbuf_i + 1,
vbuf_i + 2,
vbuf_i + 2,
vbuf_i + 1,
vbuf_i + 3
);
}
function dist_to_center(a, b) {
const center = [
(a[0] + b[0]) * 0.5,
(a[1] + b[1]) * 0.5,
(a[2] + b[2]) * 0.5,
1
];
mat4_transform_vec4(center, center, gizmo_mvp);
const w = center[3];
center[0] = (0 + (center[0]/w*0.5 + 0.5))*window.innerWidth;
center[1] = (1 - (center[1]/w*0.5 + 0.5))*window.innerHeight;
const dist = Math.sqrt((center[0] - input.mouse_x)*(center[0] - input.mouse_x) +
(center[1] - input.mouse_y)*(center[1] - input.mouse_y) );
return dist;
}
{
function eye_to_mouse_world_dir(vec) {
vec[0] = -1 + (vec[0] / window.innerWidth )*2;
vec[1] = +1 - (vec[1] / window.innerHeight)*2;
vec[2] = 1;
vec[3] = 1;
mat4_transform_vec4(vec, vec, u_vp_inv);
vec[0] /= vec[3];
vec[1] /= vec[3];
vec[2] /= vec[3];
const eye = input.eye;
const x = vec[0] - eye[0];
const y = vec[1] - eye[1];
const z = vec[2] - eye[2];
/* normalize */
const nlen = Math.sqrt(vec[0]*vec[0] +
vec[1]*vec[1] +
vec[2]*vec[2]);
vec[0] /= nlen;
vec[1] /= nlen;
vec[2] /= nlen;
return vec;
}
function screen_to_plane(plane_vector_x, plane_vector_y, plane_vector_z, vec) {
vec[0] = -1 + (vec[0] / window.innerWidth )*2;
vec[1] = +1 - (vec[1] / window.innerHeight)*2;
vec[2] = 1;
vec[3] = 1;
mat4_transform_vec4(vec, vec, u_vp_inv);
vec[0] /= vec[3];
vec[1] /= vec[3];
vec[2] /= vec[3];
const eye = input.eye;
ray_hit_plane(
vec,
eye[0], eye[1], eye[2],
vec[0] - eye[0], vec[1] - eye[1], vec[2] - eye[2],
input.lmb_down_transform[12], input.lmb_down_transform[13], input.lmb_down_transform[14],
plane_vector_x, plane_vector_y, plane_vector_z,
);
return vec;
}
function gizmo(line, doInput) {
doInput &&= input.lmb_down;
const ev = input.damped_event;
const LINE_BASE = 1.75;
const ARROW_BASE = 2.2;
const ARROW_TIP = 2.4;
const SCALE_P = 1.99;
let gizmo_i = 1;
/* lines */
{
line([LINE_BASE, 0, 0, 1], [ARROW_BASE, 0, 0, 1], 0.04, 0.04, RED, null);
line([ 0, LINE_BASE, 0, 1], [ 0, ARROW_BASE, 0, 1], 0.04, 0.04, GREEN, null);
line([ 0, 0, LINE_BASE, 1], [ 0, 0, ARROW_BASE, 1], 0.04, 0.04, BLUE, null);
}
const tip = [0, 0, 0, 1];
const base = [0, 0, 0, 1];
const colors = [RED, GREEN, BLUE];
/* translation arrows */
{
for (let i = 0; i < colors.length; i++) {
tip[0] = base[0] = 0;
tip[1] = base[1] = 0;
tip[2] = base[2] = 0;
tip[3] = base[3] = 1;
tip[i] = ARROW_TIP;
base[i] = ARROW_BASE;
line(tip, base, 0.00, 0.10, colors[i], gizmo_i);
if (doInput && input.captured_mouse == gizmo_i) {
let plane_x = +(i == 2);
let plane_y = +(i == 2);
let plane_z = +(i != 2);
const current = screen_to_plane(plane_x, plane_y, plane_z, [input. mouse_x, input. mouse_y, 1, 1]);
const lmb_down = screen_to_plane(plane_x, plane_y, plane_z, [input.lmb_down_x, input.lmb_down_y, 1, 1]);
const x = (i == 0) ? (current[0] - lmb_down[0]) : 0;
const y = (i == 1) ? (current[1] - lmb_down[1]) : 0;
const z = (i == 2) ? (current[2] - lmb_down[2]) : 0;
mat4_from_translation(scratch, x, y, z);
mat4_mul(
save.entities[input.entity_selected].transform,
scratch,
input.lmb_down_transform
);
}
gizmo_i++;
}
}
/* scale boxes */
{
const S = 0.075;
for (let i = 0; i < colors.length; i++) {
tip[0] = base[0] = 0;
tip[1] = base[1] = 0;
tip[2] = base[2] = 0;
tip[3] = base[3] = 1;
tip[i] = SCALE_P - S;
base[i] = SCALE_P + S;
line(tip, base, 0.10, 0.10, colors[i], gizmo_i);
if (doInput && input.captured_mouse == gizmo_i) {
}
if (doInput && input.captured_mouse == gizmo_i) {
/* there are three possible scale vectors; find the one visually closest
* to the scale box they're pulling right now.
*
* this still works if you don't shadow and override `i`, but the user experience
* is ever so slightly worse. the box you're pulling doesn't always visually
* match up with the resulting scale.
* */
let diverted_axis_i;
{
let axis_best_dot = 0;
const world_i = i;
const grabby_vec = [
+(world_i == 0),
+(world_i == 1),
+(world_i == 2)
];
for (let axis_i = 0; axis_i < 3; axis_i++) {
/* the last 0 is important here; you definitely don't want to apply translation. */
const a = [0, 0, 0, 0];
a[axis_i] = 1;
mat4_transform_vec4(a, a, input.lmb_down_transform);
const a_len = Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
a[0] /= a_len;
a[1] /= a_len;
a[2] /= a_len;
let dot = a[0]*grabby_vec[0] + a[1]*grabby_vec[1] + a[2]*grabby_vec[2];
if (Math.abs(axis_best_dot) < Math.abs(dot)) {
diverted_axis_i = axis_i;
axis_best_dot = dot;
}
}
}
let u;
{
/* base is different now, so I reset both */
{
tip[0] = base[0] = 0;
tip[1] = base[1] = 0;
tip[2] = base[2] = 0;
tip[3] = base[3] = 1;
tip[diverted_axis_i] = SCALE_P + S;
/* correct for perspective projection. */
mat4_transform_vec4(tip , tip , gizmo_mvp);
mat4_transform_vec4(base, base, gizmo_mvp);
tip[0] /= tip[3];
tip[1] /= tip[3];
tip[2] /= tip[3];
base[0] /= base[3];
base[1] /= base[3];
base[2] /= base[3];
}
/* gotta get mouse in the same space */
const mouse_x = -1 + (input.mouse_x / window.innerWidth )*2;
const mouse_y = +1 - (input.mouse_y / window.innerHeight)*2;
const m2t_x = mouse_x - tip[0];
const m2t_y = mouse_y - tip[1];
const b2t_x = base[0] - tip[0];
const b2t_y = base[1] - tip[1];
const dot = m2t_x*b2t_x + m2t_y*b2t_y;