Unity 水面效果的shader文件

本文分享了一篇关于Unity中实现水面效果的Shader代码,包括反射、折射和简单水体颜色的处理,适合对Unity图形编程感兴趣的读者深入学习。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

[javascript] view plain copy print ?
  1. Shader "FX/Water" {   
  2. Properties {  
  3. _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063  
  4. _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44  
  5. _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40  
  6. _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1)  
  7. _Fresnel ("Fresnel (A) ", 2D) = "gray" {}  
  8. _BumpMap ("Bumpmap (RGB) ", 2D) = "bump" {}  
  9. WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)  
  10. _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}  
  11. _ReflectiveColorCube ("Reflective color cube (RGB) fresnel (A)", Cube) = "" { TexGen CubeReflect }  
  12. _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1)  
  13. _MainTex ("Fallback texture", 2D) = "" {}  
  14. _ReflectionTex ("Internal Reflection", 2D) = "" {}  
  15. _RefractionTex ("Internal Refraction", 2D) = "" {}  
  16. }  
  17. // -----------------------------------------------------------  
  18. // Fragment program cards   
  19. Subshader {   
  20. Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }  
  21. Pass {  
  22. CGPROGRAM  
  23. #pragma vertex vert  
  24. #pragma fragment frag  
  25. #pragma fragmentoption ARB_precision_hint_fastest  
  26. #pragma fragmentoption ARB_fog_exp2  
  27. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE 
  28. #if defined WATER_REFLECTIVE || defined WATER_REFRACTIVE 
  29. #define HAS_REFLECTION 1  
  30. #endif  
  31. #if defined WATER_REFRACTIVE  
  32. #define HAS_REFRACTION 1  
  33. #endif  
  34. #include "UnityCG.cginc"   
  35. uniform float4 _WaveScale4;  
  36. uniform float4 _WaveOffset;  
  37. #ifdef HAS_REFLECTION   
  38. uniform float _ReflDistort;  
  39. #endif  
  40. #ifdef HAS_REFRACTION   
  41. uniform float _RefrDistort; 
  42. #endif   
  43. struct appdata {  
  44. float4 vertex : POSITION;  
  45. float3 normal : NORMAL;  
  46. };  
  47. struct v2f {  
  48. V2F_POS_FOG;  
  49. #if defined HAS_REFLECTION || defined HAS_REFRACTION  
  50. float3 ref;  
  51. #endif   
  52. float2 bumpuv[2];  
  53. float3 viewDir;  
  54. };  
  55. v2f vert(appdata v)  
  56. {  
  57. v2f o;  
  58. PositionFog( v.vertex, o.pos, o.fog );  
  59. // scroll bump waves   
  60. float4 temp;  
  61. temp.xyzw = v.vertex.xzxz * _WaveScale4 + _WaveOffset;  
  62. o.bumpuv[0] = temp.xy;  
  63. o.bumpuv[1] = temp.wz;  
  64. // object space view direction (will normalize per pixel)  
  65. o.viewDir.xzy = ObjSpaceViewDir(v.vertex);  
  66. #if defined HAS_REFLECTION || defined HAS_REFRACTION  
  67. // calculate the reflection vector  
  68. float3x4 mat = float3x4 (  
  69.    0.5, 0, 0, 0.5,  
  70.    0, 0.5 * _ProjectionParams.x, 0, 0.5,  
  71.    0, 0, 0, 1  
  72. );   
  73. o.ref = mul (mat, o.pos);  
  74. #endif   
  75. return o;  
  76. }  
  77. #if defined WATER_REFLECTIVE || defined WATER_REFRACTIVE  
  78. sampler2D _ReflectionTex;  
  79. #endif  
  80. #if defined WATER_REFLECTIVE || defined WATER_SIMPLE  
  81. sampler2D _ReflectiveColor;  
  82. #endif  
  83. #if defined WATER_REFRACTIVE   
  84. sampler2D _Fresnel;  
  85. sampler2D _RefractionTex;  
  86. uniform float4 _RefrColor;  
  87. #endif  
  88. #if defined WATER_SIMPLE   
  89. uniform float4 _HorizonColor;  
  90. #endif   
  91. sampler2D _BumpMap;  
  92. half4 frag( v2f i ) : COLOR  
  93. {  
  94. i.viewDir = normalize(i.viewDir);  
  95. // combine two scrolling bumpmaps into one  </
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity水面Shader是一种用于模拟水面效果的着色器。它可以实现浅水和深水颜色变化、水面漂浮物和岸边波纹等效果。以下是两种实现Unity水面Shader的方法: 1. 使用Unity卡通水面Shader[^1]: ```csharp // 在材质球上应用Unity卡通水面Shader Shader "Custom/WaterSurface" { Properties { _Color ("Color", Color) = (1,1,1,1) _DepthColor ("Depth Color", Color) = (0,0,0,1) _WaveSpeed ("Wave Speed", Range(0, 10)) = 1 _WaveStrength ("Wave Strength", Range(0, 1)) = 0.5 _WaveScale ("Wave Scale", Range(0, 10)) = 1 _FoamTexture ("Foam Texture", 2D) = "white" {} _FoamTiling ("Foam Tiling", Range(0, 10)) = 1 _FoamStrength ("Foam Strength", Range(0, 1)) = 0.5 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert sampler2D _FoamTexture; float _FoamTiling; float _FoamStrength; struct Input { float2 uv_Foam; }; void surf (Input IN, inout SurfaceOutput o) { // 计算水面颜色 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; fixed4 depthColor = _DepthColor; // 计算波纹效果 float2 uv_Wave = IN.uv_MainTex * _WaveScale + _Time.y * _WaveSpeed; float wave = sin(uv_Wave.x + uv_Wave.y) * _WaveStrength; c.rgb += wave; // 计算水泡效果 float foam = tex2D(_FoamTexture, IN.uv_Foam * _FoamTiling).r * _FoamStrength; c.rgb += foam; // 应用颜色和透明度 o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } ``` 2. 使用Shader Graph实现水面特效[^2]: 在Unity中使用Shader Graph可以更直观地创建水面特效。以下是一个简单的Shader Graph示例,实现了水面的波纹效果: - 创建一个新的Shader Graph材质。 - 添加一个Texture2D节点,用于水面的纹理。 - 添加一个Time节点,用于控制波纹的移动。 - 添加一个Sine波形节点,将Time节点连接到Sine节点的输入。 - 将纹理节点和Sine节点连接到PBR Master节点的Albedo输入。 - 将PBR Master节点连接到Master节点的Surface输入。 - 将Master节点连接到材质球的Shader输入。 这样就可以在Unity中实现水面的波纹效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值