GridMaterial.glsl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifdef GL_OES_standard_derivatives
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. uniform vec4 color;
  5. uniform float cellAlpha;
  6. uniform vec2 lineCount;
  7. uniform vec2 lineThickness;
  8. uniform vec2 lineOffset;
  9. czm_material czm_getMaterial(czm_materialInput materialInput)
  10. {
  11. czm_material material = czm_getDefaultMaterial(materialInput);
  12. vec2 st = materialInput.st;
  13. float scaledWidth = fract(lineCount.s * st.s - lineOffset.s);
  14. scaledWidth = abs(scaledWidth - floor(scaledWidth + 0.5));
  15. float scaledHeight = fract(lineCount.t * st.t - lineOffset.t);
  16. scaledHeight = abs(scaledHeight - floor(scaledHeight + 0.5));
  17. float value;
  18. #ifdef GL_OES_standard_derivatives
  19. // Fuzz Factor - Controls blurriness of lines
  20. const float fuzz = 1.2;
  21. vec2 thickness = (lineThickness * czm_resolutionScale) - 1.0;
  22. // From "3D Engine Design for Virtual Globes" by Cozzi and Ring, Listing 4.13.
  23. vec2 dx = abs(dFdx(st));
  24. vec2 dy = abs(dFdy(st));
  25. vec2 dF = vec2(max(dx.s, dy.s), max(dx.t, dy.t)) * lineCount;
  26. value = min(
  27. smoothstep(dF.s * thickness.s, dF.s * (fuzz + thickness.s), scaledWidth),
  28. smoothstep(dF.t * thickness.t, dF.t * (fuzz + thickness.t), scaledHeight));
  29. #else
  30. // Fuzz Factor - Controls blurriness of lines
  31. const float fuzz = 0.05;
  32. vec2 range = 0.5 - (lineThickness * 0.05);
  33. value = min(
  34. 1.0 - smoothstep(range.s, range.s + fuzz, scaledWidth),
  35. 1.0 - smoothstep(range.t, range.t + fuzz, scaledHeight));
  36. #endif
  37. // Edges taken from RimLightingMaterial.glsl
  38. // See http://www.fundza.com/rman_shaders/surface/fake_rim/fake_rim1.html
  39. float dRim = 1.0 - abs(dot(materialInput.normalEC, normalize(materialInput.positionToEyeEC)));
  40. float sRim = smoothstep(0.8, 1.0, dRim);
  41. value *= (1.0 - sRim);
  42. vec3 halfColor = color.rgb * 0.5;
  43. material.diffuse = halfColor;
  44. material.emission = halfColor;
  45. material.alpha = color.a * (1.0 - ((1.0 - cellAlpha) * value));
  46. return material;
  47. }