0

I am trying to make a fragment shader for a 2d art style im working on. But to make that work i must make a texture able to overlap itself multiple times.

result i want and the 2 current results:

img

Edit1: in my art style i have 1 texture per plane. to make the illusion of each plane having thickness i need it to draw it self 2 times, with one pixel distance.

illustration:

img

if it was drawn a green plane then a blue plane, yes i would like to replace the green area with blue where it overlaps.

I have attempted to subtract the next texture location to nullify texture blending, but resulted in also adding negative values at empty area.

```
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float shxx;
uniform float shyy;

void main()
{
    vec2 Coord1 = v_vTexcoord + vec2(0,0);
    vec2 Coord2 = v_vTexcoord + vec2(shxx,shyy);
    vec2 Coord3 = v_vTexcoord + vec2(shxx+shxx,shyy+shyy);
    vec2 Coord4 = v_vTexcoord + vec2(+shxx+shxx+shxx,+shyy+shyy+shyy);

    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, Coord1)
        - v_vColour * texture2D( gm_BaseTexture, Coord2) 
        + v_vColour * texture2D( gm_BaseTexture, Coord2)
        - v_vColour * texture2D( gm_BaseTexture, Coord3)  
        + v_vColour * texture2D( gm_BaseTexture, Coord3)
        - v_vColour * texture2D( gm_BaseTexture, Coord4) 
        + v_vColour * texture2D( gm_BaseTexture, Coord4);
}
```
Spektre
  • 49,595
  • 11
  • 110
  • 380
Adihunte
  • 1
  • 2
  • Can you give more detail on what exactly is the idea behind this desired result? Based on the images you provided, it is not clear to me what you're trying to achieve. I see two green squares that overlap and the result is the same green. Do you just want any overlapping region to be turned a given color? What should happen if I have, say, a green and a blue square instead? – Michael Kenzel Mar 26 '19 at 00:15
  • "I have attempted to subtract the next texture location to nullify texture blending, but resulted in also adding negative values at empty area." What does this even mean? What is _texture blending_? What is this formula in your code supposed to be doing? – derhass Mar 26 '19 at 01:23
  • You do not need blending for this just use depth buffer ... set each plane to different Z coordinate and that is it However I think easier would be to use sprites instead like this: [Isometric editor](https://stackoverflow.com/a/36454198/2521214) blending/transparency is your friend ... btw with height maps you can have also real 3D with this way too... **Anyway looks like you are shifting in the fragment that will work only if you enlarge the rendered geometry so the upper side of tile will not get cut off like in your 3th result** in such case you will need to `discard()` pixels not involved – Spektre Mar 26 '19 at 08:48

1 Answers1

0

Found a solution. every time i subtract next textures position i set all fragments with alpha value less than 0,9 to default color black. this way the next texture gets drawn on top of no existing colors. thus avoiding the textures blending into white.

Final Result = https://i.stack.imgur.com/bP37n.png

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float shxx;
uniform float shyy;


void main()
{
    vec2 Coord1 = v_vTexcoord + vec2(0,0);
    vec2 Coord2 = v_vTexcoord + vec2(shxx,shyy);
    vec2 Coord3 = v_vTexcoord + vec2(shxx+shxx,shyy+shyy);
    vec2 Coord4 = v_vTexcoord + vec2(+shxx+shxx+shxx,+shyy+shyy+shyy);
    vec2 Coord5 = v_vTexcoord + vec2(+shxx+shxx+shxx+shxx,+shyy+shyy+shyy+shyy);    

    gl_FragColor =  v_vColour * texture2D( gm_BaseTexture, Coord1)
    -  texture2D( gm_BaseTexture, Coord2)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord2)
    -  texture2D( gm_BaseTexture, Coord3)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord3)
    -  texture2D( gm_BaseTexture, Coord4)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord4)
    -  texture2D( gm_BaseTexture, Coord5)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord5);
}
Adihunte
  • 1
  • 2
  • As also noted by others in the comments, I'd highly recommend to look into using the depth buffer for this. It should allow you to do exactly what you need much more efficiently and in a much simpler way… – Michael Kenzel Mar 26 '19 at 19:30
  • i wish i could, but in Game Maker Studio 2 (the game engine i'm using this shadier in) you cant decide the draw depth. But i would absolutely have done that instead if it was possible. – Adihunte Mar 26 '19 at 19:52