zoukankan      html  css  js  c++  java
  • Unity-水面效果的shader文件

    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, and lookup colors 
    102. #ifdef HAS_REFLECTION   
    103. float3 uv1 = i.ref; uv1.xy += bump * _ReflDistort;  
    104. half4 refl = tex2Dproj( _ReflectionTex, uv1 );  
    105. #endif  
    106. #ifdef HAS_REFRACTION   
    107. float3 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;  
    108. half4 refr = tex2Dproj( _RefractionTex, uv2 ) * _RefrColor;  
    109. #endif   
    110. // final color is between refracted and reflected based on fresnel   
    111. half4 color;  
    112. #ifdef WATER_REFRACTIVE   
    113. half fresnel = tex2D( _Fresnel, float2(fresnelFac,fresnelFac) ).a;  
    114. color = lerp( refr, refl, fresnel );  
    115. #endif  
    116. #ifdef WATER_REFLECTIVE   
    117. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );  
    118. color.rgb = lerp( water.rgb, refl.rgb, water.a );  
    119. color.a = refl.a * water.a;  
    120. #endif  
    121. #ifdef WATER_SIMPLE   
    122. half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );  
    123. color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );  
    124. color.a = _HorizonColor.a;  
    125. #endif   
    126. return color;  
    127. }  
    128. ENDCG  
    129. }  
    130. }  
    131. // -----------------------------------------------------------  
    132. // Radeon 9000 cards   
    133. Subshader {  
    134. Tags { "WaterMode"="Reflective" "RenderType"="Opaque" }  
    135. Pass {  
    136. CGPROGRAM  
    137. #pragma vertex vert  
    138. #include "UnityCG.cginc"   
    139. uniform float4 _WaveScale4;  
    140. uniform float4 _WaveOffset;  
    141. uniform float _ReflDistort;  
    142. struct appdata {  
    143. float4 vertex : POSITION;  
    144. float3 normal : NORMAL;  
    145. };  
    146. struct v2f {  
    147. V2F_POS_FOG;  
    148. float2 bumpuv[2] : TEXCOORD0;  
    149. float3 viewDir : TEXCOORD2;  
    150. float4 ref : TEXCOORD3;  
    151. };  
    152. v2f vert(appdata v)  
    153. {  
    154. v2f o;  
    155. PositionFog( v.vertex, o.pos, o.fog );  
    156. // scroll bump waves   
    157. float4 temp;  
    158. temp.xyzw = v.vertex.xzxz * _WaveScale4 + _WaveOffset;  
    159. o.bumpuv[0] = temp.xy;  
    160. o.bumpuv[1] = temp.wz;  
    161. // object space view direction  
    162. o.viewDir.xzy = normalize( ObjSpaceViewDir(v.vertex) );  
    163. // calculate the reflection vector  
    164. float4x4 mat = float4x4 (  
    165.    .5, 0, 0,.5,  
    166.    0,.5 * _ProjectionParams.x, 0,.5,  
    167.    0, 0,.5,.5,  
    168.    0, 0, 0, 1  
    169. );   
    170. o.ref = mul (mat, o.pos);  
    171. return o;  
    172. }  
    173. ENDCG  
    174. Program "" {  
    175. SubProgram {  
    176. Keywords { "WATER_REFLECTIVE" "WATER_REFRACTIVE" }  
    177. SetTexture [_BumpMap] { 2D }  
    178. SetTexture [_BumpMap] { 2D }  
    179. SetTexture [_ReflectiveColor] { 2D }  
    180. SetTexture [_ReflectionTex] { 2D }  
    181. Local 0, ([_ReflDistort],0,0,0)  
    182. "!!ATIfs1.0  
    183. StartConstants;  
    184. CONSTANT c0 = program.local[0];  
    185. EndConstants;  
    186. StartPrelimPass;  
    187. PassTexCoord r3, t3.stq_dq; # reflection vector  
    188. SampleMap r0, t0.str; # bump1  
    189. SampleMap r1, t1.str; # bump2  
    190. PassTexCoord r2, t2.str;  
    191. ADD r1.half, r0.bias, r1.bias; # bump = bump1 + bump2 - 1  
    192. DOT3 r2, r1.2x, r2;     # fresnel: dot (bump, viewer-pos)  
    193. # add less offset because it's purely screenspace; big ones look bad  
    194. MAD r3.rg, r1, c0.r, r3;   # uv += bump * strength; add less because it's not perspective  
    195. EndPass;  
    196. StartOutputPass;  
    197. SampleMap r3, r3.str;   # reflection color  
    198.    SampleMap r2, r2.str;   # water color/fresnel  
    199. LERP r0.rgb, r2.a, r3, r2; # between water and reflected based on fresnel  
    200. MUL r0.a, r3.a, r2.a;  
    201. EndPass;  
    202. "   
    203. }  
    204. SubProgram {  
    205. Keywords { "WATER_SIMPLE" }  
    206. SetTexture [_BumpMap] { 2D }  
    207. SetTexture [_BumpMap] { 2D }  
    208. SetTexture [_ReflectiveColor] { 2D }  
    209. Local 0, [_HorizonColor]  
    210. "!!ATIfs1.0  
    211. StartConstants;  
    212. CONSTANT c0 = program.local[0];  
    213. EndConstants;  
    214. StartPrelimPass;  
    215. SampleMap r0, t0.str;  
    216. SampleMap r1, t1.str;  
    217. PassTexCoord r2, t2.str;  
    218. ADD r1, r0.bias, r1.bias; # bump = bump1 + bump2 - 1  
    219. DOT3 r2, r1, r2;    # fresnel: dot (bump, viewer-pos)  
    220. EndPass;  
    221. StartOutputPass;  
    222.    SampleMap r2, r2.str;  
    223. LERP r0.rgb, r2.a, c0, r2; # fade in reflection  
    224. MOV r0.a, c0.a;  
    225. EndPass;  
    226. "   
    227. }  
    228. }  
    229. }  
    230. }  
    231. // -----------------------------------------------------------  
    232. // Old cards   
    233. // three texture, cubemaps   
    234. Subshader {  
    235. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }  
    236. Pass {  
    237.    Color (0.5,0.5,0.5,0.5)  
    238.    SetTexture [_MainTex] {  
    239.     Matrix [_WaveMatrix]  
    240.     combine texture * primary  
    241.    }  
    242.    SetTexture [_MainTex] {  
    243.     Matrix [_WaveMatrix2]  
    244.     combine texture * primary + previous  
    245.    }  
    246.    SetTexture [_ReflectiveColorCube] {  
    247.     combine texture +- previous, primary  
    248.     Matrix [_Reflection]  
    249.    }  
    250. }  
    251. }  
    252. // dual texture, cubemaps   
    253. Subshader {  
    254. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }  
    255. Pass {  
    256.    Color (0.5,0.5,0.5,0.5)  
    257.    SetTexture [_MainTex] {  
    258.     Matrix [_WaveMatrix]  
    259.     combine texture  
    260.    }  
    261.    SetTexture [_ReflectiveColorCube] {  
    262.     combine texture +- previous, primary  
    263.     Matrix [_Reflection]  
    264.    }  
    265. }  
    266. }  
    267. // single texture   
    268. Subshader {  
    269. Tags { "WaterMode"="Simple" "RenderType"="Opaque" }  
    270. Pass {  
    271.    Color (0.5,0.5,0.5,0)  
    272.    SetTexture [_MainTex] {  
    273.     Matrix [_WaveMatrix]  
    274.     combine texture, primary  
    275.    }  
    276. }  
    277. }  
    278. }  
     

    再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 相关阅读:
    android 自定义日历控件
    android 常用类
    真假空格风波
    设计模式的初衷---“委托”有感
    pymysql.err.InterfaceError: (0, '')
    微信文章收藏到有道云笔记PC版只保留了标题
    SQL Server数据库字典生成SQL
    nhibernate常见错误
    NUnit
    使用ffmpeg截取视频
  • 原文地址:https://www.cnblogs.com/skiwnchiwns/p/10343723.html
Copyright © 2011-2022 走看看