glsl blur shader example
- User
glsl blur shader example
- Assistant
Creating a blur effect using a GLSL shader typically involves sampling the texture multiple times at slightly different offsets and then averaging the results. Here's a simple example of a 2D Gaussian blur shader for GLSL:
Vertex Shader (vertex_shader.glsl):
#version 330 core layout (location = 0) in vec2 aPos; layout (location = 1) in vec2 aTexCoords; out vec2 TexCoords; void main() { gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); TexCoords = aTexCoords; }Fragment Shader (fragment_shader.glsl):
#version 330 core out vec4 FragColor; in vec2 TexCoords; uniform sampler2D screenTexture; uniform vec2 resolution; uniform float blurRadius; const float pi = 3.14159265; // Function to calculate Gaussian weight float Gaussian(float x, float sigma) { return exp(-(x * x) / (2.0 * sigma * sigma)) / (2.0 * pi * sigma * sigma); } void main() { vec2 tex_offset = 1.0 / resolution; // Gets the size of one texel float sigma = blurRadius / 3.0; // Define the sigma value for the Gaussian function; the divide by 3 rule creates a nice fall-off float weightSum = Gaussian(0.0, sigma); // Calculate the central weight vec4 blurColor = texture(screenTexture, TexCoords) * weightSum; // Apply central texel's color by the central weight // Number of samples on each side of the central texel int blurSize = int(ceil(3.0 * sigma)); // Accumulate fragment color from neighboring texels for (int x = -blurSize; x <= blurSize; x++) { for (int y = -blurSize; y <= blurSize; y++) { // Do not blur the center twice if (x == 0 && y == 0) continue; // Calculate the weight for the current texel float w = Gaussian(distance(vec2(x, y), vec2(0.0)), sigma); blurColor += texture(screenTexture, TexCoords + vec2(tex_offset.x * x, tex_offset.y * y)) * w; weightSum += w; } } // Normalize the blurred color by the total weightSum FragColor = blurColor / weightSum; }This shader assumes that you pass in the texture you want to blur, the resolution of the screen (or the texture), and the radius of the blur effect. You'll need to set up a quad that spans the entire screen and render it with this shader.
Note that this is a very simple and not optimized way to do a blur effect. For a faster and more efficient blur, you might want to look into techniques such as two-pass Gaussian blurs, where you blur the scene horizontally in one pass and then vertically in another, making use of linear filtering to reduce the number of texture samples required.
Remember that if you run this shader in a WebGL context, replace
#version 330 corewith a suitable WebGL#versionshader directive (e.g.,#version 300 esfor WebGL 2.0). - User
glsl wave shader
- Assistant
A wave shader can simulate the visual effect of waves, such as water waves, across a flat surface. The effect can be achieved by distorting the texture coordinates based on a sine wave or a similar periodic function. Below is an example of a simple GLSL fragment shader that applies a wave effect to a texture, creating the appearance of vertical waves moving horizontally across the texture.
Vertex Shader (vertex_shader.glsl):
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoords; out vec2 TexCoords; void main() { gl_Position = vec4(aPos, 1.0); TexCoords = aTexCoords; }Fragment Shader (fragment_shader.glsl):
#version 330 core out vec4 FragColor; in vec2 TexCoords; uniform sampler2D uTexture; uniform float uTime; // Uniform value representing the elapsed time since the start void main() { // Set the wave parameters float waveAmplitude = 0.1; // Amplitude of the wave float waveFrequency = 3.0; // Frequency of the wave float waveSpeed = 1.5; // Speed at which the wave moves // Calculate the wave displacement based on the y coordinate and time float wave = sin(TexCoords.y * waveFrequency + uTime * waveSpeed) * waveAmplitude; // Add horizontal movement to the waves float waveXMovement = 0.25 * sin(uTime * 0.5); // Apply the wave effect to the texture coordinates vec2 distortedTexCoords = TexCoords + vec2(wave + waveXMovement, 0.0); // Sample the texture with the distorted coordinates to produce the wave effect FragColor = texture(uTexture, distortedTexCoords); }This fragment shader uses a sine function to create the wave effect. The
uTimeuniform needs to be continuously updated by your application (usually on the CPU side) with the elapsed time, which allows the waves to animate over time. ThewaveAmplitude,waveFrequency, andwaveSpeedvalues are parameters that you can adjust to change the appearance of the waves.The vertex shader is just a pass-through shader that doesn't alter the geometry and passes the texture coordinates through to the fragment shader.
It's worth noting here that this is a basic example. Depending on the complexity of the wave effect you're trying to create, you might approach it differently, with more sophisticated mathematics, normal mapping for the appearance of lighting on water, or other techniques.
- User
absolutely basic GLSL shader example
- Assistant
An absolutely basic GLSL shader example consists of two parts: the vertex shader and the fragment shader. The vertex shader processes each vertex passed to the GPU, and the fragment shader determines the color of each pixel.
Here is a minimal example of a GLSL shader pair that renders a colored triangle on the screen.
Vertex Shader (basic_vertex_shader.glsl):
#version 330 core layout (location = 0) in vec3 aPos; // Position input void main() { gl_Position = vec4(aPos, 1.0); // Set the position }Fragment Shader (basic_fragment_shader.glsl):
#version 330 core out vec4 FragColor; // Output that will be written to the framebuffer void main() { FragColor = vec4(1.0, 0.5, 0.2, 1.0); // Set the output color to orange }In this most basic example, the vertex shader takes the vertex positions (
aPos) and simply passes them to the rasterizer by settinggl_Position. The fragment shader sets the color of each pixel to a static value, resulting in an orange triangle.Usage of this shader would require setting up a triangle using vertex data on the CPU side, activating this shader program, and issuing a draw call for the triangle geometry.
These shaders are written with OpenGL Core Profile 3.3 in mind, which is the version that introduced the
#version 330 coredirective. For other contexts, like OpenGL ES and WebGL, you would use slightly different version declarations and potentially make syntax adjustments. For example, in WebGL you would write#version 300 esfor GLSL ES 3.00, which corresponds with WebGL 2.0.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.