generated from calcit-lang/respo-calcit-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.cirru
1524 lines (1523 loc) · 69.8 KB
/
compact.cirru
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
{} (:package |lagopus)
:configs $ {} (:init-fn |lagopus.main/main!) (:reload-fn |lagopus.main/reload!) (:version |0.5.9)
:modules $ [] |memof/ |quaternion/
:entries $ {}
:files $ {}
|lagopus.alias $ %{} :FileEntry
:defs $ {}
|buffer-format $ %{} :CodeEntry (:doc |)
:code $ quote
defn buffer-format (format)
case-default format
if (string? format) format $ do (js/console.warn "\"Unknown format" format) "\"float32"
:float32 "\"float32"
:float32x2 "\"float32x2"
:float32x3 "\"float32x3"
:float32x4 "\"float32x4"
:uint32 "\"uint32"
|collect-array! $ %{} :CodeEntry (:doc |)
:code $ quote
defn collect-array! (indices collect!)
if (list? indices)
&doseq (x indices) (collect-array! x collect!)
collect! indices
|collect-from-records! $ %{} :CodeEntry (:doc |)
:code $ quote
defn collect-from-records! (data field idx write!)
cond
list? data
&doseq (x data) (collect-from-records! x field idx write!)
(tuple? data)
do
if
not= :vertex $ nth data 0
js/console.warn "\"expected :vertex tag" data
let
xs $ nth data (inc idx)
if (tuple? xs)
tag-match xs $
:v3 x y z
do (write! x) (write! y) (write! z)
if (list? xs)
&doseq (x xs) (write! x)
write! xs
(map? data)
let
xs $ &map:get data field
if (tuple? xs)
tag-match xs $
:v3 x y z
do (write! x) (write! y) (write! z)
if (list? xs)
&doseq (x xs) (write! x)
write! xs
true $ raise "\"unknown data"
|count-recursive $ %{} :CodeEntry (:doc |)
:code $ quote
defn count-recursive (xs)
if (list? xs)
reduce xs 0 $ fn (acc y)
&+ acc $ count-recursive y
, 1
|expand-attr $ %{} :CodeEntry (:doc |)
:code $ quote
defn expand-attr (x)
if (tuple? x)
{}
:field $ &tuple:nth x 1
:format $ &tuple:nth x 0
, x
|group $ %{} :CodeEntry (:doc |)
:code $ quote
defn group (a & children)
if
not $ or (nil? a)
= a $ &{}
js/console.warn "\"group options is not used at current" a
lagopus/group nil & children
|inject-shader-snippets $ %{} :CodeEntry (:doc |)
:code $ quote
defn inject-shader-snippets (code)
-> code (.!replace "\"#import lagopus::simplex" wgsl-simplex) (.!replace "\"#import lagopus::perspective" wgsl-perspective) (.!replace "\"#import lagopus::colors" wgsl-colors) (.!replace "\"#import lagopus::rand" wgsl-rand) (.!replace "\"#import lagopus::rotation" wgsl-rotation) (.!replace "\"#import lagopus::hsluv" wgsl-hsluv)
|make-empty-js-arrays $ %{} :CodeEntry (:doc "|create nested array of js, as placeholder for attributes data")
:code $ quote
defn make-empty-js-arrays (n)
-> (new js/Array n) (.!fill 0)
.!map $ fn (x i _) (js-array)
|object $ %{} :CodeEntry (:doc |)
:code $ quote
defn object (options)
let
attrs-list $ map (&map:get options :attrs-list) expand-attr
data $ &map:get options :data
vertices-size $ count-recursive data
buffers $ js-array &
map-indexed attrs-list $ fn (idx attr)
let
buffer $ newBufferFormatLength
buffer-format $ &map:get attr :format
, vertices-size
*idx $ atom 0
field $ &map:get attr :field
write! $ fn (x)
let
idx @*idx
aset buffer idx x
swap! *idx inc
collect-from-records! data field idx write!
; js/console.log @*idx $ .-length buffer
if
not= @*idx $ .-length buffer
eprintln "\"buffer size guessed incorrectly"
, buffer
; js/console.log vertices-size buffers
createRenderer $ js-object
:shader $ inject-shader-snippets (&map:get options :shader)
:topology $ turn-string (&map:get options :topology)
:attrsList $ to-js-data attrs-list
:verticesLength vertices-size
:vertices buffers
:hitRegion nil
:indices $ if-let
indices $ &map:get options :indices
u32buffer $ let
*arr $ js-array
collect! $ fn (x) (.!push *arr x )
collect-array! indices collect!
, *arr
:getParams $ &map:get options :get-params
:textures $ js-array &
either (&map:get options :textures) ([])
:label $ &map:get options :label
:computeOptions $ &map:get options :compute-options
|object-writer $ %{} :CodeEntry (:doc |)
:code $ quote
defn object-writer (options)
let
attrs-list $ map (&map:get options :attrs-list) expand-attr
writer $ &map:get options :writer
bundles $ noted "\"actually stores data in this nested list..."
make-empty-js-arrays $ count attrs-list
*counter $ atom 0
count! $ fn (x) (swap! *counter &+ x)
collect! $ fn (chunk)
&doseq (record chunk) (count! 1)
let
*counted $ atom 0
&doseq (attr attrs-list)
let
idx @*counted
arr $ js-get bundles idx
data $ do
assert "\"expected tuple" $ tuple? record
if
not= :vertex $ nth record 0
js/console.warn "\"expected :vertex tag" record
nth record $ inc idx
if (tuple? data)
tag-match data $
:v3 x y z
do $ .!push arr x y z
if (list? data)
&doseq (d data) (.!push arr d)
.!push arr data
swap! *counted inc
buffers $ do
noted "\"call writer function to fill data into buffer" $ writer collect!
js-array & $ map-indexed attrs-list
fn (idx attr)
newBufferFormatArray
buffer-format $ &map:get attr :format
js-get bundles idx
; js/console.log @*counter buffers
createRenderer $ js-object
:shader $ inject-shader-snippets (&map:get options :shader)
:topology $ turn-string (&map:get options :topology)
:attrsList $ to-js-data attrs-list
:verticesLength @*counter
:vertices buffers
:hitRegion nil
:indices $ if-let
indices $ &map:get options :indices
u32buffer $ let
*arr $ js-array
collect! $ fn (x) (.!push *arr x)
collect-array! indices collect!
, *arr
:getParams $ &map:get options :get-params
:textures $ js-array &
either (&map:get options :textures) ([])
:label $ &map:get options :label
:computeOptions $ &map:get options :compute-options
|wgsl-colors $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-colors $ inline-shader "\"lagopus-colors"
|wgsl-hsluv $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-hsluv $ inline-shader "\"lagopus-hsluv"
|wgsl-perspective $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-perspective $ inline-shader "\"lagopus-perspective"
|wgsl-rand $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-rand $ inline-shader "\"lagopus-rand"
|wgsl-rotation $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-rotation $ inline-shader "\"lagopus-rotation"
|wgsl-simplex $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-simplex $ inline-shader "\"lagopus-simplex"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.alias $ :require
"\"@triadica/lagopus" :refer $ createRenderer u32buffer newBufferFormatLength newBufferFormatArray
"\"@triadica/lagopus" :as lagopus
lagopus.config :refer $ inline-shader
|lagopus.comp.button $ %{} :FileEntry
:defs $ {}
|comp-button $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-button (props on-click)
compButton
-> props
update :position $ fn (p) (.to-js p)
to-js-data
, on-click
|comp-drag-point $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-drag-point (props on-drag)
let
color $ &map:get props :color
position $ &map:get props :position
compDragPoint
js-object
:color $ to-js-data color
:position $ if (tuple? position)
js-array (nth position 1) (nth position 2) (nth position 3)
if (list? position) (to-js-data position)
do (eprintln "\"unknown position" position) (js-array 10 0 0)
fn (move d!)
on-drag
v3 (.-0 move) (.-1 move) (.-2 move)
, d!
|comp-flat-button $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-flat-button (props on-click)
compFlatButton
-> props
update :position $ fn (p) (.to-js p)
to-js-data
, on-click
|comp-slider $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-slider (props on-slide)
compSlider (to-js-data props)
fn (move d!)
on-slide
complex (.-0 move) (.-1 move)
, d!
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.button $ :require
"\"@triadica/lagopus" :refer $ compButton compSlider compDragPoint
"\"@triadica/lagopus/lib/comp/button.mjs" :refer $ compFlatButton
quaternion.vector :refer $ v3
quaternion.complex :refer $ complex
|lagopus.comp.container $ %{} :FileEntry
:defs $ {}
|color-default $ %{} :CodeEntry (:doc |)
:code $ quote
def color-default $ [] 1 0 0 1
|comp-bubbles-demo $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-bubbles-demo () $ let
area 2000
comp-bubbles $ {}
:bubbles $ -> (range 600)
map $ fn (idx)
[] (rand-shift 0 area) (rand-shift 0 area) (rand-shift 0 area)
+ 6 $ rand 120
|comp-city $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-city () $ object
{} (:shader city-wgsl)
:topology $ do :line-strip :triangle-list
:attrs-list $ [] (:: :float32x2 :position) (:: :float32 :normal-idx) (:: :float32 :idx)
:data $ let
size 40
d 160
-> (range-bothway size)
map $ fn (x)
-> (range-bothway size)
map $ fn (y)
let
x0 $ &* d x
y0 $ &* d y
p0 $ [] x0 y0
[]
[] (:: :vertex p0 0 0) (:: :vertex p0 0 1) (:: :vertex p0 0 2)
[] (:: :vertex p0 0 0) (:: :vertex p0 0 2) (:: :vertex p0 0 3)
[] (:: :vertex p0 1 0) (:: :vertex p0 1 1) (:: :vertex p0 1 5)
[] (:: :vertex p0 1 0) (:: :vertex p0 1 5) (:: :vertex p0 1 4)
[] (:: :vertex p0 2 1) (:: :vertex p0 2 2) (:: :vertex p0 2 6)
[] (:: :vertex p0 2 1) (:: :vertex p0 2 6) (:: :vertex p0 2 5)
[] (:: :vertex p0 3 2) (:: :vertex p0 3 3) (:: :vertex p0 3 6)
[] (:: :vertex p0 3 3) (:: :vertex p0 3 7) (:: :vertex p0 3 6)
[] (:: :vertex p0 4 0) (:: :vertex p0 4 3) (:: :vertex p0 4 4)
[] (:: :vertex p0 4 3) (:: :vertex p0 4 4) (:: :vertex p0 4 7)
[] (:: :vertex p0 5 4) (:: :vertex p0 5 5) (:: :vertex p0 5 6)
[] (:: :vertex p0 5 4) (:: :vertex p0 5 6) (:: :vertex p0 5 7)
|comp-container $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-container (store textures-dict)
let
cursor $ []
states $ :states store
group nil (memof1-call comp-tabs)
case-default (:tab store) (group nil)
:axis $ comp-axis
{} (:n 20) (:unit 20)
:mountains $ memof1-call comp-mountains
:city $ memof1-call comp-city
:bends $ group nil
:cube $ comp-cube
{}
:position $ v3 40 0 0
:radius 40
:ribbon $ comp-ribbon
:necklace $ comp-necklace
:sphere $ comp-sphere
{} (; :topology :line-strip) (:iteration 4) (:radius 160)
:color $ [] 0.6 0.9 0.7
:plate $ comp-plate
{} (; :topology :line-strip) (:iteration 20) (:radius 160)
:color $ [] 0.04 0.8 0.6
:transformer $ fn (i)
v+ i $ v3 0 0 -10
; :x-direction $ v3 1 0 0
; :y-direction $ v3 0 1 0
:chromatism 0.14
:control $ comp-control-demo (>> states :control)
:stitch $ comp-stitch-demo
:bubbles $ comp-bubbles-demo
:triangles $ comp-triangles-demo
:image $ comp-image-demo textures-dict
:kaleidoscope $ comp-kaleidoscope textures-dict
|comp-control-demo $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-control-demo (states)
let
cursor $ :cursor states
state $ either (:data states)
{} $ :pos ([] 60 0 0)
group nil
comp-slider
{} $ :position ([] 0 0 0)
fn (change on-slide) (js/console.log "\"Slide" change)
comp-drag-point
{}
:position $ :pos state
:color $ [] 0.6 0.6 1.0 1.0
fn (move d!)
d! $ : :state cursor (assoc state :pos move)
comp-flat-button
{}
:position $ v3 100 20 0
:color $ [] 0.9 0.4 0.5 1
:size 40
fn (e d!) (js/console.log "\"CLICKED")
|comp-mountains $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-mountains () $ object
{} (:shader mountains-wgsl)
:topology $ do :line-strip :triangle-list
:attrs-list $ []
{} (:field :position) (:format "\"float32x2")
:data $ let
size 80
d 32
-> (range-bothway size)
map $ fn (x)
-> (range-bothway size)
map $ fn (y)
let
x0 $ &* d x
x1 $ &+ x0 d
y0 $ &* d y
y1 $ &+ y0 d
[]
[]
:: :vertex $ [] x0 y0
:: :vertex $ [] x1 y0
:: :vertex $ [] x1 y1
[]
:: :vertex $ [] x0 y0
:: :vertex $ [] x1 y1
:: :vertex $ [] x0 y1
|comp-necklace $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-necklace () $ comp-spots
{} (; :topology :line-strip) (:radius 6) (:vertex-count 8) (:shift 12)
:points $ -> (range 80)
map $ fn (idx)
let
r $ * idx 4
[] r
* r $ cos (* 0.1129 idx)
* r $ sin (* 0.123 idx)
|comp-ribbon $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-ribbon () $ comp-curves
{} (; :topology :line-strip)
:curves $ []
-> (range 400)
map $ fn (idx)
let
angle $ * 0.1 idx
r 40
{}
:position $ v3
* r $ cos angle
* 0.6 idx
* r $ sin angle
:width 2
|comp-stitch-demo $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-stitch-demo () $ group nil
comp-stitch $ {}
:chars $ [] 0xf2dfea34 0xc3c4a59d 0x88737645
|comp-tabs $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-tabs () $ group nil
comp-button
{}
:position $ v3 0 260 0
:color $ [] 0.5 0.5 0.9 1
:size 20
fn (e d!)
d! $ : tab :axis
comp-button
{}
:position $ v3 40 260 0
:color $ [] 0.9 0.4 0.5 1
:size 20
fn (e d!)
d! $ : tab :mountains
comp-button
{}
:position $ v3 80 260 0
:color $ [] 0.8 0.9 0.2 1
:size 20
fn (e d!)
d! $ : tab :city
comp-button
{}
:position $ v3 120 260 0
:color $ [] 0.3 0.9 0.2 1
:size 20
fn (e d!)
d! $ : tab :cube
comp-button
{}
:position $ v3 160 260 0
:color $ [] 0.8 0.0 0.9 1
:size 20
fn (e d!)
d! $ : tab :ribbon
comp-button
{}
:position $ v3 200 260 0
:color $ [] 0.2 0.9 0.6 1
:size 20
fn (e d!)
d! $ : tab :necklace
comp-button
{}
:position $ v3 240 260 0
:color $ [] 0.2 0.9 0.6 1
:size 20
fn (e d!)
d! $ : tab :sphere
comp-button
{}
:position $ v3 280 260 0
:color $ [] 0.9 0.4 0.6 1
:size 20
fn (e d!)
d! $ : tab :plate
comp-button
{}
:position $ v3 20 220 0
:color $ [] 0.7 0.8 0.9 1
:size 20
fn (e d!)
d! $ : tab :control
comp-button
{}
:position $ v3 60 220 0
:color $ [] 0.9 0.7 0.6 1
:size 20
fn (e d!)
d! $ : tab :stitch
comp-button
{}
:position $ v3 100 220 0
:color $ [] 0.9 0.3 0.8 1
:size 20
fn (e d!)
d! $ : tab :bubbles
comp-button
{}
:position $ v3 140 220 0
:color $ [] 0.1 0.6 0.8 1
:size 20
fn (e d!)
d! $ : tab :triangles
comp-button
{}
:position $ v3 180 220 0
:color $ [] 0.9 0.2 0.99 1
:size 20
fn (e d!)
d! $ : tab :image
comp-button
{}
:position $ v3 220 220 0
:color $ [] 0.9 0.8 0.99 1
:size 20
fn (e d!)
d! $ : tab :kaleidoscope
|comp-triangles-demo $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-triangles-demo () $ let
width 2
group nil
; comp-polylines $ {} (; :topology :line-strip)
:writer $ fn (write!)
write! $ []
: vertex (v3 0 0 0) width
: vertex (v3 100 100 0) width
, break-mark
: vertex (v3 0 0 10) width
: vertex (v3 200 0 10) width
: vertex (v3 200 20 0) width
: vertex (v3 100 40 0) width
: vertex (v3 100 20 200) width
, break-mark
comp-polylines-marked $ {} (; :topology :line-strip)
:writer $ fn (write!)
write! $ []
: vertex (v3 0 0 0) width 0
: vertex (v3 100 100 0) width 0
, break-mark
: vertex (v3 0 0 10) width 2
: vertex (v3 200 0 10) width 2
: vertex (v3 200 20 0) width 2
: vertex (v3 100 40 0) width 2
: vertex (v3 100 20 200) width 2
, break-mark
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.container $ :require
lagopus.alias :refer $ group object
"\"../shaders/mountains.wgsl" :default mountains-wgsl
"\"../shaders/city.wgsl" :default city-wgsl
lagopus.comp.button :refer $ comp-button comp-slider comp-drag-point comp-flat-button
lagopus.comp.curves :refer $ comp-curves comp-axis comp-polylines comp-polylines-marked break-mark
lagopus.comp.spots :refer $ comp-spots comp-bubbles
memof.once :refer $ memof1-call
quaternion.vector :refer $ v+ v3
lagopus.comp.cube :refer $ comp-cube
lagopus.comp.sphere :refer $ comp-sphere
lagopus.comp.plate :refer $ comp-plate
lagopus.cursor :refer $ >>
lagopus.comp.stitch :refer $ comp-stitch
"\"@calcit/std" :refer $ rand-shift rand
lagopus.config :refer $ inline-shader
lagopus.comp.image :refer $ comp-image-demo comp-kaleidoscope
|lagopus.comp.cube $ %{} :FileEntry
:defs $ {}
|comp-cube $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-cube (options)
let
base $ either (&map:get options :position) (v3 0 0 0)
radius $ either (&map:get options :radius) 80
object $ {} (:shader wgsl-cube)
:topology $ do :line-strip :triangle-list
:attrs-list $ [] (:: :float32x3 :position) (:: :float32x3 :metrics)
:data $ []
:: :vertex
&v+ base $ v-scale (v3 -1 -1 -1) radius
v3 -1 -1 -1
:: :vertex
&v+ base $ v-scale (v3 -1 1 -1) radius
v3 -1 1 -1
:: :vertex
&v+ base $ v-scale (v3 -1 1 1) radius
v3 -1 1 1
:: :vertex
&v+ base $ v-scale (v3 -1 -1 1) radius
v3 -1 -1 1
:: :vertex
&v+ base $ v-scale (v3 1 -1 -1) radius
v3 1 -1 -1
:: :vertex
&v+ base $ v-scale (v3 1 1 -1) radius
v3 1 1 -1
:: :vertex
&v+ base $ v-scale (v3 1 1 1) radius
v3 1 1 1
:: :vertex
&v+ base $ v-scale (v3 1 -1 1) radius
v3 1 -1 1
:indices $ [] ([] 0 1 2 0 2 3) ([] 0 1 5 0 4 5) ([] 1 2 6 1 6 5) ([] 2 3 6 3 6 7) ([] 0 3 4 3 4 7) ([] 4 5 6 4 6 7)
|wgsl-cube $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-cube $ inline-shader "\"cube"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.cube $ :require
lagopus.config :refer $ inline-shader
lagopus.alias :refer $ object
quaternion.vector :refer $ &v+ v-cross v-scale v-dot &v- v+ v3
|lagopus.comp.curves $ %{} :FileEntry
:defs $ {}
|break-mark $ %{} :CodeEntry (:doc |)
:code $ quote
def break-mark $ :: :break
|build-curve-points $ %{} :CodeEntry (:doc |)
:code $ quote
defn build-curve-points (points curve-ratio)
let
size $ count points
->
range $ dec size
map $ fn (idx)
let
idx+1 $ inc idx
p-raw $ nth points idx
q-raw $ nth points idx+1
q2-raw $ nth points (inc idx+1)
p $ if (tuple? p-raw) (nth p-raw 1) (&map:get p-raw :position)
q $ if (tuple? q-raw) (nth q-raw 1)
if (map? q-raw) (&map:get q-raw :position)
q2 $ if (tuple? q2-raw) (nth q2-raw 1)
if (list? q2-raw)
if (map? q2-raw) (&map:get q2-raw :position)
direction $ &v- q p
direction2 $ if (some? q2) (&v- q2 q) direction
p-width $ either
if (tuple? p-raw) (nth p-raw 2)
if (map? p-raw) (&map:get p-raw :width)
, 1
q-width $ either
if (tuple? q-raw) (nth q-raw 2)
if (map? q-raw) (&map:get q-raw :width)
, 1
ratio $ &/ idx size
ratio+1 $ &/ idx+1 size
[] (:: :vertex p 0 direction curve-ratio idx p-width) (:: :vertex q 0 direction2 curve-ratio idx+1 q-width) (:: :vertex p 1 direction2 curve-ratio idx p-width) (:: :vertex q 0 direction2 curve-ratio idx+1 p-width) (:: :vertex q 1 direction2 curve-ratio idx+1 q-width) (:: :vertex p 1 direction curve-ratio idx p-width)
|build-polyline-points $ %{} :CodeEntry (:doc |)
:code $ quote
defn build-polyline-points (*prev p write!)
tag-match p
:break
do $ reset! *prev nil
(:vertex position width)
if-let (prev @*prev)
let
older $ :older prev
reset! *prev $ {} (:position position) (:older older)
:width $ :width prev
if (some? older)
let
p older
q $ :position prev
q2 position
direction $ &v- q p
direction2 $ &v- q2 q
p-width $ :width prev
q-width width
write! $ [] (:: :vertex q 0 direction p-width) (:: :vertex q2 0 direction2 q-width) (:: :vertex q 1 direction2 p-width) (:: :vertex q2 0 direction2 p-width) (:: :vertex q2 1 direction2 q-width) (:: :vertex q 1 direction p-width)
let
q $ :position prev
q2 position
direction $ &v- q2 q
p-width $ :width prev
q-width width
write! $ [] (:: :vertex q 0 direction p-width) (:: :vertex q2 0 direction q-width) (:: :vertex q 1 direction p-width) (:: :vertex q2 0 direction p-width) (:: :vertex q2 1 direction q-width) (:: :vertex q 1 direction p-width)
do $ reset! *prev
{} (:position position) (:older nil) (:width width)
|build-polyline-points-marked $ %{} :CodeEntry (:doc |)
:code $ quote
defn build-polyline-points-marked (*prev p write!)
tag-match p
:break
do $ reset! *prev nil
(:vertex position width mark)
if-let (prev @*prev)
let
older $ &map:get prev :older
reset! *prev $ {} (:position position) (:older older) (:mark mark)
:width $ :width prev
if (some? older)
let
p older
q $ &map:get prev :position
prev-mark $ &map:get prev :mark
q2 position
direction $ &v- q p
direction2 $ &v- q2 q
p-width $ &map:get prev :width
q-width width
write! $ [] (:: :vertex q 0 direction p-width prev-mark) (:: :vertex q2 0 direction2 q-width mark) (:: :vertex q 1 direction2 p-width prev-mark) (:: :vertex q2 0 direction2 p-width mark) (:: :vertex q2 1 direction2 q-width mark) (:: :vertex q 1 direction p-width prev-mark)
let
q $ &map:get prev :position
prev-mark $ &map:get prev :mark
q2 position
direction $ &v- q2 q
p-width $ &map:get prev :width
q-width width
write! $ [] (:: :vertex q 0 direction p-width prev-mark) (:: :vertex q2 0 direction q-width mark) (:: :vertex q 1 direction p-width prev-mark) (:: :vertex q2 0 direction p-width mark) (:: :vertex q2 1 direction q-width mark) (:: :vertex q 1 direction p-width prev-mark)
do $ reset! *prev
{} (:position position) (:older nil) (:mark mark) (:width width)
|char-x $ %{} :CodeEntry (:doc |)
:code $ quote
def char-x $ count-hex
reverse $ concat ([] true false true false false false false true) ([] false false true false true true false false) ([] false false false true true false true false) ([] false true false false false false true false)
|char-y $ %{} :CodeEntry (:doc |)
:code $ quote
def char-y $ count-hex
reverse $ concat ([] true false true false false false false true) ([] false false true false true true false false) ([] false false false false true true false false) ([] false false false false true true false false)
|char-z $ %{} :CodeEntry (:doc |)
:code $ quote
def char-z $ count-hex
reverse $ concat ([] true false true false true true false true) ([] false false false false false true false false) ([] false false false true false false false false) ([] false true true true true true true true true)
|comp-axis $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-axis (? options)
let
n $ either (get options :n) 20
unit $ either (get options :unit) 20
w $ either (get options :width) 1
group nil
comp-polylines-marked $ {} (; :topology :line-strip)
:shader $ inline-shader "\"axis"
:writer $ fn (write!)
&doseq
idx $ range-bothway n
write! $ :: :vertex
v3 (* idx unit) 0 0
, w 0
write! break-mark
&doseq
idx $ range-bothway n
write! $ :: :vertex
v3 0 (* idx unit) 0
, w 1
write! break-mark
&doseq
idx $ range-bothway n
write! $ :: :vertex
v3 0 0 $ * idx unit
, w 2
write! break-mark
; comp-stitch $ {} (:size 16)
:position $ v3 400 0 0
:chars $ [] char-x
; comp-stitch $ {} (:size 16)
:position $ v3 0 400 0
:chars $ [] char-y
; comp-stitch $ {} (:size 16)
:position $ v3 0 0 400
:chars $ [] char-z
|comp-curves $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-curves (options)
let
curves $ either (&map:get options :curves) ([])
object $ {}
:shader $ either (&map:get options :shader) wgsl-curves
:topology $ either (&map:get options :topology) :triangle-list
:attrs-list $ [] (:: :float32x3 :position) (:: :uint32 :brush) (:: :float32x3 :direction) (:: :float32 :curve_ratio) (:: :uint32 :color_index) (:: :float32 :width)
:data $ let
size $ count curves
map-indexed curves $ fn (idx c)
build-curve-points c $ &/ idx size
|comp-polylines $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-polylines (options)
let
chunk-writer! $ either (&map:get options :writer)
fn (_) (js/console.warn "\"expected polylines writer")
object-writer $ {}
:shader $ either (&map:get options :shader) wgsl-polylines
:topology $ either (&map:get options :topology) :triangle-list
:attrs-list $ [] (: float32x3 :position) (: uint32 :brush) (: float32x3 :direction) (: float32 :width)
:writer $ fn (write!)
let
*prev $ atom nil
collect! $ fn (p)
if (list? p)
&doseq (x p) (build-polyline-points *prev x write!)
build-polyline-points *prev p write!
chunk-writer! collect!
:get-params $ &map:get options :get-params
:compute-options $ &map:get options :compute-options
|comp-polylines-marked $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-polylines-marked (options)
let
chunk-writer! $ either (&map:get options :writer)
fn (_) (js/console.warn "\"expected polylines writer")
object-writer $ {}
:shader $ either (&map:get options :shader) wgsl-polylines-marked
:topology $ either (&map:get options :topology) :triangle-list
:attrs-list $ [] (: float32x3 :position) (: uint32 :brush) (: float32x3 :direction) (: float32 :width) (: float32 :mark)
:writer $ fn (write!)
let
*prev $ atom nil
collect! $ fn (p)
if (list? p)
&doseq (x p) (build-polyline-points-marked *prev x write!)
build-polyline-points-marked *prev p write!
chunk-writer! collect!
:get-params $ &map:get options :get-params
:compute-options $ &map:get options :compute-options
|count-hex $ %{} :CodeEntry (:doc |)
:code $ quote
defn count-hex (xs)
-> xs
map-indexed $ fn (idx v)
* (if v 1 0) (pow 2 idx)
reduce 0 &+
|wgsl-curves $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-curves $ inline-shader "\"curves"
|wgsl-polylines $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-polylines $ inline-shader "\"polylines"
|wgsl-polylines-marked $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-polylines-marked $ inline-shader "\"polylines-marked"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.curves $ :require
lagopus.config :refer $ inline-shader
lagopus.alias :refer $ object group object-writer
quaternion.vector :refer $ &v+ v-cross v-scale v-dot &v- v3
lagopus.comp.stitch :refer $ comp-stitch
|lagopus.comp.image $ %{} :FileEntry
:defs $ {}
|comp-image-demo $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-image-demo (textures-dict)
object $ {} (:label :image) (:shader wgsl-image)
:topology $ do :line-strip :triangle-list
:attrs-list $ []
{} (:field :position) (:format "\"float32x3")
{} (:field :uv) (:format "\"float32x2")
:data $ []
[]
:: :vertex ([] 0 0 0) ([] 0 0)
:: :vertex ([] 100 0 0) ([] 1 0)
:: :vertex ([] 100 100 0) ([] 1 1)
[]
:: :vertex ([] 100 100 0) ([] 1 1)
:: :vertex ([] 0 100 0) ([] 0 1)
:: :vertex ([] 0 0 0) ([] 0 0)
:textures $ [] (get textures-dict :tiye)
|comp-kaleidoscope $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-kaleidoscope (textures-dict)
object $ {} (:label :image) (:shader wgsl-kaleidoscope)
:topology $ do :line-strip :triangle-list
:attrs-list $ []
{} (:field :position) (:format "\"float32x3")
{} (:field :uv) (:format "\"float32x2")
:data $ []
[]
:: :vertex ([] 0 0 0) ([] 0 0)
:: :vertex ([] 100 0 0) ([] 1 0)
:: :vertex ([] 100 100 0) ([] 1 1)
[]
:: :vertex ([] 100 100 0) ([] 1 1)
:: :vertex ([] 0 100 0) ([] 0 1)
:: :vertex ([] 0 0 0) ([] 0 0)
:textures $ [] (get textures-dict :tiye)
|wgsl-image $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-image $ inline-shader "\"image"
|wgsl-kaleidoscope $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-kaleidoscope $ inline-shader "\"kaleidoscope"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.image $ :require
lagopus.alias :refer $ group object
lagopus.comp.button :refer $ comp-button comp-slider comp-drag-point
memof.once :refer $ memof1-call
quaternion.vector :refer $ v+ v3
lagopus.cursor :refer $ >>
"\"@calcit/std" :refer $ rand-shift rand
lagopus.config :refer $ inline-shader
|lagopus.comp.plate $ %{} :FileEntry
:defs $ {}
|calc-ratio $ %{} :CodeEntry (:doc |)
:code $ quote
defn calc-ratio (p)
let
x $ nth p 0
y $ nth p 1
px $ &+ x (&* 0.5 y)
py $ * 0.5 sqrt-3 y
radian $ js/Math.atan2 py px
&* 0.5 $ &* sqrt-3
cos $ &- radian (&/ &PI 6)
|comp-plate $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-plate (options)
let
base $ either (&map:get options :position) (v3 0 0 0)
x-direction $ either (&map:get options :x-direction) (v3 1 0 0)
y-direction $ either (&map:get options :y-direction) (v3 0 1 0)
radius $ either (&map:get options :radius) 80
iteration $ either (&map:get options :iteration) 4
color $ either (&map:get options :color) ([] 0.6 0.8 0.76)
*counter $ atom 0
r-unit $ &/ radius iteration
transformer $ either (&map:get options :transformer) identity
chromatism $ either (&map:get options :chromatism) 0.1
object $ {}
:shader $ either (&map:get options :shader) wgsl-plate
:topology $ either (&map:get options :topology) :triangle-list
:attrs-list $ [] (:: :float32x3 :position) (:: :uint32 :idx)
:data $ -> (range 6)
map $ fn (section-idx)
let
angle $ &* section-idx (&/ &PI 3)
angle-next $ &* (inc section-idx) (&/ &PI 3)
unit-x $ v-scale
&v+
v-scale x-direction $ cos angle
v-scale y-direction $ sin angle
, r-unit
unit-y $ v-scale
&v+
v-scale x-direction $ cos angle-next
v-scale y-direction $ sin angle-next
, r-unit
-> (range iteration)
map $ fn (idx)
->
range $ &- iteration idx
map $ fn (j)
let
c @*counter
c1 $ inc c
p0 $ [] idx j
p1 $ [] (inc idx) j
p2 $ [] idx (inc j)
p3 $ [] (inc idx) (inc j)
s0 $ calc-ratio p0
s1 $ calc-ratio p1
s2 $ calc-ratio p2
s3 $ calc-ratio p3
ps0 $ &v+ base
transformer $ &v+
v-scale unit-x $ &* s0 (nth p0 0)
v-scale unit-y $ &* s0 (nth p0 1)
ps1 $ &v+ base
transformer $ &v+
v-scale unit-x $ &* s1 (nth p1 0)
v-scale unit-y $ &* s1 (nth p1 1)
ps2 $ &v+ base
transformer $ &v+
v-scale unit-x $ &* s2 (nth p2 0)
v-scale unit-y $ &* s2 (nth p2 1)
ps3 $ &v+ base
transformer $ &v+
v-scale unit-x $ &* s3 (nth p3 0)
v-scale unit-y $ &* s3 (nth p3 1)
swap! *counter &+ 2
[] (:: :vertex ps0 c) (:: :vertex ps1 c) (:: :vertex ps2 c)
if
&< (&+ idx j) (dec iteration)
[] (:: :vertex ps3 c1) (:: :vertex ps1 c1) (:: :vertex ps2 c1)
[]
:get-params $ fn () (js-array & color chromatism)
|sqrt-3 $ %{} :CodeEntry (:doc |)
:code $ quote
def sqrt-3 $ sqrt 3
|wgsl-plate $ %{} :CodeEntry (:doc |)
:code $ quote
def wgsl-plate $ inline-shader "\"plate"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns lagopus.comp.plate $ :require
lagopus.config :refer $ inline-shader