grassProceduralTexture.fragment.fx 958 B

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