Search Unity

Unity Burn Shader for mobile question.

Discussion in 'Shaders' started by Bashinsky, Oct 27, 2016.

  1. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Hello , struggling for some time with moving this shader

    Code (CSharp):
    1. Shader "Custom/DissolveWithBurns" {
    2.      Properties {
    3.         _MainTex ("Texture (RGB)", 2D) = "white" {}
    4.         _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    5.         _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
    6.  
    7.         _BurnSize("Burn Size", Range(0.0, 1.0)) = 0.15
    8.         _BurnRamp("Burn Ramp (RGB)", 2D) = "white" {}
    9.     }
    10.     SubShader {
    11.       Tags { "RenderType" = "Opaque" }
    12.       Cull Off
    13.       CGPROGRAM
    14.      
    15.       #pragma surface surf Lambert
    16.       struct Input {
    17.           float2 uv_MainTex;
    18.           float2 uv_SliceGuide;
    19.           float _SliceAmount;
    20.       };
    21.  
    22.       sampler2D _MainTex;
    23.       sampler2D _SliceGuide;
    24.       float _SliceAmount;
    25.       sampler2D _BurnRamp;
    26.       float _BurnSize;
    27.  
    28.    
    29.       void surf(Input IN, inout SurfaceOutput o) {
    30.  
    31.           clip(tex2D(_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    32.           o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    33.  
    34.           half test = tex2D(_SliceGuide, IN.uv_MainTex).rgb - _SliceAmount;
    35.  
    36.  
    37.           if (test < _BurnSize) {
    38.           o.Emission = tex2D(_BurnRamp, float2(test *(1 / _BurnSize), 0));
    39.           o.Albedo *= o.Emission;
    40.           }
    41.       }
    42.  
    43.       ENDCG
    44.     }
    45.     Fallback "Diffuse"
    46. }
    47.  
    (direct copy-paste from http://www.codeavarice.com/dev-blog/tutorial-burning-edges-dissolve-shader-in-unity)

    for mobile.

    And it fails.
    And I can't understand 2 thins :
    1.
    half test = tex2D(_SliceGuide, IN.uv_MainTex).rgb - _SliceAmount;
    scalar = 3dimenstionVector - scalar.
    I get the extraction part, but assigning vector to scalar blows my mind.

    2. Have no idea why it fails to run on mobile.

    Any help? Pls)
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Does it work on the editor and fail on mobile?

    (and yeah, that casting from 3channels to a scalar is not something that should be done, it might as well be why it doesn't work on mobile, depending on the platform, casting like that may or may not work. Maybe change it so that it reads only one channel?)
     
  3. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Editor and windows looks and behaves just as planned.
    I've done some code juggling, "removing if", partial code disabling, but failed to extract the problem part.

    half test = tex2D(_SliceGuide, IN.uv_MainTex).r - _SliceAmount;

    Reducing rgb=>to r , didn't help(
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Is it pink on mobile, or does it simply do something different?

    What happens if you remove the clip line?
     
  5. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Not pink, shader compiles without errors.

    looks like this part is ignored.
    Code (CSharp):
    1. if (test < _BurnSize) {
    2.           o.Emission = tex2D(_BurnRamp, float2(test *(1 / _BurnSize), 0));
    3.           o.Albedo *= o.Emission;
    4.           }
    It shoud change color on the border of clipping.
     
  6. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Code (CSharp):
    1. clip(tex2D(_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    2.        
    3.  
    4.         half test = tex2D(_SliceGuide, IN.uv_MainTex).rgb - _SliceAmount;
    5.    
    6.         o.Albedo = float3(0.0, 0.0, 1.0);
    7.    
    8.  
    9.  
    10.        
    11.         o.Albedo =  float3(1.0, 0.0, 0.0)* step(test, _BurnSize) +
    12.                     float3(0.0, 1.0, 0.0)* (1-step(test, _BurnSize));

    even simplified logic to this still gets constant color on mobile.
    In opposite to desktop "burned" effect.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    Try writing to Emission instead of Albedo.
     
  8. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Nope, didn't help either. It feels like shader picks one of 2 colors and sticks to them cutting out everything else.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The step() function makes the code shorter, but doesn't actually reduce the complexity of the shader.

    The following two lines will result in identical compiled shaders.
    float val = step(y, x);
    float val = (x >= y) ? 1 : 0;

    However some android devices are totally broken when doing comparisons like that, it's entirely plausible your specific device isn't showing correctly but others would.

    My suggestion is to do the compare with out using a comparison, like:

    float burn = saturate((test - _BurnSize) * 1000.0);
    o.Albedo = lerp(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), burn);
     
    Bashinsky likes this.
  10. Bashinsky

    Bashinsky

    Joined:
    Nov 19, 2015
    Posts:
    10
    Wow! Thanks! It's sort of working) Got a little polish to do, but that I can handle)