-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheditor.lua
1118 lines (1078 loc) · 40.6 KB
/
editor.lua
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
local editor = ...
local win = require "window"
local sprites = require "sprites"
local mouse = require "mouse"
local export = require "export"
local save = require "save"
local help = require "help"
local focus = require "focus"
local download = require "download"
local upload = require "upload"
local label_w = 64
local val_w = 32
local num_brushes = 0
local brush_sprite_specs = {}
while sprites["brush"..(num_brushes + 1)] do
num_brushes = num_brushes + 1
brush_sprite_specs[num_brushes] = sprites["brush"..(num_brushes)]
end
local webcam_tex = am.texture2d(512)
num_brushes = num_brushes + 1
local webcam_brush = num_brushes
brush_sprite_specs[num_brushes] = {
texture = webcam_tex,
x1 = 0,
y1 = 0,
x2 = 256,
y2 = 256,
s1 = 0,
t1 = 1,
s2 = 1,
t2 = 0,
width = 256,
height = 256,
}
local capture_tex = am.texture2d(512)
num_brushes = num_brushes + 1
local capture_brush = num_brushes
brush_sprite_specs[num_brushes] = {
texture = capture_tex,
x1 = 0,
y1 = 0,
x2 = 256,
y2 = 256,
s1 = 0,
t1 = 1,
s2 = 1,
t2 = 0,
width = 256,
height = 256,
}
local heightmap_vshader = [[
precision highp float;
uniform mat4 P;
uniform mat4 MV;
attribute vec2 vert;
attribute vec2 uv;
varying vec2 v_uv;
void main() {
v_uv = uv;
gl_Position = P * MV * vec4(vert, 0.0, 1.0);
}
]]
local heightmap_fshader = [[
precision mediump float;
uniform sampler2D tex;
varying vec2 v_uv;
void main() {
vec4 s = texture2D(tex, v_uv);
gl_FragColor = vec4(vec3(s.a), 1.0);
}
]]
local color_fshader = [[
precision mediump float;
uniform sampler2D tex;
varying vec2 v_uv;
void main() {
vec4 s = texture2D(tex, v_uv);
gl_FragColor = vec4(s.rgb * 0.7 + s.a * 0.3, 1.0);
}
]]
local hands_fshader = [[
precision mediump float;
uniform sampler2D tex;
varying vec2 v_uv;
void main() {
vec4 s = texture2D(tex, v_uv);
float checks = mod(floor(gl_FragCoord.x / 10.0) + floor(gl_FragCoord.y / 10.0), 2.0);
gl_FragColor = vec4(mix(vec3(checks) * 0.2 + 0.4, s.rgb, s.a), 1.0);
}
]]
local heightmap_shader = am.program(heightmap_vshader, heightmap_fshader)
local color_shader = am.program(heightmap_vshader, color_fshader)
local hands_shader = am.program(heightmap_vshader, hands_fshader)
local capture_shader = am.shaders.texture2d
local draw_vshader = [[
precision highp float;
uniform mat4 P;
uniform mat4 MV;
attribute vec2 vert;
attribute vec2 uv;
varying vec2 v_uv;
void main() {
v_uv = uv;
gl_Position = P * MV * vec4(vert, 0.0, 1.0);
}
]]
local draw_fshader = [[
precision mediump float;
uniform sampler2D tex;
uniform vec4 color;
uniform float mult_alpha;
uniform float exp1;
uniform float exp2;
uniform vec4 height_src;
varying vec2 v_uv;
void main() {
vec4 s = texture2D(tex, v_uv);
//if (s.a < 1.0/255.0) discard;
vec4 v = s * height_src;
float alpha = v.r + v.g + v.b + v.a;
alpha = pow(1.0 - pow(1.0 - alpha, exp1), exp2) * color.a;
gl_FragColor = vec4(s.rgb * color.rgb * mix(1.0, color.a, mult_alpha), alpha);
}
]]
local draw_shader = am.program(draw_vshader, draw_fshader)
local
function in_bounds(p, b)
return p.x > b.l and p.x < b.l + b.w and p.y > b.b and p.y < b.b + b.h
end
local
function normalize_pos(p, b)
return (p - vec2(b.l, b.b)) * 2 / vec2(b.w, b.h) - 1
end
local
function create_select(label, nodes, x_os, y, spacing, on_change, init_sel, shortcuts)
local curr_sel = init_sel or 1
local n = #nodes
local x_start = x_os + label_w
local x_end = x_start + n * spacing
local y_top = y + spacing / 2
local y_bottom = y - spacing / 2
local group = am.group()
group:append(am.translate(x_os, y) ^ am.text(label, "left"))
local x = x_start + spacing/2
for i = 1, n do
group:append(am.translate(x, y) ^ nodes[i])
x = x + spacing
end
group:append(
am.translate((curr_sel - 1) * spacing + x_start + spacing/2, y):tag"select"
^ am.sprite(sprites.select))
group:action(function()
if win:mouse_pressed("left") then
local pos = mouse.pixel_position
if pos.x > x_start and pos.x < x_end and pos.y > y_bottom and pos.y < y_top then
curr_sel = math.ceil((pos.x - x_start) / (x_end - x_start) * n)
group"select""translate".position2d = vec2(x_start + (curr_sel - 1) * spacing + spacing/2, y)
on_change(curr_sel)
end
end
if shortcuts then
for sel, key in ipairs(shortcuts) do
if win:key_pressed(key) then
curr_sel = sel
group"select""translate".position2d = vec2(x_start + (curr_sel - 1) * spacing + spacing/2, y)
on_change(curr_sel)
break
end
end
end
end)
return group
end
local
function create_checkbox(label, x_os, y, on_change, init_state)
local sz = 32
local curr_state = init_state or false
local x_start = x_os + label_w
local x_end = x_start + sz
local y_top = y + sz / 2
local y_bottom = y - sz / 2
local group = am.group()
group:append(am.translate(x_os, y) ^ am.text(label, "left"))
local x = x_start + sz/2
local sprite = am.sprite(curr_state and sprites.checkbox_on or sprites.checkbox_off)
group:append(am.translate(x, y) ^ sprite)
group:action(function()
if win:mouse_pressed("left") then
local pos = mouse.pixel_position
if pos.x > x_start and pos.x < x_end and pos.y > y_bottom and pos.y < y_top then
curr_state = not curr_state
sprite.source = curr_state and sprites.checkbox_on or sprites.checkbox_off
on_change(curr_state)
end
end
end)
return group
end
local
function create_button(label, x_os, y, on_press)
local h = 30
local x_start = x_os
local x_end = x_start + label_w
local y_top = y + h / 2
local y_bottom = y - h / 2
local group = am.group{
am.rect(x_start, y_bottom, x_end, y_top, vec4(0, 1, 1, 1)),
am.translate(x_os + (x_end - x_start)/2, y+3) ^ am.text(label, vec4(0, 0, 0, 1), "center", "center")
}
group:action(function()
if win:mouse_pressed("left") then
local pos = mouse.pixel_position
if pos.x > x_start and pos.x < x_end and pos.y > y_bottom and pos.y < y_top then
on_press()
end
end
end)
return group
end
local
function create_slider(label, x_os, y, bg, on_change, init_val, show_val, shortcut)
local w = bg.width
local h = bg.height
local curr_val = init_val or 0
local lw = label and label_w or 0
local vw = show_val and val_w or 0
local x_start = x_os + lw + vw
local x_end = x_start + w
local y_top = y + h / 2
local y_bottom = y - h / 2
local group = am.group()
if label then
group:append(am.translate(x_os, y) ^ am.text(label, "left"))
end
local val_txt
if show_val then
val_txt = am.text(math.floor(init_val * 255), "left")
group:append(am.translate(x_os + lw, y) ^ val_txt)
end
local x = x_start + w/2
group:append(am.translate(x, y) ^ am.sprite(bg))
local slider = am.translate(x, y) ^ am.sprite(sprites.slider)
group:append(slider)
local
function set_slider(val)
slider.position2d = vec2(x_start + val * w, y)
end
set_slider(curr_val)
local sliding
local kb_start_val, mouse_start_pos
group:action(function()
local pos = mouse.pixel_position
if win:mouse_pressed"left" then
if pos.x >= x_start and pos.x <= x_end and pos.y >= y_bottom and pos.y <= y_top then
sliding = true
curr_val = (pos.x - x_start) / w
set_slider(curr_val)
on_change(curr_val)
if val_txt then
val_txt.text = math.floor(curr_val * 255)
end
end
elseif sliding then
curr_val = (math.clamp(pos.x, x_start, x_end) - x_start) / w
set_slider(curr_val)
on_change(curr_val)
if val_txt then
val_txt.text = math.floor(curr_val * 255)
end
if win:mouse_released"left" then
sliding = false
end
elseif shortcut and win:key_down(shortcut) then
if win:key_pressed(shortcut) then
kb_start_val = curr_val
mouse_start_pos = pos
mouse.set_visible(false)
mouse.clamp = false
end
curr_val = math.clamp(kb_start_val + (pos.x - mouse_start_pos.x)/255, 0, 1)
set_slider(curr_val)
on_change(curr_val)
if val_txt then
val_txt.text = math.floor(curr_val * 255)
end
elseif shortcut and mouse_start_pos and win:key_released(shortcut) then
mouse.set_position(mouse_start_pos)
mouse.set_visible(true)
mouse.clamp = true
end
end)
return group
end
local
function create_color_picker(label, x_os, y, on_change, init_val)
local group = am.group()
group:append(am.translate(x_os, y) ^ am.text(label, "left"))
local h = sprites.red_slider.height
local color = init_val or vec3(1)
local block = am.rect(x_os + label_w, y-h*1.5, x_os + label_w + h*3, y + h*1.5, vec4(color, 1))
local r_slider = create_slider(nil, x_os + label_w + h*3 + 5, y-h, sprites.red_slider, function(val)
color = color{r = val}
block.color = vec4(color, 1)
on_change(color)
end, color.r)
local g_slider = create_slider(nil, x_os + label_w + h*3 + 5, y, sprites.green_slider, function(val)
color = color{g = val}
block.color = vec4(color, 1)
on_change(color)
end, color.g)
local b_slider = create_slider(nil, x_os + label_w + h*3 + 5, y+h, sprites.blue_slider, function(val)
color = color{b = val}
block.color = vec4(color, 1)
on_change(color)
end, color.b)
group:append(block)
group:append(r_slider)
group:append(g_slider)
group:append(b_slider)
return group
end
local ys = {214}
for i = 1, 6 do
ys[#ys+1] = ys[1] - i * 32
end
local xs = {0, 580, 920}
local
function update_height_src(editor_state)
local height_src
if editor_state.curr_brush == webcam_brush and (editor_state.is_alpha_view or editor_state.all_channels) then
height_src = vec4(0.33, 0.33, 0.33, 0)
else
height_src = vec4(0, 0, 0, 1)
end
editor_state.draw_brush"bind".height_src = height_src
end
local
function update_draw_blend(editor_state)
local real_mode = editor_state.blend_mode
if not editor_state.all_channels and editor_state.blend_mode ~= "off" then
editor_state.draw_brush"bind".mult_alpha = 1
else
editor_state.draw_brush"bind".mult_alpha = 0
if real_mode == "premult" then
real_mode = "off"
end
end
editor_state.draw_brush"brush_sprite""blend".mode = real_mode
end
local
function update_draw_mask(editor_state)
local mask = editor_state.draw_brush"color_mask"
if editor_state.all_channels or (editor_state.is_color_view and editor_state.is_alpha_view) then
mask.red = true
mask.green = true
mask.blue = true
mask.alpha = true
elseif editor_state.is_alpha_view then
mask.red = false
mask.green = false
mask.blue = false
mask.alpha = true
else
mask.red = true
mask.green = true
mask.blue = true
mask.alpha = false
end
end
local
function create_controls(editor_state, terrain_state)
local view_nodes = {
am.sprite(sprites.macro_height),
am.sprite(sprites.macro_color),
am.sprite(sprites.detail_height),
am.sprite(sprites.detail_color),
am.scale(1, -1) ^ am.sprite(sprites.macro_height),
am.scale(1, -1) ^ am.sprite(sprites.macro_color),
am.scale(1, -1) ^ am.sprite(sprites.detail_height),
am.scale(1, -1) ^ am.sprite(sprites.detail_color),
am.sprite(sprites.hands),
}
local view_select = create_select("VIEW:", view_nodes, xs[1], ys[1], 35, function(val)
terrain_state.settings[editor_state.curr_texture] = editor_state.views[editor_state.curr_view].tex
if val <= 2 then
editor_state.curr_view = "floor"
editor_state.curr_texture = "floor_texture"
elseif val <= 4 then
editor_state.curr_view = "floor_detail"
editor_state.curr_texture = "floor_detail_texture"
elseif val <= 6 then
editor_state.curr_view = "ceiling"
editor_state.curr_texture = "ceiling_texture"
elseif val <= 8 then
editor_state.curr_view = "ceiling_detail"
editor_state.curr_texture = "ceiling_detail_texture"
else
editor_state.curr_view = "hands"
editor_state.curr_texture = "hands_texture"
end
terrain_state.settings[editor_state.curr_texture] = editor_state.tmp_texture
terrain_state:update_settings(terrain_state.settings)
editor_state.tmp_scene"bind".tex = editor_state.views[editor_state.curr_view].tex
if val == 9 then
-- hands
editor_state.editor_node"use_program".program = hands_shader
capture_shader = am.shaders.texture2d
editor_state.is_color_view = true
editor_state.is_alpha_view = true
elseif val % 2 == 1 then
editor_state.editor_node"use_program".program = heightmap_shader
editor_state.is_color_view = false
editor_state.is_alpha_view = true
else
editor_state.editor_node"use_program".program = color_shader
editor_state.is_color_view = true
editor_state.is_alpha_view = false
end
update_height_src(editor_state)
update_draw_mask(editor_state)
update_draw_blend(editor_state)
end, 1, {"1", "2", "3", "4", "5", "6", "7", "8"})
local channels_checkbox = create_checkbox("RGBA:", xs[1] + 390, ys[1], function(val)
editor_state.all_channels = val
update_height_src(editor_state)
update_draw_mask(editor_state)
update_draw_blend(editor_state)
end)
local brush_nodes = {}
for i = 1, num_brushes do
local sprite = am.sprite(brush_sprite_specs[i])
sprite"blend".mode = "off"
table.insert(brush_nodes, am.scale(28/256) ^ sprite)
end
local brush_select = create_select("BRUSH:", brush_nodes, xs[1], ys[2], 30, function(b)
editor_state.curr_brush = b
editor_state.draw_brush"brush_sprite".source = brush_sprite_specs[editor_state.curr_brush]
update_height_src(editor_state)
end, editor_state.curr_brush)
local exp1_slider = create_slider("CURVE:", xs[1], ys[3], sprites.exp_slider, function(val)
editor_state.draw_brush"bind".exp1 = 10 ^ (val * 2 - 1)
end, 0.5, false, "n")
local exp2_slider = create_slider(nil, xs[1] + 220, ys[3], sprites.exp_slider, function(val)
editor_state.draw_brush"bind".exp2 = 10 ^ (val * 2 - 1)
end, 0.5, false, "m")
local color_picker = create_color_picker("RGB:", xs[1], ys[4], function(val)
editor_state.draw_brush"brush_sprite".color = editor_state.draw_brush"brush_sprite".color{rgb = val}
end, editor_state.draw_brush"brush_sprite".color.rgb)
local alpha_slider = create_slider("ALPHA:", xs[1], ys[5], sprites.height_slider, function(val)
editor_state.draw_brush"brush_sprite".color = editor_state.draw_brush"brush_sprite".color{a = val}
end, editor_state.draw_brush"brush_sprite".color.a, true, "h")
local blend_modes = {"add", "subtract", "premult", "off", "multiply"}
local blend_nodes = {
am.sprite(sprites.blend_add),
am.sprite(sprites.blend_sub),
am.sprite(sprites.blend_alpha),
am.sprite(sprites.blend_eq),
am.sprite(sprites.blend_mul),
}
local blend_select = create_select("BLEND:", blend_nodes, xs[1], ys[6], 30, function(val)
editor_state.blend_mode = blend_modes[val]
update_draw_blend(editor_state)
end, table.search(blend_modes, editor_state.blend_mode))
local flow_slider = create_slider("FLOW:", xs[1], ys[7], sprites.flow_slider, function(val)
if val < 0.1 then
editor_state.flow = 0
else
editor_state.flow = (val - 0.1) / 0.9 * 60
end
end, 0)
local filter_nodes = {
am.sprite(sprites.filter_linear),
am.sprite(sprites.filter_nearest),
}
local filter_select = create_select("FLTR:", filter_nodes, xs[1] + 220, ys[7], 30, function(val)
local filter = "nearest"
if val == 1 then
filter = "linear"
end
editor_state.tmp_texture.minfilter = filter
editor_state.tmp_texture.magfilter = filter
terrain_state.settings.filter = filter
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 1)
local fog_color_picker = create_color_picker("FOG:", xs[2], ys[1], function(val)
terrain_state.settings.fog_color = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.fog_color.rgb)
local fog_dist_slider = create_slider("FOG Z:", xs[2], ys[2], sprites.height_slider, function(val)
terrain_state.settings.fog_dist = val * 2000
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.fog_dist / 2000)
local ambient_picker = create_color_picker("AMB:", xs[2], ys[3], function(val)
terrain_state.settings.ambient = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.ambient)
local diffuse_picker = create_color_picker("DIFF:", xs[2], ys[4], function(val)
terrain_state.settings.diffuse = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.diffuse)
local specular_picker = create_color_picker("SPEC:", xs[2], ys[5], function(val)
terrain_state.settings.specular = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.specular)
local shininess_slider = create_slider("SHINE:", xs[2], ys[6], sprites.height_slider, function(val)
terrain_state.settings.shininess = (val ^ 3) * 200
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0)
local speed_slider = create_slider("SPEED:", xs[2], ys[7], sprites.flow_slider, function(val)
terrain_state.settings.walk_speed = val * 100 + 10
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0.5)
local wireframe_checkbox = create_checkbox("WIRE/F:", xs[3], ys[1], function(val)
terrain_state.settings.wireframe = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.wireframe)
local noclip_checkbox = create_checkbox("NOCLIP:", xs[3] + 110, ys[1], function(val)
terrain_state.settings.noclip = val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, terrain_state.settings.noclip)
local grav_checkbox = create_checkbox("GRAV:", xs[1] + 410, ys[7], function(val)
terrain_state.settings.nograv = not val
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, not terrain_state.settings.nograv)
local mesh_nodes = {
am.sprite(sprites.mesh_low),
am.sprite(sprites.mesh_med),
am.sprite(sprites.mesh_high),
}
local mesh_select = create_select("MESH:", mesh_nodes, xs[3], ys[2], 30, function(val)
if val == 1 then
terrain_state.settings.width = 100
terrain_state.settings.depth = 100
elseif val == 2 then
terrain_state.settings.width = 400
terrain_state.settings.depth = 300
elseif val == 3 then
terrain_state.settings.width = 800
terrain_state.settings.depth = 600
end
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 3)
local detail_height_slider = create_slider("DTL Y:", xs[3], ys[3], sprites.med_slider, function(val)
terrain_state.settings.detail_height = val * 0.2 + 0.005
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0)
local detail_scale_slider = create_slider("DTL XZ:", xs[3], ys[4], sprites.med_slider, function(val)
local s = (val ^ 2) * 0.1 + 0.0005
terrain_state.settings.floor_detail_scale = s
terrain_state.settings.ceiling_detail_scale = s
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0)
local ceiling_y_slider = create_slider("SKY Y:", xs[3], ys[5], sprites.med_slider, function(val)
terrain_state.settings.ceiling_y_offset = (val ^ 2 * 1000) - 100
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0)
local y_scale_slider = create_slider("VSCALE:", xs[3], ys[6], sprites.med_slider, function(val)
local s = (val ^ 2) * 500 + 50
terrain_state.settings.floor_y_scale = s
terrain_state.settings.ceiling_y_scale = s
terrain_state:update_settings(terrain_state.settings)
editor_state.modified = true
end, 0)
local reset_button = create_button("RESET", xs[2] + 560, ys[7], function()
if am.platform ~= "html" or not editor_state.modified
or am.eval_js("confirm('You have unsaved changes, are you sure you want to reset? (all unsaved changes will be lost)');")
then
reset()
end
end)
local upload_button = create_button("UPLOAD", xs[1] + 490, ys[3], function()
win.lock_pointer = false
local img = upload.start_image_upload()
win.scene:action(function()
local base64 = upload.image_upload_successful()
if base64 then
win.lock_pointer = true
local img
local ok = pcall(function()
img = am.decode_png(am.base64_decode(base64))
end)
if not ok then
am.eval_js("alert('image not a valid png');");
return true
end
if img.width ~= 512 or img.height ~= 512 then
-- resize to 512x512
local buf = am.image_buffer(512)
local texture = am.texture2d(buf)
local fb = am.framebuffer(texture)
local tex2 = am.texture2d(img)
tex2.filter = "linear"
local quads = am.quads(1, {"vert", "vec2", "uv", "vec2"})
quads:add_quad{
vert = {-1, 1, -1, -1, 1, -1, 1, 1},
uv = {0, 1, 0, 0, 1, 0, 1, 1},
}
fb:render(am.use_program(am.shaders.texture2d)
^ am.bind{P = mat4(1), MV = mat4(1), tex = tex2}
^ quads)
fb:read_back()
img = buf
end
local view = editor_state.views[editor_state.curr_view]
view.fb:read_back()
if editor_state.all_channels or editor_state.is_color_view then
local dr = view.img.buffer:view("ubyte", 0, 4)
local sr = img.buffer:view("ubyte", 0, 4)
local dg = view.img.buffer:view("ubyte", 1, 4)
local sg = img.buffer:view("ubyte", 1, 4)
local db = view.img.buffer:view("ubyte", 2, 4)
local sb = img.buffer:view("ubyte", 2, 4)
dr:set(sr)
dg:set(sg)
db:set(sb)
if editor_state.is_alpha_view then
local da = view.img.buffer:view("ubyte", 3, 4)
local sa = img.buffer:view("ubyte", 3, 4)
da:set(sa)
end
end
if editor_state.all_channels or editor_state.is_alpha_view then
local dst = view.img.buffer:view("ubyte", 3, 4)
local src = img.buffer:view("ubyte", 0, 4)
dst:set(src)
end
return true
end
end)
end)
local download_button = create_button("DOWNLD", xs[1] + 490, ys[4], function()
local view = editor_state.views[editor_state.curr_view]
view.fb:read_back()
local dst = am.image_buffer(512)
local src = view.img
if editor_state.is_color_view then
local dr = dst.buffer:view("ubyte", 0, 4)
local sr = src.buffer:view("ubyte", 0, 4)
local dg = dst.buffer:view("ubyte", 1, 4)
local sg = src.buffer:view("ubyte", 1, 4)
local db = dst.buffer:view("ubyte", 2, 4)
local sb = src.buffer:view("ubyte", 2, 4)
local da = dst.buffer:view("ubyte", 3, 4)
dr:set(sr)
dg:set(sg)
db:set(sb)
if editor_state.is_alpha_view then
local sa = src.buffer:view("ubyte", 3, 4)
da:set(sa)
else
da:set(255)
end
else
local dr = dst.buffer:view("ubyte", 0, 4)
local dg = dst.buffer:view("ubyte", 1, 4)
local db = dst.buffer:view("ubyte", 2, 4)
local da = dst.buffer:view("ubyte", 3, 4)
local sa = src.buffer:view("ubyte", 3, 4)
dr:set(sa)
dg:set(sa)
db:set(sa)
da:set(255)
end
download.download_image(dst)
end)
local title_button = create_button("TITLE", xs[2] + 720-480, ys[7], function()
local title = terrain_state.settings.title or ""
title = title:gsub("%'", "")
title = am.eval_js("prompt('Enter a title:', '"..title.."');")
if title then
if title == "" then
title = nil
end
terrain_state.settings.title = title
focus.regain("Title updated")
else
focus.regain("Title not updated")
end
editor_state.modified = true
end)
local share_button = create_button("EXPORT", xs[2] + 800-480, ys[7], function()
export.export(
editor_state.views.floor,
editor_state.views.ceiling,
editor_state.views.floor_detail,
editor_state.views.ceiling_detail,
editor_state.views.hands,
terrain_state
)
end)
local save_button = create_button("SAVE", xs[2] + 880-480, ys[7], function()
save.save(
editor_state.views.floor,
editor_state.views.ceiling,
editor_state.views.floor_detail,
editor_state.views.ceiling_detail,
editor_state.views.hands,
terrain_state
)
editor_state.modified = false
end)
local help_button = create_button("HELP", xs[2] + 960-480, ys[7], function()
help.show()
end)
local group = am.group()
group:append(
am.bind{P = mat4(1), MV = mat4(1)}
^ am.rect(-1, -1, 1, 1, vec4(0.2, 0.2, 0.2, 1)))
group:append(view_select)
group:append(channels_checkbox)
group:append(brush_select)
group:append(exp1_slider)
group:append(exp2_slider)
group:append(color_picker)
group:append(alpha_slider)
group:append(blend_select)
group:append(flow_slider)
group:append(filter_select)
group:append(fog_color_picker)
group:append(fog_dist_slider)
group:append(ambient_picker)
group:append(diffuse_picker)
group:append(specular_picker)
group:append(shininess_slider)
group:append(speed_slider)
group:append(wireframe_checkbox)
group:append(noclip_checkbox)
group:append(grav_checkbox)
group:append(mesh_select)
group:append(detail_height_slider)
group:append(detail_scale_slider)
group:append(ceiling_y_slider)
group:append(y_scale_slider)
group:append(reset_button)
if am.platform == "html" then
group:append(upload_button)
group:append(download_button)
group:append(title_button)
group:append(share_button)
end
group:append(save_button)
group:append(help_button)
local node =
am.viewport(0, 0, 0, 0)
^ am.bind{
P = math.ortho(0, win.pixel_width, 0, 230),
MV = mat4(1),
}
^ group
function node:update_layout(layout)
local w, h = win.pixel_width, layout.bottom
node"bind".P = math.ortho(0, w, 0, h)
node"viewport".width = w
node"viewport".height = h
end
return node
end
function editor.create(floor, ceiling, floor_detail, ceiling_detail, hands, terrain_state)
local editor_state = {
flow = 0,
views = {
floor = floor,
ceiling = ceiling,
floor_detail = floor_detail,
ceiling_detail = ceiling_detail,
hands = hands,
},
curr_view = "floor",
is_color_view = false,
is_alpha_view = true,
curr_texture = "floor_texture",
curr_brush = 4,
zoom = 1,
edit_mode = false,
brush_fb = am.framebuffer(capture_tex),
modified = false,
}
local brush_size = vec2(0.0005)
local brush_angle = 0
local arrow_scale = 0.01
local editor_bounds = {
l = 0,
b = 0,
w = 0,
h = 0,
}
local draw_brush =
am.color_mask(false, false, false, true)
^ am.bind{
P = mat4(1),
MV = mat4(1),
exp1 = 1,
exp2 = 1,
height_src = vec4(0, 0, 0, 1),
mult_alpha = 0,
}
^ am.translate(0, 0)
^ am.rotate(brush_angle):tag"brush_rotate"
^ am.scale(brush_size):tag"brush_size"
^ {
am.sprite(brush_sprite_specs[editor_state.curr_brush]):tag"brush_sprite"
}
editor_state.blend_mode = "premult"
draw_brush"brush_sprite""blend".mode = editor_state.blend_mode
draw_brush"brush_sprite""use_program".program = draw_shader
editor_state.draw_brush = draw_brush
local tmp_texture = am.texture2d(floor.tex.width, floor.tex.height)
tmp_texture.wrap = "mirrored_repeat"
tmp_texture.filter = "linear"
editor_state.tmp_texture = tmp_texture
local tmp_fb = am.framebuffer(tmp_texture)
local tmp_scene =
am.use_program(am.shaders.texture)
^ am.bind{
P = mat4(1),
MV = mat4(1),
tex = editor_state.views[editor_state.curr_view].tex,
}
^ {
am.bind{
vert = am.rect_verts_3d(-1, -1, 1, 1),
uv = am.rect_verts_2d(0, 0, 1, 1),
}
^ am.draw("triangles", am.rect_indices())
,
draw_brush
}
editor_state.tmp_scene = tmp_scene
local arrow =
am.translate(0, 0)
^ am.rotate(0)
^ am.scale(arrow_scale)
^ am.sprite(sprites.arrow)
local w = sprites.brush1.width/2
local cursor =
am.translate(0, 0)
^ am.rotate(brush_angle)
^ am.scale(brush_size)
^ am.use_program(am.shaders.color2d)
^ am.bind{
color = vec4(0, 1, 1, 1),
vert = am.vec2_array{-w, w, -w, -w, w, -w, w, w, -w, w},
}
^ am.draw"line_strip"
local editor_node =
am.viewport(0, 0, 0, 0)
^ am.use_program(heightmap_shader)
^ am.bind{
P = mat4(1),
MV = mat4(1),
tex = tmp_texture,
}
^ am.scale(1):tag"bg"
^ am.translate(0, 0)
^ {
am.bind{
vert = am.rect_verts_2d(-1, -1, 1, 1),
uv = am.rect_verts_2d(0, 0, 1, 1),
}
^ am.draw("triangles", am.rect_indices())
,
arrow
,
cursor
}
editor_state.editor_node = editor_node
local controls = create_controls(editor_state, terrain_state)
local node =
am.depth_test("always", false)
^ {editor_node, controls}
local flow_accum = 0
local prev_norm_pos = nil
local norm_pos = nil
local start_angle
local rot_pos0 = nil
local x_pos0 = nil
local y_pos0 = nil
local z_pos0 = nil
local rot_mouse_start_pos = nil
local x_mouse_start_pos = nil
local y_mouse_start_pos = nil
local z_mouse_start_pos = nil
local start_size = nil
local start_alpha = nil
node:action(function()
webcam_tex:capture_video()
tmp_fb:render(tmp_scene)
local pos = mouse.pixel_position
prev_norm_pos = norm_pos
norm_pos = normalize_pos(pos, editor_bounds)
if win:key_down"r" then
if win:key_pressed"r" then
rot_pos0 = norm_pos
rot_mouse_start_pos = mouse.pixel_position
mouse.set_visible(false)
start_angle = brush_angle
mouse.clamp = false
end
local angle_change = norm_pos.x - rot_pos0.x
brush_angle = start_angle - angle_change
draw_brush"brush_rotate".angle = brush_angle
elseif win:key_released"r" then
if rot_pos0 then
mouse.set_position(rot_mouse_start_pos)
rot_pos0 = nil
mouse.set_visible(true)
mouse.clamp = true
end
end
if win:key_down"x" then
if win:key_pressed"x" then
x_pos0 = norm_pos
x_mouse_start_pos = mouse.pixel_position
mouse.set_visible(false)
start_size = brush_size
mouse.clamp = false
end
brush_size = brush_size{x = start_size.x + 0.005 * (norm_pos.x - x_pos0.x)}
draw_brush"brush_size".scale2d = brush_size
elseif win:key_released"x" then
if x_pos0 then
mouse.set_position(x_mouse_start_pos)
x_pos0 = nil
mouse.set_visible(true)
mouse.clamp = true
end
end
if win:key_down"y" then
if win:key_pressed"y" then
y_pos0 = norm_pos
y_mouse_start_pos = mouse.pixel_position
mouse.set_visible(false)
start_size = brush_size
mouse.clamp = false
end
brush_size = brush_size{y = start_size.y + 0.005 * (norm_pos.x - y_pos0.x)}
draw_brush"brush_size".scale2d = brush_size
elseif win:key_released"y" then
if y_pos0 then
mouse.set_position(y_mouse_start_pos)
y_pos0 = nil
mouse.set_visible(true)
mouse.clamp = true
end
end