From a49a1c238bfc665441a63645ba372f61b32186d9 Mon Sep 17 00:00:00 2001 From: endernon Date: Sat, 21 Dec 2024 23:56:13 +0000 Subject: [PATCH] hell yeah, glsl shaders. Credit to focalsalt for being a massive help. --- .../minecraft/shaders/core/position_color.fsh | 84 +++++++++++++++++++ .../shaders/core/position_color.json | 19 +++++ .../minecraft/shaders/core/position_color.vsh | 61 ++++++++++++++ .../minecraft/shaders/core/rendertype_gui.fsh | 82 ++++++++++++++++++ .../shaders/core/rendertype_gui.json | 19 +++++ .../minecraft/shaders/core/rendertype_gui.vsh | 61 ++++++++++++++ 6 files changed, 326 insertions(+) create mode 100644 assets/minecraft/shaders/core/position_color.fsh create mode 100644 assets/minecraft/shaders/core/position_color.json create mode 100644 assets/minecraft/shaders/core/position_color.vsh create mode 100644 assets/minecraft/shaders/core/rendertype_gui.fsh create mode 100644 assets/minecraft/shaders/core/rendertype_gui.json create mode 100644 assets/minecraft/shaders/core/rendertype_gui.vsh diff --git a/assets/minecraft/shaders/core/position_color.fsh b/assets/minecraft/shaders/core/position_color.fsh new file mode 100644 index 0000000..49c6bc0 --- /dev/null +++ b/assets/minecraft/shaders/core/position_color.fsh @@ -0,0 +1,84 @@ +#version 150 + +in vec4 vertexColor; + +flat in vec2 flatCorner; +in vec2 Pos1; +in vec2 Pos2; +in vec4 Coords; +in vec2 position; + +uniform vec4 ColorModulator; + +out vec4 fragColor; + +vec4 colors[4] = vec4[]( + vec4(0), + vec4(20, 17, 25, 255) / 255, + vec4(73, 40, 121, 255) / 255, + vec4(42, 31, 61, 255) / 255 +); + +int bitmap[64] = int[]( + 0, 0, 1, 1, 1, 1, 1, 1, + 0, 1, 3, 3, 3, 3, 3, 3, + 1, 3, 3, 2, 2, 2, 2, 2, + 1, 3, 2, 2, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3 +); + + + +void main() { + vec4 color = vertexColor; + if (color.a == 0.0) { + discard; + } + + fragColor = color * ColorModulator; + + if (flatCorner != vec2(-1)) + { + //Actual Pos + vec2 APos1 = Pos1; + vec2 APos2 = Pos2; + APos1 = round(APos1 / (flatCorner.x == 0 ? 1 - Coords.z : 1 - Coords.w)); //Right-up corner + APos2 = round(APos2 / (flatCorner.x == 0 ? Coords.w : Coords.z)); //Left-down corner + + ivec2 res = ivec2(abs(APos1 - APos2)) - 1; //Resolution of frame + ivec2 stp = ivec2(min(APos1, APos2)); //Left-Up corner + ivec2 pos = ivec2(floor(position)) - stp; //Position in frame + + vec4 col = vec4(42, 31, 61, 255) / 255.0; + col.rgb -= max(1 - length((pos - res / 2.0) / res) * 2, 0) / 10; + + ivec2 corner = min(pos, res - pos); + + if (corner.x < 8 && corner.y < 8) + { + int bit = bitmap[corner.y * 8 + corner.x]; + if (bit == 0) + discard; + if (bit != 4) + col = colors[bit]; + } + else if (corner.x == 0) // left and right side: Outer + col = colors[1]; + else if (corner.x == 2) // left and right side: Middle + col = colors[3]; + else if (corner.x == 3) // left and right side: Inner ish + col = colors[2]; + else if (corner.y == 0) // top and bottom side: Outer + col = colors[1]; + else if (corner.y == 2) // top and bottom side: Middle + col = colors[3]; + else if (corner.y == 3) // top and bottom side: Inner ish + col = colors[2]; + + fragColor = col; + + } +} diff --git a/assets/minecraft/shaders/core/position_color.json b/assets/minecraft/shaders/core/position_color.json new file mode 100644 index 0000000..84df399 --- /dev/null +++ b/assets/minecraft/shaders/core/position_color.json @@ -0,0 +1,19 @@ +{ + "blend": { + "func": "add", + "srcrgb": "srcalpha", + "dstrgb": "1-srcalpha" + }, + "vertex": "position_color", + "fragment": "position_color", + "attributes": [ + "Color" + ], + "samplers": [ + ], + "uniforms": [ + { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] } + ] +} diff --git a/assets/minecraft/shaders/core/position_color.vsh b/assets/minecraft/shaders/core/position_color.vsh new file mode 100644 index 0000000..9a84f22 --- /dev/null +++ b/assets/minecraft/shaders/core/position_color.vsh @@ -0,0 +1,61 @@ +#version 150 + +in vec3 Position; +in vec4 Color; + +uniform mat4 ModelViewMat; +uniform mat4 ProjMat; + +uniform vec2 ScreenSize; + +out vec4 vertexColor; +out vec4 Coords; +out vec2 position; + +flat out vec2 flatCorner; +out vec2 Pos1; +out vec2 Pos2; + +bool isTooltip(mat4 ProjMat,vec3 Position) { + return ProjMat[2][3] == 0 && Position.z > 300 && Position.z < 500; +} + +void main() { + vertexColor = Color; + + gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0); + + Coords = vec4(-1); + position = vec2(-1); + Pos1 = vec2(-1); + Pos2 = vec2(-1); + flatCorner = vec2(-1); + + //Tooltip frame + if (Color.r != 0 && Color.g == 0 && Color.b != 0 && ProjMat[3][0] == -1) + { + int id = gl_VertexID / 4; + + int vertID = (gl_VertexID) % 4; + const vec2[4] corners = vec2[4](vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0)); + vec2 ScrSize = 2 / vec2(ProjMat[0][0], -ProjMat[1][1]); + vec3 Pos = Position - vec3(corners[((gl_VertexID) ) % 4] * -10 - (-10 / 2.0), 0); + + Pos1 = Pos2 = vec2(0); + if (vertID == 0) Pos1 = Pos.xy; + if (vertID == 2) Pos2 = Pos.xy; + + Coords.xy = ScrSize; + Coords.zw = flatCorner = corners[vertID]; + position = Pos.xy; + + if (id != 2) + Pos.xy = vec2(0); + + gl_Position = ProjMat * ModelViewMat * vec4(Pos, 1); + } + + if (isTooltip(ProjMat, Position) && (gl_Position.x > 1 || gl_Position.x < -0.99)) { + vertexColor = vec4(0); + } +} diff --git a/assets/minecraft/shaders/core/rendertype_gui.fsh b/assets/minecraft/shaders/core/rendertype_gui.fsh new file mode 100644 index 0000000..90e5191 --- /dev/null +++ b/assets/minecraft/shaders/core/rendertype_gui.fsh @@ -0,0 +1,82 @@ +#version 150 + +in vec4 vertexColor; + +flat in vec2 flatCorner; +in vec2 Pos1; +in vec2 Pos2; +in vec4 Coords; +in vec2 position; + +uniform vec4 ColorModulator; + +out vec4 fragColor; + +vec4 colors[4] = vec4[]( + vec4(0), + vec4(20, 17, 25, 255) / 255, + vec4(73, 40, 121, 255) / 255, + vec4(42, 31, 61, 255) / 255 +); + +int bitmap[64] = int[]( + 0, 0, 1, 1, 1, 1, 1, 1, + 0, 1, 3, 3, 3, 3, 3, 3, + 1, 3, 3, 2, 2, 2, 2, 2, + 1, 3, 2, 2, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3, + 1, 3, 2, 3, 3, 3, 3, 3 +); + + + +void main() { + vec4 color = vertexColor; + if (color.a == 0.0) { + discard; + } + + fragColor = color * ColorModulator; + + if (flatCorner != vec2(-1)) + { + //Actual Pos + vec2 APos1 = Pos1; + vec2 APos2 = Pos2; + APos1 = round(APos1 / (flatCorner.x == 0 ? 1 - Coords.z : 1 - Coords.w)); //Right-up corner + APos2 = round(APos2 / (flatCorner.x == 0 ? Coords.w : Coords.z)); //Left-down corner + + ivec2 res = ivec2(abs(APos1 - APos2)) - 1; //Resolution of frame + ivec2 stp = ivec2(min(APos1, APos2)); //Left-Up corner + ivec2 pos = ivec2(floor(position)) - stp; //Position in frame + + vec4 col = vec4(42, 31, 61, 255) / 255.0; + ivec2 corner = min(pos, res - pos); + + if (corner.x < 8 && corner.y < 8) + { + int bit = bitmap[corner.y * 8 + corner.x]; + if (bit == 0) + discard; + if (bit != 4) + col = colors[bit]; + } + else if (corner.x == 0) // left and right side: Outer + col = colors[1]; + else if (corner.x == 1) // left and right side: Middle + col = colors[3]; + else if (corner.x == 2) // left and right side: Inner ish + col = colors[2]; + else if (corner.y == 0) // top and bottom side: Outer + col = colors[1]; + else if (corner.y == 1) // top and bottom side: Middle + col = colors[3]; + else if (corner.y == 2) // top and bottom side: Inner ish + col = colors[2]; + + fragColor = col; + + } +} diff --git a/assets/minecraft/shaders/core/rendertype_gui.json b/assets/minecraft/shaders/core/rendertype_gui.json new file mode 100644 index 0000000..c1bc03d --- /dev/null +++ b/assets/minecraft/shaders/core/rendertype_gui.json @@ -0,0 +1,19 @@ +{ + "blend": { + "func": "add", + "srcrgb": "srcalpha", + "dstrgb": "1-srcalpha" + }, + "vertex": "rendertype_gui", + "fragment": "rendertype_gui", + "attributes": [ + "Color" + ], + "samplers": [ + ], + "uniforms": [ + { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] } + ] +} diff --git a/assets/minecraft/shaders/core/rendertype_gui.vsh b/assets/minecraft/shaders/core/rendertype_gui.vsh new file mode 100644 index 0000000..b7ddb11 --- /dev/null +++ b/assets/minecraft/shaders/core/rendertype_gui.vsh @@ -0,0 +1,61 @@ +#version 150 + +in vec3 Position; +in vec4 Color; + +uniform mat4 ModelViewMat; +uniform mat4 ProjMat; + +uniform vec2 ScreenSize; + +out vec4 vertexColor; +out vec4 Coords; +out vec2 position; + +flat out vec2 flatCorner; +out vec2 Pos1; +out vec2 Pos2; + +bool isTooltip(mat4 ProjMat,vec3 Position) { + return ProjMat[2][3] == 0 && Position.z > 300 && Position.z < 500; +} + +void main() { + vertexColor = Color; + + gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0); + + Coords = vec4(-1); + position = vec2(-1); + Pos1 = vec2(-1); + Pos2 = vec2(-1); + flatCorner = vec2(-1); + + //Tooltip frame + if (Color.r != 0 && Color.g == 0 && Color.b != 0 && ProjMat[3][0] == -1) + { + int id = gl_VertexID / 4; + + int vertID = (gl_VertexID) % 4; + const vec2[4] corners = vec2[4](vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0)); + vec2 ScrSize = 2 / vec2(ProjMat[0][0], -ProjMat[1][1]); + vec3 Pos = Position - vec3(corners[((gl_VertexID) ) % 4] * 10 - (10 / 2.0), 0); + + Pos1 = Pos2 = vec2(0); + if (vertID == 0) Pos1 = Pos.xy; + if (vertID == 2) Pos2 = Pos.xy; + + Coords.xy = ScrSize; + Coords.zw = flatCorner = corners[vertID]; + position = Pos.xy; + + if (id != 2) + Pos.xy = vec2(0); + + gl_Position = ProjMat * ModelViewMat * vec4(Pos, 1); + } + + if (isTooltip(ProjMat, Position) && (gl_Position.x > 1 || gl_Position.x < -0.99)) { + vertexColor = vec4(0); + } +}