Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Texture Alpha Blending in Shader and remove if statement if poosible

Discussion in 'Shaders' started by nitoh, Jan 17, 2016.

  1. nitoh

    nitoh

    Joined:
    Mar 20, 2014
    Posts:
    30
    Hello Dear Unity Community!

    I need your help with this code. My problem is i cant figure out how to blend the textures at texture borders.
    My 2nd problem is, if is it possible to rewrite the code, to keep the functions but remove if statements.

    Thank you for your answers!!!

    Code (CSharp):
    1. Properties {
    2. _MainTex ("Base (RGB)", 2D) = "white" {}
    3. _Slope1 ("Slope Range 1", Vector) = (100.0,5,0.0)
    4. _SlopeMin ("Minimum Slope", Vector) = (100.0,3.0,0.0)
    5.  
    6. _SlopeMaxCol ("MaxT", 2D) = "white" {}
    7. _Slope1Col ("MedT", 2D) = "white" {}
    8. _SlopeMinCol ("MinT", 2D) = "white" {}
    9.  
    10. _VertDir ("Vertical Direction" , Vector) = (0.0,1.0,0.0)
    11. _HorDir ("Horizontal Direction" , Vector) = (1.0,0.0,0.0)
    12.  
    13. }
    14. SubShader {
    15. Tags { "RenderType"="Opaque" }
    16. LOD 200
    17.  
    18. CGPROGRAM
    19. #pragma surface surf Lambert
    20.  
    21. sampler2D _SlopeMaxCol;
    22. float4 _Slope1;
    23. float4 _SlopeMin;
    24. sampler2D _Slope1Col;
    25. sampler2D _SlopeMinCol;
    26. float4 _VertDir;
    27. float4 _HorDir;
    28.  
    29. struct Input {
    30. float2 uv_MainTex;
    31.  
    32. float2 uv_Slope1Col;
    33. float2 uv_SlopeMinCol;
    34. float2 uv_SlopeMaxCol;
    35.  
    36. float3 worldNormal;
    37.  
    38. INTERNAL_DATA
    39. };
    40.  
    41. void surf (Input IN, inout SurfaceOutput o) {
    42.  
    43. if(dot(normalize(IN.worldNormal), _VertDir.xyz)>=dot(normalize(_Slope1.xyz),normalize(_HorDir.xyz)))
    44. o.Albedo = tex2D (_Slope1Col, IN.uv_Slope1Col).rgb;
    45. if(dot(normalize(IN.worldNormal), _VertDir.xyz)>=dot(normalize(_SlopeMin.xyz),normalize(_HorDir.xyz)))
    46. o.Albedo = tex2D (_SlopeMinCol, IN.uv_SlopeMinCol).rgb;
    47. if(dot(normalize(IN.worldNormal), _VertDir.xyz)<=dot(normalize(_Slope1.xyz),normalize(_HorDir.xyz)))
    48. o.Albedo = tex2D (_SlopeMaxCol, IN.uv_SlopeMaxCol).rgb;
    49.  
    50. o.Alpha = 1;
    51. }
    52. ENDCG
    53. }
    54. FallBack "Diffuse"
    55. }
     
    Last edited: Jan 18, 2016
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
  3. nitoh

    nitoh

    Joined:
    Mar 20, 2014
    Posts:
    30
    Thanks for your answer, but Triplanar is not what im looking for. I want different textures at different slope angles. That is working, but i cant blend them by alpha, to get rid of the straight border lines.
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    It's just blending as far as I understand. Alpha is not involved. That is pretty similar to triplanar texturing, but here you go:
    Code (csharp):
    1.  
    2. void surf (Input IN, inout SurfaceOutput o) {
    3.     float cosNormalVert = dot(normalize(IN.worldNormal), _VertDir.xyz);
    4.     float cosSlope1Hor = dot(normalize(_Slope1.xyz),normalize(_HorDir.xyz));
    5.     float cosSlopeMinHor = dot(normalize(_SlopeMin.xyz),normalize(_HorDir.xyz));
    6.     float3 color1 = tex2D(_Slope1Col, IN.uv_Slope1Col).rgb;
    7.     float3 color2 = tex2D(_SlopeMinCol, IN.uv_SlopeMinCol).rgb;
    8.     float3 color3 = tex2D(_SlopeMaxCol, IN.uv_SlopeMaxCol).rgb;
    9.     float blend2 = saturate((cosNormalVert - cosSlopeMinHor) / 0.1); // 0.1 is the blending range
    10.     float blend3 = saturate((cosSlope1Hor - cosNormalVert) / 0.1); // 0.1 is the blending range
    11.     o.Albedo = lerp(lerp(color1, color2, blend2), color3, blend3);
    12.     o.Alpha = 1.0;
    13. }
    14.  
     
  5. nitoh

    nitoh

    Joined:
    Mar 20, 2014
    Posts:
    30
    Yes! This is what is wanted! Thank you very much!