hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/assets/shaders
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-22 12:24:05 +0200
committerhachem <im@hachem.wtf>2025-08-22 12:24:05 +0200
commit2c8002d30f291cba832a5750212009d48238c308 (patch)
tree4aafc468441b3e32c267040830b8e0b5af859faf /assets/shaders
parentb9de06951631d9969c52c6ed95de17a6d8aad121 (diff)
feat: add HDRI
Diffstat (limited to 'assets/shaders')
-rw-r--r--assets/shaders/equirect_to_cubemap.glsl33
-rw-r--r--assets/shaders/geodesic.glsl12
-rw-r--r--assets/shaders/skybox.glsl31
-rw-r--r--assets/shaders/sphere.glsl6
4 files changed, 80 insertions, 2 deletions
diff --git a/assets/shaders/equirect_to_cubemap.glsl b/assets/shaders/equirect_to_cubemap.glsl
new file mode 100644
index 0000000..2b99c9b
--- /dev/null
+++ b/assets/shaders/equirect_to_cubemap.glsl
@@ -0,0 +1,33 @@
+#type vertex
+
+#version 330 core
+layout (location = 0) in vec3 aPos;
+out vec3 WorldPos;
+uniform mat4 projection;
+uniform mat4 view;
+void main()
+{
+ WorldPos = aPos;
+ gl_Position = projection * view * vec4(WorldPos, 1.0);
+}
+
+#type fragment
+
+#version 330 core
+out vec4 FragColor;
+in vec3 WorldPos;
+uniform sampler2D equirectangularMap;
+const vec2 invAtan = vec2(0.1591, 0.3183);
+vec2 SampleSphericalMap(vec3 v)
+{
+ vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
+ uv *= invAtan;
+ uv += 0.5;
+ return uv;
+}
+void main()
+{
+ vec2 uv = SampleSphericalMap(normalize(WorldPos));
+ vec3 color = texture(equirectangularMap, uv).rgb;
+ FragColor = vec4(color, 1.0);
+}
diff --git a/assets/shaders/geodesic.glsl b/assets/shaders/geodesic.glsl
index 70e41ef..2e69c72 100644
--- a/assets/shaders/geodesic.glsl
+++ b/assets/shaders/geodesic.glsl
@@ -2,6 +2,7 @@
layout(local_size_x = 16, local_size_y = 16) in;
layout(binding = 0, rgba8) writeonly uniform image2D outImage;
+layout(binding = 5) uniform samplerCube u_HDRIEnvironment;
layout(std140, binding = 1) uniform Camera
{
vec3 camPos; float _pad0;
@@ -55,6 +56,11 @@ vec4 objectColor = vec4(0.0);
vec3 hitCenter = vec3(0.0);
float hitRadius = 0.0;
+vec3 SampleHDRI(vec3 direction)
+{
+ return texture(u_HDRIEnvironment, direction).rgb;
+}
+
float hash(float p)
{
p = fract(p * 0.1031);
@@ -471,7 +477,11 @@ void main()
color = vec4(shaded, objectColor.a);
color = mix(accumulatedColor, color, color.a);
} else
- color = accumulatedColor;
+ {
+ vec3 rayDirection = normalize(vec3(ray.x, ray.y, ray.z) - cam.camPos);
+ vec3 hdriColor = SampleHDRI(rayDirection);
+ color = vec4(mix(accumulatedColor.rgb, hdriColor, 1.0 - accumulatedColor.a), 1.0);
+ }
imageStore(outImage, pix, color);
}
diff --git a/assets/shaders/skybox.glsl b/assets/shaders/skybox.glsl
new file mode 100644
index 0000000..654bed1
--- /dev/null
+++ b/assets/shaders/skybox.glsl
@@ -0,0 +1,31 @@
+#type vertex
+
+#version 330 core
+
+layout (location = 0) in vec3 aPos;
+
+out vec3 TexCoords;
+
+uniform mat4 u_Projection;
+uniform mat4 u_View;
+
+void main()
+{
+ TexCoords = aPos;
+ vec4 pos = u_Projection * u_View * vec4(aPos, 1.0);
+ gl_Position = pos.xyww;
+}
+
+#type fragment
+
+#version 330 core
+
+out vec4 FragColor;
+in vec3 TexCoords;
+
+uniform samplerCube u_Skybox;
+
+void main()
+{
+ FragColor = texture(u_Skybox, TexCoords);
+}
diff --git a/assets/shaders/sphere.glsl b/assets/shaders/sphere.glsl
index 23e8f54..2d1a46e 100644
--- a/assets/shaders/sphere.glsl
+++ b/assets/shaders/sphere.glsl
@@ -33,6 +33,7 @@ uniform vec3 u_CameraPos;
uniform int u_IsSelected;
uniform vec3 u_OutlineColor;
uniform float u_OutlineWidth;
+uniform samplerCube u_HDRIEnvironment;
out vec4 o_FragColor;
@@ -43,10 +44,13 @@ void main()
vec3 viewDir = normalize(u_CameraPos - v_WorldPos);
vec3 reflectDir = reflect(-lightDir, normal);
+ vec3 hdriLight = texture(u_HDRIEnvironment, normal).rgb;
+ float hdriIntensity = 0.3;
+
float diff = max(dot(normal, lightDir), 0.0);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
- vec3 diffuse = u_Color * diff;
+ vec3 diffuse = u_Color * (diff + hdriIntensity * hdriLight);
vec3 specular = vec3(u_Specular) * spec;
vec3 emission = u_Color * u_Emission;