diff options
author | Vixid <52578495+VixidDev@users.noreply.github.com> | 2024-02-20 18:44:13 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-20 19:44:13 +0100 |
commit | cba23271513a6809f24760a4b93687d8148f813f (patch) | |
tree | 7ad6e9cbb31587cb65817745410fa92ab85272d8 /src | |
parent | 841a231b8a0d219592fbfbeb4dfb573b97e409ff (diff) | |
download | skyhanni-cba23271513a6809f24760a4b93687d8148f813f.tar.gz skyhanni-cba23271513a6809f24760a4b93687d8148f813f.tar.bz2 skyhanni-cba23271513a6809f24760a4b93687d8148f813f.zip |
Adds a chroma shader to be used on non-textured GUI elements. #960
Diffstat (limited to 'src')
11 files changed, 134 insertions, 25 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaFontRenderer.kt b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaFontRenderer.kt index 26dc45969..4e3a4d37f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaFontRenderer.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaFontRenderer.kt @@ -36,7 +36,7 @@ class ChromaFontRenderer(private val baseColor: Int) { fun newChromaEnv(): ChromaFontRenderer { if (ShaderHelper.areShadersSupported()) { - ChromaShaderManager.begin() + ChromaShaderManager.begin(ChromaType.TEXTURED) GlStateManager.shadeModel(GL11.GL_SMOOTH) } return this diff --git a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShader.kt b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShader.kt index 61e69a67b..96b6d24b7 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShader.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShader.kt @@ -14,11 +14,8 @@ import net.minecraft.client.Minecraft * Credit: [ChromaShader.java](https://github.com/BiscuitDevelopment/SkyblockAddons/blob/main/src/main/java/codes/biscuit/skyblockaddons/shader/chroma/ChromaShader.java) */ -object ChromaShader : Shader("chroma", "chroma") { - +abstract class ChromaShader(vertex: String, fragment: String) : Shader(vertex, fragment) { val config get() = SkyHanniMod.feature.chroma - val INSTANCE: ChromaShader - get() = this override fun registerUniforms() { registerUniform(Uniform.UniformType.FLOAT, "chromaSize") { diff --git a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShaderManager.kt b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShaderManager.kt index ccf0ec21e..98e1daa71 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShaderManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chroma/ChromaShaderManager.kt @@ -13,19 +13,27 @@ object ChromaShaderManager { private var chromaEnabled = false - fun begin() { + /** + * Enables the type of chroma shader passed in + * + * @param chromaType A type of chroma shader from [ChromaType] + */ + fun begin(chromaType: ChromaType) { disable() - enable() + enable(chromaType) } + /** + * Disables the currently active chroma shader + */ fun end() { disable() } - private fun enable() { + private fun enable(chromaType: ChromaType) { if (!chromaEnabled) { chromaEnabled = true - ShaderManager.enableShader("chroma") + ShaderManager.enableShader(chromaType.shaderName) } } @@ -35,4 +43,15 @@ object ChromaShaderManager { ShaderManager.disableShader() } } +} + +enum class ChromaType(val shaderName: String) { + /** + * See [StandardChromaShader] + */ + STANDARD("standard_chroma"), + /** + * See [TexturedChromaShader] + */ + TEXTURED("textured_chroma") }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/chroma/StandardChromaShader.kt b/src/main/java/at/hannibal2/skyhanni/features/chroma/StandardChromaShader.kt new file mode 100644 index 000000000..268b37b4c --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/chroma/StandardChromaShader.kt @@ -0,0 +1,20 @@ +package at.hannibal2.skyhanni.features.chroma + +/** + * A type of chroma shader generally used for standard GUI elements using Gui.drawRect and + * other GUI element drawing functions. + * + * Explicitly those that do not depend on a texture. + * + * **Usage:** + * + * ``` + * ChromaShaderManager.begin(ChromaType.STANDARD) + * // draw GUI element here + * ChromaShaderManager.end() + * ``` + */ +object StandardChromaShader : ChromaShader("standard_chroma", "standard_chroma") { + val INSTANCE: StandardChromaShader + get() = this +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/chroma/TexturedChromaShader.kt b/src/main/java/at/hannibal2/skyhanni/features/chroma/TexturedChromaShader.kt new file mode 100644 index 000000000..544d10184 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/chroma/TexturedChromaShader.kt @@ -0,0 +1,20 @@ +package at.hannibal2.skyhanni.features.chroma + +/** + * This chroma type is used for GUI elements with textures, that includes text, + * any assets from Minecraft or from the mod, etc... + * + * **Usage:** + * + * If you want to use chroma for text see [FontRendererHook][at.hannibal2.skyhanni.mixins.hooks.FontRendererHook.setupChromaFont] + * + * ``` + * ChromaShaderManager.begin(ChromaType.TEXTURE) + * // draw GUI element here + * ChromaShaderManager.end() + * ``` + */ +object TexturedChromaShader : ChromaShader("textured_chroma", "textured_chroma") { + val INSTANCE: TexturedChromaShader + get() = this +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/shader/Shader.kt b/src/main/java/at/hannibal2/skyhanni/utils/shader/Shader.kt index f846c3d4d..dedb60ef2 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/shader/Shader.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/shader/Shader.kt @@ -69,14 +69,14 @@ abstract class Shader(val vertex: String, val fragment: String) { if (ShaderHelper.glGetProgrami(shaderProgram, ShaderHelper.GL_LINK_STATUS) == GL11.GL_FALSE) { val errorMessage = "Failed to link vertex shader $vertex and fragment shader $fragment. Features that " + - "utilise this shader will not work correctly, if at all." + "utilise this shader will not work correctly, if at all" val errorLog = StringUtils.trim(ShaderHelper.glGetShaderInfoLog(shaderProgram, 1024)) if (ShaderManager.inWorld()) { ErrorManager.logErrorWithData( - OpenGLException("Shader linking error."), - errorMessage, - "Link Error:\n" to errorLog + OpenGLException("Shader linking error."), + errorMessage, + "Link Error:\n" to errorLog ) } else { LorenzUtils.consoleLog("$errorMessage $errorLog") diff --git a/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderManager.kt b/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderManager.kt index f9b5541c5..5621a5f87 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderManager.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.utils.shader -import at.hannibal2.skyhanni.features.chroma.ChromaShader +import at.hannibal2.skyhanni.features.chroma.StandardChromaShader +import at.hannibal2.skyhanni.features.chroma.TexturedChromaShader import at.hannibal2.skyhanni.features.misc.RoundedRectangleShader import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.LorenzUtils @@ -22,13 +23,15 @@ object ShaderManager { */ enum class Shaders(val shader: Shader) { - CHROMA(ChromaShader.INSTANCE), + STANDARD_CHROMA(StandardChromaShader.INSTANCE), + TEXTURED_CHROMA(TexturedChromaShader.INSTANCE), ROUNDED_RECTANGLE(RoundedRectangleShader.INSTANCE); companion object { fun getShaderInstance(shaderName: String): Shader? = when (shaderName) { - "chroma" -> CHROMA.shader + "standard_chroma" -> STANDARD_CHROMA.shader + "textured_chroma" -> TEXTURED_CHROMA.shader "rounded_rect" -> ROUNDED_RECTANGLE.shader else -> { null @@ -83,7 +86,7 @@ object ShaderManager { if (ShaderHelper.glGetShaderi(shaderID, ShaderHelper.GL_COMPILE_STATUS) == 0) { val errorMessage = "Failed to compile shader $fileName${type.extension}. Features that utilise this " + - "shader will not work correctly, if at all." + "shader will not work correctly, if at all" val errorLog = StringUtils.trim(ShaderHelper.glGetShaderInfoLog(shaderID, 1024)) if (inWorld()) { diff --git a/src/main/resources/assets/skyhanni/shaders/standard_chroma.fsh b/src/main/resources/assets/skyhanni/shaders/standard_chroma.fsh new file mode 100644 index 000000000..8e10708bb --- /dev/null +++ b/src/main/resources/assets/skyhanni/shaders/standard_chroma.fsh @@ -0,0 +1,37 @@ +// Chroma Fragment Shader +// (Same as textured_chroma.fsh but isn't restricted to textured elements) + +#version 130 + +uniform float chromaSize; +uniform float timeOffset; +uniform float saturation; +uniform bool forwardDirection; + +in vec4 originalColor; + +float rgb2b(vec3 rgb) { + return max(max(rgb.r, rgb.g), rgb.b); +} + +vec3 hsb2rgb_smooth(vec3 c) { + vec3 rgb = clamp(abs(mod(c.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0); + rgb = rgb * rgb * (3.0 - 2.0 * rgb); // Cubic smoothing + return c.z * mix(vec3(1.0), rgb, c.y); +} + +void main() { + // Determine the direction chroma moves + float fragCoord; + if (forwardDirection) { + fragCoord = gl_FragCoord.x - gl_FragCoord.y; + } else { + fragCoord = gl_FragCoord.x + gl_FragCoord.y; + } + + // The hue takes in account the position, chroma settings, and time + float hue = mod(((fragCoord) / chromaSize) - timeOffset, 1.0); + + // Set the color to use the new hue & original saturation/value/alpha values + gl_FragColor = vec4(hsb2rgb_smooth(vec3(hue, saturation, rgb2b(originalColor.rgb))), originalColor.a); +}
\ No newline at end of file diff --git a/src/main/resources/assets/skyhanni/shaders/standard_chroma.vsh b/src/main/resources/assets/skyhanni/shaders/standard_chroma.vsh new file mode 100644 index 000000000..4895d119e --- /dev/null +++ b/src/main/resources/assets/skyhanni/shaders/standard_chroma.vsh @@ -0,0 +1,13 @@ +// Chroma Vertex Shader +// (Same as textured_chroma.vsh but isn't restricted to only texture elements) + +#version 130 + +out vec4 originalColor; + +void main() { + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; + + // Pass original color to fragment + originalColor = gl_Color; +}
\ No newline at end of file diff --git a/src/main/resources/assets/skyhanni/shaders/chroma.fsh b/src/main/resources/assets/skyhanni/shaders/textured_chroma.fsh index 7b48a62f9..2f3d76af7 100644 --- a/src/main/resources/assets/skyhanni/shaders/chroma.fsh +++ b/src/main/resources/assets/skyhanni/shaders/textured_chroma.fsh @@ -1,8 +1,8 @@ -// Chroma Fragment Shader +// Textured Chroma Fragment Shader // Modified from SkyblockAddons // Credit: https://github.com/BiscuitDevelopment/SkyblockAddons/blob/main/src/main/resources/assets/skyblockaddons/shaders/program/chroma_screen_textured.fsh -#version 120 +#version 130 uniform float chromaSize; uniform float timeOffset; @@ -11,8 +11,8 @@ uniform bool forwardDirection; uniform sampler2D outTexture; -varying vec2 outTextureCoords; -varying vec4 outColor; +in vec2 outTextureCoords; +in vec4 outColor; float rgb2b(vec3 rgb) { return max(max(rgb.r, rgb.g), rgb.b); diff --git a/src/main/resources/assets/skyhanni/shaders/chroma.vsh b/src/main/resources/assets/skyhanni/shaders/textured_chroma.vsh index 5f5030d07..87ca9fece 100644 --- a/src/main/resources/assets/skyhanni/shaders/chroma.vsh +++ b/src/main/resources/assets/skyhanni/shaders/textured_chroma.vsh @@ -1,10 +1,10 @@ -// Chroma Vertex Shader +// Textured Chroma Vertex Shader // Credit: https://github.com/BiscuitDevelopment/SkyblockAddons/blob/main/src/main/resources/assets/skyblockaddons/shaders/program/chroma_screen_textured.vsh -#version 120 +#version 130 -varying vec2 outTextureCoords; -varying vec4 outColor; +out vec2 outTextureCoords; +out vec4 outColor; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; |