Advertisement
443eb9

Untitled

Aug 18th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. Shader "Unlit/Water"
  2. {
  3. Properties
  4. {
  5. _Smoothness ("Smoothness", Range(0.5, 1.5)) = 1
  6.  
  7. _ShallowColor ("ShallowColor", Color) = (1, 1, 1, 1)
  8. _DeepColor ("DeepColor", Color) = (1, 1, 1, 1)
  9. _BlendStrength ("BlendStrength", Range(0, 1)) = 0.5
  10. _MaxDepth ("MaxDepth", Float) = 1
  11.  
  12. _WaveNormal ("WaveNormal", 2D) = "bump"{}
  13. _WaveNormalScale ("WaveNormalScale", Range(0, 1)) = 0.5
  14. _WaveNormalSpeed ("WaveNormalSpeed", Range(0, 2)) = 1
  15.  
  16. _WaveVertexFlow ("WaveVertexFlow", Vector) = (1, 1, 1, 1)
  17. _WaveVertexScale ("WaveVertexScale", Range(0, 1)) = 0.5
  18. _WaveVertexFrequency ("WaveVertexFrequency", Float) = 1
  19. }
  20. SubShader
  21. {
  22. Tags
  23. {
  24. "RenderPipeline"="UniversalPipeline"
  25. "Queue"="Transparent"
  26. "RenderType"="Transparent"
  27. }
  28. LOD 100
  29.  
  30. Pass
  31. {
  32. Blend SrcAlpha OneMinusSrcAlpha
  33.  
  34. HLSLPROGRAM
  35. #pragma vertex vert
  36. #pragma fragment frag
  37. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  38. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  39. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  40. #include "Assets/Shaders/Lib/FastNoiseLite.hlsl"
  41.  
  42. CBUFFER_START(UnityPerMaterial)
  43. float _Smoothness;
  44.  
  45. float4 _ShallowColor;
  46. float4 _DeepColor;
  47. float _BlendStrength;
  48. float _MaxDepth;
  49.  
  50. float4 _WaveNormal_ST;
  51. float _WaveNormalScale;
  52. float _WaveNormalSpeed;
  53.  
  54. float4 _WaveVertexFlow;
  55. float _WaveVertexScale;
  56. float _WaveVertexFrequency;
  57. CBUFFER_END
  58.  
  59. TEXTURE2D(_WaveNormal);
  60. SAMPLER(sampler_WaveNormal);
  61.  
  62. struct Attributes
  63. {
  64. float4 positionOS : POSITION;
  65. float3 normalOS : NORMAL;
  66. float2 uv : TEXCOORD0;
  67. };
  68.  
  69. struct Varyings
  70. {
  71. float4 positionHCS : SV_POSITION;
  72. float3 normalWS : TEXCOORD0;
  73. float4 uvNormal : TEXCOORD1;
  74. float3 positionWS : TEXCOORD2;
  75. float4 positionNDC : TEXCOORD3;
  76. };
  77.  
  78. Varyings vert(Attributes IN)
  79. {
  80. Varyings OUT;
  81.  
  82. float2 noisePos = float2(IN.positionOS.x + _Time.y * _WaveVertexFlow.x,
  83. IN.positionOS.z + _Time.y * _WaveVertexFlow.y) * _WaveVertexFrequency;
  84. float noise = (fnlGetNoise2D(fnlCreateState(), noisePos.x, noisePos.y) * _WaveVertexScale + 1) / 2;
  85. float4 positionOS = IN.positionOS + float4(IN.normalOS * noise, 0);
  86.  
  87. VertexPositionInputs vpi = GetVertexPositionInputs(positionOS);
  88. OUT.positionHCS = vpi.positionCS;
  89. OUT.positionNDC = vpi.positionNDC;
  90.  
  91. VertexNormalInputs vni = GetVertexNormalInputs(IN.normalOS);
  92. OUT.normalWS = vni.normalWS;
  93.  
  94. float2 uv = TRANSFORM_TEX(IN.uv, _WaveNormal);
  95. OUT.uvNormal.xy = uv + float2(-1, 1) * _Time.x * _WaveNormalSpeed;
  96. OUT.uvNormal.zw = uv + float2(1, 1) * _Time.x * _WaveNormalSpeed;
  97.  
  98. OUT.positionWS = TransformObjectToWorld(IN.positionOS);
  99. return OUT;
  100. }
  101.  
  102. float4 frag(Varyings IN) : SV_Target
  103. {
  104. float3 normal1 = UnpackNormalScale(
  105. SAMPLE_TEXTURE2D(_WaveNormal, sampler_WaveNormal, IN.uvNormal.xy), _WaveNormalScale);
  106. float3 normal2 = UnpackNormalScale(
  107. SAMPLE_TEXTURE2D(_WaveNormal, sampler_WaveNormal, IN.uvNormal.zw), _WaveNormalScale);
  108. float3 normalWS = normalize(lerp(normal1, normal2, 0.5));
  109. float3 viewWS = GetWorldSpaceNormalizeViewDir(IN.positionWS);
  110. Light mainLight = GetMainLight();
  111.  
  112. float depth = LinearEyeDepth(SampleSceneDepth(IN.positionNDC.xy / IN.positionNDC.w), _ZBufferParams) - IN.positionNDC.w;
  113. float4 albedo = lerp(_ShallowColor, _DeepColor, saturate(pow(depth, _MaxDepth)));
  114.  
  115. BRDFData data;
  116. InitializeBRDFData(albedo.xyz, 0, 1, _Smoothness, albedo.a, data);
  117. float3 color = LightingPhysicallyBased(data, mainLight, normalWS, viewWS);
  118.  
  119. return float4(color, albedo.a);
  120. }
  121. ENDHLSL
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement