ParallaxCloud.shader 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. Shader "Custom/ParallaxCloud"
  2. {
  3. Properties
  4. {
  5. _MainTex ("BaseMap:基础贴图", 2D) = "white" {}
  6. // _TurbulenceTex ("turbulence:扰动图", 2D) = "white" {}
  7. _Color ("Color:颜色", Color) = (0.0, 0.0, 0.0, 1.0)
  8. _ParallaxSize ("ParallaxSize:视差映射强度", Range(0.0, 0.1)) = 0.0
  9. _ParallaxLayers ("ParallaxLayers:视差映射层数", Range(1.0, 50.0)) = 20
  10. _MoveSpeed ("MoveSpeed:移动速度", Range(-0.5, 0.5)) = 0.1
  11. _Alpha ("Alpha:透明度", Range(0, 1)) = 0.5
  12. _AlphaExtent ("AlphaExtent:透明对比度", Range(0.0, 5.0)) = 5.0
  13. // _worldDis("云的最远距离", float) = 0
  14. _ruiHua("虚化距离", float) = 300
  15. }
  16. SubShader
  17. {
  18. Tags
  19. {
  20. "RenderPipeline"="UniversalPipeline"
  21. "Queue"="Transparent"
  22. "RenderType"="Transparent"
  23. }
  24. Pass
  25. {
  26. Blend SrcAlpha OneMinusSrcAlpha
  27. Cull off
  28. ZWrite off
  29. HLSLPROGRAM
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  33. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  34. struct Attributes
  35. {
  36. float4 positionOS : POSITION;
  37. float3 normalOS : NORMAL;
  38. float2 uv : TEXCOORD0;
  39. float4 tangent : TANGENT;
  40. };
  41. struct Varyings
  42. {
  43. float4 positionCS : SV_POSITION;
  44. float3 positionWS : TEXCOORD0;
  45. float4 uv : TEXCOORD1;
  46. float3 nDirWS : TEXCOORD2;
  47. float3 tDirWS : TEXCOORD3;
  48. float3 bDirWS : TEXCOORD4;
  49. float4 screenPos : TEXCOORD5;
  50. };
  51. CBUFFER_START(UnityPerMaterial)
  52. half4 _MainTex_ST;
  53. half4 _Color;
  54. half _ParallaxSize;
  55. half _ParallaxLayers;
  56. half _MoveSpeed;
  57. half _Alpha;
  58. half _AlphaExtent;
  59. // float _worldDis;
  60. float _ruiHua;
  61. CBUFFER_END
  62. TEXTURE2D(_MainTex);
  63. SAMPLER(sampler_MainTex);
  64. TEXTURE2D(_NormalTex);
  65. SAMPLER(sampler_NormalTex);
  66. TEXTURE2D(_ParallaxTex);
  67. SAMPLER(sampler_ParallaxTex);
  68. sampler2D _CameraDepthTexture;
  69. // TEXTURE2D(_TurbulenceTex);
  70. // SAMPLER(sampler_TurbulenceTex);
  71. Varyings vert(Attributes v)
  72. {
  73. Varyings o;
  74. o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
  75. o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
  76. o.nDirWS = TransformObjectToWorldNormal(v.normalOS);
  77. o.tDirWS = normalize(TransformObjectToWorld(v.tangent.xyz));
  78. o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);
  79. // o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex) + float2(frac(_Time.y * _MoveSpeed), 0.0);
  80. float2 newUV=o.positionWS.xz*0.06;
  81. o.uv.xy =TRANSFORM_TEX( newUV, _MainTex) + float2(frac(_Time.y * _MoveSpeed), 0.0);
  82. o.uv.zw = o.positionWS.xz * 0.02;
  83. o.screenPos = ComputeScreenPos(o.positionCS);
  84. return o;
  85. }
  86. // 从深度值重建世界坐标
  87. float3 ReconstructWorldPosition(float2 uv, float depth)
  88. {
  89. // 1. 将 UV 转换为 NDC 空间 (Normalized Device Coordinates)
  90. float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0);
  91. // 2. 从 NDC 转换到裁剪空间
  92. float4 clipPos = ndc;
  93. clipPos.z = depth * 2.0 - 1.0; // 将深度从 [0,1] 转换为 [-1,1]
  94. // 3. 应用逆投影矩阵,转换到视图空间
  95. float4 viewPos = mul(unity_CameraInvProjection, clipPos); // 使用 Unity 内置的逆投影矩阵
  96. viewPos /= viewPos.w; // 透视除法
  97. // 4. 应用逆视图矩阵,转换到世界空间
  98. float4 worldPos = mul(UNITY_MATRIX_I_V, viewPos); // 使用 Unity 内置的逆视图矩阵
  99. return worldPos.xyz;
  100. }
  101. half4 frag(Varyings i) : SV_Target
  102. {
  103. float2 positionSS = i.positionCS.xy * (_ScreenParams.zw - 1);
  104. float depth = tex2D(_CameraDepthTexture, positionSS).r;
  105. float3 positionNDC = float3(positionSS * 2 - 1, depth);
  106. #if UNITY_UV_STARTS_AT_TOP
  107. positionNDC.y = -positionNDC.y;
  108. #endif
  109. #if REQUIRE_POSITION_VS
  110. float4 positionVS = mul(UNITY_MATRIX_I_P, float4(positionNDC, 1));
  111. positionVS /= positionVS.w;
  112. float4 positionWS = mul(UNITY_MATRIX_I_V, positionVS);
  113. #else
  114. float4 positionWS = mul(UNITY_MATRIX_I_VP, float4(positionNDC, 1));
  115. positionWS /= positionWS.w;
  116. #endif
  117. float dist = length(positionWS - i.positionWS);
  118. // return half4(dist * 0.1, 0, 0, 1.0);
  119. dist = dist * 0.2;
  120. dist = clamp(dist, 0, 1);
  121. float ptcpos = length(i.positionWS - _WorldSpaceCameraPos);
  122. ptcpos = 1 - (ptcpos - 0) / _ruiHua;
  123. ptcpos = clamp(ptcpos, 0, 1);
  124. //Light mylight = GetMainLight();
  125. //half3 light = mylight.color;
  126. float3x3 TBN = float3x3(i.tDirWS, i.bDirWS, i.nDirWS);
  127. float3 vDir = normalize(i.positionWS - _WorldSpaceCameraPos.xyz);
  128. float3 vDirTS = mul(TBN, vDir);
  129. // float2 raoDongUv = SAMPLE_TEXTURE2D(_TurbulenceTex, sampler_TurbulenceTex,i.positionWS.xz * 0.06).r*0.1;
  130. // i.uv.xy += raoDongUv;
  131. // i.uv.zw += raoDongUv;
  132. float3 var_MainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv.zw).rgb;
  133. half2 offset = 0.0;
  134. half currentDepth = 0.0;
  135. half parallaxDepth = 0.0;
  136. half preParallaxDepth = 0.0;
  137. half heightStep = 1.0 / _ParallaxLayers;
  138. half2 offsetTemp = vDirTS.xy / -vDirTS.z * _ParallaxSize;
  139. for (int j = 0; j < _ParallaxLayers; j++)
  140. {
  141. parallaxDepth = 1.0 - SAMPLE_TEXTURE2D_LOD(_MainTex, sampler_MainTex, i.uv.xy + offset, 0).r *
  142. var_MainTex.r;
  143. if (currentDepth > parallaxDepth) break;
  144. preParallaxDepth = parallaxDepth;
  145. currentDepth += heightStep;
  146. offset = offsetTemp * currentDepth;
  147. }
  148. half preDepth = currentDepth - heightStep;
  149. half A_C = preDepth - preParallaxDepth;
  150. half D_B = parallaxDepth - currentDepth;
  151. half t = A_C / (D_B + A_C);
  152. half height = lerp(preDepth, currentDepth, t);
  153. offset += offsetTemp * height;
  154. i.uv.xy += offset;
  155. half3 finalColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv.xy).rgb * var_MainTex.rgb;
  156. float alphaTex = max(var_MainTex.r * finalColor.r, 0.0);
  157. half alpha = max(pow(abs(lerp(alphaTex, 1.0, _Alpha)), _AlphaExtent), 0.0) * _Alpha * dist * ptcpos;
  158. return half4(finalColor * _Color.rgb, alpha);
  159. }
  160. ENDHLSL
  161. }
  162. }
  163. }