jellyfish.scm 6.72 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
1
2
3
4
5
6
7
8
9
10
11
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; jellyfish livecoding stuff

(define (jelly-compiled code)
  (define addr 0)
  (for-each
   (lambda (v)
     (pdata-set! "x" addr v)
     (set! addr (+ addr 1)))
   code))

Dave Griffiths's avatar
Dave Griffiths committed
12
13
(define jellyfish
  (list
14
15
   (build-jellyfish 4096)
   (build-jellyfish 4096)))
Dave Griffiths's avatar
Dave Griffiths committed
16

Dave Griffiths's avatar
Dave Griffiths committed
17
(define current 0)
Dave Griffiths's avatar
Dave Griffiths committed
18
19

(define (make-jelly speed prim-type code)
Dave Griffiths's avatar
Dave Griffiths committed
20
21
  (let ((p (list-ref jellyfish current)))
    (msg p)
Dave Griffiths's avatar
Dave Griffiths committed
22
23
24
    (with-primitive
     p
     (let ((c (compile-program speed prim-type 1 code)))
25
       ;(disassemble c)
Dave Griffiths's avatar
Dave Griffiths committed
26
       (jelly-compiled c))
Dave Griffiths's avatar
Dave Griffiths committed
27
     (set! current (modulo (+ current 1) (length jellyfish)))
Dave Griffiths's avatar
Dave Griffiths committed
28
29
     p)))

30
31
32
33
34
35
36
37
(define weft
  (make-jelly
   50 prim-triangles
   '(let ((vertex positions-start)
          (t 0)
          (v 0)
          (weft-direction (vector 3 0 0))
          (weft-position (vector 0 0 0)))
Dave Griffiths's avatar
Dave Griffiths committed
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
      (define right-selvedge
        (lambda (gap)
          ;; top corner
          (write! vertex
                  (- (+ weft-position (vector 3 0 0)) gap)
                  (- (+ weft-position (vector 4 1 0)) gap)
                  (- (+ weft-position (vector 3 1 0)) gap))
          (set! vertex (+ vertex 3))
          ;; vertical connection
          (write! vertex
                  (- (+ weft-position (vector 4 1 0)) gap)
                  (- (+ weft-position (vector 3 1 0)) gap)
                  (+ weft-position (vector 3 0 0))
                  (- (+ weft-position (vector 4 1 0)) gap)
                  (+ weft-position (vector 3 0 0))
                  (+ weft-position (vector 4 0 0)))
          (set! vertex (+ vertex 6))
          ;; bottom corner
          (write! vertex
                  (+ weft-position (vector 3 0 0))
                  (+ weft-position (vector 4 0 0))
                  (+ weft-position (vector 3 1 0)))
          (set! vertex (+ vertex 3))
          ))

      (define left-selvedge
        (lambda (gap)
          ;; top corner
          (write! vertex
                  (- (+ weft-position (vector 0 0 0)) gap)
                  (- (+ weft-position (vector -1 1 0)) gap)
                  (- (+ weft-position (vector 0 1 0)) gap))
          (set! vertex (+ vertex 3))
          ;; vertical connection
          (write! vertex
                  (- (+ weft-position (vector -1 1 0)) gap)
                  (- (+ weft-position (vector 0 1 0)) gap)
                  (+ weft-position (vector 0 0 0))
                  (- (+ weft-position (vector -1 1 0)) gap)
                  (+ weft-position (vector 0 0 0))
                  (+ weft-position (vector -1 0 0)))
          (set! vertex (+ vertex 6))
          ;; bottom corner
          (write! vertex
                  (+ weft-position (vector 0 0 0))
                  (+ weft-position (vector -1 0 0))
                  (+ weft-position (vector 0 1 0)))
          (set! vertex (+ vertex 3))
          ))

      (forever
       (set! vertex positions-start)
       (loop (< vertex positions-end)
             (set! weft-position (+ weft-position weft-direction))
             ;; selvedge time?
             (cond
              ((> (mag (*v weft-position (vector 1 0 0))) 40)
               (set! weft-position (- (+ weft-position (vector 0 1.5 0))
                                      weft-direction))
               (set! weft-direction (* weft-direction -1))
               (cond
                ((> 0 weft-direction) (right-selvedge (vector 0 1.5 0)))
                ((< 0 weft-direction) (left-selvedge (vector 0 1.5 0))))))

             (write! vertex
                     weft-position
                     (+ weft-position (vector 3 1 0))
                     (+ weft-position (vector 3 0 0))
                     weft-position
                     (+ weft-position (vector 3 1 0))
                     (+ weft-position (vector 0 1 0)))
             (set! vertex (+ vertex 6)))
       ;;(set! t (+ t 0.01))
       ))))


;;  weave section
;;  top shed
;;  bottom shed
;;  back section

(define warp
Dave Griffiths's avatar
Dave Griffiths committed
121
  (make-jelly
122
   500 prim-triangles
Dave Griffiths's avatar
Dave Griffiths committed
123
   '(let ((vertex positions-start)
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
          (warp-end 0)
          (warp-position (vector 0 0 0))
          (shed 0))

      (define build-quad
        (lambda (tl size)
          (write! vertex
                  tl (+ tl size)
                  (+ tl (*v size (vector 1 0 0)))
                  tl (+ tl size)
                  (+ tl (*v size (vector 0 1 0))))
          (set! vertex (+ vertex 6))))

      (define animate-shed
        (lambda (i v)
          (set! warp-end 0)
          (loop (< warp-end 10)
                (write-add! (- i 6) 0 v 0 0 v v)
                (write-add! i v 0 v v)
                (set! i (+ i 24))
                (set! warp-end (+ warp-end 1)))))


      (set! vertex positions-start)
      ; build 4 segments X warp-ends
      (loop (< warp-end 10)
            (set! warp-position (+ (vector -25 -35 0)
                                   (* (vector 5 0 0) warp-end)))
            (build-quad warp-position (vector 1 35 0))
            (build-quad (+ warp-position (vector 0 35 0)) (vector 1 15 0))
            (build-quad (+ warp-position (vector 0 50 0)) (vector 1 15 0))
            (build-quad (+ warp-position (vector 0 65 0)) (vector 1 25 0))
            (set! warp-end (+ warp-end 1)))

      (forever
       (set! vertex (+ positions-start 12))
       (animate-shed vertex (*v (vector 1 0 0) (* (sincos shed) 1)))
       (set! shed (+ shed 5))

       ))))


 (define weave-scale (vector 0.1 -0.1 0.1))

(with-primitive
 warp
 (hint-unlit)
 (texture (load-texture "thread.png"))
 (scale weave-scale)
 (pdata-index-map! (lambda (i t)
                     (cond
                      ((eqv? (modulo i 6) 0) (vector 0 0 0))
                      ((eqv? (modulo i 6) 1) (vector 1 10 0))
                      ((eqv? (modulo i 6) 2) (vector 1 0 0))
                      ((eqv? (modulo i 6) 3) (vector 0 0 0))
                      ((eqv? (modulo i 6) 4) (vector 1 10 0))
                      ((eqv? (modulo i 6) 5) (vector 0 10 0))
                      )) "t")
 (pdata-map! (lambda (c) (vector 1 1 1)) "c")
 (pdata-map! (lambda (n) (vector 0 0 0)) "n"))


(with-primitive
 weft
 (hint-unlit)
 (texture (load-texture "thread.png"))
 (scale weave-scale)
 (pdata-index-map! (lambda (i t)
                     (cond
                      ((eqv? (modulo i 6) 0) (vector 0 0 0))
                      ((eqv? (modulo i 6) 1) (vector 1 1 0))
                      ((eqv? (modulo i 6) 2) (vector 1 0 0))
                      ((eqv? (modulo i 6) 3) (vector 0 0 0))
                      ((eqv? (modulo i 6) 4) (vector 1 1 0))
                      ((eqv? (modulo i 6) 5) (vector 0 1 0))
                      )) "t")
 (pdata-map! (lambda (c) (vector 1 1 1)) "c")
 (pdata-map! (lambda (n) (vector 0 0 0)) "n"))

(every-frame
 (with-primitive
  weft
  (when
   (< (vy (vtransform  (pdata-ref "x" 11) (get-transform))) 0)
   (translate (vector 0 -0.1 0)))))