Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Xorton

Pages: [1]
1
Graphics / Re: Drawing Text on transparent RenderTexture leaves outline
« on: August 30, 2017, 07:16:39 pm »
Here is my new solution:
https://gist.github.com/Xorton/b88c1a3a1701ceb992c5922a701bda69

Main idea:
1. Move alpha multiplying from blend state to shader (instead of custom blend mode).
2. Use separate texture as mask to define fully transparent background regions (to avoid using same texture for reading and writing).

2
Graphics / Re: Drawing Text on transparent RenderTexture leaves outline
« on: August 20, 2017, 02:33:58 pm »
Multiplying an alpha value of 0.5 to a color value of (1, 0, 0) yields a premultiplied color value of (1, 0, 0, 0.5).

In formulas there is no color or alpha values. They are just vectors. And if you multiply vector (1, 0, 0) by scalar 0.5 you'll get vector  (0.5, 0, 0).

3
Graphics / Re: Drawing Text on transparent RenderTexture leaves outline
« on: August 19, 2017, 11:15:42 pm »

co = 0.5 * (1, 0, 0) + 0.0 * (x1, x2, x3) * (1 - 0.5) = 0.5 * (1, 0, 0)


And you get  0.5 * (1, 0, 0) = (0.5, 0, 0) as result color (instead of (1, 0, 0)).

4
Graphics / Re: Drawing Text on transparent RenderTexture leaves outline
« on: August 19, 2017, 04:40:59 pm »
The color and alpha formulae for this mode are:

co = αs x Cs + αb x Cb x (1 – αs)
αo = αs + αb x (1 – αs)



This formula will add same black borders due to alpha premultiplication. Blending color rgba(1.0, 1.0, 1.0, 0.5) on color rgba(..., ..., ..., 0.0) will result in color rgba(0.5, 0.5, 0.5, 0.5) instead of rgba(1.0, 1.0, 1.0, 0.5).

5
Thanks for answer!
Yes, in most cases discarding transparent pixels (pixel.a < e) should do the trick.

I think about more complex case 8):

Text with border and shadow (prerendered or generated) with letter-by-letter appearance animation :)
Shadows usually rendered on separate render-texture, and glyphs overlaps each other.

6
... as long as glyphs don't overlap.

That's the point - they do overlap



Here is simple shader with custom blending.
Vertex Shader
varying vec2 backPos;
void main()
{
        // transform the vertex position
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

        // transform the texture coordinates
        gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

        // forward the vertex color
        gl_FrontColor = gl_Color;

        // convert screen coordinates to texture coordinates
        backPos = gl_Position.xy / 2.0 + vec2(0.5, 0.5);
}
 

Fragment shader:
uniform sampler2D texture;
uniform sampler2D background;

varying vec2 backPos;

void main()
{
        // lookup the pixel in the texture
        vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;

        // lookup the pixel in the background
        vec4 back = texture2D(background, backPos);

        if(back.a < 0.001)
        {
                // special case
                gl_FragColor = pixel;
        }
        else
        {
                // usual alpha blend
                gl_FragColor.rgb = pixel.rgb * pixel.a + back.rgb * (1.0 - pixel.a);
                gl_FragColor.a = pixel.a + back.a * (1.0 - pixel.a);
        }
}
 

Code (SFML.Net):
RenderTexture target = new RenderTexture(...);
Shader customBlendShader = ...;
customBlendShader.SetParameter("texture", Shader.CurrentTexture);
customBlendShader.SetParameter("background", target.Texture);
RenderStates customBlendState = new RenderStates(customBlendShader);
customBlendState.BlendMode = BlendMode.None;

...

target.Clear(Color.Transparent);
target.Draw(someSprite, customBlendState);
target.Display();

window.Clear(Color.White);
window.Draw(targetSprite);
window.Display();
 

But this is not ideal solution, because:
1. If operator in fragment shaders should be avoided
2. AFAIK same texture for reading and writing (target.Texture) may cause undefined behavior.

In my test application there is a small flickering on rendered RenderTexture.

7
Seems like pygame have special case for drawing on fully transparent surface.



It looks like:
if dstAlpha==0 then blend mode is color=srcColor
else blend mode is color=srcColor*srcAlpha+dstColor*(1-srcAlpha)

I don't know how to achieve this in SFML. Custom blend mode in shader?

UPDATE:
I think it's SDL thing (which pygame is based on). SDL2 shows same picture as SFML.

Pages: [1]
anything