Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprite Shader not working in build, but working in Editor

Discussion in '2D' started by TheSixthHammer, Feb 26, 2020.

  1. TheSixthHammer

    TheSixthHammer

    Joined:
    Feb 13, 2017
    Posts:
    6
    Hi, I have a problem with my Sprite shaders in Unity 2017.4. In the Editor they work just fine (both in scene and game view). But when I run the build it freezes after the splashscreens.

    I am using tex2Dlod() not tex2D(), because tex2D() cannot be used in a loop with varying number of cycles. I know it does not figure out the mip-map like the tex2D() but in my case I do not care about mip-maps.

    What might be the problem?

    * I also tried making the for loops not with varying number of cycles (I hardcoded the step variable) and in that case the build no longer freezed but all sprites using this shader were still not draw on the screen.

    ** The shader uses a simple blur algorithm for achieving in-game blur (I have already tried with pre-baked blur and at this stage the project needs real-time sprite blurring, so baking blur is not an option. Also depth-of-field is not an option as well, because most of the in-game graphics have transparent pixels)



    This is the relevant part of the shader code:
    Code (CSharp):
    1. fixed4 SampleSpriteTextureBlurred (float2 uv)
    2. {
    3.     fixed4 color = tex2Dlod (_MainTex, float4(uv,0,0));
    4.  
    5. #if ETC1_EXTERNAL_ALPHA
    6.     fixed4 alpha = tex2Dlod (_AlphaTex, float4(uv,0,0));
    7.     color.a = lerp (color.a, alpha.r, _EnableExternalAlpha);
    8. #endif
    9.  
    10.     return color;
    11. }
    12. fixed4 CalculateBlurredColor(float2 texCoordXY, float worldPosZ){  
    13.     _ApertureSize = abs(_ApertureSize);
    14.     float worldPosNorm = _BlurStrength* _ApertureSize*sqrt((1/_ApertureSize)*abs(worldPosZ-_NoBlurZ));
    15.    
    16.    
    17.     float _Size = worldPosNorm * 3 * when_lt(worldPosZ, 0) + worldPosNorm * 0.7 * when_gt(worldPosZ, 0) ;
    18.              
    19.        
    20.     fixed4 sum = fixed4(0,0,0,0);
    21.     int counter = 0;
    22.     if(_Size == 0){
    23.         sum += SampleSpriteTexture(texCoordXY)  ;
    24.         counter++;
    25.     }else{
    26.         float step = abs(1 / _Size);  
    27.         for(float y = -1;y<=1;y+=step){
    28.             for(float x = -1;x<=1;x+=step){
    29.                     sum += SampleSpriteTextureBlurred(texCoordXY + float2(x * ( _MainTex_TexelSize.x * _Size), y * (_MainTex_TexelSize.y * _Size) ) )  ;
    30.                     counter++;
    31.             }
    32.         }
    33.     }
    34.     sum = sum / counter;
    35.     return sum;
    36. }
     
  2. TrueNoob141

    TrueNoob141

    Joined:
    Sep 18, 2019
    Posts:
    30
    I don't understand shaders, but it's included in build?
     
  3. ShiroTheWhite

    ShiroTheWhite

    Joined:
    Apr 4, 2018
    Posts:
    16
    Do you dynamically add this shader on runtime or is it fixed?
     
  4. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    What build you are speaking about? Mobile device? How big is your texture? Any chance your texture is bigger than 2048x2048 for device that support only max. 2048x2048?
     
  5. TheSixthHammer

    TheSixthHammer

    Joined:
    Feb 13, 2017
    Posts:
    6
    The build is for PC and the graphics are in different sized, but less than 2048
     
  6. TheSixthHammer

    TheSixthHammer

    Joined:
    Feb 13, 2017
    Posts:
    6
    No, the problem is not with the shader loading, but with this specific shader code.