-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathx-modeleditor.js
2296 lines (1834 loc) · 51.2 KB
/
x-modeleditor.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
/*
3D model editor widget.
Written by Cosmin Apreutesei. Public domain.
*/
TRACE = 0
LOG = 1
DEBUG = 0
component('x-modeleditor', function(e) {
let pe = e
function log(s, ...args) {
assert(LOG)
print(s, ...args)
}
// colors ------------------------------------------------------------------
let white = 0xffffff
let black = 0x000000
let selected_color = 0x0000ff
let ref_color = 0xff00ff
let x_axis_color = 0x990000
let y_axis_color = 0x000099
let z_axis_color = 0x006600
// canvas & webgl context -------------------------------------------------
focusable_widget(e)
selectable_widget(e)
let canvas = tag('canvas')
e.add(canvas)
let gl = assert(canvas.getContext('webgl2'))
//gl.wrap_calls()
// camera -----------------------------------------------------------------
let camera = camera3()
let min_distance = 0.001 // min line distance
let max_distance = 1e4 // max model total distance
function update_camera_proj() {
camera.view_size.set(floor(canvas.cw), floor(canvas.ch))
if (e.projection == 'ortho') {
camera.ortho(-10, 10, -10, 10, -1e2, 1e2)
} else {
camera.fov = e.fov
camera.near = min_distance * 100
camera.far = max_distance * 100
camera.perspective()
}
update_camera()
}
e.set_ortho = function(v) { update_camera_proj() }
e.set_fov = function(v) { update_camera_proj() }
e.set_camera_pos = function(v) { camera.pos.set(v); update_camera() }
e.set_camera_dir = function(v) { camera.dir.set(v); update_camera() }
e.set_camera_up = function(v) { camera .up.set(v); update_camera() }
e.prop('projection' , {store: 'var', type: 'enum' , enum_values: ['perspective', 'ortho'], default: 'perspective'})
e.prop('fov' , {store: 'var', type: 'number', default: 60})
e.prop('camera_pos' , {store: 'var', type: 'v3', default: camera.pos})
e.prop('camera_dir' , {store: 'var', type: 'v3', default: camera.dir})
e.prop('camera_up' , {store: 'var', type: 'v3', default: camera.up })
function update_camera() {
camera.update()
skybox.update_view(camera.pos)
update_helper_points()
update_sunlight_pos()
update_renderer()
mouse.valid = false
fire_pointermove()
}
// shadows ----------------------------------------------------------------
e.set_shadows = function(v) { renderer.enable_shadows = v; update_renderer() }
e.prop('shadows', {store: 'var', type: 'boolean', default: false})
// sun position -----------------------------------------------------------
e.set_sunlight = function(v) { update_sunlight_pos(); update_renderer() }
e.set_time = function(v) { update_sun_pos(); update_renderer() }
e.set_north = function(v) { update_sun_pos(); update_renderer() }
e.set_latitude = function(v) { update_sun_pos(); update_renderer() }
e.set_longitude = function(v) { update_sun_pos(); update_renderer() }
e.prop('sunlight' , {store: 'var', type: 'boolean', default: false})
e.prop('time' , {store: 'var', type: 'datetime', default: 1596272400})
e.prop('north' , {store: 'var', type: 'number', default: 0})
e.prop('latitude' , {store: 'var', type: 'number', default: 44.42314, decimals: null})
e.prop('longitude' , {store: 'var', type: 'number', default: 26.35673, decimals: null})
let sun_dir = v3()
function update_sun_pos() {
let {azimuth, altitude} = suncalc.sun_position(e.time, e.latitude, e.longitude)
sun_dir.set(v3.z_axis)
.rotate(v3.x_axis, -altitude)
.rotate(v3.y_axis, -azimuth + rad * e.north)
update_sunlight_pos()
}
function update_sunlight_pos() {
if (e.sunlight || e.shadows) {
renderer.sunlight_dir.set(sun_dir)
} else {
renderer.sunlight_dir.set(camera.dir)
}
}
// rendering --------------------------------------------------------------
e.prop('skybox', {store: 'var', type: 'bool', default: true, attr: true})
e.prop('background_color', {store: 'var', type: 'color', default: '#ffffff', attr: true})
bg_color = () => v3().from_rgb(e.background_color)
e.set_background_color = function(v) {
renderer.background_color = bg_color()
render()
}
function draw(prog) {
if (TRACE)
gl.start_trace()
let t0 = time()
if (e.skybox)
skybox.draw(prog)
//ground_rr.draw(prog)
draw_model(prog)
if (TRACE)
print(gl.stop_trace())
helper_lines_rr.draw(prog)
}
let renderer
function init_renderer() {
renderer = gl.scene_renderer({
enable_shadows: e.shadows,
camera: camera,
background_color: bg_color(),
})
}
let raf_id
function do_render() {
renderer.render(draw)
raf_id = null
}
function render() {
if (!raf_id)
raf_id = raf(do_render)
}
function update_renderer() {
renderer.update()
render()
}
e.detect_resize()
e.on('resize', function(r) {
let w = floor(r.w)
let h = floor(r.h)
canvas.w = w
canvas.h = h
canvas.width = w
canvas.height = h
update_camera_proj()
})
// undo/redo stacks -------------------------------------------------------
let undo_groups = [] // [i1, ...] indices in undo_stack where groups start
let undo_stack = [] // [args1...,argc1,f1, ...]
let redo_groups = [] // same
let redo_stack = [] // same
function start_undo() {
undo_groups.push(undo_stack.length)
}
function push_undo(f, ...args) {
undo_stack.push(...args, args.length, f)
}
function undo_from(stack, start) {
if (start == null)
return
start_undo()
while (stack.length > start) {
let f = stack.pop()
let argc = stack.pop()
f(...stack.splice(-argc))
}
}
function undo() {
let stack = undo_stack
let groups = undo_groups
let start = groups.pop()
if (start == null)
return
undo_groups = redo_groups
undo_stack = redo_stack
undo_from(stack, start)
undo_groups = groups
undo_stack = stack
}
function redo() {
undo_from(redo_stack, redo_groups.pop())
}
// materials --------------------------------------------------------------
let materials = [] // [{diffuse_color:, diffuse_map:, uv: , opacity: , faces: [face1,...]},...]
let next_material_num = 0
function add_material(opt) {
let mat = assign({
diffuse_color: 0xffffff,
uv: v2(1, 1),
opacity: 1,
}, opt)
mat.name = mat.name || 'material ' + (next_material_num++)
mat.opacity = clamp(mat.opacity, 0, 1)
materials.push(mat)
return mat
}
let default_material = add_material({name: 'white', diffuse_color: 0xffffff})
for (let m of [
{name: 'black' , diffuse_color: 0},
{name: 'red' , diffuse_color: 0xff0000},
{name: 'green' , diffuse_color: 0x00ff00},
{name: 'blue' , diffuse_color: 0x0000ff},
])
add_material(m)
// layers -----------------------------------------------------------------
let layers = []
let default_layer
function init_layers() {
default_layer = add_layer({name: 'Default', can_hide: false})
}
function layer_changed(node, layer) {
// TODO
}
function add_layer(opt, ev) {
layer = ev && ev.layer || assign({visible: true, can_hide: true}, opt)
layers.insert(ev && ev.index, layer)
instances_valid = false
let i = or(ev && ev.index, layers.length - 1)
if (LOG)
log('add_layer', opt, ev, layer, i)
push_undo(remove_layer, layer, {index: i})
if (layers_list && !(ev && ev.input == layers_list)) {
layers_list.insert_row({
name: layer.name,
visible: layer.visible,
}, {input: e, row_index: i, layer: layer})
}
return layer
}
function remove_layer(layer, ev) {
// TODO: move tbis layer's instances to the default layer.
let i = assert(layers.remove_value(layer))
instances_valid = false
if (LOG)
log('remove_layer', layer, ev, i)
assert(!ev || ev.index == null || ev.index == i)
push_undo(add_layer, null, {layer: layer, index: i})
if (layers_list && !(ev && ev.input == layers_list)) {
let row = layers_list.rows[i]
assert(row.layer == layer)
layers_list.remove_row(row, {input: e})
}
}
function move_layers(i, n, insert_i, ev) {
if (LOG)
log('move_layers', i, n, insert_i, ev)
layers.move(i, n, insert_i, ev)
push_undo(move_layers, insert_i, n, i)
if (layers_list && !(ev && ev.input == layers_list)) {
layers_list.move_rows(i, n, insert_i, null, {input: e})
}
}
function layer_set_visibile(layer, visible) {
if (LOG)
log('layer_set_visibile', layer, visible)
layer.visible = !!visible
instances_valid = false
push_undo(layer_set_visibile, layer, !visible)
}
// components -------------------------------------------------------------
let comps = [] // [comp1,...]
function create_component(opt) {
let comp = model3_component(assign(opt || {}, {
gl : gl,
push_undo : push_undo,
default_material : default_material,
default_layer : default_layer,
child_added : child_added,
child_removed : child_removed,
layer_changed : layer_changed,
helper_point : helper_point,
}))
let id = comps.length
comps[id] = comp
comp.id = id
return comp
}
function remove_component(comp) {
comps.remove(comp.id)
let id = 0
for (let comp of comps)
comp.id = id++
}
function update_comp(comp) {
if (
comp.update(
comp.davib.dabs.model.buffer,
comp.davib.dabs.disabled.buffer)
)
mouse.valid = false
}
// component instances ----------------------------------------------------
// NOTE: child component objects are mat4's, that's ok, don't sweat it.
let root
let instances_valid
function child_added(parent_comp, node) {
instances_valid = false
}
function child_removed(parent_comp, node) {
instances_valid = false
}
function child_changed(node) {
//
instances_valid = false
}
function mat4_stack() {
let e = {}
}
{
let mstack = freelist_stack(() => mat4(), noop, noop)
let disabled_arr = [0]
function update_instances_for(node, parent, path_depth, on_cur_path) {
let davib = node.comp.davib
let i = davib.len
davib.len = i + 1
let parent_m = mstack.stack.last || mat4.identity
let m = mstack.push()
mat4.mul(parent_m, node, m)
m.to_mat4_array(davib.dabs.model.array, i)
disabled_arr[0] = !on_cur_path || cur_path.length-1 > path_depth
davib.dabs.disabled.set(i, disabled_arr)
node.parent = parent
let children = node.comp.children
if (children) {
let cur_child = cur_path[path_depth + 1]
for (let child of children)
if (child.layer.visible) {
update_instances_for(child, node,
path_depth + 1,
cur_child == null || cur_child == child
)
}
}
mstack.pop()
}}
function update_instances() {
if (instances_valid)
return
for (let comp of comps)
if (comp.davib)
comp.davib.len = 0
else
comp.davib = gl.dyn_arr_vertex_instance_buffer({model: 'mat4', disabled: 'i8'})
update_instances_for(root, null, 0, true)
for (let comp of comps)
comp.davib.upload()
mouse.valid = false
instances_valid = true
}
function init_root() {
e.root = create_component({name: '<root>'})
root = mat4()
root.comp = e.root
}
function gc_components() {
for (let [comp, insts] of instances) {
if (!insts.length) {
if (insts.dab)
insts.dab.free()
instances.delete(comp)
remove_component(comp)
comp.free()
}
}
}
// drawing
function update() {
update_instances()
update_comp(cur_comp)
render()
}
function update_all() {
update_instances()
for (let comp of comps)
update_comp(comp)
}
function draw_model(prog) {
for (let i = 0, n = comps[0] && comps[0].renderers.length || 0; i < n; i++)
for (let comp of comps)
comp.renderers[i].draw(prog)
axes_rr.draw(prog)
}
function draw_model_for_hit_test(prog) {
for (let comp of comps)
comp.face_renderer.draw(prog)
}
// instance path finding for hit testing
{
let path = []
function instance_path_for(target_comp, target_inst_id, node) {
path.push(node)
if (target_comp == node.comp && target_inst_id == target_comp._inst_id)
return true
node.comp._inst_id++
for (let child of node.comp.children) {
if (instance_path_for(target_comp, target_inst_id, child))
return true
}
path.pop(node)
}
function instance_path(comp, inst_id) {
for (let comp of comps)
comp._inst_id = 0
path.length = 0
instance_path_for(comp, inst_id, root)
return path
}}
// instance-space <-> world-space transforms
function inst_model(comp, inst_id, out) {
out.inst_id = inst_id
return out.from_mat4_array(comp.davib.dabs.model.array, inst_id)
}
function inst_inv_model(comp, inst_id, out) {
out.inst_id = inst_id
return inst_model(comp, inst_id, out).invert()
}
// model-wide instance intersection tests
{
let _m0 = mat4()
function line_hit_lines(target_line, max_d, p2p_distance2, int_mode, is_line_valid, is_int_line_valid) {
if (!max_d)
return
for (let comp of comps) {
for (let i = 0, n = comp.davib.len; i < n; i++) {
let model = inst_model(comp, i, _m0)
let int_p = comp.line_hit_lines(model, target_line, max_d, p2p_distance2, int_mode, is_line_valid, is_int_line_valid)
if (int_p) {
int_p.model = model
int_p.comp = comp
int_p.path = instance_path(int_p.comp, i)
int_p.snap = 'line'
return int_p
}
}
}
}}
// selection
function select_all(sel, with_children) {
cur_comp.select_all(sel, with_children)
}
// reference planes -------------------------------------------------------
function ref_plane(
name, normal, plane_hit_tooltip,
main_axis_snap, main_axis, main_axis_snap_tooltip
) {
let e = {}
let plane = plane3(normal)
{
let _l0 = line3()
let _pl0 = plane3()
let _p0 = v3()
let _p1 = v3()
e.mouse_hit_plane = function(model) {
let p = plane.to(_pl0).transform(model).intersect_line(mouse.ray, _p0)
if (!p || p.t < 0)
return
let angle = mouse.ray.delta(_p1).angle_to(normal)
if (angle > PI / 2)
angle = abs(angle - PI)
p.angle = angle
p.tooltip = plane_hit_tooltip
return p
}}
{
let _l0 = line3()
let _l1 = line3()
let _l2 = line3()
let _l3 = line3()
e.mouse_hit_main_axis = function(model, max_hit_distance, out) {
assert(out.is_v3)
out.ds = 1/0
out.line_snap = null
out.tooltip = null
let axis = _l0.set(v3.origin, main_axis).transform(model)
camera.world_to_screen(axis[0], _l1[0])
camera.world_to_screen(axis[1], _l1[1])
let int_p = _l1.closest_point_to_point(mouse, false, out)
if (!int_p)
return
let ds = mouse.distance2(int_p)
if (ds > max_hit_distance ** 2)
return
let ray = camera.raycast(int_p.x, int_p.y, _l2)
let int_line = ray.intersect_line(axis, _l3)
if (!int_line)
return
out.set(int_line[1])
out.ds = ds
out.line_snap = main_axis_snap
out.tooltip = main_axis_snap_tooltip
return out
}}
{
let _p0 = v3()
let _p1 = v3()
e.point_along_main_axis = function(model, p) {
let abs_axis = main_axis.to(_p1).transform(model)
if (v3.cross(p, abs_axis, abs_axis).len2() < NEAR ** 2) {
p.line_snap = main_axis_snap
return true
}
}}
// intersect the plane's main axis with a line
// and return the projected point on the line.
{
let int_line = line3()
let _l1 = line3()
e.main_axis_hit_line = function(model, line, out) {
let axis = _l1.set(v3.origin, main_axis).transform(model)
if (!axis.intersect_line(line, int_line))
return
let ds = int_line[0].distance2(int_line[1])
if (ds > NEAR ** 2)
return
out.set(int_line[1])
out.line_snap = main_axis_snap
return out
}}
return e
}
let xyplane = ref_plane(
'xyplane', v3(0, 0, 1), 'on the blue-red vertical plane',
'y_axis', v3(0, 1, 0), 'on blue axis')
let zyplane = ref_plane(
'zyplane', v3(1, 0, 0), 'on the blue-green vertical plane',
'z_axis', v3(0, 0, 1), 'on green axis')
let xzplane = ref_plane(
'xzplane', v3(0, 1, 0), 'on the horizontal plane',
'x_axis', v3(1, 0, 0), 'on red axis')
let ref_planes = [xyplane, zyplane, xzplane]
function hit_ref_planes(model) {
// hit horizontal plane first.
let p = xzplane.mouse_hit_plane(model)
if (p)
return p
// hit vertical ref planes.
let p1 = xyplane.mouse_hit_plane(model)
let p2 = zyplane.mouse_hit_plane(model)
// pick whichever plane is facing the camera more straightly.
return (p1 ? p1.angle : 1/0) < (p2 ? p2.angle : 1/0) ? p1 : p2
}
{
let ps = [v3(), v3(), v3()]
let cmp_ps = function(p1, p2) {
return p1.ds == p2.ds ? 0 : (p1.ds < p2.ds ? -1 : 1)
}
function mouse_hit_axes(model, max_hit_distance) {
let i = 0
for (let plane of ref_planes)
plane.mouse_hit_main_axis(model, max_hit_distance, ps[i++])
ps.sort(cmp_ps)
return ps[0].line_snap ? ps[0] : null
}}
// given `p` on `line`, get the axis-intersects-line point that is closest to `p`.
{
let int_p = v3()
let ret = v3()
function axes_hit_line(model, p, line, max_hit_distance) {
let min_ds = 1/0
let min_int_p
for (let plane of ref_planes) {
if (plane.main_axis_hit_line(model, line, int_p)) {
let ds = sqrt(screen_p2p_distance2(p, int_p))
if (ds <= max_hit_distance ** 2 && ds <= min_ds) {
min_ds = ds
min_int_p = assign(min_int_p || v3(), int_p)
}
}
}
return min_int_p
}}
function check_point_on_axes(model, p) {
for (let plane of ref_planes)
if (plane.point_along_main_axis(model, p))
return true
}
// hybrid render/analytic-based hit-testing -------------------------------
let hit_test_rr = gl.hit_test_renderer()
let max_hit_distances = {
snap : 20, // max pixel distance for snapping
select : 8, // max pixel distance for selecting
drag_pull : 2, // max pixel distance before drag-pull starts
}
// connect rendered comp_id/face_id/inst_id back to the model.
{
let _v0 = v3()
let _l0 = line3()
let _m0 = mat4()
let _pl0 = plane()
function mouse_hit_faces() {
if (!(mouse.valid || mouse.prevent_validate)) {
hit_test_rr.render(draw_model_for_hit_test)
mouse.valid = true
}
let rr_hit = hit_test_rr.hit_test(mouse.x, mouse.y)
if (!rr_hit)
return
let comp_id = rr_hit.comp_id
let face_id = rr_hit.face_id
let inst_id = rr_hit.inst_id
let comp = comps[comp_id]
let face = comp.faces[face_id]
let model = inst_model(comp, inst_id, _m0)
let plane = face.plane().to(_pl0).transform(model)
let int_p = plane.intersect_line(mouse.ray, _v0)
if (!int_p)
return
int_p.comp = comp
int_p.model = model
int_p.plane = plane
int_p.face = face
int_p.snap = 'face'
int_p.path = instance_path(comp, inst_id)
return int_p
}}
let screen_p2p_distance2 = camera.screen_distance2
function snap_point_on_line(p, line, max_d, p2p_distance2, plane_int_p, axes_int_p) {
p.i = null
p.snap = 'line'
max_d = max_d ** 2
let mp = line.at(.5, v3())
let d1 = p2p_distance2(p, line[0])
let d2 = p2p_distance2(p, line[1])
let dm = p2p_distance2(p, mp)
let dp = plane_int_p ? p2p_distance2(p, plane_int_p) : 1/0
let dx = axes_int_p ? p2p_distance2(p, axes_int_p ) : 1/0
if (d1 <= max_d && d1 <= d2 && d1 <= dm && d1 <= dp && d1 <= dx) {
p.assign(line[0]) // comes with its own point index.
p.snap = 'point'
} else if (d2 <= max_d && d2 <= d1 && d2 <= dm && d2 <= dp && d2 <= dx) {
p.assign(line[1]) // comes with its own point index.
p.snap = 'point'
} else if (dp <= max_d && dp <= d1 && dp <= d2 && dp <= dm && dp <= dx) {
p.assign(plane_int_p)
p.snap = 'line_plane_intersection'
} else if (dm <= max_d && dm <= d1 && dm <= d2 && dm <= dp && dm <= dx) {
line.at(.5, p)
p.snap = 'line_middle'
} else if (dx <= max_d && dx <= d1 && dx <= d2 && dx <= dm && dx <= dp) {
p.assign(axes_int_p) // comes with its own snap flags and indices.
}
}
{
let _v0 = v3()
function mouse_hit_model(opt) {
let int_p = mouse_hit_faces()
let hit_d = max_hit_distances[opt.distance]
let axes_model = opt.axes_model
let mode = opt.mode
if (int_p) {
// we've hit a face, but we still have to hit any lines
// that lie in front of it, on it, or intersecting it.
let face_plane = int_p.plane
function is_int_line_valid(int_p) {
let t = int_p.t
if (t < 0 || t > 1) return // not intersecting the segment.
return face_plane.distance_to_point(int_p) >= -NEAR // not behind the plane
}
let line_int_p = line_hit_lines(
mouse.ray, hit_d, screen_p2p_distance2, 't',
null, is_int_line_valid)
if (line_int_p) {
// we've hit a line. snap to it.
let hit_line = line_int_p.line
// check if the hit line intersects the face plane: that's a snap point.
let plane_int_p = face_plane.intersect_line(hit_line, _v0, 'strict')
// check if the hit line intersects any axes originating at line start: that's a snap point.
let axes_int_p = axes_model && axes_hit_line(axes_model, line_int_p, hit_line, hit_d)
// snap the hit point along the hit line along with any additional snap points.
snap_point_on_line(line_int_p, hit_line, hit_d, screen_p2p_distance2, plane_int_p, axes_int_p)
if (axes_model)
check_point_on_axes(axes_model, line_int_p)
// if the snapped point is not behind the plane, use it, otherwise forget that we even hit the line.
if (face_plane.distance_to_point(line_int_p) >= -NEAR)
int_p = line_int_p
} else {
// free moving on the face face.
}
} else {
function is_int_line_valid(int_p) {
let t = int_p.t
if (t < 0 || t > 1) return // not intersecting the segment.
return true
}
// we haven't hit a face: hit the line closest to the ray regardless of depth.
int_p = line_hit_lines(
mouse.ray, hit_d, screen_p2p_distance2, 't',
null, is_int_line_valid)
if (int_p) {
// we've hit a line. snap to it.
let hit_line = int_p.line
// check if the hit line intersects any axes originating at line start: that's a snap point.
let axes_int_p = axes_model && axes_hit_line(axes_model, int_p, hit_line, hit_d)
// snap the hit point along the hit line along with any additional snap points.
snap_point_on_line(int_p, hit_line, hit_d, screen_p2p_distance2, null, axes_int_p)
if (axes_model)
check_point_on_axes(axes_model, int_p)
} else if (mode == 'camera') {
// chose an arbitrary point at a proportional distance from the camera.
int_p = mouse.ray.at(min(FAR / 10, camera.pos.len()), _v0)
} else if (mode == 'draw') {
// we've hit squat: hit the axes and the ref planes.
int_p = axes_model && mouse_hit_axes(axes_model, hit_d)
int_p ||= hit_ref_planes(axes_model || cur_model)
} else if (mode == 'select') {
// don't hit anything else so we can unselect all.
} else {
assert(false)
}
}
return int_p
}}
// currently editing instance ---------------------------------------------
let cur_path = []
let cur_comp = root
let cur_model = mat4()
let cur_inv_model = mat4()
let axes_rr = gl.axes_renderer()
let axes = axes_rr.axes()
function enter_edit(path) {
if (!path.length)
path = [root]
if (cur_path.equals(path))
return
if (cur_comp) {
select_all(false)
update() // update cur_comp before it changes
}
cur_path.set(path)
cur_model.reset()
cur_comp = cur_path.last.comp
for (let node of path)
cur_model.mul(node)
cur_inv_model.set(cur_model).invert()
axes.model.set(cur_model)
axes.update()
instances_valid = false
}
function exit_edit() {
enter_edit(cur_path.slice(0, -1))
}
function from_world(v, out) {
return out.set(v).transform(cur_inv_model)
}
function to_world(v, out) {
return out.set(v).transform(cur_model)
}
// skybox -----------------------------------------------------------------
let skybox = gl.skybox({
images: {
posx: 'x-modeleditor/skybox_posx.jpg',
negx: 'x-modeleditor/skybox_negx.jpg',
posy: 'x-modeleditor/skybox_posy.jpg',
negy: 'x-modeleditor/skybox_negy.jpg',
posz: 'x-modeleditor/skybox_posz.jpg',
negz: 'x-modeleditor/skybox_negz.jpg',
},
})
skybox.on('load', render)
// shadow ground plane ----------------------------------------------------
//let ground_rr = gl.ground_plane_renderer()
// cursor -----------------------------------------------------------------
{
let builtin_cursors = {
grab: true,
}
let offsets = {
line : [0, 25],
pull : [12, 0],
pullno : [12, 0],
select : [5, 12],
select_add : [5, 12],
select_remove : [5, 12],
select_toggle : [5, 12],
paint : [6, 28],
move : [15,17],
}
let cursor
e.property('cursor', () => cursor, function(name) {
if (cursor == name)
return
cursor = name
let x = offsets[name] && offsets[name][0] || 0
let y = offsets[name] && offsets[name][1] || 0
e.style.cursor = builtin_cursors[name] ? name : 'url(x-modeleditor/cursor_'+name+'.png) '+x+' '+y+', auto'
})
}
// cursor tooltip ---------------------------------------------------------