Jump to content
  • Advertisement
Alexander Nazarov

Applying a Shader to a sprites leads to strange things

Recommended Posts

In my game I have multiple sprites and I want to apply my shader to them. I do things by this sequence: Create Shader -> Create Material -> Add the shader to the Material -> Add the material to my sprite(s). But when I add my shader to the material, images get started looking like below. What I do wrong and which way is right?

P.S.

I create Unlit shader and do not change nothing inside it.

 

 

 

Before applying shader

918535113_ScreenShot2019-05-27at7_16_41PM.thumb.png.0581f8dc0edca2bf48570e865fedd891.png

After applying shader

1905460040_ScreenShot2019-05-27at7_16_22PM.thumb.png.d2a22b38d4860195630616ddfeee3615.png

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

So I just reproduced a version of your problem. Given this image (generated in paint.net):

test.png.e301c3c5984f933552a32cfcbefea42e.png

And creating a sprite in unity and then assigning the unlit shader to material and assigning that material to a mesh I'm seeing this:

image.png.1a597bcf520848438c978e21a00e45fb.png

Now the problem seems to be the the unlit shader is marked as "Opaque", which means it renders mostly solid. The weird cutout you see is in the parts where the alpha is below a specific threshold (without going into details, this is then where the renderer just discards the pixels). 

The different colors you see in the transparent parts in your image are 'hidden' in the alpha channel of the images you have. If you have the source image you can verify this by upping the alpha for that layer, and they will then show.

If you were to want to see this:

image.png.e394ae3bede08e13b8cf610aeca8cc59.png

Then you would just use a sprite and not assign an unlit shader. In general you should not be using an unlit shader with sprites.

image.png.287a4b04289216baaf6bd9929a4b043d.png

I'm not sure what specific use you could have for wanting to override this shader.

Edited by deadc0deh

Share this post


Link to post
Share on other sites

Hi, @deadc0deh. Can you help me with some question about shaders?

 

I have written the simplest possible shader.

On the image below the fragment shader do nothing (it has no functionality).

It looks like this

fixed4 fragmentFunc(v2f IN) : SV_Target {
    float2 uv = IN.uv;
    float4 time = _Time;
    
    //uv *= 2;
    
    
    fixed4 pixColor = tex2D(_MainTexture, uv);
    return pixColor;

}


}231831206_ScreenShot2019-08-01at5_56_47PM.thumb.png.fa62d639877e00eb90c93c8e66b3af33.png

 

 

And this is the same sprite with the same shader, but with multiplied by 2 pixel position

fixed4 fragmentFunc(v2f IN) : SV_Target {
    float2 uv = IN.uv;
    float4 time = _Time;
    
    uv *= 2;
    
    
    fixed4 pixColor = tex2D(_MainTexture, uv);
    return pixColor;
}

199268789_ScreenShot2019-08-01at5_57_22PM.thumb.png.4c6b9dba765b13d2df6f14d9f501a79a.png

Why this weird thing appears above the sprite. What should I do to avoid such things.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!