Splitter.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import ShaderSource from "../Renderer/ShaderSource.js";
  2. /**
  3. * Support for rendering things on only one side of the screen.
  4. *
  5. * @private
  6. */
  7. const Splitter = {
  8. /**
  9. * Given a fragment shader string, returns a modified version of it that
  10. * only renders on one side of the screen or the other. Fragments on the
  11. * other side are discarded. The screen side is given by a uniform called
  12. * `czm_splitDirection`, which can be added by calling
  13. * {@link Splitter#addUniforms}, and the split position is given by an
  14. * automatic uniform called `czm_splitPosition`.
  15. */
  16. modifyFragmentShader: function modifyFragmentShader(shader) {
  17. shader = ShaderSource.replaceMain(shader, "czm_splitter_main");
  18. shader +=
  19. // czm_splitPosition is not declared because it is an automatic uniform.
  20. "uniform float czm_splitDirection; \n" +
  21. "void main() \n" +
  22. "{ \n" +
  23. // Don't split when rendering the shadow map, because it is rendered from
  24. // the perspective of a totally different camera.
  25. "#ifndef SHADOW_MAP\n" +
  26. " if (czm_splitDirection < 0.0 && gl_FragCoord.x > czm_splitPosition) discard; \n" +
  27. " if (czm_splitDirection > 0.0 && gl_FragCoord.x < czm_splitPosition) discard; \n" +
  28. "#endif\n" +
  29. " czm_splitter_main(); \n" +
  30. "} \n";
  31. return shader;
  32. },
  33. /**
  34. * Add `czm_splitDirection` to the given uniform map.
  35. *
  36. * @param {Object} object The object on which the `splitDirection` property may be found.
  37. * @param {Object} uniformMap The uniform map.
  38. */
  39. addUniforms: function addUniforms(object, uniformMap) {
  40. uniformMap.czm_splitDirection = function () {
  41. return object.splitDirection;
  42. };
  43. },
  44. };
  45. export default Splitter;