forked from Shirakumo/kandria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lighting.lisp
423 lines (352 loc) · 15.5 KB
/
lighting.lisp
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
(in-package #:org.shirakumo.fraf.kandria)
(defvar *gi-info* (make-hash-table :test 'eq))
(defmethod gi ((name symbol))
(or (gethash name *gi-info*)
(error "No GI with name ~s found." name)))
(defmethod gi ((null null))
(gi 'null))
(defmethod (setf gi) (value (name symbol))
(when *context*
(update-lighting (unit 'lighting-pass T)))
(if value
(setf (gethash name *gi-info*) value)
(remhash name *gi-info*))
value)
(defun list-gis ()
(loop for v being the hash-values of *gi-info*
collect v))
(defmacro define-gi (name &body initargs)
(let ((existing (gensym "EXISTING")))
`(let ((,existing (ignore-errors (gi ',name))))
(setf (gi ',name) (trial::ensure-instance ,existing 'gi-info :name ',name ,@initargs)))))
(define-gl-struct gi
(activep :int :accessor active-p)
(location :vec2 :accessor location)
(light :vec3 :accessor light)
(ambient :vec3 :accessor ambient)
(attenuation :float :accessor attenuation))
(define-asset (kandria gi) uniform-block
'gi)
(defclass gi-info ()
((name :initform NIL :initarg :name :accessor name)
(location :initform NIL :initarg :location :accessor location)
(light :initform NIL :initarg :light :accessor light)
(ambient :initform 1 :initarg :ambient :accessor ambient)
(attenuation :initform 0.0 :initarg :attenuation :accessor attenuation)))
(defmethod shared-initialize :after ((info gi-info) slots &key (light-multiplier 1.0) (ambient-multiplier 1.0))
(flet ((normalize-light (light mult)
(etypecase light
(null (vec 0 0 0))
(real (v* (vec light light light) mult))
(vec3 (v* light mult))
(cons (multiply-gradient (make-gradient light) mult))
(gradient (multiply-gradient light mult)))))
(setf (light info) (normalize-light (slot-value info 'light) light-multiplier))
(setf (ambient info) (normalize-light (slot-value info 'ambient) ambient-multiplier))))
(defmethod print-object ((info gi-info) stream)
(print-unreadable-object (info stream :type T)
(format stream "~s" (name info))))
(defmethod location ((info gi-info))
(let ((loc (slot-value info 'location)))
(flet ((time-position (hour)
(let ((tt (* (/ hour 24) 2 PI)))
(nv+ (vec2 (* 10000000 (sin tt)) (* -10000000 (cos tt)))
(location (camera +world+))))))
(etypecase loc
(vec2
loc)
(located-entity
(location loc))
(real
(time-position loc))
((eql :sun)
(time-position (hour +world+)))
(null
NIL)
(symbol
(let ((unit (unit loc +world+)))
(if unit
(location unit)
(vec 0 0))))))))
(flet ((evaluate-light (light)
(etypecase light
(vec3
light)
(gradient
(gradient-value (float (hour +world+) 0f0) light)))))
(defmethod light ((info gi-info))
(evaluate-light (slot-value info 'light)))
(defmethod ambient ((info gi-info))
(evaluate-light (slot-value info 'ambient))))
(define-shader-pass lighting-pass (per-object-pass)
((color :port-type output :texspec (:internal-format :rgba16f))
(gi-a :initform (make-instance 'gi-info) :accessor gi-a)
(gi-b :initform (make-instance 'gi-info) :accessor gi-b)
(mix :initform 1.0 :accessor mix)
(name :initform 'lighting-pass)))
(defmethod render :before ((pass lighting-pass) target)
(gl:clear-color 0 0 0 0))
(defun update-lighting (pass)
(let* ((b (gi-b pass))
(location (location b))
(light (light b))
(ambient (ambient b))
(attenuation (attenuation b))
(mix (mix pass)))
(when (< mix 1)
(let* ((a (gi-a pass))
(loc (location a)))
(setf light (vlerp (light a) light mix))
(setf ambient (vlerp (ambient a) ambient mix))
(setf attenuation (lerp (attenuation a) attenuation mix))
(when loc
(if location
(setf location (vlerp loc location mix))
(setf location loc)))))
(with-buffer-tx (gi (// 'kandria 'gi))
(setf (active-p gi) (if location 1 0))
(setf (location gi) (or location (vec 0 0)))
(setf (light gi) light)
(setf (ambient gi) ambient)
(setf (attenuation gi) attenuation))))
(defmethod (setf lighting) ((value gi-info) (pass lighting-pass))
(unless (eql (gi-b pass) value)
(setf (mix pass) (- 1.0 (min 1.0 (mix pass))))
(setf (gi-a pass) (gi-b pass))
(setf (gi-b pass) value)
(update-lighting pass))
value)
(defmethod lighting ((pass lighting-pass))
(gi-b pass))
(defmethod force-lighting ((pass lighting-pass))
(setf (mix pass) 1.0)
(update-lighting pass))
(defmethod handle ((ev force-lighting) (pass lighting-pass))
(force-lighting pass))
(defmethod handle ((ev switch-chunk) (pass lighting-pass))
(setf (lighting pass) (gi (chunk ev))))
(defmethod handle ((ev tick) (pass lighting-pass))
(when (< (mix pass) 1)
(incf (mix pass) (dt ev)))
(update-lighting pass))
(defmethod reset ((pass lighting-pass))
(setf (lighting pass) (gi 'none))
(force-lighting pass))
(define-shader-entity light (ephemeral vertex-entity sized-entity)
((name :initform NIL)))
(defmethod object-renderable-p ((light light) (pass lighting-pass)) T)
(defmethod object-renderable-p ((light light) (pass render-pass)) NIL)
(defmethod object-renderable-p ((renderable renderable) (pass lighting-pass)) NIL)
(define-shader-pass rendering-pass (render-pass)
((lighting :port-type input :texspec (:internal-format :rgba16f))
(local-shade :initform 0.15 :accessor local-shade)
(shadow-map :port-type input)
(exposure :initform 0.5 :accessor exposure)
(gamma :initform 2.2 :accessor gamma)
(monitor-gamma :initform (setting :display :gamma) :accessor monitor-gamma)
(name :initform 'render)
#++(color :port-type output :attachment :color-attachment0
:texspec (:width 640 :height 416))
#++(depth :port-type output :attachment :depth-stencil-attachment
:texspec (:width 640 :height 416))))
(defmethod object-renderable-p ((controller controller) (pass rendering-pass)) NIL)
(defmethod sort-frame ((pass rendering-pass) frame)
(stable-sort frame #'< :key (lambda (c)
(let* ((object (car c))
(idx (layer-index object)))
(etypecase object
(layer (if (<= 2 idx) (+ idx 0.2) (- idx 0.1)))
(water (+ 0.1 idx))
(T idx))))))
(defmethod prepare-pass-program :after ((pass rendering-pass) (program shader-program))
(setf (uniform program "exposure") (exposure pass))
(setf (uniform program "gamma") (* (gamma pass) (/ (monitor-gamma pass) 2.2))))
(defun light-intensity (gi shade)
(let ((color (vlerp (v+ (ambient gi) (light gi)) (ambient gi) (clamp 0 shade 1))))
(clamp 0 (* (expt (vlength color) 1/3) 1.5) 2.75)))
(defmethod handle ((ev tick) (pass rendering-pass))
(when (= 0 (mod (fc ev) 2))
(let ((gi (buffer-data (// 'kandria 'gi))))
(if (= 1 (active-p gi))
(let* ((shade (local-shade (flow:other-node pass (first (flow:connections (flow:port pass 'shadow-map))))))
(current (local-shade pass)))
(let ((intensity (light-intensity gi current)))
(setf (exposure pass) (clamp 0.5 (- 3.5 intensity) 10.0)
(gamma pass) (clamp 0.5 (- 3.75 intensity) 3.0)))
(setf (local-shade pass) (cond ((< (abs (- current shade)) 0.05)
shade)
((< current shade)
(+ current 0.02))
(T
(- current 0.02)))))
(setf (exposure pass) 0.5
(gamma pass) 2.2)))))
(define-class-shader (rendering-pass :fragment-shader -100)
"out vec4 color;
uniform float gamma = 2.2;
uniform float exposure = 0.5;
void main(){
maybe_call_next_method();
vec3 mapped = vec3(1.0) - exp((-color.rgb) * exposure);
color.rgb = pow(mapped, vec3(1.0 / gamma));
}")
(define-shader-entity lit-entity (renderable)
()
(:buffers (kandria gi)))
;; FIXME: We might want the incidence computation to smoothen when
;; facing away from the light source to avoid extremely hard
;; shadows appearing on cylindrical shapes.
;; FIXME: If light is on player layers in front of the player
;; get lit even when the player is behind them, which is not
;; correct. No idea how to fix that, though.
(define-class-shader (lit-entity :fragment-shader 100)
"uniform sampler2D lighting;
uniform sampler2D shadow_map;
#define PI 3.1415926538
vec4 apply_lighting_flat(vec4 color, vec2 offset, float absorption, vec2 world_pos){
vec3 truecolor = pow(color.rgb, vec3(2.2));
ivec2 pos = ivec2(gl_FragCoord.xy-vec2(0.5)+offset);
vec4 light = texelFetch(lighting, pos, 0);
absorption = pow(absorption, 1.0/2.2);
truecolor *= gi.ambient;
truecolor += light.rgb*max(0, light.a-absorption)*color.rgb;
if(gi.activep != 0){
vec2 dir = gi.location - world_pos;
float dirl = (length(dir)-10)/10;
float attenuation = 1.0/max(1.0, pow(dirl, gi.attenuation));
float shade = clamp(2-3*texelFetch(shadow_map, pos, 0).r, 0, 1);
truecolor += gi.light*clamp(shade*(1-absorption)*attenuation, 0, 1)*color.rgb;
}
return vec4(truecolor, color.a);
}
vec4 apply_lighting(vec4 color, vec2 offset, float absorption, vec2 normal, vec2 world_pos){
vec3 truecolor = pow(color.rgb, vec3(2.2));
ivec2 pos = ivec2(gl_FragCoord.xy-vec2(0.5)+offset);
vec4 light = texelFetch(lighting, pos, 0);
absorption = pow(absorption, 1.0/2.2);
truecolor *= gi.ambient;
truecolor += light.rgb*max(0, light.a-absorption)*color.rgb;
if(gi.activep != 0){
vec2 dir = gi.location - world_pos;
float dirl = (length(dir)-10)/10;
float incidence = clamp(dot(normalize(dir), normal), 0, 1);
float attenuation = 1.0/max(1.0, pow(dirl, gi.attenuation));
float shade = clamp(2-3*texelFetch(shadow_map, pos, 0).r, 0, 1);
incidence = mix(1.0, incidence, clamp((dirl-1)/3, 0, 1));
truecolor += gi.light*clamp(shade*(1-absorption)*incidence*attenuation, 0, 1)*color.rgb;
}
return vec4(truecolor, color.a);
}")
(define-shader-entity lit-vertex-entity (lit-entity vertex-entity)
())
(define-class-shader (lit-vertex-entity :vertex-shader)
"layout (location = 0) in vec3 position;
uniform mat4 model_matrix;
out vec2 world_pos;
void main(){
maybe_call_next_method();
world_pos = (model_matrix*vec4(position, 1)).xy;
}")
(define-class-shader (lit-vertex-entity :fragment-shader -10)
"out vec4 color;
in vec2 world_pos;
void main(){
maybe_call_next_method();
color = apply_lighting_flat(color, vec2(0, -5), 0, world_pos);
}")
(define-shader-entity lit-sprite (lit-vertex-entity sprite-entity)
())
(define-shader-entity lit-animated-sprite (lit-vertex-entity trial:animated-sprite facing-entity sized-entity)
((layer-index :initarg :layer-index :initform 0 :accessor layer-index)))
(defmethod apply-transforms progn ((subject animated-sprite))
(translate-by 0 (- (vy (bsize subject))) 0))
(define-shader-entity basic-light (light colored-entity creatable)
((color :initarg :color :initform (vec4 0.3 0.25 0.1 1.0)
:type vec4 :documentation "The color of the light")))
(defmethod initialize-instance :after ((light basic-light) &key data)
(unless (slot-boundp light 'vertex-array)
(let* ((data (make-array (length data) :adjustable T :fill-pointer T :element-type 'single-float
:initial-contents data))
(vbo (make-instance 'vertex-buffer :data-usage :dynamic-draw :buffer-data data))
(vao (make-instance 'vertex-array :vertex-form :triangle-fan
:bindings `((,vbo :size 2 :offset 0 :stride 8))
:size (/ (length data) 2))))
(setf (vertex-array light) vao))
(update-bounding-box light)))
(defmethod initargs append ((light basic-light))
'(:color :vertex-array))
(defmethod update-bounding-box ((light basic-light))
(let* ((data (buffer-data (caar (bindings (vertex-array light)))))
(loc (location light))
(x+ (vx loc)) (x- (vx loc))
(y+ (vy loc)) (y- (vy loc)))
;; Determine bounds
(loop for i from 0 below (length data) by 2
for x = (+ (vx loc) (aref data (+ i 0)))
for y = (+ (vy loc) (aref data (+ i 1)))
do (when (or (null x+) (< x+ x)) (setf x+ x))
(when (or (null x-) (< x x-)) (setf x- x))
(when (or (null y+) (< y+ y)) (setf y+ y))
(when (or (null y-) (< y y-)) (setf y- y)))
;; Update coordinates
(vsetf (bsize light) (/ (- x+ x-) 2) (/ (- y+ y-) 2))
(let ((newloc (vec (+ x- (vx (bsize light)))
(+ y- (vy (bsize light))))))
(loop for i from 0 below (length data) by 2
do (incf (aref data (+ i 0)) (- (vx loc) (vx newloc)))
(incf (aref data (+ i 1)) (- (vy loc) (vy newloc))))
(setf (location light) newloc))))
(defmethod add-vertex ((light basic-light) &key location)
(let* ((vao (vertex-array light))
(vbo (caar (bindings vao)))
(data (buffer-data vbo))
(loc (v- (vfloor location) (location light))))
(loop for i from 0 below (length data) by 2
do (when (and (= (vx loc) (aref data (+ i 0)))
(= (vy loc) (aref data (+ i 1))))
(array-utils:array-shift data :n -2 :from (+ 2 i))
(return))
finally (vector-push-extend (vx loc) data)
(vector-push-extend (vy loc) data))
(update-bounding-box light)
(resize-buffer vbo (* (length data) 4) :data data)
(setf (size vao) (if (<= 6 (length data))
(/ (length data) 2)
0))))
(defmethod clear ((light basic-light))
(let* ((vao (vertex-array light))
(vbo (caar (bindings vao)))
(data (buffer-data vbo)))
(setf (fill-pointer data) 0)
(update-bounding-box light)
(resize-buffer vbo (* (length data) 4) :data data)
(setf (size vao) (if (<= 6 (length data))
(/ (length data) 2)
0))))
(define-shader-entity per-vertex-light (light vertex-colored-entity)
())
(define-shader-entity textured-light (light sprite-entity resizable creatable)
((multiplier :initform 1.0f0 :initarg :multiplier :accessor multiplier
:type single-float :documentation "Light intensity multiplier")
(texture :initform (locally (declare (notinline asset))
(resource (asset 'kandria 'lights) T)))))
(defmethod initargs append ((light textured-light))
'(:multiplier))
(defmethod render :before ((light textured-light) (program shader-program))
(setf (uniform program "multiplier") (multiplier light)))
(defmethod resize ((sprite sprite-entity) width height)
(vsetf (bsize sprite) (/ width 2) (/ height 2)))
(define-class-shader (textured-light :fragment-shader)
"uniform float multiplier = 1.0;
out vec4 color;
void main(){
maybe_call_next_method();
color *= multiplier;
}")
(define-shader-entity flash (textured-light listener)
((time-scale :initform 1.0 :initarg :time-scale :accessor time-scale)))
(defmethod handle ((ev tick) (flash flash))
(cond ((<= (multiplier flash) 0.0)
(leave flash T))
(T
(decf (multiplier flash) (* (time-scale flash) (dt ev))))))