-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-shaders.ss
More file actions
353 lines (328 loc) · 12.1 KB
/
user-shaders.ss
File metadata and controls
353 lines (328 loc) · 12.1 KB
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
(define-shader constant ()
(color-mul Os Cs))
(define-shader matte ([Ka 1] [Kd 1])
(let ([Nf (faceforward (vec-normalize N) I)])
(color-mul Os Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))))
(define-shader metal ([Ka 1] [Ks 1] [roughness .05])
(let* ([Nf (faceforward (vec-normalize N) I)]
[V (vec-normalize (vec-reverse I))])
(color-mul Os Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks)))))
(define-shader shiny-metal ([Ka 1] [Kd .1] [Ks 1] [roughness .2] [Kr .8])
(let* ([Nf (faceforward (vec-normalize N) I)]
[IN (vec-normalize I)]
[V (vec-reverse IN)]
[R (reflect IN Nf)])
(color-mul Os Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks)
(sample-environment P R Kr)))))
(define-shader plastic ([Ks .5] [Kd .5] [Ka 1] [roughness .1]
[specularcolor white])
(let ([Nf (faceforward (vec-normalize N) I)]
[V (vec-normalize (vec-reverse I))])
(color-add
(color-mul Os Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
(color-mul
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks)
specularcolor))))
(define-shader stripes ([Kd 1] [Ka .5] [frequency 10] [blackcolor black])
(let ([pnt (point->surface ($object) P)]
[Nf (faceforward (vec-normalize N) I)])
(let* ([t (vec-j pnt)]
[tmod (mod (* t frequency) 1)])
(color-mul Os
(if (< tmod .5)
Cs
blackcolor)
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd))))))
(define-shader checker ([Kd 1] [Ka .5] [frequency 4] [blackcolor black])
(let ([pnt (point->surface ($object) P)]
[Nf (faceforward (vec-normalize N) I)])
(let ([xmod (mod (* (vec-i pnt) frequency) (+ 1 EPSILON))]
[ymod (mod (* (vec-j pnt) frequency) (+ 1 EPSILON))]
[zmod (mod (* (vec-k pnt) frequency) (+ 1 EPSILON))])
(color-mul Os
(if (< zmod .5)
(if (< xmod .5)
(if (< ymod .5)
Cs
blackcolor)
(if (< ymod .5)
blackcolor
Cs))
(if (< xmod .5)
(if (< ymod .5)
blackcolor
Cs)
(if (< ymod .5)
Cs
blackcolor)))
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd))))))
(define-shader mirror ([Ks 1] [Kr 1] [roughness 0.05])
(let* ([Nf (faceforward (vec-normalize N) I)]
[IN (vec-normalize I)]
[V (vec-reverse IN)]
[R (reflect IN Nf)])
;; TODO: Revisit for opacity and color?
(color-add
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks)
(sample-environment P R Kr))))
(define-shader simple-tex
([Ka 1] [Kd 1] [Ks 0.5] [roughness 0.1] [specularcolor white]
[tex #f]
[sflip? #f] [tflip? #f]
[sstart 0] [sscale 1] [tstart 0] [tscale 1])
(let ([s (if sflip? (- 1 s) s)]
[t (if tflip? (- 1 t) t)])
(let ([ss (/ (- s sstart) sscale)]
[tt (/ (- t tstart) tscale)]
[Nf (faceforward (vec-normalize N) I)]
[V (vec-normalize (vec-reverse I))])
(color-add
(color-mul Os
(if tex
;; May also want to use opacity from the texture file here
(texture tex ss tt)
Cs)
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
(color-mul
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks)
specularcolor)))))
(define-shader marble
([Ks .4] [Kd .6] [Ka .1] [roughness .1] [specularcolor white]
[octaves 6] [lacunarity 2] [gain .5]
[colorlist
(list
(make-color .71 .71 1.0) ; pale blue
(make-color .71 .71 1.0) ; pale blue
(make-color .57 .57 .86) ; medium blue
(make-color .57 .57 .86) ; medium blue
(make-color .57 .57 .86) ; medium blue
(make-color .71 .71 1.0) ; pale blue
(make-color .71 .71 1.0) ; pale blue
(make-color .43 .43 .74) ; medium dark blue
(make-color .43 .43 .74) ; medium dark blue
(make-color .29 .29 .57) ; dark blue
(make-color .29 .29 .57) ; dark blue
(make-color .71 .71 1.0) ; pale blue
(make-color .29 .29 .57) ; dark blue
)])
(let* ([pnt (point->surface ($object) P)]
[Nf (faceforward (vec-normalize N) I)]
[IN (vec-normalize I)]
[V (vec-reverse IN)]
[color (color-spline
(abs (sin (vec-i (vec-num-add pnt
(turbulence pnt octaves lacunarity gain)))))
colorlist)])
(color-add
(color-mul Os color
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness])) Ks))))
(define-shader granite ([Ka .2] [Kd .8])
(let ([Nf (faceforward (vec-normalize N) I)]
[sum
(let lp ([i 0] [sum 0] [freq 1])
(if (= i 6)
sum
(lp (+ i 1)
(+ sum (/ (abs (- .5 (noise (vec-num-mul I
(* 4 freq)))))
freq))
(* 2 freq))))])
(color-mul Cs
(color-num-mul
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd))
sum))))
(define (oaktexture Pshad
ringfreq ringnoise ringnoisefreq
grainfreq
trunkwobble trunkwobblefreq
angularwobble angularwobblefreq
ringy grainy)
(let* ([offset (vfBm (vec-num-mul Pshad ringnoisefreq) 2 4 .5)]
[Pring (vec-add Pshad (vec-num-mul offset ringnoise))]
[Pring (vec-add Pring
(vec-num-mul
(vec-mul
(vsnoise (* (vec-k Pshad) trunkwobblefreq))
(make-vec 1 1 0))
trunkwobble))]
[r2 (+ (sqr (vec-i Pring)) (sqr (vec-j Pring)))]
[r (* (sqrt r2) ringfreq)]
[r (+ r (* angularwobble (smoothstep 0 5 r)
(snoise (vec-num-mul
(vec-mul Pring (make-vec 1 1 .1))
angularwobblefreq))))]
[dr (filterwidth r)]
[r (+ r (* .5 (filteredsnoise r dr)))]
[inring (smoothpulsetrain .1 .55 .7 .95 1 r)])
(* inring ringy)))
(define (material-plastic Nf base-color Ka Kd Ks roughness)
(color-add
(color-mul base-color
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
(color-num-mul ((specular [N Nf]
[eye (vec-reverse (vec-normalize I))]
[roughness roughness]))
Ks)))
(define-shader wood
([Ka 1] [Kd 1] [Ks .25]
[roughness .2]
[ringfreq 8] [ringnoise .02] [ringnoisefreq 1]
[grainfreq 25]
[trunkwobble .15] [trunkwobblefreq .025]
[angularwobble 1] [angularwobblefreq 1.5]
[lightwood (make-color .5 .2 .067)]
[darkwood (make-color .15 .077 .028)]
[ringy 1] [grainy 1])
(let* ([pnt (point->surface ($object) P)]
[Nf (faceforward (vec-normalize N) I)]
;;[IN (vec-normalize I)]
;;[V (vec-reverse IN)]
[wood (oaktexture pnt
ringfreq ringnoise ringnoisefreq
grainfreq
trunkwobble trunkwobblefreq
angularwobble angularwobblefreq
ringy grainy)]
[Cwood (color-mix lightwood darkwood wood)])
;; TODO: Try to displace Nf
(color-mul Os
(material-plastic Nf Cwood Ka Kd (* Ks (- 1 (* .5 wood))) roughness))))
(define-shader glow ([attenuation 2])
(let ([falloff (vec-dot I N)])
(if (< falloff 0)
(let ([n (expt (/ (* falloff falloff)
(* (vec-dot I I)
(vec-dot N N)))
attenuation)])
(values (color-num-mul Cs n) (make-color n n n)))
(values Cs transparent))))
(define-shader screen
([Ka 1] [Kd .75] [Ks .4] [roughness .1] [specularcolor white]
[density .25] [frequency 20])
(if (or (< (mod (* s frequency) 1) density)
(< (mod (* t frequency) 1) density))
(let ([Nf (faceforward (vec-normalize N) I)]
[V (vec-normalize (vec-reverse I))])
(values
(color-mul Os
(color-add
(color-mul Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
(color-mul
(color-num-mul ((specular [N Nf] [eye V] [roughness roughness]))
Ks)
specularcolor)))
opaque))
(values black transparent)))
(define-shader glass
([Ka .2] [Kd 0] [Ks .5] [roughness .05]
[Kr 1] [Kt 1] [eta 1.5] [transmitcolor white])
(let* ([Nf (faceforward (vec-normalize N) I)]
[IN (vec-normalize I)]
[V (vec-reverse IN)])
(let-values
([(kr kt Rfldir Rfrdir)
(fresnel IN Nf (if (< (vec-dot I N) 0)
(/ 1.0 eta)
eta))])
(let* ([kt (- 1 kr)]
[kr (* kr Kr)]
[kt (* kt Kt)]
[Crefl (sample-environment P (vec-normalize Rfldir) kr)]
[Crefr (sample-environment P (vec-normalize Rfrdir) kt)])
(color-add
(color-mul Cs
(color-add
(color-num-mul ((ambient)) Ka)
(color-num-mul ((diffuse [N Nf])) Kd)))
Crefl
(color-num-mul
((specular [N Nf] [eye V] [roughness roughness]))
Ks) ; not quite what RenderMan does
(color-mul transmitcolor Crefr))))))
(define-shader show-st ()
(make-color s t 0))
(define-shader show-xyz
([xmin -1] [ymin -1] [zmin -1] [xmax 1] [ymax 1] [zmax 1])
(let* ([scale
(make-vec (/ 1 (- xmax xmin)) (/ 1 (- ymax ymin)) (/ 1 (- zmax zmin)))]
[zero (make-vec xmin ymin zmin)]
[pnt (point->surface ($object) P)]
[cubeP (vec-mul (vec-sub pnt zero) scale)])
(make-color (vec-i cubeP) (vec-j cubeP) (vec-k cubeP))))
(define-shader simple-bumpmap
([normals #f]
[sflip? #f] [tflip? #f]
[sstart 0] [sscale 1] [tstart 0] [tscale 1])
(let ([s (if sflip? (- 1 s) s)]
[t (if tflip? (- 1 t) t)])
(let ([ss (/ (- s sstart) sscale)]
[tt (/ (- t tstart) tscale)])
(let ([pnt (point->surface ($object) P)]
[c (texture normals ss tt)])
(bump-normal pnt N
(lambda (x y z)
(- 1 (+ (* x (color-r c)) (* y (color-g c)) (* z (color-b c))))))))))
(module perlin-helpers
(noise stripes turbulence)
;; Based on Ken Perlin's bump textures
(define (noise x y z freq)
(let* ([x1 (- (* .707 x) (* .707 z))]
[z1 (+ (* .707 x) (* .707 z))]
[y1 (+ (* .707 x1) (* .707 y))]
[x1 (- (* .707 x1) (* .707 y))])
(perlin-noise (+ (* freq x1) 100) (* freq y1) (* freq z1))))
(define (stripes x f)
(- (sqr (+ .5 (* .5 (sin (* f 2 pi x))))) .5))
(define (turbulence x y z f)
(let lp ([f f] [t -0.5])
(if (> f 25)
t
(lp (* f 2) (+ t (abs (/ (noise x y z f) f)))))))
)
(define-shader lumpy ()
(import perlin-helpers)
(let ([pnt (point->surface ($object) P)])
(bump-normal pnt N
(lambda (x y z)
(* 0.03 (noise x y z 8))))))
(define-shader crinkly ()
(import perlin-helpers)
(let ([pnt (point->surface ($object) P)])
(bump-normal pnt N
(lambda (x y z)
(* -0.1 (turbulence x y z 1))))))
(define-shader marbled ()
(import perlin-helpers)
(let ([pnt (point->surface ($object) P)])
(bump-normal pnt N
(lambda (x y z)
(* 0.01 (stripes (+ x (* 2 (turbulence x y z 1))) 1.6))))))