Search code examples
animationglslshadervertex-shader

GLSL sparking vertex shader


I am trying to tweak this ShaderToy example for vertices to create 'sparks' out of them. Have tried to play with gl_PointCoord and gl_FragCoord without any results. Maybe, someone here could help me?

I need effect similar to this animated gif:

]

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

#define M_PI 3.1415926535897932384626433832795

float rand(vec2 co)
{
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main( ) {

    float size = 30.0;
    float prob = 0.95;

    vec2 pos = floor(1.0 / size * gl_FragCoord.xy);

    float color = 0.0;
    float starValue = rand(pos);

    if (starValue > prob)
    {
        vec2 center = size * pos + vec2(size, size) * 0.5;

        float t = 0.9 + sin(time + (starValue - prob) / (1.0 - prob) * 45.0);

        color = 1.0 - distance(gl_FragCoord.xy, center) / (0.5 * size);
        color = color * t / (abs(gl_FragCoord.y - center.y)) * t / (abs(gl_FragCoord.x - center.x));
    }
    else if (rand(gl_FragCoord.xy / resolution.xy) > 0.996)
    {
        float r = rand(gl_FragCoord.xy);
        color = r * ( 0.25 * sin(time * (r * 5.0) + 720.0 * r) + 0.75);
    }

    gl_FragColor = vec4(vec3(color), 1.0);


}

As I understand have to play with vec2 pos, setting it to a vertex position.


Solution

  • You don't need to play with pos. As Vertex Shader is only run by each vertex, there is no way to process its pixel values there using Pos. However, you can do processing pixel using gl_PointCoord.

    I can think of two ways only for changing the scale of a texture

    1. gl_PointSize in Vertex Shader in opengl es
    2. In Fragment Shader, you can change the texture UV value, for example,

      vec4 color = texture(texture0, ((gl_PointCoord-0.5) * factor) + vec2(0.5));

    If you don't want to use any texture but only pixel processing in FS,

    you can set UV like ((gl_PointCoord-0.5) * factor) + vec2(0.5) instead of uv which is normally set as fragCoord.xy / iResolution.xy in Shadertoy