Unity 水面效果的shader文件

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/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  
  96. half3 bump1 = tex2D( _BumpMap, i.bumpuv[0] ).rgb;  
  97. half3 bump2 = tex2D( _BumpMap, i.bumpuv[1] ).rgb;  
  98. half3 bump = bump1 + bump2 - 1;  
  99. // fresnel factor   
  100. half fresnelFac = dot( i.viewDir, bump );  
  101. // perturb reflection/refraction UVs by bumpmap,&nb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值