From 55aa0574e940061f5fa94223c3c91b55a6c4edab Mon Sep 17 00:00:00 2001 From: Lyzev Date: Sat, 25 May 2024 18:06:01 +0200 Subject: [PATCH 1/2] added feature for taking depth screenshots Signed-off-by: Lyzev --- .../ModuleRunnableDepthTextureScreenshot.kt | 69 +++++++++++++++++++ .../LinearizeDepth/LinearizeDepth_FP.glsl | 25 +++++++ .../LinearizeDepth/LinearizeDepth_VP.glsl | 14 ++++ 3 files changed, 108 insertions(+) create mode 100644 src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/util/ModuleRunnableDepthTextureScreenshot.kt create mode 100644 src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_FP.glsl create mode 100644 src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_VP.glsl diff --git a/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/util/ModuleRunnableDepthTextureScreenshot.kt b/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/util/ModuleRunnableDepthTextureScreenshot.kt new file mode 100644 index 0000000000..58d6a1c9de --- /dev/null +++ b/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/util/ModuleRunnableDepthTextureScreenshot.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024. Schizoid + * All rights reserved. + */ + +package dev.lyzev.schizoid.feature.features.module.modules.util + +import com.mojang.blaze3d.systems.RenderSystem +import dev.lyzev.api.events.Event +import dev.lyzev.api.events.EventListener +import dev.lyzev.api.events.EventRenderWorld +import dev.lyzev.api.events.on +import dev.lyzev.api.opengl.Render +import dev.lyzev.api.opengl.WrappedFramebuffer +import dev.lyzev.api.opengl.clear +import dev.lyzev.api.opengl.save +import dev.lyzev.api.opengl.shader.Shader +import dev.lyzev.api.opengl.shader.ShaderLinearizeDepth +import dev.lyzev.api.setting.settings.text +import dev.lyzev.schizoid.feature.IFeature +import dev.lyzev.schizoid.feature.features.module.ModuleRunnable +import net.minecraft.client.render.GameRenderer +import org.lwjgl.opengl.GL13.GL_TEXTURE0 + +object ModuleRunnableDepthTextureScreenshot : ModuleRunnable( + "Depth Texture Screenshot", "Takes a screenshot of the depth texture.", category = IFeature.Category.UTIL +), EventListener { + + private val fbo = WrappedFramebuffer() + private var isTakingScreenshot = false + + override fun invoke(): String? { + if (isTakingScreenshot) { + return "Already taking a screenshot." + } else + isTakingScreenshot = true + return null + } + + override val shouldHandleEvents: Boolean + get() = isTakingScreenshot + + init { + on(Event.Priority.HIGHEST) { + isTakingScreenshot = false + Render.store() + RenderSystem.disableCull() + RenderSystem.defaultBlendFunc() + RenderSystem.enableBlend() + + fbo.clear() + fbo.beginWrite(true) + + ShaderLinearizeDepth.bind() + RenderSystem.activeTexture(GL_TEXTURE0) + RenderSystem.bindTexture(mc.framebuffer.depthAttachment) + ShaderLinearizeDepth["Tex0"] = 0 + ShaderLinearizeDepth["Near"] = GameRenderer.CAMERA_DEPTH + ShaderLinearizeDepth["Far"] = mc.gameRenderer.farPlaneDistance + Shader.drawFullScreen() + ShaderLinearizeDepth.unbind() + + RenderSystem.enableCull() + Render.restore() + + fbo.save() + } + } +} diff --git a/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_FP.glsl b/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_FP.glsl new file mode 100644 index 0000000000..ad667b4b7d --- /dev/null +++ b/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_FP.glsl @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023. Schizoid + * All rights reserved. + */ + +#version 330 + +#include "Depth.glsl" + +in vec2 uv; +out vec4 color; + +uniform sampler2D Tex0; +uniform float Near; +uniform float Far; + +void main() { + // Read in depth value from depth texture + float depthValue = texture(Tex0, uv).x; + + // Convert depth value to distance + float distance = linearizeDepth(depthValue, Near, Far) / Far; + + color = vec4(vec3(distance), 1); +} diff --git a/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_VP.glsl b/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_VP.glsl new file mode 100644 index 0000000000..4606c2e40f --- /dev/null +++ b/src/main/resources/assets/schizoid/shaders/core/LinearizeDepth/LinearizeDepth_VP.glsl @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023. Schizoid + * All rights reserved. + */ + +#version 330 + +in vec2 position; +out vec2 uv; + +void main() { + gl_Position = vec4(position, 0, 1); + uv = position * .5 + .5; +} \ No newline at end of file From 9b827a25e0cae4d25f07245e0d2f1531eb68ce8d Mon Sep 17 00:00:00 2001 From: Lyzev Date: Sat, 25 May 2024 18:06:29 +0200 Subject: [PATCH 2/2] missing shader object Signed-off-by: Lyzev --- src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt index 3be509d686..3d04df3028 100644 --- a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt +++ b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt @@ -82,6 +82,7 @@ object ShaderAdd : ShaderVertexFragment("Add") object ShaderPassThrough : ShaderVertexFragment("PassThrough") object ShaderDepth : ShaderVertexFragment("Depth") +object ShaderLinearizeDepth : ShaderVertexFragment("LinearizeDepth") object ShaderThreshold : ShaderVertexFragment("Threshold") object ShaderBlend : ShaderVertexFragment("Blend")