-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1539 lines (1248 loc) · 46.5 KB
/
main.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
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
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { TransformControls } from 'three/addons/controls/TransformControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
//EXTRA: for smooth lasso drawing we can draw default buffer to a saved render target, and draw lasso on top of that pre rendererd background (although this will stop us from being able to highlight objects on the fly)
//
const debug = false;
const eSelectMode = {
SELECT: 1,
LASSO: 2
};
const eEditMode = {
OBJECT: 1,
VERTEX: 2
};
const eSnapMode = {
FACE: 1,
VERTEX: 2,
};
const eState = {
SELECT: 0,
SNAP: 1,
JOIN_SELECT_VERTEX1: 2,
JOIN_SELECT_VERTEX2: 3
};
let should_update_render = false;
function node_destroy(node)
{
if (node.geometry !== undefined) {
// check if geometry is referenced by other nodes to skip deleting
let geo_is_refd = false;
editor.scene.traverse((obj) => {
if (obj !== node && obj.geometry === node.geometry) {
geo_is_refd = true;
}
});
if (!geo_is_refd) {
node.geometry.dispose();
}
}
node.parent.remove(node);
}
function node_create_verts_drawing(node)
{
node_destroy_verts_drawing(node);
const verts_geo = new THREE.BufferGeometry();
// clone vertices from original geo
const verts_pos_attr = node.geometry.getAttribute('position').clone();
verts_geo.setAttribute('position', verts_pos_attr);
const colors_f32 = new Float32Array(verts_pos_attr.count * 3);
const color_attr = new THREE.BufferAttribute(colors_f32, 3, false);
verts_geo.setAttribute('color', color_attr);
for (let vi = 0; vi < color_attr.count; ++vi) {
color_attr.setXYZ(vi, 1.0,1.0,1.0);
}
node.verts_drawing = new THREE.Points(verts_geo, editor.mat_points);
// NOTE: remove points from raycast
node.verts_drawing.raycast = () => {};
// draw on top
node.verts_drawing.renderOrder = 1;
node.add(node.verts_drawing);
}
function node_destroy_verts_drawing(node)
{
if (node.verts_drawing === undefined) {
return;
}
node_destroy(node.verts_drawing);
node.verts_drawing = undefined;
}
function highlight_verts(node, indices)
{
if (node.verts_drawing === undefined) {
return;
}
const vcolor_attr = node.verts_drawing.geometry.getAttribute('color');
for (let ii = 0; ii < indices.length; ++ii) {
const vi = indices[ii];
vcolor_attr.setXYZ(vi, 1.0,1.0,0.0);
}
vcolor_attr.needsUpdate = true;
}
function start_perf_ms()
{
return Date.now();
}
function end_perf_ms(start)
{
return Date.now() - start;
}
class Editor {
constructor() {
this.obj_count = 0;
this.objects = [];
this.select_mode = eSelectMode.SELECT;
this.edit_mode = eEditMode.OBJECT;
this.snap_mode = eSnapMode.FACE;
this.should_snap_align_rotation = false;
this.selected_nodes = [];
this.mat_selected = new THREE.MeshBasicMaterial({
color: 0x505050,
wireframe: true });
this.scene = undefined;
this.state = eState.SELECT;
this.og_quat_before_snap = undefined;
this.mat_points = new THREE.PointsMaterial({
size:0.01,
color: 0xFFFFFF,
vertexColors:true,
depthTest:false});
}
count_selected_verts() {
let count = 0;
for (let i = 0; i < this.selected_nodes.length; ++i) {
const node = this.selected_nodes[i];
if (node.selected_indices === undefined) {
continue;
}
count += node.selected_indices.length;
}
return count;
}
clear_selected() {
g.tform_gizmo.detach();
for (const node of this.selected_nodes) {
node_destroy_verts_drawing(node);
node.is_selected = false;
node.material = node.og_mat;
node.selected_indices = undefined;
}
this.selected_nodes.length = 0;
this.edit_mode = eEditMode.OBJECT;
}
switch_to_object_mode() {
if (this.edit_mode === eEditMode.VERTEX) {
for (const node of this.selected_nodes) {
node_destroy_verts_drawing(node);
}
}
this.edit_mode = eEditMode.OBJECT;
if (this.selected_nodes !== undefined
&& this.selected_nodes.length === 1) {
g.tform_gizmo.attach(this.selected_nodes[0]);
}
}
select_node(node) {
if (node.is_selected) {
return;
}
node.is_selected = true;
node.og_mat = node.material;
node.material = this.mat_selected;
this.selected_nodes.push(node);
if (this.selected_nodes.length == 1) {
g.tform_gizmo.attach(node);
} else {
g.tform_gizmo.detach();
}
}
mouse_pick_node() {
if (this.scene === undefined) {
return [];
}
const recursive = true;
const results = raycaster.intersectObjects(
this.scene.children, recursive);
return results;
}
merge_selected_verts() {
console.assert(this.selected_nodes !== undefined);
console.assert(this.selected_nodes.length === 1);
const node = this.selected_nodes[0];
console.assert(node.selected_indices.length >= 2);
const geo = node.geometry;
const idx0 = node.selected_indices[0];
const idx1 = node.selected_indices[1];
if (geo.index !== undefined) {
// update old indices pointing at idx0 to now point to idx1
const array = geo.index.array;
for (let i = 0; i < array.length; ++i) {
if (array[i] == idx0) {
array[i] = idx1;
}
}
geo.index.needsUpdate = true;
} else {
//TODO: need test case for non-indexed triangles
}
node_destroy_verts_drawing(node);
node.selected_indices = undefined;
}
begin_merge_vertex_mode() {
if (this.selected_nodes.length !== 1) {
console.warn('Must have object selected to merge vertex');
this.edit_mode = eEditMode.OBJECT;
return;
}
this.state = eState.JOIN_SELECT_VERTEX1;
const node = this.selected_nodes[0];
node.selected_indices = undefined;
node_create_verts_drawing(node);
g.tform_gizmo.detach();
g.cam_controls.enabled = false;
}
end_merge_vertex_mode() {
this.state = eState.SELECT;
const node = this.selected_nodes[0];
g.tform_gizmo.attach(node);
g.cam_controls.enabled = true;
}
setup_vertex_mode() {
if (this.selected_nodes.length <= 0) {
console.warn('Must have object selected to edit vertices');
this.edit_mode = eEditMode.OBJECT;
return;
}
this.edit_mode = eEditMode.VERTEX;
const node = this.selected_nodes[0];
node_create_verts_drawing(node);
g.tform_gizmo.detach();
on_render();
}
setup_lasso_mode() {
this.state = eState.SELECT;
this.select_mode = eSelectMode.LASSO;
g.cam_controls.enabled = false;
g.tform_gizmo.detach();
}
setup_select_mode() {
this.state = eState.SELECT;
this.select_mode = eSelectMode.SELECT;
g.cam_controls.enabled = true;
}
begin_snap() {
//NOTE: only allow snapping 1 node/group
if (this.selected_nodes.length != 1) {
this.state = eState.SELECT;
return;
}
this.state = eState.SNAP;
const node = this.selected_nodes[0];
g.tform_gizmo.detach();
this.og_quat_before_snap = new THREE.Quaternion();
node.getWorldQuaternion(this.og_quat_before_snap);
}
end_snap() {
this.state = eState.SELECT;
const node = this.selected_nodes[0];
g.tform_gizmo.attach(node);
this.og_quat_before_snap = undefined;
}
process_lasso_region() {
if (lasso.point_count >= 3 && this.scene !== undefined) {
if (this.state === eState.JOIN_SELECT_VERTEX1
|| this.state === eState.JOIN_SELECT_VERTEX2) {
const node = this.selected_nodes[0];
//NOTE: only grab first index we find
const hl_indices = lasso_vs_node(node, true);
if (node.selected_indices === undefined) {
node.selected_indices = hl_indices;
} else {
node.selected_indices = node.selected_indices.concat(hl_indices);
}
highlight_verts(node, node.selected_indices);
} else {
if (this.edit_mode === eEditMode.OBJECT) {
// lasso vs editor objects
if (debug) {
dlines.clear();
dlines.add_box2(lasso.bbox2_vp, new THREE.Color().setHex(0xFF00FF));
}
lasso_vs_nodes(this.scene.children);
} else {
// lasso vs selected nodes verts
const node = this.selected_nodes[0];
node.selected_indices = lasso_vs_node(node, false);
highlight_verts(node, node.selected_indices);
}
}
}
}
on_pointerdown() {
if (this.state == eState.SNAP) {
// nothing
} else if (this.state === eState.SELECT) {
if (this.select_mode===eSelectMode.LASSO) {
g.cam_controls.enabled = false;
g.tform_gizmo.detach();
lasso.begin_new_selection();
}
} else if (this.state === eState.JOIN_SELECT_VERTEX1
|| this.state === eState.JOIN_SELECT_VERTEX2) {
lasso.begin_new_selection();
}
}
on_pointerup() {
if (this.state == eState.SNAP) {
this.end_snap();
} else if (this.state === eState.SELECT) {
if (this.select_mode === eSelectMode.LASSO) {
this.process_lasso_region();
lasso.end_selection();
this.setup_select_mode();
} else {
// click to select object
if (this.edit_mode == eEditMode.OBJECT) {
const mouse_ndc = vec2_vp_to_ndc(input.mouse);
raycaster.setFromCamera(mouse_ndc, g.cam);
const results = this.mouse_pick_node();
if (results.length > 0) {
const obj = results[0].object;
this.select_node(obj);
}
}
}
} else if (this.state === eState.JOIN_SELECT_VERTEX1
|| this.state === eState.JOIN_SELECT_VERTEX2) {
this.process_lasso_region();
lasso.end_selection();
const node = this.selected_nodes[0];
if (this.state === eState.JOIN_SELECT_VERTEX2) {
if (node.selected_indices.length === 2) {
this.merge_selected_verts();
this.end_merge_vertex_mode();
}
} else {
if (node.selected_indices !== undefined
&& node.selected_indices.length === 1) {
this.state = eState.JOIN_SELECT_VERTEX2;
}
}
}
}
on_pointermove() {
if (this.scene === undefined) {
return;
}
if (this.state == eState.SNAP) {
const node = this.selected_nodes[0];
const recursive = true;
const results = raycaster.intersectObjects(
this.scene.children, recursive);
for (let ri = 0; ri < results.length; ++ri) {
const hit = results[ri];
//NOTE: don't snap node to itself
if (hit.object !== node) {
// set new position of node based off faces or nearest vertex
//
if (this.snap_mode == eSnapMode.FACE) {
//NOTE: hit.point is in world space, convert to local
const new_pos = hit.point.clone();
// position relative to parent
node.parent.worldToLocal(new_pos);
node.position.copy(new_pos);
} else {
//find closest vertex on face to hit point to snap to
let new_dist = Number.MAX_VALUE;
const new_pos = new THREE.Vector3();
const indices = [hit.face.a, hit.face.b, hit.face.c];
for (let ii = 0; ii < indices.length; ++ii) {
const pos = new THREE.Vector3().fromBufferAttribute(
hit.object.geometry.attributes.position, indices[ii]);
// get vertex pos in world space
hit.object.localToWorld(pos);
const dist = pos.distanceToSquared(hit.point);
if (dist < new_dist) {
new_dist = dist;
new_pos.copy(pos);
}
}
//NOTE: hit.point is in world space
// convert to local since position is relative to parent
node.parent.worldToLocal(new_pos);
node.position.copy(new_pos);
}
// align rotation of node to match target
if (editor.should_snap_align_rotation) {
//NOTE: hit normal is in hit.object local space, convert to worldspace
const hit_normal_ws = hit.face.normal.clone();
hit_normal_ws.transformDirection(hit.object.matrixWorld);
const node_up = new THREE.Vector3(0,1,0);
// rotate to our new normal direction
const quat_to_new_up = new THREE.Quaternion().setFromUnitVectors(
node_up, hit_normal_ws);
node.quaternion.multiplyQuaternions(quat_to_new_up, this.og_quat_before_snap);
node.quaternion.normalize();
}
should_update_render = true;
break;
}
}
} else if (this.state === eState.SELECT) {
if (this.select_mode === eSelectMode.LASSO) {
if (lasso.is_drawing && lasso.add_point()) {
should_update_render = true;
}
}
} else if (this.state === eState.JOIN_SELECT_VERTEX1
|| this.state === eState.JOIN_SELECT_VERTEX2) {
if (lasso.is_drawing && lasso.add_point()) {
should_update_render = true;
}
}
}
split_selected_node() {
if (this.selected_nodes.length !== 1) {
console.warn('Splitting requires 1 object to be selected');
return;
}
const node = this.selected_nodes[0];
split_node_geo(node);
}
join_selected_nodes() {
if (this.selected_nodes.length !== 2) {
console.warn('You must have 2 nodes selected in order to join');
return;
}
const node0 = this.selected_nodes[0];
const node1 = this.selected_nodes[1];
//XXX: this join code currently assumes both meshes have a single material (no groups)
// NOTE: meshes could have different parents and not be part of same transform hierarchy, so we will bring geometry into world space and add new merged node at root level
// NOTE: create clones of geo to modify in case other nodes reference original geo
const node0_geo = node0.geometry.clone();
node0_geo.applyMatrix4(node0.matrixWorld);
const node1_geo = node1.geometry.clone();
node1_geo.applyMatrix4(node1.matrixWorld);
const geo_merged = BufferGeometryUtils.mergeGeometries(
[node0_geo, node1_geo],
true);
node0_geo.dispose();
node1_geo.dispose();
const node_merged = new THREE.Mesh(
geo_merged,
[node0.og_mat, node1.og_mat]);
editor.scene.add(node_merged);
//NOTE: clear selected before destroying
this.clear_selected();
node_destroy(node0);
node_destroy(node1);
}
};
function node_create_new_geometry_with_indices(geo, indices)
{
const unique_indices = Array.from(new Set(indices));
const new_vert_count = unique_indices.length;
const indices_map = new Map();
const new_geo = new THREE.BufferGeometry();
// recreate buffer attributes for position/normal so they only contain our new set of indices
const new_pos_array = new Float32Array(new_vert_count * 3);
const og_pos_array = geo.getAttribute('position').array;
for (let i = 0; i < unique_indices.length; ++i) {
const old_idx = unique_indices[i];
const new_idx = i;
indices_map.set(old_idx, new_idx);
new_pos_array[new_idx*3] = og_pos_array[old_idx*3];
new_pos_array[new_idx*3 + 1] = og_pos_array[old_idx*3 + 1];
new_pos_array[new_idx*3 + 2] = og_pos_array[old_idx*3 + 2];
}
const new_pos_attr = new THREE.BufferAttribute(new_pos_array, 3);
new_geo.setAttribute('position', new_pos_attr);
if (geo.attributes.normal !== undefined) {
const new_normal_arr = new Float32Array(new_vert_count * 3);
const og_normal_arr = geo.attributes.normal.array;
for (let i = 0; i < unique_indices.length; ++i) {
const old_idx = unique_indices[i];
const new_idx = i;
new_normal_arr[new_idx*3] = og_normal_arr[old_idx*3];
new_normal_arr[new_idx*3 + 1] = og_normal_arr[old_idx*3 + 1];
new_normal_arr[new_idx*3 + 2] = og_normal_arr[old_idx*3 + 2];
}
const new_normal_attr = new THREE.BufferAttribute(new_normal_arr, 3);
new_geo.setAttribute('normal', new_normal_attr);
}
if (geo.attributes.uv !== undefined) {
const new_uv_arr = new Float32Array(new_vert_count * 2);
const og_uv_arr = geo.attributes.normal.array;
for (let i = 0; i < unique_indices.length; ++i) {
const old_idx = unique_indices[i];
const new_idx = i;
new_uv_arr[new_idx*2] = og_uv_arr[old_idx*2];
new_uv_arr[new_idx*2 + 1] = og_uv_arr[old_idx*2 + 1];
}
const new_uv_attr = new THREE.BufferAttribute(new_uv_arr, 2);
new_geo.setAttribute('uv', new_uv_attr);
}
const new_indices = new Uint16Array(indices);
// update old indices to point to our new arrangement of indices
for (let i = 0; i < new_indices.length; ++i) {
new_indices[i] = indices_map.get(new_indices[i]);
}
// create new index buffer
new_geo.setIndex(new THREE.BufferAttribute(new_indices, 1));
// update bounds for geometry (three.js does not auto calculate)
new_geo.computeBoundingBox();
new_geo.computeBoundingSphere();
return new_geo;
}
function split_node_geo(node)
{
if (node.selected_indices === undefined || node.selected_indices.length < 3) {
console.warn('Node must have vertices selected in order to split');
return;
}
const geo = node.geometry;
const new_tris = [];
const old_tris = [];
// NOTE: create a set here for faster lookups instead of using array.includes()
const indices_set = new Set(node.selected_indices);
// indexed triangles
if (geo.index !== undefined) {
const array = geo.index.array;
// categorize triangles based on whether their vertices are selected or not
for (let i = 0; (i+2) < array.length; i+=3) {
const a_idx = array[i];
const b_idx = array[i+1];
const c_idx = array[i+2];
if (indices_set.has(a_idx)
&& indices_set.has(b_idx)
&& indices_set.has(c_idx)) {
new_tris.push(a_idx, b_idx, c_idx);
} else {
old_tris.push(a_idx, b_idx, c_idx);
}
}
// create new geometries based off our new indices
const geo_split_new = node_create_new_geometry_with_indices(geo, new_tris);
const geo_split_old = node_create_new_geometry_with_indices(geo, old_tris);
// free original geometry
geo.dispose();
node.geometry = geo_split_old;
node_destroy_verts_drawing(node);
node.selected_indices = undefined;
const new_node = node.clone();
new_node.geometry = geo_split_new;
new_node.material = node.og_mat;
node.parent.add(new_node);
} else {
// non-indexed triangles
// TODO: need a test case here...
}
}
function add_on_click(el, f)
{
el.addEventListener('click', f);
}
function toggle_selected(el, cond)
{
if(cond) {
el.classList.add('selected');
} else {
el.classList.remove('selected');
}
}
function toggle_visible(el, cond)
{
if (cond) {
el.style.display = 'block';
} else {
el.style.display = 'none';
}
}
function toggle_enabled(el, cond)
{
if (cond) {
el.classList.remove('disabled');
for (let i = 0; i < el.children.length; ++i) {
const child = el.children[i];
child.disabled = false;
}
} else {
el.classList.add('disabled');
for (let i = 0; i < el.children.length; ++i) {
const child = el.children[i];
child.disabled = true;
}
}
}
class Ui {
constructor() {
const els = document.querySelectorAll('#controls button[id], #controls div[id], #controls input[id]');
for (let i = 0; i < els.length; ++i) {
const el = els[i];
this[el.id] = el;
}
add_on_click(this.btn_load, () => {
load_model();
this.btn_load.remove();
});
add_on_click(this.btn_select, () => {
editor.setup_select_mode();
this.update();
});
add_on_click(this.btn_lasso, () => {
editor.setup_lasso_mode();
this.update();
});
add_on_click(this.btn_vertex, () => {
editor.setup_vertex_mode();
this.update();
});
add_on_click(this.btn_object, () => {
editor.switch_to_object_mode();
this.update();
on_render();
});
add_on_click(this.btn_snap_mode_face, () => {
editor.snap_mode = eSnapMode.FACE;
this.update();
});
add_on_click(this.btn_snap_mode_vertex, () => {
editor.snap_mode = eSnapMode.VERTEX;
this.update();
});
add_on_click(this.btn_clear, () => {
editor.clear_selected();
this.update();
on_render();
});
this.chk_snap_align_rotation.addEventListener('change', () => {
editor.should_snap_align_rotation = this.chk_snap_align_rotation.checked;
});
add_on_click(this.btn_snap_to, () => {
editor.begin_snap();
this.update();
on_render();
});
add_on_click(this.btn_split, () => {
editor.split_selected_node();
editor.clear_selected();
on_render();
});
add_on_click(this.btn_join, () => {
editor.join_selected_nodes();
on_render();
});
add_on_click(this.btn_merge_vertex, () => {
editor.begin_merge_vertex_mode();
this.update();
on_render();
});
this.lbl_instruct = document.getElementById('lbl_instruct');
this.update();
}
update() {
toggle_selected(this.btn_select,
editor.state === eState.SELECT
&& editor.select_mode === eSelectMode.SELECT);
toggle_selected(this.btn_lasso,
editor.state === eState.SELECT
&& editor.select_mode === eSelectMode.LASSO);
toggle_selected(this.btn_object,
editor.edit_mode == eEditMode.OBJECT);
toggle_selected(this.btn_vertex,
editor.edit_mode == eEditMode.VERTEX);
toggle_selected(this.btn_snap_to,
editor.state === eState.SNAP);
toggle_selected(this.btn_snap_mode_face,
editor.snap_mode == eSnapMode.FACE);
toggle_selected(this.btn_snap_mode_vertex,
editor.snap_mode == eSnapMode.VERTEX);
this.lbl_selected_nodes.innerText = 'Selected Nodes: ' + editor.selected_nodes.length;
this.lbl_selected_verts.innerText = 'Selected Verts: ' + editor.count_selected_verts();
toggle_visible(this.btn_clear,
editor.selected_nodes.length > 0);
toggle_visible(this.panel_snap,
editor.selected_nodes.length === 1);
this.chk_snap_align_rotation.checked = editor.should_snap_align_rotation;
toggle_visible(this.btn_split,
editor.selected_nodes.length > 0
&& editor.count_selected_verts() > 0);
toggle_visible(this.btn_join,
editor.selected_nodes.length === 2);
toggle_visible(this.btn_merge_vertex,
editor.selected_nodes.length === 1);
toggle_visible(this.lbl_instruct,
editor.state === eState.JOIN_SELECT_VERTEX1
|| editor.state === eState.JOIN_SELECT_VERTEX2);
if (editor.state === eState.JOIN_SELECT_VERTEX1) {
this.lbl_instruct.innerText = 'Select First Vertex';
} else if (editor.state === eState.JOIN_SELECT_VERTEX2) {
this.lbl_instruct.innerText = 'Select Second Vertex';
}
}
};
function load_model()
{
const loading_panel = document.getElementById('panel_loading');
const loading_fill = document.getElementById('loading-bar-fill');
const loading_text = document.getElementById('loading-text');
loading_panel.style.display = 'block';
gltf_loader.load(
//'./synode_202_assets_others_3d_General_Final_119.glb',
'./Ex3_snapping.glb',
(gltf) => {
loading_panel.style.display = 'none';
editor.scene = gltf.scene;
g.scene.add(editor.scene);
editor.obj_count = 0;
if (debug) {
// display bounding boxes for objects
for (const obj of gltf.scene.children) {
obj.bbox_draw = new THREE.BoxHelper(obj, 0xFF0000);
g.scene.add(obj.bbox_draw);
}
}
on_render();
},
(xhr) => {
const pct = Math.round((xhr.loaded / xhr.total) * 100);
loading_fill.style.width = pct + '%';
loading_text.textContent = 'Loading gltf model... Bytes Loaded:' +xhr.loaded;// pct + '%';
}
);
}
class Input {
constructor() {
this.mouse = new THREE.Vector2();
this.is_pointer_down = false;
// flags if orbit controls were used during pointer events, to skip our logic
this.used_cam_controls = false;
}
};
function app_on_pointerdown(ev) {
input.used_cam_controls = false;
if (ev.pointerType === 'mouse' && ev.button !== 0) {
// ignore clicks other than left
return;
}
input.is_pointer_down = true;
input.mouse.x = ev.clientX;
input.mouse.y = ev.clientY;
editor.on_pointerdown();
on_render();
}
function app_on_pointerup(ev) {
if (ev.pointerType === 'mouse' && ev.button !== 0) {
// ignore clicks other than left
return;
}
if (g.tform_gizmo.visible && g.tform_gizmo.dragging) {
return;
}
// skip executing our events if camera was used (not a click)
if (input.used_cam_controls) {
input.used_cam_controls = false;
return;
}
// We had a full click without using transform gizmo and without dragging camera
editor.on_pointerup();
on_render();
}
function app_on_pointermove(ev) {
input.mouse.x = ev.clientX;
input.mouse.y = ev.clientY;
const mouse_ndc = vec2_vp_to_ndc(input.mouse);
raycaster.setFromCamera(mouse_ndc, g.cam);
editor.on_pointermove();
ui.update();
if (should_update_render) {
should_update_render = false;
on_render();
}
}
function gpu_get_res()
{
gpu.getSize(gpu.res);
return gpu.res;
}
function update_gpu_backbuffer()
{
const new_width = gpu.domElement.clientWidth;
const new_height = gpu.domElement.clientHeight;
const should_resize = gpu.domElement.width !== new_width
|| gpu.domElement.height !== new_height;
if (should_resize) {
gpu.setSize(new_width, new_height, false);
}
}
function update_cameras()
{
const res = gpu_get_res();
g.cam.aspect = res.x / res.y;
g.cam.updateProjectionMatrix();
//centered ortho
/*cam_ui.left = -res.x * 0.5;
cam_ui.right = res.x * 0.5;
cam_ui.top = res.y * 0.5;
cam_ui.bottom = -res.y * 0.5;*/
// top left aligned ortho
cam_ui.left = 0;
cam_ui.right = res.x;
cam_ui.top = 0;
cam_ui.bottom = res.y;
cam_ui.near = 0.1;
cam_ui.far = 10;
cam_ui.updateProjectionMatrix();
}
function setup_lui()
{
const panel_debug_info = gui.addFolder('Debug Info');
panel_debug_info.add(input.mouse, 'x').listen().name('Mouse X').disable();
panel_debug_info.add(input.mouse, 'y').listen().name('Mouse Y').disable();
const panel_debug_lasso = panel_debug_info.addFolder('Lasso');
panel_debug_lasso.add(lasso, 'point_count').listen().disable();
const panel_render_info = panel_debug_info.addFolder('Render');
panel_render_info.add(editor, 'obj_count').listen().disable();
panel_render_info.add(gpu.info.memory, 'geometries').name('Geometry Count').listen().disable();
panel_render_info.add(gpu.info.programs, 'length').name('Program Count').listen().disable();
panel_render_info.add(gpu.info.render, 'calls').name('Draw Calls').listen().disable();
panel_render_info.add(gpu.info.render, 'frame').name('Frame').listen().disable();
panel_render_info.add(gpu.info.render, 'lines').name('Line Count').listen().disable();
panel_render_info.add(gpu.info.render, 'points').name('Point Count').listen().disable();
panel_render_info.add(gpu.info.render, 'triangles').name('Triangle Count').listen().disable();