ParallaxCloud.shader 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Shader "Custom/ParallaxCloud"
  2. {
  3. Properties
  4. {
  5. _MainTex ("BaseMap:基础贴图", 2D) = "white" {}
  6. _Color ("Color:颜色", Color) = (0.0, 0.0, 0.0, 1.0)
  7. _ParallaxSize ("ParallaxSize:视差映射强度", Range(0.0, 0.1)) = 0.0
  8. _ParallaxLayers ("ParallaxLayers:视差映射层数", Range(1.0, 50.0)) = 20
  9. _MoveSpeed ("MoveSpeed:移动速度", Range(-0.5, 0.5)) = 0.1
  10. _Alpha ("Alpha:透明度", Range(0, 1)) = 0.5
  11. _AlphaExtent ("AlphaExtent:透明对比度", Range(0.0, 5.0)) = 5.0
  12. }
  13. SubShader
  14. {
  15. Tags
  16. {
  17. "RenderPipeline"="UniversalPipeline"
  18. "Queue"="Transparent"
  19. "RenderType"="Transparent"
  20. }
  21. Pass
  22. {
  23. Blend SrcAlpha OneMinusSrcAlpha
  24. Cull off
  25. HLSLPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  29. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  30. struct Attributes
  31. {
  32. float4 positionOS : POSITION;
  33. float3 normalOS : NORMAL;
  34. float2 uv : TEXCOORD0;
  35. float4 tangent : TANGENT;
  36. };
  37. struct Varyings
  38. {
  39. float4 positionCS : SV_POSITION;
  40. float3 positionWS : TEXCOORD0;
  41. float4 uv : TEXCOORD1;
  42. float3 nDirWS : TEXCOORD2;
  43. float3 tDirWS : TEXCOORD3;
  44. float3 bDirWS : TEXCOORD4;
  45. };
  46. CBUFFER_START(UnityPerMaterial)
  47. half4 _MainTex_ST;
  48. half4 _Color;
  49. half _ParallaxSize;
  50. half _ParallaxLayers;
  51. half _MoveSpeed;
  52. half _Alpha;
  53. half _AlphaExtent;
  54. CBUFFER_END
  55. TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
  56. TEXTURE2D(_NormalTex); SAMPLER(sampler_NormalTex);
  57. TEXTURE2D(_ParallaxTex); SAMPLER(sampler_ParallaxTex);
  58. Varyings vert (Attributes v)
  59. {
  60. Varyings o;
  61. o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
  62. o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
  63. o.nDirWS = TransformObjectToWorldNormal(v.normalOS);
  64. o.tDirWS = normalize(TransformObjectToWorld(v.tangent.xyz));
  65. o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);
  66. o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex) + float2(frac(_Time.y * _MoveSpeed), 0.0);
  67. o.uv.zw = v.uv;
  68. return o;
  69. }
  70. half4 frag (Varyings i) : SV_Target
  71. {
  72. //Light mylight = GetMainLight();
  73. //half3 light = mylight.color;
  74. float3x3 TBN = float3x3(i.tDirWS, i.bDirWS, i.nDirWS);
  75. float3 vDir = normalize(i.positionWS - _WorldSpaceCameraPos.xyz);
  76. float3 vDirTS = mul(TBN, vDir);
  77. float3 var_MainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw).rgb;
  78. half2 offset = 0.0;
  79. half currentDepth = 0.0;
  80. half parallaxDepth = 0.0;
  81. half preParallaxDepth = 0.0;
  82. half heightStep = 1.0 / _ParallaxLayers;
  83. half2 offsetTemp = vDirTS.xy / -vDirTS.z * _ParallaxSize;
  84. for (int j = 0; j < _ParallaxLayers; j++)
  85. {
  86. parallaxDepth = 1.0 - SAMPLE_TEXTURE2D_LOD(_MainTex, sampler_MainTex, i.uv.xy + offset, 0).r * var_MainTex.r;
  87. if (currentDepth > parallaxDepth) break;
  88. preParallaxDepth = parallaxDepth;
  89. currentDepth += heightStep;
  90. offset = offsetTemp * currentDepth;
  91. }
  92. half preDepth = currentDepth - heightStep;
  93. half A_C = preDepth - preParallaxDepth;
  94. half D_B = parallaxDepth - currentDepth;
  95. half t = A_C / (D_B + A_C);
  96. half height = lerp(preDepth, currentDepth, t);
  97. offset += offsetTemp * height;
  98. i.uv.xy += offset;
  99. half3 finalColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv.xy).rgb * var_MainTex.rgb;
  100. float alphaTex = max(var_MainTex.r * finalColor.r,0.0);
  101. half alpha = max(pow(abs(lerp(alphaTex, 1.0, _Alpha)), _AlphaExtent), 0.0) * _Alpha;
  102. return half4(finalColor * _Color.rgb, alpha);
  103. }
  104. ENDHLSL
  105. }
  106. }
  107. }