GridMaterial.glsl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // Fuzz Factor - Controls blurriness of lines
  19. #if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives))
  20. const float fuzz = 1.2;
  21. vec2 thickness = (lineThickness * czm_pixelRatio) - 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. // If no derivatives available (IE 10?), revert to view-dependent fuzz
  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. vec4 halfColor;
  43. halfColor.rgb = color.rgb * 0.5;
  44. halfColor.a = color.a * (1.0 - ((1.0 - cellAlpha) * value));
  45. halfColor = czm_gammaCorrect(halfColor);
  46. material.diffuse = halfColor.rgb;
  47. material.emission = halfColor.rgb;
  48. material.alpha = halfColor.a;
  49. return material;
  50. }