Search Unity

Problems with a dissolve shader. Unity 5 [With Pics]

Discussion in 'Shaders' started by ChaosRobin, Mar 9, 2015.

  1. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    [Resolved!]

    Hey all, I've been messing with this too long now, its frustrating me and I need help! I bought a shader off of the unity asset store, I put it into my scene in unity 5 hoping it would work and it doesn't!! I'm not amazing at coding at java or c# but I've never even touched a shader script at all!! Okay so, here's the problem, when I apply the shader to my model, or when I load up the demo scenes, the models turn pink!! And they don't animate the shader effect as they are meant to.. When i look at the shader in the inspector window, it gives an error, I put the error code in one of the attached pictures. I tried reaching out to the publisher of the asset with no response, I would try to get a refund, but when this thing is working it will be perfect for my project so I really want to get it working. I was hoping one of you could look at the pics, understand what the problem is, and maybe even post the solution, but i don't understand if you just explain the fix to me it would be really helpful if you could fix the code and tell me where to paste it in. Any help on this will be greatly appreciated!!!!! I need your help!!

    [/URL][/IMG][/URL][/IMG]

    Here is the code direct from the script:

    Code (csharp):
    1.  
    2. Shader "Dissolve/Dissolve_WorldCoords" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    7.     _Amount ("Amount", Range (0, 1)) = 0.5
    8.     _StartAmount("StartAmount", float) = 0.1
    9.     _Illuminate ("Illuminate", Range (0, 1)) = 0.5
    10.     _Tile("Tile", float) = 1
    11.     _DissColor ("DissColor", Color) = (1,1,1,1)
    12.     _ColorAnimate ("ColorAnimate", vector) = (1,1,1,1)
    13.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    14.     _BumpMap ("Normalmap", 2D) = "bump" {}
    15.     _DissolveSrc ("DissolveSrc", 2D) = "white" {}
    16.     _DissolveSrcBump ("DissolveSrcBump", 2D) = "white" {}
    17.  
    18. }
    19. SubShader {
    20.     Tags { "RenderType"="Opaque" }
    21.     LOD 400
    22.     cull off
    23.  
    24.  
    25. CGPROGRAM
    26. #pragma target 3.0
    27. #pragma surface surf BlinnPhong addshadow
    28.  
    29.  
    30.  
    31. sampler2D _MainTex;
    32. sampler2D _BumpMap;
    33. sampler2D _DissolveSrc;
    34. sampler2D _DissolveSrcBump;
    35.  
    36. fixed4 _Color;
    37. half4 _DissColor;
    38. half _Shininess;
    39. half _Amount;
    40. static half3 Color = float3(1,1,1);
    41. half4 _ColorAnimate;
    42. half _Illuminate;
    43. half _Tile;
    44. half _StartAmount;
    45.  
    46.  
    47.  
    48. struct Input {
    49.     float2 uv_MainTex;
    50.     float2 uv_BumpMap;
    51.     float2 uvDissolveSrc;
    52.     float3 worldPos;
    53. };
    54.  
    55. void vert (inout appdata_full v, out Input o) {}
    56.  
    57. void surf (Input IN, inout SurfaceOutput o) {
    58.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    59.     o.Albedo = tex.rgb * _Color.rgb;
    60.  
    61.     float ClipTex = tex2D (_DissolveSrc, float2(IN.worldPos.x,IN.worldPos.y)/_Tile).r * tex2D (_DissolveSrc, float2(IN.worldPos.y,IN.worldPos.z)/_Tile).g;
    62.     float ClipAmount = ClipTex - _Amount;
    63.     float Clip = 0;
    64.     float4 DematBump =  tex2D (_DissolveSrcBump, float2(IN.worldPos.x,IN.worldPos.y)/_Tile) * tex2D (_DissolveSrcBump, float2(IN.worldPos.y,IN.worldPos.z)/_Tile);
    65.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    66. if (_Amount > 0)
    67. {
    68.     if (ClipAmount <0)
    69.     {
    70.         Clip = 1; //clip(-0.1);
    71.  
    72.     }
    73.     else
    74.     {
    75.  
    76.         if (ClipAmount < _StartAmount)
    77.         {
    78.             if (_ColorAnimate.x == 0)
    79.                 Color.x = _DissColor.x;
    80.             else
    81.                 Color.x = ClipAmount/_StartAmount;
    82.  
    83.             if (_ColorAnimate.y == 0)
    84.                 Color.y = _DissColor.y;
    85.             else
    86.                 Color.y = ClipAmount/_StartAmount;
    87.  
    88.             if (_ColorAnimate.z == 0)
    89.                 Color.z = _DissColor.z;
    90.             else
    91.                 Color.z = ClipAmount/_StartAmount;
    92.  
    93.             o.Albedo  = (o.Albedo *((Color.x+Color.y+Color.z))* Color*((Color.x+Color.y+Color.z)))/(1 - _Illuminate);
    94.             o.Normal = UnpackNormal(tex2D(_DissolveSrcBump, IN.uvDissolveSrc));
    95.  
    96.         }
    97.     }
    98. }
    99.  
    100.  
    101. if (Clip == 1)
    102. {
    103. clip(-0.1);
    104. }
    105.  
    106.  
    107.     //////////////////////////////////
    108.     //
    109.     o.Gloss = tex.a;
    110.     o.Alpha = tex.a * _Color.a;
    111.     o.Specular = _Shininess;
    112.  
    113. }
    114. ENDCG
    115. }
    116.  
    117. FallBack "Specular"
    118. }
    119.  
     
    Last edited: Mar 19, 2015
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356

    Hey, please use Code tags? They just make things easier for everyone ^_^ But sorry I can't help, I'm no good with shaders
     
  3. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    there we go i added the code tag, can anyone help?
     
  4. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    im still looking for some help on this..
     
  5. SkermunkelStudios

    SkermunkelStudios

    Joined:
    Dec 29, 2012
    Posts:
    156
  6. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    Thanks for the heads up! I tried this fix and am still getting an error, is it working for you?
     
  7. SkermunkelStudios

    SkermunkelStudios

    Joined:
    Dec 29, 2012
    Posts:
    156
  8. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    Thanks again!
     
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    A simple fix for the error is to replace the line with this

    Code (csharp):
    1.  float ClipTex = tex2Dlod (_DissolveSrc, float4(IN.worldPos.xy/_Tile, 0, 0) ).r * tex2Dlod (_DissolveSrc, float4(IN.worldPos.yz/_Tile, 0, 0) ).g;
    The problem is (on the whole) you can't do regular texture accesses inside a dynamic branch ( though there might be some exceptions). A simple workaround is instead of doing standard tex2D lookups you use tex2Dlod instead where you specify the specific mipmap level, hence why the uv changes from float2 to float4 in this case. However as i'm just using mipmap level 0, doing so the shader is likely to be slightly less efficient as it will always be using the full size dissolve texture for this lookup.

    Untested with actual textures, but the error is fixed, though you may need to add #pragam glsl for opengl support.

    Alternatively if you are not targeting dx9 hardware you could add
    #pragma exclude_renderers d3d9

    Hope this helps
     
  10. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    Thanks for the assist!

    okay so i added the code you posted to line 59, then in line 27 i put #pragma exclude_renderers d3d9

    now its giving me the error:

    extression left of "worldPos" is not a stuct or array
     
  11. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    but just posting the line "#pragma exclude_renderers d3d9" made some of the textures fix, but they still aren't desolving
     
  12. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Actually its one or the other. Adding the #pragma will mean the shader is not built for Dx9, meaning you must have Unity set to dx11 mode. In dx9 mode the shader will simply not work, off the top of my head, probably just act like diffuse.

    Not sure why you are getting that error or why you posted the original code to line 59, it should replace line 61 in your original shader post.
     
  13. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Ok i'll post the whole shader. This compiles fine to all target platforms in Unity 5.0.0f4
    I did comment out the DematBump line since it wasn't used, but not doing so shouldn't have caused problems.
    If you still get errors, please post error report, Unity version, OS, build target etc.

    Code (csharp):
    1.  
    2. Shader "Dissolve/Dissolve_WorldCoords" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    7.     _Amount ("Amount", Range (0, 1)) = 0.5
    8.     _StartAmount("StartAmount", float) = 0.1
    9.     _Illuminate ("Illuminate", Range (0, 1)) = 0.5
    10.     _Tile("Tile", float) = 1
    11.     _DissColor ("DissColor", Color) = (1,1,1,1)
    12.     _ColorAnimate ("ColorAnimate", vector) = (1,1,1,1)
    13.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    14.     _BumpMap ("Normalmap", 2D) = "bump" {}
    15.     _DissolveSrc ("DissolveSrc", 2D) = "white" {}
    16.     _DissolveSrcBump ("DissolveSrcBump", 2D) = "white" {}
    17.  
    18. }
    19. SubShader {
    20.     Tags { "RenderType"="Opaque" }
    21.     LOD 400
    22.     cull off
    23.  
    24.  
    25. CGPROGRAM
    26. #pragma target 3.0
    27. #pragma surface surf BlinnPhong addshadow
    28. // #pragma exclude_renderers d3d9
    29. // #pragma glsl
    30. // #pragma debug
    31.  
    32. sampler2D _MainTex;
    33. sampler2D _BumpMap;
    34. sampler2D _DissolveSrc;
    35. sampler2D _DissolveSrcBump;
    36.  
    37. fixed4 _Color;
    38. half4 _DissColor;
    39. half _Shininess;
    40. half _Amount;
    41. static half3 Color = float3(1,1,1);
    42. half4 _ColorAnimate;
    43. half _Illuminate;
    44. half _Tile;
    45. half _StartAmount;
    46.  
    47.  
    48.  
    49. struct Input {
    50.     float2 uv_MainTex;
    51.     float2 uv_BumpMap;
    52.     float2 uvDissolveSrc;
    53.     float3 worldPos;
    54. };
    55.  
    56.  
    57. void surf (Input IN, inout SurfaceOutput o) {
    58.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    59.     o.Albedo = tex.rgb * _Color.rgb;
    60.  
    61.     float ClipTex = tex2Dlod (_DissolveSrc, float4(IN.worldPos.xy/_Tile, 0, 0) ).r * tex2Dlod (_DissolveSrc, float4(IN.worldPos.yz/_Tile, 0, 0) ).g;      
    62.        
    63.     float ClipAmount = ClipTex - _Amount;
    64.     float Clip = 0;
    65.    // float4 DematBump =  tex2D (_DissolveSrcBump, float2(IN.worldPos.x,IN.worldPos.y)/_Tile) * tex2D (_DissolveSrcBump, float2(IN.worldPos.y,IN.worldPos.z)/_Tile);
    66.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    67.    
    68. if (_Amount > 0)
    69. {
    70.     if (ClipAmount <0)
    71.     {
    72.         Clip = 1; //clip(-0.1);
    73.  
    74.     }
    75.     else
    76.     {
    77.  
    78.         if (ClipAmount < _StartAmount)
    79.         {          
    80.             if (_ColorAnimate.x == 0)
    81.                 Color.x = _DissColor.x;
    82.             else
    83.                 Color.x = ClipAmount/_StartAmount;
    84.  
    85.             if (_ColorAnimate.y == 0)
    86.                 Color.y = _DissColor.y;
    87.             else
    88.                 Color.y = ClipAmount/_StartAmount;
    89.  
    90.             if (_ColorAnimate.z == 0)
    91.                 Color.z = _DissColor.z;
    92.             else
    93.                 Color.z = ClipAmount/_StartAmount;
    94.  
    95.             o.Albedo  = (o.Albedo *((Color.x+Color.y+Color.z))* Color*((Color.x+Color.y+Color.z)))/(1 - _Illuminate);
    96.             o.Normal = UnpackNormal(tex2D(_DissolveSrcBump, IN.uvDissolveSrc));
    97.  
    98.         }
    99.     }
    100. }
    101.  
    102.  
    103. if (Clip == 1)
    104. {
    105. clip(-0.1);
    106. }
    107.  
    108.  
    109.     //////////////////////////////////
    110.     //
    111.     o.Gloss = tex.a;
    112.     o.Alpha = tex.a * _Color.a;
    113.     o.Specular = _Shininess;
    114.  
    115. }
    116. ENDCG
    117. }
    118.  
    119. FallBack "Specular"
    120. }
    121.  
     
  14. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    YAY!!!!, you fixed my project lol! Thank you so much for your time and patience!!
     
  15. esg

    esg

    Joined:
    May 19, 2015
    Posts:
    1
    Thank you so much for your time!!