Preview_FresnelNode.shader 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. Shader "Hidden/FresnelNode"
  2. {
  3. Properties
  4. {
  5. _A ("_Normal", 2D) = "white" {}
  6. _B ("_Bias", 2D) = "white" {}
  7. _C ("_Scale", 2D) = "white" {}
  8. _D ("_Power", 2D) = "white" {}
  9. _E ("_View", 2D) = "white" {}
  10. }
  11. SubShader
  12. {
  13. Pass //not connected world
  14. {
  15. CGPROGRAM
  16. #include "UnityCG.cginc"
  17. #include "Preview.cginc"
  18. #pragma vertex vert_img
  19. #pragma fragment frag
  20. //sampler2D _A;
  21. sampler2D _B;
  22. sampler2D _C;
  23. sampler2D _D;
  24. int _FresnelType;
  25. float4 frag(v2f_img i) : SV_Target
  26. {
  27. float b = tex2D( _B, i.uv ).r;
  28. float s = tex2D( _C, i.uv ).r;
  29. float pw = tex2D( _D, i.uv ).r;
  30. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  31. float3 vertexNormal = PreviewFragmentNormalOS( i.uv );
  32. float3 worldNormal = UnityObjectToWorldNormal( vertexNormal );
  33. float3 worldViewDir = normalize(preview_WorldSpaceCameraPos - vertexPos);
  34. float fresnel = 0;
  35. if(_FresnelType == 0)
  36. fresnel = (b + s*pow(1 - dot( worldNormal, worldViewDir ) , pw));
  37. else if(_FresnelType == 1)
  38. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  39. else if(_FresnelType == 2)
  40. {
  41. float f0 = pow((1-s)/(1+s),2);
  42. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  43. }
  44. return fresnel;
  45. }
  46. ENDCG
  47. }
  48. Pass //connected world
  49. {
  50. CGPROGRAM
  51. #include "UnityCG.cginc"
  52. #include "Preview.cginc"
  53. #pragma vertex vert_img
  54. #pragma fragment frag
  55. sampler2D _A;
  56. sampler2D _B;
  57. sampler2D _C;
  58. sampler2D _D;
  59. int _FresnelType;
  60. float4 frag(v2f_img i) : SV_Target
  61. {
  62. float b = tex2D( _B, i.uv ).r;
  63. float s = tex2D( _C, i.uv ).r;
  64. float pw = tex2D( _D, i.uv ).r;
  65. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  66. float3 worldNormal = tex2D( _A, i.uv );
  67. float3 worldViewDir = normalize(preview_WorldSpaceCameraPos - vertexPos);
  68. float fresnel = 0;
  69. if(_FresnelType == 0)
  70. fresnel = (b + s*pow(1 - dot( worldNormal, worldViewDir ) , pw));
  71. else if(_FresnelType == 1)
  72. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  73. else if(_FresnelType == 2)
  74. {
  75. float f0 = pow((1-s)/(1+s),2);
  76. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  77. }
  78. return fresnel;
  79. }
  80. ENDCG
  81. }
  82. Pass //connected tangent
  83. {
  84. CGPROGRAM
  85. #include "UnityCG.cginc"
  86. #include "Preview.cginc"
  87. #pragma vertex vert_img
  88. #pragma fragment frag
  89. sampler2D _A;
  90. sampler2D _B;
  91. sampler2D _C;
  92. sampler2D _D;
  93. int _FresnelType;
  94. float4 frag(v2f_img i) : SV_Target
  95. {
  96. float b = tex2D( _B, i.uv ).r;
  97. float s = tex2D( _C, i.uv ).r;
  98. float pw = tex2D( _D, i.uv ).r;
  99. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  100. float3 vertexNormal = PreviewFragmentNormalOS( i.uv );
  101. float3 worldNormal = UnityObjectToWorldNormal( vertexNormal );
  102. float3 tangent = PreviewFragmentTangentOS( i.uv );
  103. float3 worldPos = mul(unity_ObjectToWorld, float4(vertexPos,1)).xyz;
  104. float3 worldTangent = UnityObjectToWorldDir(tangent);
  105. float tangentSign = -1;
  106. float3 worldBinormal = normalize( cross(worldNormal, worldTangent) * tangentSign);
  107. float4 tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
  108. float4 tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
  109. float4 tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
  110. float2 sphereUVs = i.uv;
  111. sphereUVs.x = (atan2(vertexPos.x, -vertexPos.z) / (UNITY_PI) + 0.5);
  112. float3 tangentNormal = tex2D(_A, sphereUVs).xyz;
  113. worldNormal = fixed3( dot( tSpace0.xyz, tangentNormal ), dot( tSpace1.xyz, tangentNormal ), dot( tSpace2.xyz, tangentNormal ) );
  114. float3 worldViewDir = normalize(preview_WorldSpaceCameraPos - vertexPos);
  115. float fresnel = 0;
  116. if(_FresnelType == 0)
  117. fresnel = (b + s*pow(1 - dot( worldNormal, worldViewDir ) , pw));
  118. else if(_FresnelType == 1)
  119. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  120. else if(_FresnelType == 2)
  121. {
  122. float f0 = pow((1-s)/(1+s),2);
  123. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  124. }
  125. return fresnel;
  126. }
  127. ENDCG
  128. }
  129. Pass //not connected half vector
  130. {
  131. CGPROGRAM
  132. #include "UnityCG.cginc"
  133. #include "Preview.cginc"
  134. #pragma vertex vert_img
  135. #pragma fragment frag
  136. //sampler2D _A;
  137. sampler2D _B;
  138. sampler2D _C;
  139. sampler2D _D;
  140. int _FresnelType;
  141. float4 _EditorWorldLightPos;
  142. float4 frag(v2f_img i) : SV_Target
  143. {
  144. float b = tex2D( _B, i.uv ).r;
  145. float s = tex2D( _C, i.uv ).r;
  146. float pw = tex2D( _D, i.uv ).r;
  147. float2 xy = 2 * i.uv - 1;
  148. float z = -sqrt(1-(dot(xy,xy)));
  149. float3 vertexPos = normalize(float3(xy, z));
  150. float3 worldViewDir = normalize(preview_WorldSpaceCameraPos - vertexPos);
  151. float3 lightDir = normalize( _EditorWorldLightPos.xyz );
  152. float3 halfVector = normalize(worldViewDir+lightDir);
  153. float fresnel = 0;
  154. if(_FresnelType == 0)
  155. fresnel = (b + s*pow(1 - dot( halfVector, worldViewDir ) , pw));
  156. else if(_FresnelType == 1)
  157. fresnel = (b + (1-b) * pow(1 - dot( halfVector, worldViewDir ) , 5));
  158. else if(_FresnelType == 2)
  159. {
  160. float f0 = pow((1-s)/(1+s),2);
  161. fresnel = (f0 + (1-f0) * pow(1 - dot( halfVector, worldViewDir ) , 5));
  162. }
  163. return fresnel;
  164. }
  165. ENDCG
  166. }
  167. Pass //connected both
  168. {
  169. CGPROGRAM
  170. #include "UnityCG.cginc"
  171. #include "Preview.cginc"
  172. #pragma vertex vert_img
  173. #pragma fragment frag
  174. sampler2D _A;
  175. sampler2D _B;
  176. sampler2D _C;
  177. sampler2D _D;
  178. sampler2D _E;
  179. int _FresnelType;
  180. float4 frag(v2f_img i) : SV_Target
  181. {
  182. float b = tex2D( _B, i.uv ).r;
  183. float s = tex2D( _C, i.uv ).r;
  184. float pw = tex2D( _D, i.uv ).r;
  185. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  186. float3 worldNormal = tex2D( _A, i.uv );
  187. float3 worldViewDir = tex2D( _E, i.uv );;
  188. float fresnel = 0;
  189. if(_FresnelType == 0)
  190. fresnel = (b + s*pow(1 - dot( worldNormal, worldViewDir ) , pw));
  191. else if(_FresnelType == 1)
  192. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  193. else if(_FresnelType == 2)
  194. {
  195. float f0 = pow((1-s)/(1+s),2);
  196. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  197. }
  198. return fresnel;
  199. }
  200. ENDCG
  201. }
  202. Pass //not connected world and light
  203. {
  204. CGPROGRAM
  205. #include "UnityCG.cginc"
  206. #include "Preview.cginc"
  207. #pragma vertex vert_img
  208. #pragma fragment frag
  209. //sampler2D _A;
  210. sampler2D _B;
  211. sampler2D _C;
  212. sampler2D _D;
  213. int _FresnelType;
  214. float4 _EditorWorldLightPos;
  215. float4 frag(v2f_img i) : SV_Target
  216. {
  217. float b = tex2D( _B, i.uv ).r;
  218. float s = tex2D( _C, i.uv ).r;
  219. float pw = tex2D( _D, i.uv ).r;
  220. float2 xy = 2 * i.uv - 1;
  221. float z = -sqrt(1-(dot(xy,xy)));
  222. float3 vertexPos = normalize(float3(xy, z));
  223. float3 normal = normalize(vertexPos);
  224. float3 worldNormal = UnityObjectToWorldNormal(normal);
  225. float3 lightDir = normalize( _EditorWorldLightPos.xyz );
  226. float fresnel = 0;
  227. if(_FresnelType == 0)
  228. fresnel = (b + s*pow(1 - dot( worldNormal, lightDir ) , pw));
  229. else if(_FresnelType == 1)
  230. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, lightDir ) , 5));
  231. else if(_FresnelType == 2)
  232. {
  233. float f0 = pow((1-s)/(1+s),2);
  234. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, lightDir ) , 5));
  235. }
  236. return fresnel;
  237. }
  238. ENDCG
  239. }
  240. Pass //connected view
  241. {
  242. CGPROGRAM
  243. #include "UnityCG.cginc"
  244. #include "Preview.cginc"
  245. #pragma vertex vert_img
  246. #pragma fragment frag
  247. sampler2D _A;
  248. sampler2D _B;
  249. sampler2D _C;
  250. sampler2D _D;
  251. sampler2D _E;
  252. int _FresnelType;
  253. float4 frag(v2f_img i) : SV_Target
  254. {
  255. float b = tex2D( _B, i.uv ).r;
  256. float s = tex2D( _C, i.uv ).r;
  257. float pw = tex2D( _D, i.uv ).r;
  258. float3 vertexPos = PreviewFragmentPositionOS( i.uv );
  259. float3 normal = normalize(vertexPos);
  260. float3 worldNormal = UnityObjectToWorldNormal(normal);
  261. float3 worldViewDir = tex2D( _E, i.uv );
  262. float fresnel = 0;
  263. if(_FresnelType == 0)
  264. fresnel = (b + s*pow(1 - dot( worldNormal, worldViewDir ) , pw));
  265. else if(_FresnelType == 1)
  266. fresnel = (b + (1-b) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  267. else if(_FresnelType == 2)
  268. {
  269. float f0 = pow((1-s)/(1+s),2);
  270. fresnel = (f0 + (1-f0) * pow(1 - dot( worldNormal, worldViewDir ) , 5));
  271. }
  272. return fresnel;
  273. }
  274. ENDCG
  275. }
  276. Pass //not connected half vector with connected view
  277. {
  278. CGPROGRAM
  279. #include "UnityCG.cginc"
  280. #include "Preview.cginc"
  281. #pragma vertex vert_img
  282. #pragma fragment frag
  283. //sampler2D _A;
  284. sampler2D _B;
  285. sampler2D _C;
  286. sampler2D _D;
  287. sampler2D _E;
  288. int _FresnelType;
  289. float4 _EditorWorldLightPos;
  290. float4 frag(v2f_img i) : SV_Target
  291. {
  292. float b = tex2D( _B, i.uv ).r;
  293. float s = tex2D( _C, i.uv ).r;
  294. float pw = tex2D( _D, i.uv ).r;
  295. float2 xy = 2 * i.uv - 1;
  296. float z = -sqrt(1-(dot(xy,xy)));
  297. float3 vertexPos = normalize(float3(xy, z));
  298. float3 worldViewDir = tex2D( _E, i.uv );
  299. float3 lightDir = normalize( _EditorWorldLightPos.xyz );
  300. float3 halfVector = normalize(worldViewDir+lightDir);
  301. float fresnel = 0;
  302. if(_FresnelType == 0)
  303. fresnel = (b + s*pow(1 - dot( halfVector, worldViewDir ) , pw));
  304. else if(_FresnelType == 1)
  305. fresnel = (b + (1-b) * pow(1 - dot( halfVector, worldViewDir ) , 5));
  306. else if(_FresnelType == 2)
  307. {
  308. float f0 = pow((1-s)/(1+s),2);
  309. fresnel = (f0 + (1-f0) * pow(1 - dot( halfVector, worldViewDir ) , 5));
  310. }
  311. return fresnel;
  312. }
  313. ENDCG
  314. }
  315. }
  316. }