Guest User

The Witcher 3 - portal pixel shader (HLSL)

a guest
May 18th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // go to line 474 for code
  2.  
  3. // cbuffers
  4. cbuffer cbuffer0 : register(b0) {
  5. float4 cb0_v0 : packoffset(c0.x);
  6. float4 cb0_v1 : packoffset(c1.x);
  7. float4 cb0_v2 : packoffset(c2.x);
  8. };
  9.  
  10. cbuffer cbuffer2 : register(b2) {
  11. float4 cb2_v0 : packoffset(c0.x);
  12. float4 cb2_v1 : packoffset(c1.x);
  13. float4 cb2_v2 : packoffset(c2.x);
  14. };
  15.  
  16. cbuffer cbuffer4 : register(b4) {
  17. float4 cb4_v0 : packoffset(c0.x);
  18. float4 cb4_v1 : packoffset(c1.x);
  19. float4 cb4_v2 : packoffset(c2.x);
  20. float4 cb4_v3 : packoffset(c3.x);
  21. float4 cb4_v4 : packoffset(c4.x);
  22. float4 cb4_v5 : packoffset(c5.x);
  23. float4 cb4_v6 : packoffset(c6.x);
  24. float4 cb4_v7 : packoffset(c7.x);
  25. float4 cb4_v8 : packoffset(c8.x);
  26. float4 cb4_v9 : packoffset(c9.x);
  27. float4 cb4_v10 : packoffset(c10.x);
  28. float4 cb4_v11 : packoffset(c11.x);
  29. float4 cb4_v12 : packoffset(c12.x);
  30. float4 cb4_v13 : packoffset(c13.x);
  31. };
  32.  
  33. cbuffer cbuffer12 : register(b12) {
  34. float4 cb12_v0 : packoffset(c0.x);
  35. row_major float4x4 worldToView : packoffset(c1.x);
  36. float4 cb12_v5 : packoffset(c5.x);
  37. float4 cb12_v6 : packoffset(c6.x);
  38. float4 cb12_v7 : packoffset(c7.x);
  39. float4 cb12_v8 : packoffset(c8.x);
  40. float4 cb12_v9 : packoffset(c9.x);
  41. float4 cb12_v10 : packoffset(c10.x);
  42. float4 cb12_v11 : packoffset(c11.x);
  43. float4 cb12_v12 : packoffset(c12.x);
  44. float4 cb12_v13 : packoffset(c13.x);
  45. float4 cb12_v14 : packoffset(c14.x);
  46. float4 cb12_v15 : packoffset(c15.x);
  47. float4 cb12_v16 : packoffset(c16.x);
  48. float4 cb12_v17 : packoffset(c17.x);
  49. float4 cb12_v18 : packoffset(c18.x);
  50. float4 cb12_v19 : packoffset(c19.x);
  51. float4 cb12_v20 : packoffset(c20.x);
  52. float4 cb12_v21 : packoffset(c21.x);
  53. float4 cb12_v22 : packoffset(c22.x);
  54. float4 cb12_v23 : packoffset(c23.x);
  55. float4 cb12_v24 : packoffset(c24.x);
  56. float4 cb12_v25 : packoffset(c25.x);
  57. float4 cb12_v26 : packoffset(c26.x);
  58. float4 cb12_v27 : packoffset(c27.x);
  59. float4 cb12_v28 : packoffset(c28.x);
  60. float4 cb12_v29 : packoffset(c29.x);
  61. float4 cb12_v30 : packoffset(c30.x);
  62. float4 cb12_v31 : packoffset(c31.x);
  63. float4 cb12_v32 : packoffset(c32.x);
  64. float4 cb12_v33 : packoffset(c33.x);
  65. float4 cb12_v34 : packoffset(c34.x);
  66. float4 cb12_v35 : packoffset(c35.x);
  67. float4 cb12_v36 : packoffset(c36.x);
  68. float4 cb12_v37 : packoffset(c37.x);
  69. float4 cb12_v38 : packoffset(c38.x);
  70. float4 cb12_v39 : packoffset(c39.x);
  71. float4 cb12_v40 : packoffset(c40.x);
  72. float4 cb12_v41 : packoffset(c41.x);
  73. float4 cb12_v42 : packoffset(c42.x);
  74. float4 cb12_v43 : packoffset(c43.x);
  75. float4 cb12_v44 : packoffset(c44.x);
  76. float4 cb12_v45 : packoffset(c45.x);
  77. float4 cb12_v46 : packoffset(c46.x);
  78. float4 cb12_v47 : packoffset(c47.x);
  79. float4 cb12_v48 : packoffset(c48.x);
  80. float4 cb12_v49 : packoffset(c49.x);
  81. float4 cb12_v50 : packoffset(c50.x);
  82. float4 cb12_v51 : packoffset(c51.x);
  83. float4 cb12_v52 : packoffset(c52.x);
  84. float4 cb12_v53 : packoffset(c53.x);
  85. float4 cb12_v54 : packoffset(c54.x);
  86. float4 cb12_v55 : packoffset(c55.x);
  87. float4 cb12_v56 : packoffset(c56.x);
  88. float4 cb12_v57 : packoffset(c57.x);
  89. float4 cb12_v58 : packoffset(c58.x);
  90. float4 cb12_v59 : packoffset(c59.x);
  91. float4 cb12_v60 : packoffset(c60.x);
  92. float4 cb12_v61 : packoffset(c61.x);
  93. float4 cb12_v62 : packoffset(c62.x);
  94. float4 cb12_v63 : packoffset(c63.x);
  95. float4 cb12_v64 : packoffset(c64.x);
  96. float4 cb12_v65 : packoffset(c65.x);
  97. float4 cb12_v66 : packoffset(c66.x);
  98. float4 cb12_v67 : packoffset(c67.x);
  99. float4 cb12_v68 : packoffset(c68.x);
  100. float4 cb12_v69 : packoffset(c69.x);
  101. float4 cb12_v70 : packoffset(c70.x);
  102. float4 cb12_v71 : packoffset(c71.x);
  103. float4 cb12_v72 : packoffset(c72.x);
  104. float4 cb12_v73 : packoffset(c73.x);
  105. float4 cb12_v74 : packoffset(c74.x);
  106. float4 cb12_v75 : packoffset(c75.x);
  107. float4 cb12_v76 : packoffset(c76.x);
  108. float4 cb12_v77 : packoffset(c77.x);
  109. float4 cb12_v78 : packoffset(c78.x);
  110. float4 cb12_v79 : packoffset(c79.x);
  111. float4 cb12_v80 : packoffset(c80.x);
  112. float4 cb12_v81 : packoffset(c81.x);
  113. float4 cb12_v82 : packoffset(c82.x);
  114. float4 cb12_v83 : packoffset(c83.x);
  115. float4 cb12_v84 : packoffset(c84.x);
  116. float4 cb12_v85 : packoffset(c85.x);
  117. float4 cb12_v86 : packoffset(c86.x);
  118. float4 cb12_v87 : packoffset(c87.x);
  119. float4 cb12_v88 : packoffset(c88.x);
  120. float4 cb12_v89 : packoffset(c89.x);
  121. float4 cb12_v90 : packoffset(c90.x);
  122. float4 cb12_v91 : packoffset(c91.x);
  123. float4 cb12_v92 : packoffset(c92.x);
  124. float4 cb12_v93 : packoffset(c93.x);
  125. float4 cb12_v94 : packoffset(c94.x);
  126. float4 cb12_v95 : packoffset(c95.x);
  127. float4 cb12_v96 : packoffset(c96.x);
  128. float4 cb12_v97 : packoffset(c97.x);
  129. float4 cb12_v98 : packoffset(c98.x);
  130. float4 cb12_v99 : packoffset(c99.x);
  131. float4 cb12_v100 : packoffset(c100.x);
  132. float4 cb12_v101 : packoffset(c101.x);
  133. float4 cb12_v102 : packoffset(c102.x);
  134. float4 cb12_v103 : packoffset(c103.x);
  135. float4 cb12_v104 : packoffset(c104.x);
  136. float4 cb12_v105 : packoffset(c105.x);
  137. float4 cb12_v106 : packoffset(c106.x);
  138. float4 cb12_v107 : packoffset(c107.x);
  139. float4 cb12_v108 : packoffset(c108.x);
  140. float4 cb12_v109 : packoffset(c109.x);
  141. float4 cb12_v110 : packoffset(c110.x);
  142. float4 cb12_v111 : packoffset(c111.x);
  143. float4 cb12_v112 : packoffset(c112.x);
  144. float4 cb12_v113 : packoffset(c113.x);
  145. float4 cb12_v114 : packoffset(c114.x);
  146. float4 cb12_v115 : packoffset(c115.x);
  147. float4 cb12_v116 : packoffset(c116.x);
  148. float4 cb12_v117 : packoffset(c117.x);
  149. float4 cb12_v118 : packoffset(c118.x);
  150. float4 cb12_v119 : packoffset(c119.x);
  151. float4 cb12_v120 : packoffset(c120.x);
  152. float4 cb12_v121 : packoffset(c121.x);
  153. float4 cb12_v122 : packoffset(c122.x);
  154. float4 cb12_v123 : packoffset(c123.x);
  155. float4 cb12_v124 : packoffset(c124.x);
  156. float4 cb12_v125 : packoffset(c125.x);
  157. float4 cb12_v126 : packoffset(c126.x);
  158. float4 cb12_v127 : packoffset(c127.x);
  159. float4 cb12_v128 : packoffset(c128.x);
  160. float4 cb12_v129 : packoffset(c129.x);
  161. float4 cb12_v130 : packoffset(c130.x);
  162. float4 cb12_v131 : packoffset(c131.x);
  163. float4 cb12_v132 : packoffset(c132.x);
  164. float4 cb12_v133 : packoffset(c133.x);
  165. float4 cb12_v134 : packoffset(c134.x);
  166. float4 cb12_v135 : packoffset(c135.x);
  167. float4 cb12_v136 : packoffset(c136.x);
  168. float4 cb12_v137 : packoffset(c137.x);
  169. float4 cb12_v138 : packoffset(c138.x);
  170. float4 cb12_v139 : packoffset(c139.x);
  171. float4 cb12_v140 : packoffset(c140.x);
  172. float4 cb12_v141 : packoffset(c141.x);
  173. float4 cb12_v142 : packoffset(c142.x);
  174. float4 cb12_v143 : packoffset(c143.x);
  175. float4 cb12_v144 : packoffset(c144.x);
  176. float4 cb12_v145 : packoffset(c145.x);
  177. float4 cb12_v146 : packoffset(c146.x);
  178. float4 cb12_v147 : packoffset(c147.x);
  179. float4 cb12_v148 : packoffset(c148.x);
  180. float4 cb12_v149 : packoffset(c149.x);
  181. float4 cb12_v150 : packoffset(c150.x);
  182. float4 cb12_v151 : packoffset(c151.x);
  183. float4 cb12_v152 : packoffset(c152.x);
  184. float4 cb12_v153 : packoffset(c153.x);
  185. float4 cb12_v154 : packoffset(c154.x);
  186. float4 cb12_v155 : packoffset(c155.x);
  187. float4 cb12_v156 : packoffset(c156.x);
  188. float4 cb12_v157 : packoffset(c157.x);
  189. float4 cb12_v158 : packoffset(c158.x);
  190. float4 cb12_v159 : packoffset(c159.x);
  191. float4 cb12_v160 : packoffset(c160.x);
  192. float4 cb12_v161 : packoffset(c161.x);
  193. float4 cb12_v162 : packoffset(c162.x);
  194. float4 cb12_v163 : packoffset(c163.x);
  195. float4 cb12_v164 : packoffset(c164.x);
  196. float4 cb12_v165 : packoffset(c165.x);
  197. float4 cb12_v166 : packoffset(c166.x);
  198. float4 cb12_v167 : packoffset(c167.x);
  199. float4 cb12_v168 : packoffset(c168.x);
  200. float4 cb12_v169 : packoffset(c169.x);
  201. float4 cb12_v170 : packoffset(c170.x);
  202. float4 cb12_v171 : packoffset(c171.x);
  203. float4 cb12_v172 : packoffset(c172.x);
  204. float4 cb12_v173 : packoffset(c173.x);
  205. float4 cb12_v174 : packoffset(c174.x);
  206. float4 cb12_v175 : packoffset(c175.x);
  207. float4 cb12_v176 : packoffset(c176.x);
  208. float4 cb12_v177 : packoffset(c177.x);
  209. float4 cb12_v178 : packoffset(c178.x);
  210. float4 cb12_v179 : packoffset(c179.x);
  211. float4 cb12_v180 : packoffset(c180.x);
  212. float4 cb12_v181 : packoffset(c181.x);
  213. float4 cb12_v182 : packoffset(c182.x);
  214. float4 cb12_v183 : packoffset(c183.x);
  215. float4 cb12_v184 : packoffset(c184.x);
  216. float4 cb12_v185 : packoffset(c185.x);
  217. float4 cb12_v186 : packoffset(c186.x);
  218. float4 cb12_v187 : packoffset(c187.x);
  219. float4 cb12_v188 : packoffset(c188.x);
  220. float4 cb12_v189 : packoffset(c189.x);
  221. float4 cb12_v190 : packoffset(c190.x);
  222. float4 cb12_v191 : packoffset(c191.x);
  223. float4 cb12_v192 : packoffset(c192.x);
  224. float4 cb12_v193 : packoffset(c193.x);
  225. float4 cb12_v194 : packoffset(c194.x);
  226. float4 cb12_v195 : packoffset(c195.x);
  227. float4 cb12_v196 : packoffset(c196.x);
  228. float4 cb12_v197 : packoffset(c197.x);
  229. float4 cb12_v198 : packoffset(c198.x);
  230. float4 cb12_v199 : packoffset(c199.x);
  231. float4 cb12_v200 : packoffset(c200.x);
  232. float4 cb12_v201 : packoffset(c201.x);
  233. float4 cb12_v202 : packoffset(c202.x);
  234. float4 cb12_v203 : packoffset(c203.x);
  235. float4 cb12_v204 : packoffset(c204.x);
  236. float4 cb12_v205 : packoffset(c205.x);
  237. float4 cb12_v206 : packoffset(c206.x);
  238. float4 cb12_v207 : packoffset(c207.x);
  239. float4 cb12_v208 : packoffset(c208.x);
  240. float4 cb12_v209 : packoffset(c209.x);
  241. float4 cb12_v210 : packoffset(c210.x);
  242. float4 cb12_v211 : packoffset(c211.x);
  243. float4 cb12_v212 : packoffset(c212.x);
  244. float4 cb12_v213 : packoffset(c213.x);
  245. float4 cb12_v214 : packoffset(c214.x);
  246. float4 cb12_v215 : packoffset(c215.x);
  247. float4 cb12_v216 : packoffset(c216.x);
  248. float4 cb12_v217 : packoffset(c217.x);
  249. float4 cb12_v218 : packoffset(c218.x);
  250. float4 cb12_v219 : packoffset(c219.x);
  251. float4 cb12_v220 : packoffset(c220.x);
  252. float4 cb12_v221 : packoffset(c221.x);
  253. float4 cb12_v222 : packoffset(c222.x);
  254. float4 cb12_v223 : packoffset(c223.x);
  255. float4 cb12_v224 : packoffset(c224.x);
  256. float4 cb12_v225 : packoffset(c225.x);
  257. float4 cb12_v226 : packoffset(c226.x);
  258. float4 cb12_v227 : packoffset(c227.x);
  259. };
  260.  
  261.  
  262. #define OVERRIDE_FOG
  263.  
  264.  
  265. struct FogResult
  266. {
  267. float4 paramsFog;
  268. float4 paramsAerial;
  269. };
  270.  
  271. SamplerState sampler0 : register(s0); // can't disambiguate
  272. SamplerState sampler6 : register(s6); // can't disambiguate
  273. SamplerState sampler15 : register(s15); // can't disambiguate
  274.  
  275. Texture2D texFire : register(t0);
  276. Texture2D texNoise : register(t1);
  277. Texture2D texScene : register(t6);
  278. Texture2D texDepth : register(t15);
  279.  
  280.  
  281. float4 SelectDensityParams(const bool isSky)
  282. {
  283. float4 FogDensityParamsScene = cb12_v43;
  284. float4 FogDensityParamsSky = cb12_v44;
  285.  
  286. if (isSky)
  287. return FogDensityParamsSky;
  288. else
  289. return FogDensityParamsScene;
  290. }
  291.  
  292. FogResult CalculateFog(float3 fragPosWorldSpace, float3 customCameraPos, float fogStartDist, const bool isSky)
  293. {
  294. // *** Inputs *** //
  295. float3 FogSunDir = cb12_v38.xyz;
  296. float3 FogColorFront = cb12_v39.xyz;
  297. float3 FogColorMiddle = cb12_v40.xyz;
  298. float3 FogColorBack = cb12_v41.xyz;
  299.  
  300. float4 FogBaseParams = cb12_v42;
  301. float4 FogDensityParamsScene = cb12_v43;
  302. float4 FogDensityParamsSky = cb12_v44;
  303.  
  304. float3 AerialColorFront = cb12_v45.xyz;
  305. float3 AerialColorMiddle = cb12_v46.xyz;
  306. float3 AerialColorBack = cb12_v47.xyz;
  307. float4 AerialParams = cb12_v48;
  308.  
  309. float3 GlobalLightDirection = FogSunDir;
  310.  
  311. float4 density_params = SelectDensityParams(isSky);
  312.  
  313.  
  314. // *** Shader data *** //
  315. float density = density_params.x;
  316. float dist_clamp = FogBaseParams.z;
  317. float final_exp_fog = FogBaseParams.w;
  318. float final_exp_aerial = AerialParams.x;
  319.  
  320. float3 frag_vec = fragPosWorldSpace.xyz - customCameraPos.xyz;
  321. float frag_dist = length(frag_vec);
  322.  
  323. float3 frag_dir = frag_vec / frag_dist;
  324.  
  325. customCameraPos += frag_dir * fogStartDist;
  326. frag_dist = max(0, frag_dist - fogStartDist);
  327.  
  328. frag_dist = (isSky) ? dist_clamp : min(dist_clamp, frag_dist);
  329. frag_vec = frag_dir * frag_dist;
  330.  
  331. const float inv_num_samples = 1.0 / 16;
  332. float3 frag_step = frag_vec * inv_num_samples;
  333. float density_sample_scale = frag_dist * density * inv_num_samples;
  334. float dot_fragDirSunDir = dot(GlobalLightDirection.xyz, frag_dir);
  335.  
  336. float density_factor = 1.0;
  337. {
  338. float density_shift = FogBaseParams.x;
  339.  
  340. float fc_t = dot_fragDirSunDir;
  341. fc_t = (fc_t + density_shift) / (1.0 + density_shift);
  342. fc_t = saturate(fc_t);
  343. density_factor = lerp(density_params.z, density_params.y, fc_t);
  344. }
  345.  
  346. float3 curr_col_fog;
  347. float3 curr_col_aerial;
  348. {
  349. float _dot = dot_fragDirSunDir;
  350.  
  351. float _dd = _dot;
  352. {
  353. const float _distOffset = -150;
  354. const float _distRange = 500;
  355. const float _mul = 1.0 / _distRange;
  356. const float _bias = _distOffset * _mul;
  357.  
  358. _dd = abs(_dd);
  359. _dd *= _dd;
  360. _dd *= saturate( frag_dist * _mul + _bias );
  361. }
  362.  
  363. curr_col_fog = lerp( FogColorMiddle.xyz, (_dot>0.0f ? FogColorFront.xyz : FogColorBack.xyz), _dd );
  364. curr_col_aerial = lerp( AerialColorMiddle.xyz, (_dot>0.0f ? AerialColorFront.xyz : AerialColorBack.xyz), _dd );
  365. }
  366.  
  367. float fog_amount = 1;
  368. float fog_amount_scale = 0;
  369. [branch]
  370. if ( frag_dist >= AerialParams.y )
  371. {
  372. float curr_pos_z_base = (customCameraPos.z + FogBaseParams.y) * density_factor;
  373. float curr_pos_z_step = frag_step.z * density_factor;
  374.  
  375. [unroll] for ( int i=16; i>0; --i )
  376. {
  377. fog_amount *= 1 - saturate( density_sample_scale / (1 + max( 0.0, curr_pos_z_base + (i) * curr_pos_z_step ) ) );
  378. }
  379.  
  380. fog_amount = 1 - fog_amount;
  381. fog_amount_scale = saturate( (frag_dist - AerialParams.y) * AerialParams.z );
  382. }
  383. //ret.paramsFog.xyz = fog_amount;
  384. //return ret;
  385.  
  386. FogResult ret;
  387.  
  388. ret.paramsFog = float4 ( curr_col_fog, fog_amount_scale * pow( abs(fog_amount), final_exp_fog ) );
  389. ret.paramsAerial = float4 ( curr_col_aerial, fog_amount_scale * pow( abs(fog_amount), final_exp_aerial ) );
  390.  
  391.  
  392. #ifdef OVERRIDE_FOG
  393.  
  394. // Override
  395. float fog_influence = ret.paramsFog.w; // r0.w
  396.  
  397. float override1ColorScale = cb12_v189.x;
  398. float override1ColorBias = cb12_v189.y;
  399. float3 override1Color = cb12_v188.rgb;
  400.  
  401. float override1InfluenceScale = cb12_v189.z;
  402. float override1InfluenceBias = cb12_v189.w;
  403. float override1Influence = cb12_v188.w;
  404.  
  405.  
  406. float override1ColorAmount = saturate(fog_influence * override1ColorScale + override1ColorBias);
  407. float override1InfluenceAmount = saturate(fog_influence * override1InfluenceScale + override1InfluenceBias);
  408.  
  409. float4 paramsFogOverride;
  410. paramsFogOverride.rgb = lerp(curr_col_fog, override1Color, override1ColorAmount ); // ***r5.xyz
  411.  
  412.  
  413. float param1 = lerp(1.0, override1Influence, override1InfluenceAmount); // r0.x
  414. paramsFogOverride.w = saturate(fog_influence * param1); // ** r5.w
  415.  
  416.  
  417. const float extraFogOverride = cb12_v192.x;
  418.  
  419. [branch]
  420. if (extraFogOverride > 0.0)
  421. {
  422. float override2ColorScale = cb12_v191.x;
  423. float override2ColorBias = cb12_v191.y;
  424. float3 override2Color = cb12_v190.rgb;
  425.  
  426. float override2InfluenceScale = cb12_v191.z;
  427. float override2InfluenceBias = cb12_v191.w;
  428. float override2Influence = cb12_v190.w;
  429.  
  430. float override2ColorAmount = saturate(fog_influence * override2ColorScale + override2ColorBias);
  431. float override2InfluenceAmount = saturate(fog_influence * override2InfluenceScale + override2InfluenceBias);
  432.  
  433. float4 paramsFogOverride2;
  434. paramsFogOverride2.rgb = lerp(curr_col_fog, override2Color, override2ColorAmount); // r3.xyz
  435.  
  436. float ov_param1 = lerp(1.0, override2Influence, override2InfluenceAmount); // r0.z
  437. paramsFogOverride2.w = saturate(fog_influence * ov_param1); // r3.w
  438.  
  439. paramsFogOverride = lerp(paramsFogOverride, paramsFogOverride2, extraFogOverride);
  440.  
  441. }
  442. ret.paramsFog = paramsFogOverride;
  443.  
  444. #endif
  445.  
  446. return ret;
  447. }
  448.  
  449. float getDepth(float rawDepth)
  450. {
  451. // Calculate inverted depth
  452. float depthScale = cb12_v22.x;
  453. float depthBias = cb12_v22.y;
  454. float depth = rawDepth * depthScale + depthBias;
  455.  
  456.  
  457. float cameraNear = cb12_v21.x;
  458. float cameraFar = cb12_v21.y;
  459.  
  460. depth = depth * cameraNear + cameraFar;
  461.  
  462. return 1.0 / max(depth, 1e-4);
  463. }
  464.  
  465. struct VS_OUTPUT
  466. {
  467. float3 TexcoordAndViewSpaceDepth : TEXCOORD0;
  468. float3 Color : TEXCOORD1;
  469. float3 WorldSpacePosition : TEXCOORD2;
  470. float3 Tangent : TEXCOORD3;
  471. float4 PositionH : SV_Position;
  472. };
  473.  
  474. float4 EditedShaderPS( in VS_OUTPUT Input ) : SV_Target0
  475. {
  476. // * Vertex inputs
  477. const float2 texcoords = Input.TexcoordAndViewSpaceDepth.xy;
  478. const float ViewSpaceDepth = Input.TexcoordAndViewSpaceDepth.z;
  479. const float3 WSPosition = Input.WorldSpacePosition;
  480. float3 Tangent = Input.Tangent;
  481.  
  482. // Masks:
  483. // The portal mesh comes with three masks from COLOR channel
  484. // to help determine where the indidual subeffects should be applied.
  485.  
  486. // Fades smoothly from the outer edges to the back of a portal
  487. const float heatHazeMask = Input.Color.x;
  488.  
  489. // Determines the "inner" part of a portal
  490. const float innerMask = Input.Color.y;
  491.  
  492. // Determines the back part of a portal
  493. const float backMask = Input.Color.z;
  494.  
  495. // * General inputs
  496. const float elapsedTime = cb0_v0.x;
  497. const float2 invViewportSize = cb0_v1.zw;
  498.  
  499. const float2 pixelUv = Input.PositionH.xy * invViewportSize;
  500.  
  501. // * Portal-specific inputs
  502. const float viewSpaceDepthScale = cb4_v0.x; // 0.50
  503. const float backgroundDistortionStrength = cb4_v1.x; // 0.40
  504. const float uvScaleGlobal1 = cb4_v2.x; // 1.00
  505. const float uvScale1 = cb4_v3.x; // 0.15
  506. const float uvSquash = cb4_v4.x; // 2.50
  507. const float3 colorPortalInner = cb4_v5.rgb; // (1.00, 0.60, 0.21961)
  508. const float uvScale2 = cb4_v6.x; // 0.06
  509. const float uvScaleGlobal2 = cb4_v7.x; // 1.00
  510. const float portalInnerColorSqueeze = cb4_v8.x; // 3.00
  511. const float portalInnerColorBoost = cb4_v9.x; // 188.00
  512. const float portalOuterGlowAttenuation = cb4_v10.x; // 0.30
  513. const float portalOuterColorBoost = cb4_v11.x; // 1.50
  514. const float3 colorPortalOuterGlow = cb4_v12.rgb; // (1.00, 0.61961, 0.30196)
  515. const float cb4_13 = cb4_v13.x; // 0.00 - no effect
  516.  
  517. const float3 portalFinalColorFilter = cb12_v227.rgb; // (1.0, 1.0, 1.0)
  518. const float2 backgroundSceneMaxUv = cb0_v2.zw; // (1.0, 1.0)
  519.  
  520. const float finalPortalAmount = cb2_v0.x; // 0.99443
  521. const float3 finalColorFilter = cb2_v2.rgb; // (1.0, 1.0, 1.0)
  522. const float finalOpacityFilter = cb2_v2.a; // 1.0
  523.  
  524.  
  525. ///////////////////////////////////////////////
  526. // Stage 1: Extract view space depth
  527. ///////////////////////////////////////////////
  528.  
  529. // Load depth from texture
  530. float hardwareDepth = texDepth.SampleLevel(sampler15, pixelUv, 0).x;
  531. float linearDepth = getDepth(hardwareDepth);
  532.  
  533. // cb4_v0.x = 0.5
  534. float vsDepthScale = saturate( (linearDepth - ViewSpaceDepth) * viewSpaceDepthScale );
  535.  
  536. float vsDepth1 = 2*vsDepthScale; // line 8, r0.w
  537. float vsDepth2 = max( (vsDepthScale * 2.0 - 1.0), 0.0 ); // line 10, r0.z
  538.  
  539.  
  540. ///////////////////////////////////////////////
  541. // Stage 2: Determine offset for heat haze effect
  542. ///////////////////////////////////////////////
  543. float2 heatHazeOffset = mul( normalize(Tangent), worldToView );
  544. heatHazeOffset *= float2(-1, 1);
  545.  
  546. // The further camera is from the portal the weaker heat haze effect is
  547. const float heatHaze_distanceFade = backgroundDistortionStrength / ViewSpaceDepth;
  548. heatHazeOffset *= heatHaze_distanceFade;
  549.  
  550. heatHazeOffset *= heatHazeMask;
  551.  
  552.  
  553. const float y_cutoff = 0.2;
  554. const float y_offset = pow(texcoords.y - y_cutoff, uvSquash);
  555.  
  556.  
  557. // Sample fire1 - the first layer of fire
  558. float fire1; // r1.w
  559. {
  560. const float scaleGlobal = floor(uvScaleGlobal1); // 1.0
  561.  
  562. float2 fire1Uv;
  563. fire1Uv.x = texcoords.x;
  564. fire1Uv.y = uvScale1 * elapsedTime + y_offset;
  565.  
  566. fire1Uv *= scaleGlobal;
  567.  
  568. fire1 = texFire.Sample(sampler0, fire1Uv).x;
  569. }
  570.  
  571. // this is the thing which animates the heat haze
  572. heatHazeOffset *= pow(fire1, 0.2);
  573.  
  574. heatHazeOffset *= vsDepth2;
  575.  
  576. ///////////////////////////////////////////////
  577. // Stage 3: Fog (destination color) (35-135)
  578. ///////////////////////////////////////////////
  579. FogResult fog;
  580. {
  581. const float3 CameraPosition = cb12_v0.xyz;
  582. const float fogStart = cb12_v22.z; // near plane
  583.  
  584. fog = CalculateFog( WSPosition, CameraPosition, fogStart, false );
  585. }
  586.  
  587.  
  588. ///////////////////////////////////////////////
  589. // Stage 4: further calcs
  590. ///////////////////////////////////////////////
  591. vsDepth1 = min(vsDepth1, 1.0); // r0.z, line 136
  592.  
  593. // Calculate 'portal destination' mask - maybe we would like see a glimpse of where a portal leads
  594. // like landscape from another planet - the shader allows for it.
  595. float portal_destination_mask;
  596. {
  597. const float region_mask = dot(backMask.xx, vsDepth1.xx);
  598.  
  599. const float2 _UVScale = float2(4.0, 1.0);
  600. const float2 _TimeScale = float2(0.0, 0.2);
  601. const float2 _UV = texcoords * _UVScale + elapsedTime * _TimeScale;
  602.  
  603. portal_destination_mask = texNoise.Sample(sampler0, _UV).x;
  604. portal_destination_mask = saturate(portal_destination_mask + region_mask - 1.0);
  605. portal_destination_mask *= portal_destination_mask; // line 143, r0.w
  606. }
  607.  
  608. // Sample fire2
  609. float fire2;
  610. {
  611. float2 fire2Uv;
  612. fire2Uv.x = texcoords.x - elapsedTime*uvScale2;
  613. fire2Uv.y = elapsedTime*uvScale2 + y_offset;
  614.  
  615. const float fire2_scale = floor(uvScaleGlobal2);
  616. fire2Uv *= fire2_scale;
  617.  
  618. fire2 = texFire.Sample(sampler0, fire2Uv).x;
  619. }
  620.  
  621.  
  622. // Calculate inner fire influence
  623. float inner_influence;
  624. {
  625. // innerMask and "-1.0" are used here to control where the inner part of a portal is.
  626. inner_influence = fire1 * fire2 + innerMask;
  627. inner_influence = saturate(inner_influence - 1.0);
  628.  
  629. // Exponentation to hide less luminous elements of inner portal
  630. inner_influence = pow(inner_influence, portalInnerColorSqueeze);
  631.  
  632. // Boost the color
  633. inner_influence *= portalInnerColorBoost;
  634. }
  635.  
  636. // I put it here to keep the output assembly the same as in the game
  637. // Otherwise, it makes sense to move it later when "portal_inner_color" is calculated.
  638. const float3 portalInner = pow(colorPortalInner, 2.2);
  639.  
  640.  
  641. // Calculate outer portal glow
  642. float outer_glow_influence;
  643. {
  644. float outer_mask = (1.0 - backMask) * innerMask;
  645.  
  646. const float perturbParam = fire1*fire1;
  647. float outer_mask_perturb = lerp( 1.0 - portalOuterGlowAttenuation, 1.0, perturbParam );
  648.  
  649. outer_mask *= outer_mask_perturb;
  650. outer_glow_influence = outer_mask * portalOuterColorBoost;
  651. }
  652.  
  653. // Calculate portal color
  654. float3 portal_final;
  655. {
  656. const float3 portal_inner_color = portalInner * inner_influence;
  657. const float3 portal_outer_color = pow(colorPortalOuterGlow, 2.2) * outer_glow_influence;
  658.  
  659. portal_final = portal_inner_color + portal_outer_color;
  660. portal_final *= vsDepth1; // fade the effect to avoid harsh artifacts due to depth test
  661. portal_final *= portalFinalColorFilter; // this was (1,1,1) - so not relevant
  662. }
  663.  
  664. // Alpha component for blending
  665. float opacity = saturate( lerp(cb2_v0.x, 1, cb4_v13.x) );
  666.  
  667. // Obtain background scene color - we need to obtain it from texture
  668. // for heat haze effect
  669. float3 sceneColor;
  670. {
  671. const float2 sceneUv_0 = pixelUv + backgroundSceneMaxUv * heatHazeOffset;
  672. const float2 sceneUv_1 = backgroundSceneMaxUv - 0.5*invViewportSize;
  673.  
  674. const float2 sceneUv = min(sceneUv_0, sceneUv_1);
  675.  
  676. sceneColor = texScene.SampleLevel(sampler6, sceneUv, 0).rgb;
  677. }
  678.  
  679.  
  680. // Calculate the final color
  681. float3 finalColor;
  682. {
  683. // Mix the scene color (with heat haze effect) with the 'destination color'.
  684. // In this particular example fog is used as destination (which is black where camera is nearby)
  685. // but in theory there is nothing which stops us from putting here a landscape from another world.
  686. const float3 destination_color = fog.paramsFog.a * fog.paramsFog.rgb;
  687. finalColor = lerp( sceneColor, destination_color, portal_destination_mask );
  688.  
  689. // Add the portal color
  690. finalColor += portal_final * finalPortalAmount;
  691.  
  692. // Final filter
  693. finalColor *= finalColorFilter;
  694. }
  695.  
  696. opacity *= finalOpacityFilter;
  697.  
  698. return float4(finalColor * opacity, opacity);
  699. }
RAW Paste Data