From 629dc808c595d65cda74a86975ebd780113f3431 Mon Sep 17 00:00:00 2001 From: quou Date: Sun, 9 Feb 2025 18:50:58 +1100 Subject: Properly send lights from the CPU --- intermediate/scene.glb | Bin 0 -> 12360 bytes intermediate/surface.glsl | 59 +++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 intermediate/scene.glb (limited to 'intermediate') diff --git a/intermediate/scene.glb b/intermediate/scene.glb new file mode 100644 index 0000000..5335c55 Binary files /dev/null and b/intermediate/scene.glb differ diff --git a/intermediate/surface.glsl b/intermediate/surface.glsl index 0cba7f8..fff15ed 100644 --- a/intermediate/surface.glsl +++ b/intermediate/surface.glsl @@ -54,9 +54,24 @@ type: float name: ao type: float [variable] +name: light_count +type: int +[variable] name: camera_pos type: vec3 +[struct] +name: Light +[variable] +name: dir +type: vec3 +[variable] +name: brightness +type: float +[variable] +name: colour +type: vec3 + [cbuffer] name: c_mvp type: MVP @@ -67,6 +82,11 @@ name: material type: Material stage: fragment +[sbuffer] +name: lights +type: Light +stage: fragment + [texture] name: albedo stage: fragment @@ -147,6 +167,7 @@ float specular_brdf(vec2 uv, vec3 ref, vec3 l, vec3 v, vec3 n) { } void main() { + int i; vec2 uv = interpolator.uv; vec3 p = interpolator.position.xyz; @@ -158,29 +179,33 @@ void main() { else nrm = normalize(interpolator.tbn * nrm); - vec3 light_dir = vec3(0, 0, 1); vec3 col = texture(albedo, uv).rgb * material.albedo; - float cos_theta_i = max(dot(nrm, light_dir), 0.0); vec3 view_dir = normalize(material.camera_pos - p); float met = texture(metal, uv).r * material.metalness; - vec3 ref = reflect(-view_dir, nrm); - vec3 spec_col = texture(env_cube, ref, material.roughness * 8.0).rgb; - vec3 amb_col = textureLod(env_cube, nrm, 8.0).rgb; - - vec3 diffuse = - diffuse_brdf(uv) * - (1.0 - met) * - cos_theta_i; - vec3 spec = - mix(spec_col, spec_col * col, met) * - specular_brdf(uv, ref, light_dir, view_dir, nrm) * - cos_theta_i; + vec3 ref_col = texture(env_cube, ref, material.roughness * 8.0).rgb; + vec3 spec_col = mix(ref_col, ref_col * col, met); + vec3 amb_col = min(textureLod(env_cube, nrm, 8.0).rgb, 0.05); vec3 ambient = - min(amb_col, 0.05) * + amb_col * texture(ao, uv).r * material.ao; - - colour = vec4(ambient + diffuse + spec, 1.0); + + vec3 base_diffuse = diffuse_brdf(uv) * (1.0 - met); + + vec3 light = 0.0.xxx; + for (i = 0; i < material.light_count; i++) { + Light l = lights[i]; + vec3 light_dir = l.dir; + float cos_theta_i = max(dot(nrm, light_dir), 0.0); + vec3 diffuse = base_diffuse * cos_theta_i; + vec3 spec = + spec_col * + specular_brdf(uv, ref, light_dir, view_dir, nrm) * + cos_theta_i; + light += (diffuse + spec) * l.brightness; + } + + colour = vec4(ambient + light, 1.0); } #endif -- cgit v1.2.3-54-g00ecf