当前位置:   article > 正文

Unity学习之Shader Water_unity water

unity water
  1. Shader "URP/Water"
  2. {
  3. Properties
  4. {
  5. _ShallowWater ("shallowColor", Color) = (1.0, 1.0, 1.0, 1.0)
  6. _DeepWater ("DeepColor", Color) = (1.0, 1.0, 1.0, 1.0)
  7. _WaterAlpha("WaterAlpha",Range(0,1)) = 0.5
  8. _SurfaceNoise("Surface Noise", 2D) = "white" {}
  9. _MoveSpeed("MoveSpeed",Range(0,1)) = 0.5
  10. _FoamDistance("Foam Distance",Range(0,10)) = 0.4
  11. _FoamColor("FoamColor", Color) = (1.0, 1.0, 1.0, 1.0)
  12. }
  13. SubShader
  14. {
  15. Tags
  16. {
  17. "RenderPipeline"="UniversalPipeline"
  18. "RenderType"="Transparent"
  19. "Queue"="Transparent"
  20. }
  21. Pass
  22. {
  23. Blend SrcAlpha OneMinusSrcAlpha
  24. HLSLPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  28. float4 _ShallowWater;
  29. float4 _DeepWater;
  30. float _WaterAlpha;
  31. sampler2D _SurfaceNoise;
  32. float4 _SurfaceNoise_ST;
  33. float _MoveSpeed;
  34. float _FoamDistance;
  35. float4 _FoamColor;
  36. // 顶点着色器的输入
  37. struct a2v
  38. {
  39. float3 positionOS : POSITION;
  40. float4 uv : TEXCOORD0;
  41. };
  42. // 顶点着色器的输出
  43. struct v2f
  44. {
  45. float4 positionCS : SV_POSITION;
  46. float4 screenPosition : TEXCOORD0;
  47. float2 noiseUV : TEXCOORD1;
  48. float2 distortUV : TEXCOORD2;
  49. };
  50. v2f vert(a2v v)
  51. {
  52. v2f o;
  53. VertexPositionInputs positionInputs = GetVertexPositionInputs(v.positionOS);
  54. o.positionCS = positionInputs.positionCS;
  55. o.noiseUV = TRANSFORM_TEX(v.uv, _SurfaceNoise);
  56. o.screenPosition = ComputeScreenPos(positionInputs.positionCS);
  57. return o;
  58. }
  59. half4 frag(v2f i) : SV_Target
  60. {
  61. // 通过深度纹理的采样 计算屏幕深度
  62. float sceneRawDepth = SampleSceneDepth(i.screenPosition.xy / i.screenPosition.w);
  63. // 深度纹理的采样结果转换到视图空间下的深度值
  64. float sceneEyeDepth = LinearEyeDepth(sceneRawDepth, _ZBufferParams);
  65. // 因为关心的是这个深度值相对于我们的水面有多深,所以需要把视图深度,减去模型顶点的深度
  66. // 最终得到水的深度
  67. float waterDepth = sceneEyeDepth - i.screenPosition.w;
  68. // 拿到水的颜色
  69. float3 waterColor = lerp(_ShallowWater, _DeepWater, waterDepth);
  70. float surfaceNoiseSample = tex2D(_SurfaceNoise, i.noiseUV + _Time.y * _MoveSpeed * 0.1).r;
  71. // 浮沫
  72. float foam = saturate(waterDepth / _FoamDistance);
  73. float surfaceNoise = smoothstep(0, foam, surfaceNoiseSample) ;
  74. // 混合水面透明度
  75. float4 col = float4(waterColor + surfaceNoise * _FoamColor, _WaterAlpha) ;
  76. return col;
  77. }
  78. ENDHLSL
  79. }
  80. }
  81. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/108503?site
推荐阅读
相关标签
  

闽ICP备14008679号