grass.fragment.fx 978 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. varying vec2 vPosition;
  5. varying vec2 vUV;
  6. uniform vec3 herb1Color;
  7. uniform vec3 herb2Color;
  8. uniform vec3 herb3Color;
  9. uniform vec3 groundColor;
  10. float rand(vec2 n) {
  11. return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
  12. }
  13. float noise(vec2 n) {
  14. const vec2 d = vec2(0.0, 1.0);
  15. vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
  16. return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
  17. }
  18. float fbm(vec2 n) {
  19. float total = 0.0, amplitude = 1.0;
  20. for (int i = 0; i < 4; i++) {
  21. total += noise(n) * amplitude;
  22. n += n;
  23. amplitude *= 0.5;
  24. }
  25. return total;
  26. }
  27. void main(void) {
  28. vec3 color = mix(groundColor, herb1Color, rand(gl_FragCoord.xy * 4.0));
  29. color = mix(color, herb2Color, rand(gl_FragCoord.xy * 8.0));
  30. color = mix(color, herb3Color, rand(gl_FragCoord.xy));
  31. color = mix(color, herb1Color, fbm(gl_FragCoord.xy * 16.0));
  32. gl_FragColor = vec4(color, 1.0);
  33. }