TerrificTable55

Untitled

Nov 13th, 2022
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. package com.arsenicclient.utils.render.shader;
  2.  
  3. import net.minecraft.client.Minecraft;
  4. import net.minecraft.client.gui.ScaledResolution;
  5. import net.minecraft.client.shader.Framebuffer;
  6.  
  7. import java.util.ArrayList;
  8.  
  9. import static org.lwjgl.opengl.GL14.glBlendFuncSeparate;
  10. import static org.lwjgl.opengl.GL20.*;
  11. import static org.lwjgl.opengl.GL11.*;
  12.  
  13. public class BloomRenderer {
  14.  
  15. private static final String OUTLINE_SHADER =
  16. "#version 120\n" +
  17. "\n" +
  18. "uniform sampler2D texture;\n" +
  19. "uniform vec2 texelSize;\n" +
  20. "\n" +
  21. "uniform vec4 colour;\n" +
  22. "uniform float radius;\n" +
  23. "\n" +
  24. "void main() {\n" +
  25. " float a = 0.0;\n" +
  26. " vec3 rgb = colour.rgb;\n" +
  27. " float closest = 1.0;\n" +
  28. " for (float x = -radius; x <= radius; x++) {\n" +
  29. " for (float y = -radius; y <= radius; y++) {\n" +
  30. " vec2 st = gl_TexCoord[0].st + vec2(x, y) * texelSize;\n" +
  31. " vec4 smpl = texture2D(texture, st);\n" +
  32. " float dist = distance(st, gl_TexCoord[0].st);\n" +
  33. " if (smpl.a > 0.0 && dist < closest) {" +
  34. " rgb = smpl.rgb;\n" +
  35. " closest = dist;\n" +
  36. " }\n" +
  37. " a += smpl.a*smpl.a;\n" +
  38. " }\n" +
  39. " }\n" +
  40. " vec4 smpl = texture2D(texture, gl_TexCoord[0].st);\n" +
  41. " gl_FragColor = vec4(rgb, a * colour.a / (4.0 * radius * radius)) * (smpl.a > 0.0 ? 0.0 : 1.0);\n" +
  42. "}\n";
  43.  
  44. private static final String VERTEX_SHADER =
  45. "#version 120 \n" +
  46. "\n" +
  47. "void main() {\n" +
  48. " gl_TexCoord[0] = gl_MultiTexCoord0;\n" +
  49. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
  50. "}";
  51.  
  52.  
  53. private static final GLSLShader shader = new GLSLShader(VERTEX_SHADER, OUTLINE_SHADER) {
  54. @Override
  55. public void setupUniforms() {
  56. this.setupUniform("texture");
  57. this.setupUniform("texelSize");
  58. this.setupUniform("colour");
  59. this.setupUniform("radius");
  60. glUniform1i(this.getUniformLocation("texture"), 0);
  61. }
  62.  
  63. @Override
  64. public void updateUniforms(float width, float height, float mouseX, float mouseY, float time) {
  65.  
  66. glUniform4f(this.getUniformLocation("colour"),
  67. 1.0f,
  68. 1.0f,
  69. 1.0f,
  70. 1.0f);
  71. glUniform2f(this.getUniformLocation("texelSize"),
  72. 1f / Minecraft.getMinecraft().displayWidth,
  73. 1f / Minecraft.getMinecraft().displayHeight);
  74. glUniform1f(this.getUniformLocation("radius"), 5f);
  75. }
  76. };
  77.  
  78. private static Framebuffer framebuffer;
  79. public static boolean disableBloom;
  80. private static List<RenderCallback> renders = new ArrayList<>();
  81.  
  82. private BloomRenderer() {
  83. }
  84.  
  85. public static void bloom(RenderCallback render) {
  86. if (disableBloom) return;
  87. renders.add(render);
  88. }
  89.  
  90. public static void drawAndBloom(RenderCallback render) {
  91. render.render();
  92. if (disableBloom) return;
  93. renders.add(render);
  94. }
  95.  
  96. public static void onRenderGameOverlay(ScaledResolution scaledResolution, Framebuffer mcFramebuffer) {
  97. if (framebuffer == null) return;
  98.  
  99. framebuffer.bindFramebuffer(false);
  100.  
  101. for (RenderCallback callback : renders) {
  102. callback.render();
  103. }
  104.  
  105. renders.clear();
  106.  
  107. mcFramebuffer.bindFramebuffer(false);
  108.  
  109. // Use the outline shader
  110. // Draw our framebuffer
  111. shader.useShader(scaledResolution.getScaledWidth(), scaledResolution.getScaledHeight(), 0, 0, 0);
  112. glDrawFramebuffer(framebuffer.framebufferTexture, scaledResolution.getScaledWidth(), scaledResolution.getScaledHeight());
  113. // Disable shader
  114. glUseProgram(0);
  115. // Clear our framebuffer
  116. framebuffer.framebufferClear();
  117. // Need to rebind minecraft framebuffer after clearing ours
  118. mcFramebuffer.bindFramebuffer(false);
  119. }
  120.  
  121.  
  122. public static boolean glEnableBlend() {
  123. final boolean wasEnabled = glIsEnabled(GL_BLEND);
  124.  
  125. if (!wasEnabled) {
  126. glEnable(GL_BLEND);
  127. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1, 0);
  128. }
  129.  
  130. return wasEnabled;
  131. }
  132.  
  133.  
  134. public static void glDrawFramebuffer(final int framebufferTexture, final int width, final int height) {
  135. // Bind the texture of our framebuffer
  136. glBindTexture(GL_TEXTURE_2D, framebufferTexture);
  137. // Disable alpha testing so fading out outline works
  138. glDisable(GL_ALPHA_TEST);
  139. // Make sure blend is enabled
  140. final boolean restore = glEnableBlend();
  141. // Draw the frame buffer texture upside-down
  142. glBegin(GL_QUADS);
  143. {
  144. glTexCoord2f(0, 1);
  145. glVertex2f(0, 0);
  146.  
  147. glTexCoord2f(0, 0);
  148. glVertex2f(0, height);
  149.  
  150. glTexCoord2f(1, 0);
  151. glVertex2f(width, height);
  152.  
  153. glTexCoord2f(1, 1);
  154. glVertex2f(width, 0);
  155. }
  156. glEnd();
  157. // Restore blend
  158. glRestoreBlend(restore);
  159. // Restore alpha test
  160. glEnable(GL_ALPHA_TEST);
  161. }
  162.  
  163. public static void glRestoreBlend(final boolean wasEnabled) {
  164. if (!wasEnabled) {
  165. glDisable(GL_BLEND);
  166. }
  167. }
  168.  
  169. public static void onFrameBufferResize(final int width, final int height) {
  170. if (framebuffer != null) {
  171. // Delete old buffers as to not cause a memory leak
  172. framebuffer.deleteFramebuffer();
  173. }
  174. // Create new framebuffer
  175. // False means it doesn't allocate a depth buffer which we don't need
  176. framebuffer = new Framebuffer(width, height, false);
  177. }
  178.  
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment