Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. float4x4 World;
  2. float4x4 View;
  3. float4x4 Projection;
  4. float4x4 ReflectionMatrix;
  5.  
  6. float3 CameraPosition;
  7.  
  8. float FogNear = 250;
  9. float FogFar = 300;
  10. //float4 FogColor;
  11. //float3 SunColor;
  12. float TimeOfDay;
  13.  
  14. float4 HorizonColor;
  15. float4 SunColor;
  16. float4 NightColor;
  17. float4 DeepWaterColor;
  18.  
  19. float4 MorningTint;
  20. float4 EveningTint;
  21.  
  22. float3 HeldItemLight;
  23. bool HasFlicker;
  24.  
  25. float3 SkyLightPosition;
  26.  
  27. bool IsUnderWater;
  28. float RippleTime;
  29. float Random;
  30.  
  31. /* Effect IDs */
  32. const int NONE = 0;
  33. const int LightLiquidEffect = 1; // will apply ripples and vertex distortion
  34. const int HeavyLiquidEffect = 2; // will apply slow ripples and short range visibility
  35. const int VegetationWindEffect = 3; // will apply a "sway in the breeze" effect
  36.  
  37. float ScreenWidth;
  38. float ScreenHeight;
  39.  
  40. float4 g_vSine9 = float4(-0.16161616f, 0.0083333f, -0.00019841f, 0.000002755731f);
  41. float4 g_fFixupFactor = 1.07f;
  42. float4 g_vWaveDistortX = float4(3.0f, 0.4f, 0.0f, 0.3f);
  43. float4 g_vWaveDistortY = float4(3.0f, 0.4f, 0.0f, 0.3f);
  44. float4 g_vWaveDistortZ = float4(-1.0f, -0.133f, -0.333f, -0.10f);
  45. float4 g_vWaveDirX = float4(-0.006f, -0.012f, 0.024f, 0.048f);
  46. float4 g_vWaveDirY = float4(-0.003f, -0.006f, -0.012f, -0.048f);
  47. float4 g_vWaveSpeed = float4(0.3f, 0.6f, 0.7f, 1.4f);
  48. float g_fPIx2 = 6.28318530f;
  49. float4 g_vLightingWaveScale = float4(0.35f, 0.10f, 0.10f, 0.03f);
  50. float4 g_vLightingScaleBias = float4(0.6f, 0.7f, 0.2f, 0.0f);
  51.  
  52. Texture Texture1;
  53. sampler Texture1Sampler = sampler_state
  54. {
  55. texture = <Texture1>;
  56. magfilter = POINT;
  57. minfilter = POINT;
  58. mipfilter = POINT;
  59. AddressU = WRAP;
  60. AddressV = WRAP;
  61. };
  62.  
  63. sampler ColorMap = sampler_state
  64. {
  65. Texture = <ColorTexture>;
  66. };
  67.  
  68. struct VertexShaderInput
  69. {
  70. float4 Position : POSITION0;
  71. float2 TexCoords1 : TEXCOORD0;
  72. float4 Overlay : COLOR0;
  73. float SunLight : COLOR1;
  74. float3 LocalLight : COLOR2;
  75. float Effect : BLENDWEIGHT1;
  76. };
  77.  
  78. struct VertexShaderOutput
  79. {
  80. float4 Position : POSITION0;
  81. float2 TexCoords1 : TEXCOORD0;
  82. float3 CameraView : TEXCOORD1;
  83. float Distance : TEXCOORD2;
  84. float4 Color : COLOR0;
  85. float4 Overlay : COLOR1;
  86. float Effect : BLENDWEIGHT1;
  87. };
  88.  
  89. int LFSR_Rand_Gen(int n)
  90. {
  91. // <<, ^ and & require GL_EXT_gpu_shader4.
  92. n = (n << 13) ^ n;
  93. return (n * (n*n * 15731 + 789221) + 1376312589) & 0x7fffffff;
  94. }
  95.  
  96. float LFSR_Rand_Gen_f(int n)
  97. {
  98. return float(LFSR_Rand_Gen(n));
  99. }
  100.  
  101. float noise3f(float3 p)
  102. {
  103. float3 ip = float3(floor(p));
  104. float3 u = frac(p);
  105. u = u*u*(3.0 - 2.0*u);
  106.  
  107. int n = ip.x + ip.y * 57 + ip.z * 113;
  108.  
  109. float res = lerp(lerp(lerp(LFSR_Rand_Gen_f(n + (0 + 57 * 0 + 113 * 0)),
  110. LFSR_Rand_Gen_f(n + (1 + 57 * 0 + 113 * 0)), u.x),
  111. lerp(LFSR_Rand_Gen_f(n + (0 + 57 * 1 + 113 * 0)),
  112. LFSR_Rand_Gen_f(n + (1 + 57 * 1 + 113 * 0)), u.x), u.y),
  113. lerp(lerp(LFSR_Rand_Gen_f(n + (0 + 57 * 0 + 113 * 1)),
  114. LFSR_Rand_Gen_f(n + (1 + 57 * 0 + 113 * 1)), u.x),
  115. lerp(LFSR_Rand_Gen_f(n + (0 + 57 * 1 + 113 * 1)),
  116. LFSR_Rand_Gen_f(n + (1 + 57 * 1 + 113 * 1)), u.x), u.y), u.z);
  117.  
  118. return 1.0 - res*(1.0 / 1073741824.0);
  119. }
  120.  
  121. VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
  122. {
  123. input.Position.w = 1.0f;
  124.  
  125. VertexShaderOutput output;
  126.  
  127. float4 worldPosition = mul(input.Position, World);
  128. float4 viewPosition = mul(worldPosition, View);
  129. output.Position = mul(viewPosition, Projection);
  130.  
  131. output.CameraView = normalize(CameraPosition - worldPosition);
  132. output.Distance = length(CameraPosition - worldPosition);
  133. output.TexCoords1 = input.TexCoords1;
  134.  
  135. float4 sColor = SunColor;
  136.  
  137. if (TimeOfDay <= 12)
  138. {
  139. sColor *= TimeOfDay / 12;
  140. }
  141. else
  142. {
  143. sColor *= (TimeOfDay - 24) / -12;
  144. }
  145.  
  146. switch (input.Effect)
  147. {
  148. case 1: // LightLiquidEffect
  149. output.Position.y += (0.1f * sin(RippleTime + input.Position.x + (input.Position.z * 2))) - 0.2f;
  150. output.Color.rgb = (sColor * input.SunLight) + (input.LocalLight.rgb * 1.5);
  151. output.Color.a = 1;
  152. // TODO: display reflection
  153. break;
  154. case 3: // grass wave effect
  155. float n = noise3f(input.Position.xyz);
  156. output.Position.x += (0.05f * sin((RippleTime/2)*n*2 + input.Position.x + (input.Position.z * 2)));
  157. if (HasFlicker)
  158. {
  159. output.Color.rgb = (sColor * (input.SunLight * saturate(length(SkyLightPosition - input.Position)) + (input.Overlay.rgb * input.Overlay.a))) + (input.LocalLight.rgb * 1.5) + (((HeldItemLight * 0.75 * (1 + sin(RippleTime)*(Random*0.1))) / output.Distance) * 0.25);
  160. }
  161. else
  162. {
  163. output.Color.rgb = (sColor * (input.SunLight * saturate(length(SkyLightPosition - input.Position)) + (input.Overlay.rgb * input.Overlay.a))) + (input.LocalLight.rgb * 1.5);
  164. }
  165. output.Color.a = 0.3;
  166. break;
  167. default:
  168. if (HasFlicker)
  169. {
  170. output.Color.rgb = (sColor * (input.SunLight * saturate(length(SkyLightPosition - input.Position)) + (input.Overlay.rgb * input.Overlay.a))) + (input.LocalLight.rgb * 1.5) + (((HeldItemLight * 0.75 * (1 + sin(RippleTime)*(Random*0.1))) / output.Distance) * 0.25);
  171. }
  172. else
  173. {
  174. output.Color.rgb = (sColor * (input.SunLight * saturate(length(SkyLightPosition - input.Position)) + (input.Overlay.rgb * input.Overlay.a))) + (input.LocalLight.rgb * 1.5);
  175. }
  176. output.Color.a = 1;
  177. break;
  178. }
  179. //output.Color.xyz += (input.Overlay.rgb * input.Overlay.a) * input.SunLight;
  180.  
  181. if (IsUnderWater)
  182. {
  183. output.Position.x += (0.05f * sin(RippleTime + input.Position.x + (input.Position.z * 2))) - 0.2f;
  184. //output.Position.z += (0.2f * sin(RippleTime + input.Position.x + (input.Position.z * 2))) - 0.2f;
  185. }
  186.  
  187. // gives a rounded shape to the landscape, making it seem like an actual planet.
  188. // this value needs to be dependant on world size so smaller planets have a larger bend.
  189. output.Position.y -= output.Distance * output.Distance *0.001f;
  190. output.Overlay = input.Overlay;
  191. output.Effect = input.Effect;
  192. return output;
  193. }
  194.  
  195. float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
  196. {
  197. float4 texColor1 = tex2D(Texture1Sampler, input.TexCoords1);
  198. float fog = saturate((input.Distance - FogNear) / (FogNear - FogFar));
  199. float4 topColor = SunColor;
  200. float4 FogColor = HorizonColor;
  201. float4 nColor = NightColor;
  202. float4 color;
  203. float4 outputFogColor;
  204. color.rgb = texColor1.rgb * input.Color.rgb;
  205. if (IsUnderWater)
  206. {
  207. float vis = saturate((input.Distance - 50) / -25);
  208. color = lerp(DeepWaterColor, color, vis);
  209. color.r /= 2;
  210. color.g /= 1.5;
  211.  
  212. }
  213. color.a = texColor1.a;
  214. switch (input.Effect)
  215. {
  216. case 1:
  217. //color.a = 0.3;
  218. // use texCUBE here to reflect. Idfk how tho cause I'm retarded
  219. input.Position.x += (0.1f * sin(RippleTime + input.Position.x + (input.Position.z * 2))) - 0.2f;
  220. break;
  221. default:
  222. if (color.a == 0)
  223. {
  224. clip(-1);
  225. }
  226. break;
  227. }
  228.  
  229. nColor *= (4 - input.TexCoords1.y) * .125f;
  230.  
  231. if (TimeOfDay <= 12)
  232. {
  233. topColor *= TimeOfDay / 12;
  234. FogColor *= TimeOfDay / 12;
  235. nColor *= TimeOfDay / 12;
  236. }
  237. else
  238. {
  239. FogColor *= (TimeOfDay - 24) / -12;
  240. topColor *= (TimeOfDay - 24) / -12;
  241. nColor *= (TimeOfDay - 24) / -12;
  242. }
  243.  
  244. FogColor += (MorningTint * .05) * ((24 - TimeOfDay) / 24);
  245. FogColor += (EveningTint * .05) * (TimeOfDay / 24);
  246. topColor += nColor;
  247. FogColor += nColor;
  248.  
  249. outputFogColor = lerp(FogColor, topColor, saturate((input.TexCoords1.y) / 0.9f));
  250.  
  251. return lerp(FogColor, color, fog);
  252. }
  253.  
  254. technique BlockTechnique
  255. {
  256. pass Pass1
  257. {
  258. VertexShader = compile vs_5_0 VertexShaderFunction();
  259. PixelShader = compile ps_5_0 PixelShaderFunction();
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement