custom.fragment.fx 821 B

1234567891011121314151617181920212223242526272829303132
  1. varying vec2 vPosition;
  2. varying vec2 vUV;
  3. uniform sampler2D dirtSampler;
  4. uniform sampler2D grassSampler;
  5. uniform float dirtAmplifier;
  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);
  12. vec2 f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
  13. return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
  14. }
  15. float fbm(vec2 n) {
  16. float total = 0.0, amplitude = 0.5;
  17. for (int i = 0; i < 4; i++) {
  18. total += noise(n) * amplitude;
  19. n += n;
  20. amplitude *= 0.5;
  21. }
  22. return total;
  23. }
  24. void main(void) {
  25. vec3 color = mix(texture2D(dirtSampler, vUV).xyz, texture2D(grassSampler, vUV).xyz, fbm(vUV * dirtAmplifier));
  26. gl_FragColor = vec4(color, 1.0);
  27. }