Skip to content

Commit

Permalink
Merge pull request #90 from Lyzev/89-suggestion-depth-texture-screenshot
Browse files Browse the repository at this point in the history
89 suggestion depth texture screenshot
  • Loading branch information
Lyzev authored May 25, 2024
2 parents 30003ca + 9b827a2 commit aa3363a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/dev/lyzev/api/opengl/shader/Shaders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EventRenderWorld>(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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit aa3363a

Please sign in to comment.