From 707c8896cef6bff291f5d2f3339cf9bf46ee2242 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 14 Sep 2026 11:46:56 +0530 Subject: Refactor stuff --- flake.nix | 5 +- src/main/kotlin/phenax/extensions/CameraManager.kt | 85 +++++++++ src/main/kotlin/phenax/utils/program.kt | 9 +- src/main/kotlin/play/PlayProgram.kt | 82 +++------ src/main/resources/shaders/modules/noise.glsl | 67 +++++++ src/main/resources/shaders/modules/sdf.cs | 12 -- src/main/resources/shaders/modules/sdf.glsl | 12 ++ src/main/resources/shaders/modules/utils.cs | 4 - src/main/resources/shaders/modules/utils.glsl | 4 + src/main/resources/shaders/play_shader.cs | 205 --------------------- src/main/resources/shaders/play_shader.glsl | 140 ++++++++++++++ 11 files changed, 335 insertions(+), 290 deletions(-) create mode 100644 src/main/kotlin/phenax/extensions/CameraManager.kt create mode 100644 src/main/resources/shaders/modules/noise.glsl delete mode 100644 src/main/resources/shaders/modules/sdf.cs create mode 100644 src/main/resources/shaders/modules/sdf.glsl delete mode 100644 src/main/resources/shaders/modules/utils.cs create mode 100644 src/main/resources/shaders/modules/utils.glsl delete mode 100644 src/main/resources/shaders/play_shader.cs create mode 100644 src/main/resources/shaders/play_shader.glsl diff --git a/flake.nix b/flake.nix index 399f1a6..d305eac 100644 --- a/flake.nix +++ b/flake.nix @@ -20,10 +20,11 @@ jdk21 libGL kotlin - kotlin-language-server - kotlin-interactive-shell just xvfb-run + kotlin-language-server + kotlin-interactive-shell + glsl_analyzer ]; LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs; diff --git a/src/main/kotlin/phenax/extensions/CameraManager.kt b/src/main/kotlin/phenax/extensions/CameraManager.kt new file mode 100644 index 0000000..ab2403c --- /dev/null +++ b/src/main/kotlin/phenax/extensions/CameraManager.kt @@ -0,0 +1,85 @@ +package phenax.extensions + +import org.openrndr.* +import org.openrndr.color.* +import org.openrndr.draw.* +import org.openrndr.math.Vector3 +import phenax.utils.recordingEnabled + +enum class ActionType { + PAN, + LIGHT_MOVE +} + +class CameraManager : Extension { + override var enabled: Boolean = true + + public var cameraPosition: Vector3 = Vector3(0.0, 1.0, 0.0) + public var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0) + + var font: FontImageMap? = null + var actionType: ActionType? = null + var lastPos: Vector3? = null + + override fun setup(program: Program) { + if (recordingEnabled) return + + font = program.loadFont("data/fonts/default.otf", 16.0) + + program.keyboard.keyDown.listen { if (it.key == KEY_ESCAPE) program.application.exit() } + + program.mouse.moved.listen { + val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0) + val pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens) + if (actionType == ActionType.PAN) { + lastPos?.let { lastPos -> cameraPosition -= (pos - lastPos) } + } else if (actionType == ActionType.LIGHT_MOVE) { + lastPos?.let { lastPos -> lightPosition += (pos - lastPos) } + } + actionType?.let { lastPos = pos } + } + + program.mouse.buttonDown.listen { + val isCtrl = KeyModifier.CTRL in it.modifiers + actionType = if (isCtrl) ActionType.LIGHT_MOVE else ActionType.PAN + } + + program.mouse.scrolled.listen { + val isCtrl = KeyModifier.CTRL in it.modifiers + if (isCtrl) { + lightPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) + } else { + cameraPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) + } + } + + program.mouse.buttonUp.listen { + actionType = null + lastPos = null + } + } + + fun setUniforms(cs: ComputeShader) { + cs.uniform("camera_position", cameraPosition) + cs.uniform("primary_light_position", lightPosition) + } + + override fun beforeDraw(drawer: Drawer, program: Program) {} + + override fun afterDraw(drawer: Drawer, program: Program) { + if (recordingEnabled) return + + drawer.fontMap = font + drawer.fill = ColorRGBa.WHITE + drawer.text( + "C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z), + 8.0, + 16.0 + ) + drawer.text( + "L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z), + 8.0, + 32.0 + ) + } +} diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt index d3788ea..ced3e3a 100644 --- a/src/main/kotlin/phenax/utils/program.kt +++ b/src/main/kotlin/phenax/utils/program.kt @@ -8,11 +8,6 @@ import org.openrndr.draw.* import org.openrndr.extra.filewatcher.watchingFile import org.openrndr.extra.olive.oliveProgram as oliveProgramOriginal -enum class ActionType { - PAN, - LIGHT_MOVE -} - fun Int.seconds(): Int = this * 1000 fun Int.minutes(): Int = this.seconds() * 60 @@ -51,7 +46,7 @@ fun preprocessShader(shaderCode: String): String { parts.add(shaderCode.slice(lastIndex..(it.range.first - 1))) lastIndex = it.range.last + 1 val modulePath = it.groupValues[1] - val moduleContents = File("src/main/resources/shaders/modules/${modulePath}.cs").readText() + val moduleContents = File("src/main/resources/shaders/modules/${modulePath}.glsl").readText() parts.add("/* Module ${modulePath} */") parts.add(moduleContents) parts.add("/* End module ${modulePath} */") @@ -64,7 +59,7 @@ val defaultShaderCode: String = "" fun Program.liveComputeShader(name: String): ReadOnlyProperty { val shaderCode by - watchingFile(File("src/main/resources/shaders/${name}.cs")) { file -> + watchingFile(File("src/main/resources/shaders/${name}.glsl")) { file -> println("change") preprocessShader(file.readText().toString()) } diff --git a/src/main/kotlin/play/PlayProgram.kt b/src/main/kotlin/play/PlayProgram.kt index ea54c35..93af7b3 100644 --- a/src/main/kotlin/play/PlayProgram.kt +++ b/src/main/kotlin/play/PlayProgram.kt @@ -1,11 +1,11 @@ package play -import org.openrndr.KeyModifier import org.openrndr.application import org.openrndr.color.* import org.openrndr.draw.* import org.openrndr.extra.noise.filters.SimplexNoise3D import org.openrndr.math.Vector3 +import phenax.extensions.CameraManager import phenax.utils.* fun main() = application { @@ -18,77 +18,39 @@ fun main() = application { } oliveProgram { - val font = loadFont("data/fonts/default.otf", 16.0) val cs by liveComputeShader("play_shader") val outputBuffer = colorBuffer(width, height, type = ColorType.UINT8) - var cameraPosition: Vector3 = Vector3(0.0, 1.0, 0.0) - var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0) - var actionType: ActionType? = null - var lastPos: Vector3? = null + val cloudNoiseBuffer = colorBuffer(width, height, type = ColorType.UINT8) + val cloudNoiseFilter = SimplexNoise3D() + cloudNoiseFilter.scale = Vector3.ONE * 1.0 + cloudNoiseFilter.octaves = 8 + cloudNoiseFilter.premultipliedAlpha = true - if (!recordingEnabled) { - mouse.moved.listen { - val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0) - val pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens) - if (actionType == ActionType.PAN) { - lastPos?.let { lastPos -> cameraPosition -= (pos - lastPos) } - } else if (actionType == ActionType.LIGHT_MOVE) { - lastPos?.let { lastPos -> lightPosition += (pos - lastPos) } - } - actionType?.let { lastPos = pos } - } - mouse.buttonDown.listen { - val isCtrl = KeyModifier.CTRL in it.modifiers - actionType = if (isCtrl) ActionType.LIGHT_MOVE else ActionType.PAN - } - mouse.scrolled.listen { - val isCtrl = KeyModifier.CTRL in it.modifiers - if (isCtrl) { - lightPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) - } else { - cameraPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) - } - } - mouse.buttonUp.listen { - actionType = null - lastPos = null - } - } + // val terrainNoiseBuffer = colorBuffer(2000, 2000, type = ColorType.UINT8) + // val terrainNoiseFilter = SimplexNoise3D() + // terrainNoiseFilter.seed = Vector3(0.0, 0.0, 0.0) + // terrainNoiseFilter.scale = Vector3.ONE * 1.0 + // terrainNoiseFilter.octaves = 8 + // terrainNoiseFilter.premultipliedAlpha = true - val noiseBuffer = colorBuffer(width, height, type = ColorType.UINT8) - val noiseFilter = SimplexNoise3D() - noiseFilter.seed = Vector3(0.0, 0.0, 0.1) - noiseFilter.scale = Vector3.ONE * 1.0 - noiseFilter.octaves = 8 - noiseFilter.premultipliedAlpha = true - noiseFilter.apply(emptyArray(), noiseBuffer) + var camera_manager = CameraManager() + extend(camera_manager) extend { - cs.image("noise_texture", 0, noiseBuffer.imageBinding(0, ImageAccess.READ)) + cloudNoiseFilter.seed = Vector3(0.02 * seconds, 0.0, 0.01 * seconds) + cloudNoiseFilter.apply(emptyArray(), cloudNoiseBuffer) + cs.image("cloud_noise_texture", 1, cloudNoiseBuffer.imageBinding(0, ImageAccess.READ)) + + // terrainNoiseFilter.apply(emptyArray(), terrainNoiseBuffer) + // cs.image("terrain_noise_texture", 2, terrainNoiseBuffer.imageBinding(0, ImageAccess.READ)) cs.uniform("seconds", seconds) - cs.uniform("camera_position", cameraPosition) - cs.uniform("primary_light_position", lightPosition) - cs.image("output_image", 2, outputBuffer.imageBinding(0, ImageAccess.WRITE)) + camera_manager.setUniforms(cs) + cs.image("output_image", 0, outputBuffer.imageBinding(0, ImageAccess.WRITE)) cs.execute(outputBuffer.width, outputBuffer.height, 1) drawer.image(outputBuffer) - - if (!recordingEnabled) { - drawer.fontMap = font - drawer.fill = ColorRGBa.WHITE - drawer.text( - "C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z), - 8.0, - 16.0 - ) - drawer.text( - "L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z), - 8.0, - 32.0 - ) - } } } } diff --git a/src/main/resources/shaders/modules/noise.glsl b/src/main/resources/shaders/modules/noise.glsl new file mode 100644 index 0000000..ebedd6d --- /dev/null +++ b/src/main/resources/shaders/modules/noise.glsl @@ -0,0 +1,67 @@ +// Noise Related Functions +float mod289(float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } +vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } +vec4 perm(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); } +float noise(vec3 p) { + vec3 a = floor(p); + vec3 d = p - a; + d = d * d * (3.0 - 2.0 * d); + vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); + vec4 k1 = perm(b.xyxy); + vec4 k2 = perm(k1.xyxy + b.zzww); + vec4 c = k2 + a.zzzz; + vec4 k3 = perm(c); + vec4 k4 = perm(c + 1.0); + vec4 o1 = fract(k3 * (1.0 / 41.0)); + vec4 o2 = fract(k4 * (1.0 / 41.0)); + vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); + vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); + return o4.y * d.y + o4.x * (1.0 - d.y); +} +float PHI = 1.61803398874989484820459; // Φ = Golden Ratio +float gold_noise(in vec2 xy, in float seed) { + return fract(tan(distance(xy * PHI, xy) * seed) * xy.x); +} +float random(vec3 scale, float seed) { + return fract(sin(dot(gl_GlobalInvocationID.xyz + seed, scale)) * 43758.5453 + + seed); +} + +// Fractal Brownian Noise +float fbm(vec3 x, int octaves) { + float v = 0.0; + float a = 0.5; + vec3 shift = vec3(100); + for (int i = 0; i < octaves; ++i) { + v += a * noise(x); + x = x * 2.0 + shift; + a *= 0.5; + } + return v; +} + +float random(vec2 st) { + return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123); +} + +float noise(vec2 st) { + vec2 i = floor(st); + vec2 f = fract(st); + float a = random(i); + float b = random(i + vec2(1.0, 0.0)); + float c = random(i + vec2(0.0, 1.0)); + float d = random(i + vec2(1.0, 1.0)); + vec2 u = f * f * (3.0 - 2.0 * f); + return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y; +} + +float fbm(vec2 st) { + float value = 0.0; + float amplitude = 0.5; + for (int i = 0; i < 5; i++) { + value += amplitude * noise(st); + st *= 2.0; + amplitude *= 0.5; + } + return value; +} diff --git a/src/main/resources/shaders/modules/sdf.cs b/src/main/resources/shaders/modules/sdf.cs deleted file mode 100644 index 8121b18..0000000 --- a/src/main/resources/shaders/modules/sdf.cs +++ /dev/null @@ -1,12 +0,0 @@ -float sdf_circle(in vec3 p, in float radius) { - return length(p) - radius; -} - -float sdf_plane(in vec3 p, in vec3 normal, in float offset) { - return dot(p, normal) + offset; -} - -float sdf_box(in vec3 p, in vec3 dimens, in float rounding) { - vec3 q = abs(p) - dimens + rounding; - return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - rounding; -} diff --git a/src/main/resources/shaders/modules/sdf.glsl b/src/main/resources/shaders/modules/sdf.glsl new file mode 100644 index 0000000..8121b18 --- /dev/null +++ b/src/main/resources/shaders/modules/sdf.glsl @@ -0,0 +1,12 @@ +float sdf_circle(in vec3 p, in float radius) { + return length(p) - radius; +} + +float sdf_plane(in vec3 p, in vec3 normal, in float offset) { + return dot(p, normal) + offset; +} + +float sdf_box(in vec3 p, in vec3 dimens, in float rounding) { + vec3 q = abs(p) - dimens + rounding; + return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - rounding; +} diff --git a/src/main/resources/shaders/modules/utils.cs b/src/main/resources/shaders/modules/utils.cs deleted file mode 100644 index 1616e8c..0000000 --- a/src/main/resources/shaders/modules/utils.cs +++ /dev/null @@ -1,4 +0,0 @@ -float smin(float a, float b, float k) { - float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); - return mix(b, a, h) - k * h * (1.0 - h); -} diff --git a/src/main/resources/shaders/modules/utils.glsl b/src/main/resources/shaders/modules/utils.glsl new file mode 100644 index 0000000..1616e8c --- /dev/null +++ b/src/main/resources/shaders/modules/utils.glsl @@ -0,0 +1,4 @@ +float smin(float a, float b, float k) { + float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); + return mix(b, a, h) - k * h * (1.0 - h); +} diff --git a/src/main/resources/shaders/play_shader.cs b/src/main/resources/shaders/play_shader.cs deleted file mode 100644 index ec19900..0000000 --- a/src/main/resources/shaders/play_shader.cs +++ /dev/null @@ -1,205 +0,0 @@ -#version 430 -layout(local_size_x = 1, local_size_y = 1) in; - -#include -#include - -uniform float seconds = 0; -layout(rgba8) uniform readonly image2D noise_texture; -layout(rgba8) uniform writeonly image2D output_image; -uniform vec3 camera_position = vec3(0, 1, 0); -uniform vec3 uv_direction = vec3(0); -uniform vec3 primary_light_position = vec3(1, 2, 1); - -#define MAX_RAYMARCH_ITERATIONS 500 -#define MAX_DISTANCE 100.0 -#define SURFACE_DIST 0.01 - -// Noise Related Functions -float mod289(float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } -vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } -vec4 perm(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); } -float noise(vec3 p) { - vec3 a = floor(p); - vec3 d = p - a; - d = d * d * (3.0 - 2.0 * d); - vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); - vec4 k1 = perm(b.xyxy); - vec4 k2 = perm(k1.xyxy + b.zzww); - vec4 c = k2 + a.zzzz; - vec4 k3 = perm(c); - vec4 k4 = perm(c + 1.0); - vec4 o1 = fract(k3 * (1.0 / 41.0)); - vec4 o2 = fract(k4 * (1.0 / 41.0)); - vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); - vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); - return o4.y * d.y + o4.x * (1.0 - d.y); -} -float PHI = 1.61803398874989484820459; // Φ = Golden Ratio -float gold_noise(in vec2 xy, in float seed) { - return fract(tan(distance(xy * PHI, xy) * seed) * xy.x); -} -float random(vec3 scale, float seed) { - return fract(sin(dot(gl_GlobalInvocationID.xyz + seed, scale)) * 43758.5453 + - seed); -} - -// Fractal Brownian Noise -float fbm(vec3 x, int octaves) { - float v = 0.0; - float a = 0.5; - vec3 shift = vec3(100); - for (int i = 0; i < octaves; ++i) { - v += a * noise(x); - x = x * 2.0 + shift; - a *= 0.5; - } - return v; -} - -float random(vec2 st) { - return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123); -} - -float noise(vec2 st) { - vec2 i = floor(st); - vec2 f = fract(st); - float a = random(i); - float b = random(i + vec2(1.0, 0.0)); - float c = random(i + vec2(0.0, 1.0)); - float d = random(i + vec2(1.0, 1.0)); - vec2 u = f * f * (3.0 - 2.0 * f); - return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y; -} - -float fbm(vec2 st) { - float value = 0.0; - float amplitude = 0.5; - for (int i = 0; i < 5; i++) { - value += amplitude * noise(st); - st *= 2.0; - amplitude *= 0.5; - } - return value; -} - -// TODO: Enumify -#define OID_TERRAIN 4. - -vec2 scene(in vec3 p) { - float c1 = sdf_circle(p - vec3(0, 1, 2.4), 0.2); - float c2 = sdf_circle(p - vec3(-0.6, 1, 3), 0.3); - float box = sdf_box(p - vec3(1, 0.5, 3), vec3(0.5, 0.18, 0.2), 0.07); - float terrain_noise = 6.0 * noise(p / 20.0) + 0.2 * noise((p + 200.0) / 2.0) + - 0.005 * noise((p - 50.0) * 20.0); - float terrain = - sdf_plane(p + vec3(0.0, terrain_noise, 0.0), vec3(0., 1., 0.), 0.0); - float light_indicator = - sdf_circle(p - primary_light_position + vec3(0.1, 0.1, -0.4), 0.01); - float dist = min(min(min(min(c1, c2), terrain), box), light_indicator); - float oid = 0.; - if (dist == c1) - oid = 1.; - else if (dist == c2) - oid = 2.; - else if (dist == box) - oid = 3.; - else if (dist == terrain) - oid = OID_TERRAIN; - else if (dist == light_indicator) - oid = -1.; - return vec2(dist, oid); -} - -vec2 ray_march_scene(in vec3 ray_origin, in vec3 ray_direction) { - float dist = 0.; - for (int i = 0; i < MAX_RAYMARCH_ITERATIONS && dist < MAX_DISTANCE; i++) { - vec3 p = ray_origin + dist * ray_direction; - vec2 obj = scene(p); - float gap = obj.x; - dist += gap; - if (gap <= SURFACE_DIST) - return vec2(dist, obj.y); - } - return vec2(dist, 0.0); -} - -vec3 surface_normal_scene(in vec3 p) { - vec2 inc = vec2(0.01, 0); - return normalize(vec3(scene(p + inc.xyy).x - scene(p - inc.xyy).x, - scene(p + inc.yxy).x - scene(p - inc.yxy).x, - scene(p + inc.yyx).x - scene(p - inc.yyx).x)); -} - -float shadow_scene(in vec3 ray_origin, in vec3 ray_direction, - in float light_intensity) { - float dist = 0.; - float res = 1.; - float prev_gap = 1e10; - for (int i = 0; i < 50 && dist < MAX_DISTANCE; i++) { - vec3 p = ray_origin + dist * ray_direction; - vec2 obj = scene(p); - float gap = obj.x; - if (obj.y < 0.) // Debug objects dont cast shadows - continue; - float y = gap * gap / (10000.0 * prev_gap); - float d = sqrt(abs(gap * gap - y * y)); - res = min(res, light_intensity * d / max(0., dist - y)); - prev_gap = gap; - dist += gap; - - if (res < 0.0001) - break; - } - res = clamp(res, 0.08, 1.0); - - return res * res * (3.0 - 2.0 * res); -} - -void main() { - vec2 resolution = vec2(imageSize(output_image).xy); - vec3 uv = vec3((gl_GlobalInvocationID.xy - resolution / 2.0) / resolution, 0); - float aspect_ratio = resolution.y / resolution.x; - uv /= vec3(aspect_ratio, 1, 1); - - vec3 uv_direction = normalize(vec3(uv.x, uv.y, 1)); - - vec3 light_pos = primary_light_position; - - vec2 obj = ray_march_scene(camera_position, uv_direction); - float depth = obj.x; - float oid = obj.y; - vec3 p = camera_position + uv_direction * depth; - vec3 normal = surface_normal_scene(p); - vec3 light_dir = normalize(light_pos - p); - vec3 hal = normalize(light_dir - uv_direction); - - vec3 col = vec3(0.9, 0.3, 0.4); - if (oid < 0.) // Debug objects - col = vec3(1.0, 0.2, 0.2); - else if (oid == OID_TERRAIN) - col = vec3(0.24, 0.61, 0.08); - else if (oid == 0) { // Sky - vec4 cloud_noise = - imageLoad(noise_texture, ivec2(gl_GlobalInvocationID.xy)); - col = vec3(0.35, 0.6, 0.9); - col = mix(col, cloud_noise.xyz, clamp(0.5, 0., 1.)); - } - - if (depth < MAX_DISTANCE && oid > 0.) { - float contrast = 1.; - contrast *= clamp(dot(normal, light_dir), 0, 1.); - contrast *= shadow_scene(p, light_dir, 24.0); - float spe = - pow(clamp(dot(normal, hal), 0.0, 1.0), 16.0) * contrast * - (0.04 + 0.96 * pow(clamp(1.0 + dot(hal, uv_direction), 0.0, 1.0), 5.0)); - col = col * 1.1 + 0.01; - if (oid != OID_TERRAIN) - col += 12.0 * spe * vec3(1.00, 0.70, 0.5); - col *= contrast; - // col *= max(0.1, exp(-0.00005 * depth * depth * depth)); - } - - imageStore(output_image, ivec2(gl_GlobalInvocationID.xy), - vec4(clamp(col, 0., 1.), 1)); -} diff --git a/src/main/resources/shaders/play_shader.glsl b/src/main/resources/shaders/play_shader.glsl new file mode 100644 index 0000000..0737bed --- /dev/null +++ b/src/main/resources/shaders/play_shader.glsl @@ -0,0 +1,140 @@ +#version 430 +layout(local_size_x = 1, local_size_y = 1) in; + +#include +#include +#include + +uniform float seconds = 0; +layout(rgba8) uniform readonly image2D cloud_noise_texture; +// layout(rgba8) uniform readonly image2D terrain_noise_texture; +layout(rgba8) uniform writeonly image2D output_image; +uniform vec3 camera_position = vec3(0, 1, 0); +uniform vec3 uv_direction = vec3(0); +uniform vec3 primary_light_position = vec3(1, 2, 1); + +#define MAX_RAYMARCH_ITERATIONS 500 +#define MAX_DISTANCE 100.0 +#define SURFACE_DIST 0.01 + +// TODO: Enumify +#define OID_TERRAIN 4. + +vec2 scene(in vec3 p) { + float c1 = sdf_circle(p - vec3(0, 1, 2.4), 0.2); + float c2 = sdf_circle(p - vec3(-0.6, 1, 3), 0.3); + float box = sdf_box(p - vec3(1, 0.5, 3), vec3(0.5, 0.18, 0.2), 0.07); + float terrain_noise = 6.0 * noise(p / 20.0) + 0.2 * noise((p + 10.0) / 2.0); + float terrain = + sdf_plane(p + vec3(0.0, terrain_noise, 0.0), vec3(0., 1., 0.), 0.0); + float light_indicator = + sdf_circle(p - primary_light_position + vec3(0.1, 0.1, -0.4), 0.01); + float dist = min(min(min(min(c1, c2), terrain), box), light_indicator); + float oid = 0.; + if (dist == c1) + oid = 1.; + else if (dist == c2) + oid = 2.; + else if (dist == box) + oid = 3.; + else if (dist == terrain) + oid = OID_TERRAIN; + else if (dist == light_indicator) + oid = -1.; + return vec2(dist, oid); +} + +vec2 ray_march_scene(in vec3 ray_origin, in vec3 ray_direction) { + float dist = 0.; + for (int i = 0; i < MAX_RAYMARCH_ITERATIONS && dist < MAX_DISTANCE; i++) { + vec3 p = ray_origin + dist * ray_direction; + vec2 obj = scene(p); + float gap = obj.x; + dist += gap; + if (gap <= SURFACE_DIST) + return vec2(dist, obj.y); + } + return vec2(dist, 0.0); +} + +float shadow_scene(in vec3 ray_origin, in vec3 ray_direction, + in float light_intensity) { + float dist = 0.; + float res = 1.; + float prev_gap = 1e10; + for (int i = 0; i < 50 && dist < MAX_DISTANCE; i++) { + vec3 p = ray_origin + dist * ray_direction; + vec2 obj = scene(p); + float gap = obj.x; + if (obj.y < 0.) // Debug objects dont cast shadows + continue; + float y = gap * gap / (10000.0 * prev_gap); + float d = sqrt(abs(gap * gap - y * y)); + res = min(res, light_intensity * d / max(0., dist - y)); + prev_gap = gap; + dist += gap; + + if (res < 0.0001) + break; + } + res = clamp(res, 0.08, 1.0); + + return res * res * (3.0 - 2.0 * res); +} + +vec3 surface_normal_scene(in vec3 p) { + vec2 inc = vec2(0.01, 0); + return normalize(vec3(scene(p + inc.xyy).x - scene(p - inc.xyy).x, + scene(p + inc.yxy).x - scene(p - inc.yxy).x, + scene(p + inc.yyx).x - scene(p - inc.yyx).x)); +} + +void main() { + vec2 resolution = vec2(imageSize(output_image).xy); + vec3 uv = vec3((gl_GlobalInvocationID.xy - resolution / 2.0) / resolution, 0); + float aspect_ratio = resolution.y / resolution.x; + uv /= vec3(aspect_ratio, 1, 1); + + vec3 uv_direction = normalize(vec3(uv.x, uv.y, 1)); + + vec3 light_pos = primary_light_position; + + vec2 obj = ray_march_scene(camera_position, uv_direction); + float depth = obj.x; + float oid = obj.y; + vec3 p = camera_position + uv_direction * depth; + vec3 normal = surface_normal_scene(p); + vec3 light_dir = normalize(light_pos - p); + vec3 hal = normalize(light_dir - uv_direction); + + vec3 col = vec3(0.9, 0.3, 0.4); + if (oid < 0.) // Debug objects + col = vec3(1.0, 0.2, 0.2); + else if (oid == OID_TERRAIN) + col = vec3(0.24, 0.61, 0.08); + else if (oid == 0) { // Sky + vec4 cloud_noise = + imageLoad(cloud_noise_texture, ivec2(gl_GlobalInvocationID.xy)); + col = vec3(0.35, 0.6, 0.9); + col = mix(col, cloud_noise.xyz, clamp(0.5, 0., 1.)); + } + + if (depth < MAX_DISTANCE && oid > 0.) { + float contrast = 1.; + contrast *= clamp(dot(normal, light_dir), 0, 1.); + contrast *= shadow_scene(p, light_dir, 24.0); + col = col * 1.1 + 0.01; + col *= contrast; + if (oid != OID_TERRAIN) { + float spe = + pow(clamp(dot(normal, hal), 0.0, 1.0), 16.0) * contrast * + (0.04 + + 0.96 * pow(clamp(1.0 + dot(hal, uv_direction), 0.0, 1.0), 5.0)); + col += 12.0 * spe * vec3(1.00, 0.70, 0.5); + } + // col *= max(0.1, exp(-0.00005 * depth * depth * depth)); + } + + imageStore(output_image, ivec2(gl_GlobalInvocationID.xy), + vec4(clamp(col, 0., 1.), 1)); +} -- cgit v1.3.1