S
skeptile
Guest
Like many people (based on my Googling), I watched this Shaun Spaulding video but have questions about getting it to work just the way I need it to.
The problem I have is that I need the outline to consistently draw a one-pixel-wide outline even when a sprite is stretched (its image_xscale and image_yscale change). With the shader in the video, the result on a stretched sprite is something like this:
The outline is the same scale as the sprite.
What I'm going for is this: (In this image, the outline is drawn by drawing the sprite 5 times: 4 times in black at a slight offset to form the outline, then once for the actual sprite. This method is quite hard on framerate, which is why I'm looking for a shader-based solution)
Any help you can offer is appreciated. Forgive me if I'm missing something; I'm fairly new to shaders and don't fully understand everything about how they work. Alternatively, if you have any non-shader methods of obtaining this result, feel free to share, as long as they're not taxing on framerate.
Here's the code in the shader:
Vertex
and Fragment
The problem I have is that I need the outline to consistently draw a one-pixel-wide outline even when a sprite is stretched (its image_xscale and image_yscale change). With the shader in the video, the result on a stretched sprite is something like this:
The outline is the same scale as the sprite.
What I'm going for is this: (In this image, the outline is drawn by drawing the sprite 5 times: 4 times in black at a slight offset to form the outline, then once for the actual sprite. This method is quite hard on framerate, which is why I'm looking for a shader-based solution)
Any help you can offer is appreciated. Forgive me if I'm missing something; I'm fairly new to shaders and don't fully understand everything about how they work. Alternatively, if you have any non-shader methods of obtaining this result, feel free to share, as long as they're not taxing on framerate.
Here's the code in the shader:
Vertex
Code:
attribute vec3 in_Position; // (x,y,z)
//attribute vec3 in_Normal; // (x,y,z) unused in this shader.
attribute vec4 in_Colour; // (r,g,b,a)
attribute vec2 in_TextureCoord; // (u,v)
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main()
{
vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float pixelH;
uniform float pixelW;
void main()
{
vec2 offsetx;
offsetx.x = pixelW;
vec2 offsety;
offsety.y = pixelH;
float alpha = texture2D( gm_BaseTexture, v_vTexcoord ).a;
alpha += ceil(texture2D( gm_BaseTexture, v_vTexcoord + offsetx ).a);
alpha += ceil(texture2D( gm_BaseTexture, v_vTexcoord - offsetx ).a);
alpha += ceil(texture2D( gm_BaseTexture, v_vTexcoord + offsety ).a);
alpha += ceil(texture2D( gm_BaseTexture, v_vTexcoord - offsety ).a);
gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
gl_FragColor.a = alpha;
}
Last edited by a moderator: