You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

279 lines
7.2 KiB

  1. [vert.glsl]
  2. #version 120
  3. uniform float u_Time;
  4. attribute vec2 in_Position;
  5. varying vec4 pass_Position;
  6. varying vec3 water[4];
  7. varying vec3 fire[4];
  8. float mod289(float x)
  9. {
  10. return x - floor(x * (1.0 / 289.0)) * 289.0;
  11. }
  12. void main(void)
  13. {
  14. mat3 r = mat3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
  15. vec3 p_pos = r * vec3(in_Position * vec2(16.0, 9.0), 0.0);
  16. vec3 p_time = r * vec3(0.0, 0.0, u_Time * 2.0);
  17. /* Noise sampling points for water */
  18. water[0] = p_pos / 2.0 + p_time;
  19. water[1] = p_pos / 4.0 + p_time;
  20. water[2] = p_pos / 8.0 + p_time;
  21. water[3] = p_pos / 16.0 + p_time;
  22. /* Noise sampling points for fire */
  23. p_pos = 16.0 * p_pos - r * vec3(0.0, mod289(u_Time) * 128.0, 0.0);
  24. fire[0] = p_pos / 2.0 + p_time * 2.0;
  25. fire[1] = p_pos / 4.0 + p_time * 1.5;
  26. fire[2] = p_pos / 8.0 + p_time;
  27. fire[3] = p_pos / 16.0 + p_time;
  28. /* Pass rotated screen coordinates */
  29. pass_Position.xy = in_Position;
  30. mat2 rot = mat2(cos(u_Time), sin(u_Time), -sin(u_Time), cos(u_Time));
  31. pass_Position.zw = rot * in_Position;
  32. gl_Position = vec4(in_Position, 0.0, 1.0);
  33. }
  34. [frag.glsl]
  35. #version 120
  36. #if defined GL_ES
  37. precision highp float;
  38. #endif
  39. uniform float u_Time;
  40. varying vec4 pass_Position;
  41. varying vec3 water[4];
  42. varying vec3 fire[4];
  43. vec4 mod289(vec4 x)
  44. {
  45. return x - floor(x * (1.0 / 289.0)) * 289.0;
  46. }
  47. vec4 perm(vec4 x)
  48. {
  49. return mod289(((x * 34.0) + 1.0) * x);
  50. }
  51. float noise3d(vec3 p)
  52. {
  53. vec3 a = floor(p);
  54. vec3 d = p - a;
  55. d = d * d * (3.0 - 2.0 * d);
  56. vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
  57. vec4 k1 = perm(b.xyxy);
  58. vec4 k2 = perm(k1.xyxy + b.zzww);
  59. vec4 c = k2 + a.zzzz;
  60. vec4 k3 = perm(c);
  61. vec4 k4 = perm(c + 1.0);
  62. vec4 o1 = fract(k3 * (1.0 / 41.0));
  63. vec4 o2 = fract(k4 * (1.0 / 41.0));
  64. vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
  65. vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
  66. return o4.y * d.y + o4.x * (1.0 - d.y);
  67. }
  68. void main(void)
  69. {
  70. /* Dither the transition between water and fire */
  71. float test = pass_Position.z * pass_Position.w + 1.5 * sin(u_Time);
  72. vec2 d = vec2(16.0, 9.0) * pass_Position.xy;
  73. test += 0.5 * (length(fract(d) - 0.5) - length(fract(d + 0.5) - 0.5));
  74. /* Compute 4 octaves of noise */
  75. #if defined GL_ES
  76. vec4 n = vec4(noise3d((test > 0.0) ? fire[0] : water[0]),
  77. noise3d((test > 0.0) ? fire[1] : water[1]),
  78. noise3d((test > 0.0) ? fire[2] : water[2]),
  79. noise3d((test > 0.0) ? fire[3] : water[3]));
  80. #else
  81. vec3 points[4] = (test > 0.0) ? fire : water;
  82. vec4 n = vec4(noise3d(points[0]),
  83. noise3d(points[1]),
  84. noise3d(points[2]),
  85. noise3d(points[3]));
  86. #endif
  87. vec4 color;
  88. if (test > 0.0)
  89. {
  90. /* Use noise results for fire */
  91. float p = dot(n, vec4(0.125, 0.125, 0.25, 0.5));
  92. /* Fade to black on top of screen */
  93. p -= pass_Position.y * 0.8 + 0.25;
  94. p = max(p, 0.0);
  95. p = min(p, 1.0);
  96. float q = p * p * (3.0 - 2.0 * p);
  97. float r = q * q * (3.0 - 2.0 * q);
  98. color = vec4(min(q * 2.0, 1.0),
  99. max(r * 1.5 - 0.5, 0.0),
  100. max(q * 8.0 - 7.3, 0.0),
  101. 1.0);
  102. }
  103. else
  104. {
  105. /* Use noise results for water */
  106. float p = dot(abs(2.0 * n - 1.0),
  107. vec4(0.5, 0.25, 0.125, 0.125));
  108. float q = sqrt(p);
  109. color = vec4(1.0 - q,
  110. 1.0 - 0.5 * q,
  111. 1.0,
  112. 1.0);
  113. }
  114. gl_FragColor = color;
  115. }
  116. [vert.hlsl]
  117. float mod289(float x)
  118. {
  119. return x - floor(x * (1.0 / 289.0)) * 289.0;
  120. }
  121. void main(float2 in_Position : POSITION,
  122. uniform float u_Time,
  123. out float4 out_Position : POSITION,
  124. out float4 pass_Position : TEXCOORD0,
  125. out float3 water[4] : TEXCOORD1,
  126. out float3 fire[4] : TEXCOORD5)
  127. {
  128. float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
  129. float3 p_pos = mul(r, float3(in_Position * float2(16.0, 9.0), 0.0));
  130. float3 p_time = mul(r, float3(0.0, 0.0, u_Time * 2.0));
  131. /* Noise sampling points for water */
  132. water[0] = p_pos / 2.0 + p_time;
  133. water[1] = p_pos / 4.0 + p_time;
  134. water[2] = p_pos / 8.0 + p_time;
  135. water[3] = p_pos / 16.0 + p_time;
  136. /* Noise sampling points for fire */
  137. p_pos = 16.0 * p_pos - mul(r, float3(0.0, mod289(u_Time) * 128.0, 0.0));
  138. fire[0] = p_pos / 2.0 + p_time * 2.0;
  139. fire[1] = p_pos / 4.0 + p_time * 1.5;
  140. fire[2] = p_pos / 8.0 + p_time;
  141. fire[3] = p_pos / 16.0 + p_time;
  142. /* Pass rotated screen coordinates */
  143. pass_Position.xy = in_Position;
  144. float2x2 rot = float2x2(cos(u_Time), sin(u_Time), -sin(u_Time), cos(u_Time));
  145. pass_Position.zw = mul(rot, in_Position);
  146. out_Position = float4(in_Position, 0.0, 1.0);
  147. }
  148. [frag.hlsl]
  149. float4 mod289(float4 x)
  150. {
  151. return x - floor(x * (1.0 / 289.0)) * 289.0;
  152. }
  153. float4 perm(float4 x)
  154. {
  155. return mod289(((x * 34.0) + 1.0) * x);
  156. }
  157. float noise3d(float3 p)
  158. {
  159. float3 a = floor(p);
  160. float3 d = p - a;
  161. d = d * d * (3.0 - 2.0 * d);
  162. float4 b = a.xxyy + float4(0.0, 1.0, 0.0, 1.0);
  163. float4 k1 = perm(b.xyxy);
  164. float4 k2 = perm(k1.xyxy + b.zzww);
  165. float4 c = k2 + a.zzzz;
  166. float4 k3 = perm(c);
  167. float4 k4 = perm(c + 1.0);
  168. float4 o1 = frac(k3 * (1.0 / 41.0));
  169. float4 o2 = frac(k4 * (1.0 / 41.0));
  170. float4 o3 = o2 * d.z + o1 * (1.0 - d.z);
  171. float2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
  172. return o4.y * d.y + o4.x * (1.0 - d.y);
  173. }
  174. void main(in float4 pass_Position : TEXCOORD0,
  175. in float3 water[4] : TEXCOORD1,
  176. in float3 fire[4] : TEXCOORD5,
  177. uniform float u_Time,
  178. out float4 out_FragColor : COLOR)
  179. {
  180. /* Dither the transition between water and fire */
  181. float test = pass_Position.z * pass_Position.w + 1.5 * sin(u_Time);
  182. float2 d = float2(16.0, 9.0) * pass_Position.xy;
  183. test += 0.5 * (length(frac(d) - 0.5) - length(frac(d + 0.5) - 0.5));
  184. float3 points[4];
  185. points[0] = (test > 0.0) ? fire[0] : water[0];
  186. points[1] = (test > 0.0) ? fire[1] : water[1];
  187. points[2] = (test > 0.0) ? fire[2] : water[2];
  188. points[3] = (test > 0.0) ? fire[3] : water[3];
  189. /* Compute 4 octaves of noise */
  190. float4 n = float4(noise3d(points[0]),
  191. noise3d(points[1]),
  192. noise3d(points[2]),
  193. noise3d(points[3]));
  194. float4 color;
  195. if (test > 0.0)
  196. {
  197. /* Use noise results for fire */
  198. float p = dot(n, float4(0.125, 0.125, 0.25, 0.5));
  199. /* Fade to black on top of screen */
  200. p -= pass_Position.y * 0.8 + 0.25;
  201. p = max(p, 0.0);
  202. p = min(p, 1.0);
  203. float q = p * p * (3.0 - 2.0 * p);
  204. float r = q * q * (3.0 - 2.0 * q);
  205. color = float4(min(q * 2.0, 1.0),
  206. max(r * 1.5 - 0.5, 0.0),
  207. max(q * 8.0 - 7.3, 0.0),
  208. 1.0);
  209. }
  210. else
  211. {
  212. /* Use noise results for water */
  213. float p = dot(abs(2.0 * n - 1.0),
  214. float4(0.5, 0.25, 0.125, 0.125));
  215. float q = sqrt(p);
  216. color = float4(1.0 - q,
  217. 1.0 - 0.5 * q,
  218. 1.0,
  219. 1.0);
  220. }
  221. out_FragColor = color;
  222. }