Skip to content
Permalink
Browse files

Minor. Still getting weird ass results with reflectance

  • Loading branch information...
Shinmera committed Mar 23, 2019
1 parent 7e4e90c commit 6158225a90559c8f3bef8b5602727438f6b8ee7c
Showing with 77 additions and 116 deletions.
  1. +11 −8 data/deferred-pbr.frag
  2. +3 −6 deferred.lisp
  3. +1 −1 hdr.lisp
  4. +62 −101 workbench.lisp
@@ -8,7 +8,7 @@ uniform sampler2D normal_map;
uniform sampler2D albedo_map;
uniform sampler2D metal_map;
uniform vec3 view_position;
const float PI = 3.14159265;
const float PI = 3.14159265359;

float NDF_ggx(vec3 N, vec3 H, float roughness){
float a = roughness*roughness;
@@ -32,7 +32,7 @@ float G_smith(vec3 N, vec3 V, vec3 L, float roughness){
}

vec3 F_s(float cos_theta, vec3 F0){
return F0 + (1.0 - F0) * pow(1.0 - cos_theta, 5.0);
return F0 + (1.0 - F0) * pow(1.0 - cos_theta, 5.0);
}

float light_attenuation(Light light, vec3 position){
@@ -70,11 +70,15 @@ void main(){
vec3 normal = texture(normal_map, tex_coord).rgb;
vec3 albedo = texture(albedo_map, tex_coord).rgb;
if(albedo.x <= 0 && position.x == 0) discard;
// r = metalness, g = roughness, b = occlusion;

vec3 metal = texture(metal_map, tex_coord).rgb;
float metallic = metal.r;
float roughness = metal.g;
float occlusion = metal.b;

vec3 N = normalize(normal);
vec3 V = normalize(view_position - position);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 Lo = vec3(0);
for(int i=0; i<light_block.count; ++i){
Light light = light_block.lights[i];
@@ -91,14 +95,13 @@ void main(){
vec3 radiance = data.radiance;

// Cook-Torrance BRDF
vec3 F0 = mix(vec3(0.04), albedo, metal.r);
float NDF = NDF_ggx(N, H, roughness);
float G = G_smith(N, V, L, roughness);
vec3 F = F_s(max(dot(H, V), 0.0), F0);
float NDF = NDF_ggx(N, H, metal.g);
float G = G_smith(N, V, L, metal.g);

vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metal.r;
kD *= 1.0 - metallic;

vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
@@ -110,6 +113,6 @@ void main(){
}

// Add ambient part
vec3 ambient = vec3(0.03) * albedo * metal.b;
vec3 ambient = vec3(0.03) * albedo * occlusion;
color = vec4(ambient + (Lo*lighting_strength), 1);
}
@@ -36,7 +36,6 @@ layout (location = 3) in vec3 tangent;
out GEOM{
vec3 position;
vec2 texcoord;
vec3 normal;
mat3 TBN;
} geom;
@@ -45,10 +44,9 @@ uniform mat4 model_matrix;
void main(){
geom.position = vec3(model_matrix * vec4(position, 1.0));
geom.texcoord = texcoord;
geom.normal = mat3(transpose(inverse(model_matrix))) * normal;
vec3 T = normalize(vec3(model_matrix * vec4(tangent, 0.0)));
vec3 N = normalize(vec3(model_matrix * vec4(normal, 0.0)));
vec3 T = normalize(mat3(model_matrix) * tangent);
vec3 N = normalize(mat3(model_matrix) * normal);
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T);
geom.TBN = mat3(T, B, N);
@@ -64,7 +62,6 @@ layout (location = 3) out vec3 metal_map;
in GEOM{
vec3 position;
vec2 texcoord;
vec3 normal;
mat3 TBN;
} geom;
@@ -81,7 +78,7 @@ void main(){
position_map = geom.position;
normal_map = local_normal;
albedo_map.rgb = texture(diffuse, geom.texcoord).rgb;
albedo_map = texture(diffuse, geom.texcoord).rgb;
metal_map.r = texture(specular, geom.texcoord).r;
metal_map.g = texture(roughness, geom.texcoord).r;
metal_map.b = texture(occlusion, geom.texcoord).r;
@@ -58,7 +58,7 @@ uniform sampler2D high_pass;
in vec2 tex_coord;
out vec4 color;
uniform float gamma = 2.2;
uniform float exposure = 0.5;
uniform float exposure = 0.75;
void main(){
vec4 source = texture(previous_pass, tex_coord);
@@ -25,64 +25,31 @@
(define-asset (workbench neutral-normal) image
#p"neutral-normal.png")

;; Chalet
(define-asset (workbench chalet) mesh
#p"/home/linus/models/alpine_chalet/Alpine_chalet.obj"
:geometry-name "Alpine_chalet.obj")
;; Building
(define-asset (workbench building-a) mesh
#p"/home/linus/models/building_08/obj/Building08.obj"
:geometry-name "B_Set06_5_A")

(define-asset (workbench chalet-albedo) image
#p"/home/linus/models/alpine_chalet/Diffuse_map.png"
:internal-fromat :srgb)

(define-asset (workbench chalet-specular) image
#p"/home/linus/models/alpine_chalet/Metallic_map.png")

(define-asset (workbench chalet-normal) image
#p"/home/linus/models/alpine_chalet/Normal_map.png")

(define-asset (workbench chalet-roughness) image
#p"/home/linus/models/alpine_chalet/Roughness_map.png")

;; Wood house
(define-asset (workbench wood-house) mesh
#p"/home/linus/models/wood_house/hatka_local_.obj"
:geometry-name "fence")

(define-asset (workbench wood-house-albedo) image
#p"/home/linus/models/wood_house/home_hatka_Base_Color.png"
:internal-format :srgb)

(define-asset (workbench wood-house-specular) image
#p"/home/linus/models/wood_house/home_hatka_Metallic.png")

(define-asset (workbench wood-house-normal) image
#p"/home/linus/models/wood_house/home_hatka_Normal_OpenGL.png")
(define-asset (workbench building-b) mesh
#p"/home/linus/models/building_08/obj/Building08.obj"
:geometry-name "B_Set06_5_B")

(define-asset (workbench wood-house-roughness) image
#p"/home/linus/models/wood_house/home_hatka_Roughness.png")

(define-asset (workbench wood-house-occlusion) image
#p"/home/linus/models/wood_house/home_hatka_Mixed_AO.png")

(define-asset (workbench wood-ground) mesh
#p"/home/linus/models/wood_house/hatka_local_.obj"
:geometry-name "grount")

(define-asset (workbench wood-ground-albedo) image
#p"/home/linus/models/wood_house/grunt_Base_Color.png"
:internal-format :srgb)
(define-asset (workbench building-a-albedo) image
#p"/home/linus/models/building_08/obj/B_Set06_5_A_A.png"
:internal-fromat :srgb)

(define-asset (workbench wood-ground-specular) image
#p"/home/linus/models/wood_house/grunt_Metallic.png")
(define-asset (workbench building-b-albedo) image
#p"/home/linus/models/building_08/obj/B_Set06_5_B_A.jpg"
:internal-fromat :srgb)

(define-asset (workbench wood-ground-normal) image
#p"/home/linus/models/wood_house/grunt_Normal_OpenGL.png")
(define-asset (workbench building-a-specular) image
#p"/home/linus/models/building_08/obj/B_Set06_5_A_M.png")

(define-asset (workbench wood-ground-roughness) image
#p"/home/linus/models/wood_house/grunt_Roughness.png")
(define-asset (workbench building-a-normal) image
#p"/home/linus/models/building_08/obj/B_Set06_5_A_N.png")

(define-asset (workbench wood-ground-occlusion) image
#p"/home/linus/models/wood_house/grunt_Mixed_AO.png")
(define-asset (workbench building-a-roughness) image
#p"/home/linus/models/building_08/obj/B_Set06_5_A_R.png")

;; Extra
(define-asset (workbench sphere) mesh
@@ -158,10 +125,13 @@ void main(){
lighting_strength = 1-(0.9 * shadow);
}")

(print (list :location (location (unit :camera (scene (handler *context*))))
:rotation (rotation (unit :camera (scene (handler *context*))))))

(progn
(defmethod setup-scene ((workbench workbench) scene)
(enter (make-instance 'editor-camera :location (VEC3 -485.24792 39.60954 468.25104)
:rotation (VEC3 6.0731845 0.9768292 0.0))
(enter (make-instance 'editor-camera :LOCATION (VEC3 588.8497 96.14274 479.87396)
:ROTATION (VEC3 0.11000238 5.4900184 0.0))
scene)
;; (enter (make-instance 'skybox :texture (asset 'workbench 'skybox)) scene)
(flet ((add (vert diff spec norm rough ao &rest initargs)
@@ -174,35 +144,30 @@ void main(){
:vertex-array (asset 'workbench vert)
initargs)
scene)))
(add 'wood-ground
'wood-ground-albedo
'wood-ground-specular
'wood-ground-normal
'wood-ground-roughness
'wood-ground-occlusion)
;; (add 'wood-house
;; 'wood-house-albedo
;; 'wood-house-specular
;; 'wood-house-normal
;; 'wood-house-roughness
;; 'wood-house-occlusion)
(add 'chalet
'chalet-albedo
'chalet-specular
'chalet-normal
'chalet-roughness
(add 'building-a
'building-a-albedo
'building-a-specular
'building-a-normal
'building-a-roughness
'white
:name :chalet
:location (vec 100 177 -300)
:scaling (vec 80 80 80)))
(dotimes (i (1- MAX-LIGHTS))
(enter (make-instance 'point-light :index (1+ i)
:location (vec3-random (- *scene-size*) *scene-size*)
:color (vec3-random 500 700)
:attenuation '(0.07 0.017))
scene))
(let* ((shadow (make-instance 'shadow-map-pass :projection-matrix (mortho -800 800 -800 800 1.0 1500)
:view-matrix (mlookat (vec 600 600 -600) (vec 0 0 0) (vec 0 1 0))
:scaling (vec 100 100 100)
:location (vec -400 0 0))
(add 'building-b
'building-b-albedo
'black
'neutral-normal
'black
'white
:scaling (vec 100 100 100)
:location (vec -400 0 0)))
;; (dotimes (i (1- MAX-LIGHTS))
;; (enter (make-instance 'point-light :index (1+ i)
;; :location (vec3-random (- *scene-size*) *scene-size*)
;; :color (vec3-random 500 700)
;; :attenuation '(0.07 0.017))
;; scene))
(let* ((shadow (make-instance 'shadow-map-pass :projection-matrix (mortho -800 800 -800 800 1.0 2000)
:view-matrix (mlookat (vec 400 300 150) (vec 0 0 0) (vec 0 1 0))
:name :shadow-map-pass))
(geometry (make-instance 'geometry-pass))
(lighting (make-instance 'deferred+shadow-pass :shadow-map-pass shadow))
@@ -213,7 +178,7 @@ void main(){
(skybox (make-instance 'skybox-pass :texture (asset 'workbench 'skybox)))
(tone-map (make-instance 'bloom-pass))
(blend (make-instance 'blend-pass)))
;;(connect (port shadow 'shadow) (port (make-instance 'copy-pass) 'previous-pass) scene)
;;(connect (port geometry 'normal) (port (make-instance 'copy-pass) 'previous-pass) scene)
(connect (port shadow 'shadow) (port lighting 'shadow-map) scene)
(connect (port geometry 'position) (port lighting 'position-map) scene)
(connect (port geometry 'normal) (port lighting 'normal-map) scene)
@@ -226,33 +191,29 @@ void main(){
(connect (port v-blur2 'color) (port tone-map 'high-pass) scene)
(connect (port lighting 'color) (port tone-map 'previous-pass) scene)
(connect (port skybox 'color) (port blend 'a-pass) scene)
(connect (port tone-map 'color) (port blend 'b-pass) scene)))
(connect (port tone-map 'color) (port blend 'b-pass) scene)
))

(defmethod change-scene :after ((workbench workbench) scene &key old)
(declare (ignore old))
(let ((buffer (asset 'trial 'light-block)))
(flet ((field (i field)
(format NIL "LightBlock.lights[~d].~(~a~)" i field)))
(setf (buffer-field buffer (field 0 'type)) 2)
(setf (buffer-field buffer (field 0 'direction)) (nvunit (nv- (vec 600 600 -600))))
(setf (buffer-field buffer (field 0 'type)) 1)
(setf (buffer-field buffer (field 0 'direction)) (nv- (vec 400 300 150)))
(setf (buffer-field buffer (field 0 'color)) (vec 0.9 0.85 0.6)))
(setf (buffer-field buffer "LightBlock.count") MAX-LIGHTS)))
(setf (buffer-field buffer "LightBlock.count") 1)))
(maybe-reload-scene))

(defun daytime-color (tt)
(if (< 0 tt)
(vec (ease (min 1 (* tt 3)) 'quint-in-out 0 1.4)
(* tt 1.2)
tt)
(vec 0 0 0)))

(defmethod update :after ((workbench workbench) tt dt)
(let* ((buffer (asset 'trial 'light-block))
(shadow (unit :shadow-map-pass (scene workbench)))
(tt (/ PI 3))
(light (vec (* 600 (sin tt)) (* 600 (cos tt)) 600))
(color (v* (daytime-color (/ (vy light) 600)) 4)))
(setf (buffer-field buffer "LightBlock.lights[0].type") 1)
(setf (buffer-field buffer "LightBlock.lights[0].color") color)
(setf (buffer-field buffer "LightBlock.lights[0].direction") light)
(setf (shadow-view-matrix shadow) (mlookat light (vec 0 0 0) (vec 0 1 0)))))
(light (vec 400 400 300))
(color (vunit (vec 9 7 5))))
;; (setf (buffer-field buffer "LightBlock.lights[0].type") 1)
;; (setf (buffer-field buffer "LightBlock.lights[0].color") color)
;; (setf (buffer-field buffer "LightBlock.lights[0].position") light)
;; (setf (buffer-field buffer "LightBlock.lights[0].direction") (v- light))
;; (setf (shadow-projection-matrix shadow) (mortho -800 800 -800 800 1.0 1500))
;; (setf (shadow-view-matrix shadow) (mlookat (vec 400 300 150) (vec 0 0 0) (vec 0 1 0)))
))

0 comments on commit 6158225

Please sign in to comment.
You can’t perform that action at this time.