grass.fragment.fx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. varying vec2 vPosition;
  5. varying vec2 vUV;
  6. float rand(vec2 n) {
  7. return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
  8. }
  9. float noise(vec2 n) {
  10. const vec2 d = vec2(0.0, 1.0);
  11. vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
  12. return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
  13. }
  14. float fbm(vec2 n) {
  15. float total = 0.0, amplitude = 1.0;
  16. for (int i = 0; i < 4; i++) {
  17. total += noise(n) * amplitude;
  18. n += n;
  19. amplitude *= 0.5;
  20. }
  21. return total;
  22. }
  23. void main(void) {
  24. vec3 herb1 = vec3(0.29, 0.38, 0.02);
  25. vec3 herb2 = vec3(0.36, 0.49, 0.09);
  26. vec3 herb3 = vec3(0.51, 0.6, 0.28);
  27. vec3 dirt = vec3(0.6, 0.46, 0.13);
  28. vec3 ground = vec3(1,1,1);
  29. ground = mix(ground, herb1, rand(gl_FragCoord.xy * 4.0));
  30. ground = mix(ground, herb2, rand(gl_FragCoord.xy * 8.0));
  31. ground = mix(ground, herb3, rand(gl_FragCoord.xy));
  32. ground = mix(ground, herb1, fbm(gl_FragCoord.xy * 16.0));
  33. gl_FragColor = vec4(ground, 1.0);
  34. }