marbleProceduralTexture.fragment.fx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. precision highp float;
  2. varying vec2 vPosition;
  3. varying vec2 vUV;
  4. uniform float numberOfTilesHeight;
  5. uniform float numberOfTilesWidth;
  6. uniform float amplitude;
  7. uniform vec3 marbleColor;
  8. uniform vec3 jointColor;
  9. const vec3 tileSize = vec3(1.1, 1.0, 1.1);
  10. const vec3 tilePct = vec3(0.98, 1.0, 0.98);
  11. float rand(vec2 n) {
  12. return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
  13. }
  14. float noise(vec2 n) {
  15. const vec2 d = vec2(0.0, 1.0);
  16. vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
  17. return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
  18. }
  19. float turbulence(vec2 P)
  20. {
  21. float val = 0.0;
  22. float freq = 1.0;
  23. for (int i = 0; i < 4; i++)
  24. {
  25. val += abs(noise(P*freq) / freq);
  26. freq *= 2.07;
  27. }
  28. return val;
  29. }
  30. float round(float number){
  31. return sign(number)*floor(abs(number) + 0.5);
  32. }
  33. vec3 marble_color(float x)
  34. {
  35. vec3 col;
  36. x = 0.5*(x + 1.);
  37. x = sqrt(x);
  38. x = sqrt(x);
  39. x = sqrt(x);
  40. col = vec3(.2 + .75*x);
  41. col.b *= 0.95;
  42. return col;
  43. }
  44. void main()
  45. {
  46. float brickW = 1.0 / numberOfTilesWidth;
  47. float brickH = 1.0 / numberOfTilesHeight;
  48. float jointWPercentage = 0.01;
  49. float jointHPercentage = 0.01;
  50. vec3 color = marbleColor;
  51. float yi = vUV.y / brickH;
  52. float nyi = round(yi);
  53. float xi = vUV.x / brickW;
  54. if (mod(floor(yi), 2.0) == 0.0){
  55. xi = xi - 0.5;
  56. }
  57. float nxi = round(xi);
  58. vec2 brickvUV = vec2((xi - floor(xi)) / brickH, (yi - floor(yi)) / brickW);
  59. if (yi < nyi + jointHPercentage && yi > nyi - jointHPercentage){
  60. color = mix(jointColor, vec3(0.37, 0.25, 0.25), (yi - nyi) / jointHPercentage + 0.2);
  61. }
  62. else if (xi < nxi + jointWPercentage && xi > nxi - jointWPercentage){
  63. color = mix(jointColor, vec3(0.44, 0.44, 0.44), (xi - nxi) / jointWPercentage + 0.2);
  64. }
  65. else {
  66. float t = 6.28 * brickvUV.x / (tileSize.x + noise(vec2(vUV)*6.0));
  67. t += amplitude * turbulence(brickvUV.xy);
  68. t = sin(t);
  69. color = marble_color(t);
  70. }
  71. gl_FragColor = vec4(color, 0.0);
  72. }