/
user-shaders.ss
311 lines (292 loc) · 11.6 KB
/
user-shaders.ss
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
(define-shader constant ()
(color-mul Os Cs))
(define-shader matte ([Ka 1] [Kd 1])
(let ([Nf (faceforward (vec-normalize normal) incoming)])
(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 normal) incoming)]
[V (vec-normalize (vec-reverse incoming))])
(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 normal) incoming)]
[IN (vec-normalize incoming)]
[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 scene intersect-point R Kr depth)))))
(define-shader plastic ([Ks .5] [Kd .5] [Ka 1] [roughness .1]
[specularcolor white])
(let ([Nf (faceforward (vec-normalize normal) incoming)]
[V (vec-normalize (vec-reverse incoming))])
(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 intersect-point)]
[Nf (faceforward (vec-normalize normal) incoming)])
(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 intersect-point)]
[Nf (faceforward (vec-normalize normal) incoming)])
(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 normal) incoming)]
[IN (vec-normalize incoming)]
[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 scene intersect-point R Kr depth))))
(define-shader simple-texmap
([Ka 1] [Kd 1] [Ks 0.5] [roughness 0.1] [specularcolor white]
[texture #f]
[sstart 0] [sscale 1] [tstart 0] [tscale 1])
(let* ([st (point->texture object
(point->surface object intersect-point))]
[ss (/ (- (vec-i st) sstart) sscale)]
[tt (/ (- (vec-j st) tstart) tscale)]
[Nf (faceforward (vec-normalize normal) incoming)]
[V (vec-normalize (vec-reverse incoming))])
(color-add
(color-mul Os
(if texture
;; May also want to use opacity from the texture file here
(texture 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 simple-bumpmap
([normals #f]
[sstart 0] [sscale 1] [tstart 0] [tscale 1])
(let* ([st (point->texture object
(point->surface object intersect-point))]
[ss (/ (- (vec-i st) sstart) sscale)]
[tt (/ (- (vec-j st) tstart) tscale)])
(normals ss tt)))
(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 intersect-point)]
[Nf (faceforward (vec-normalize normal) incoming)]
[IN (vec-normalize incoming)]
[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 normal) incoming)]
[sum
(let lp ([i 0] [sum 0] [freq 1])
(if (= i 6)
sum
(lp (+ i 1)
(+ sum (/ (abs (- .5 (noise (vec-num-mul incoming
(* 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 incoming))]
[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 intersect-point)]
[Nf (faceforward (vec-normalize normal) incoming)]
;;[IN (vec-normalize incoming)]
;;[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 incoming normal)])
(if (< falloff 0)
(let ([n (expt (/ (* falloff falloff)
(* (vec-dot incoming incoming)
(vec-dot normal normal)))
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])
(let ([st (point->texture object (point->surface object intersect-point))])
(if (or (< (mod (* (vec-i st) frequency) 1) density)
(< (mod (* (vec-j st) frequency) 1) density))
(let ([Nf (faceforward (vec-normalize normal) incoming)]
[V (vec-normalize (vec-reverse incoming))])
(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 normal) incoming)]
[IN (vec-normalize incoming)]
[V (vec-reverse IN)])
(let-values
([(kr kt Rfldir Rfrdir)
(fresnel IN Nf (if (< (vec-dot incoming normal) 0)
(/ 1.0 eta)
eta))])
(let* ([kt (- 1 kr)]
[kr (* kr Kr)]
[kt (* kt Kt)]
[Crefl (sample-environment scene intersect-point
(vec-normalize Rfldir) kr depth)]
[Crefr (sample-environment scene intersect-point
(vec-normalize Rfrdir) kt depth)])
(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 ()
(let* ([Nf (faceforward (vec-normalize normal) incoming)]
[st (point->texture object (point->surface object intersect-point))])
(make-color (vec-i st) (vec-j st) 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)]
[P (point->surface object intersect-point)]
[cubeP (vec-mul (vec-sub P zero) scale)])
(make-color (vec-i cubeP) (vec-j cubeP) (vec-k cubeP))))