getClipAndStyleCode.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. import Check from "../Core/Check.js";
  2. /**
  3. * Gets a GLSL snippet that clips a fragment using the `clip` function from {@link getClippingFunction} and styles it.
  4. *
  5. * @param {string} samplerUniformName Name of the uniform for the clipping planes texture sampler.
  6. * @param {string} matrixUniformName Name of the uniform for the clipping planes matrix.
  7. * @param {string} styleUniformName Name of the uniform for the clipping planes style, a vec4.
  8. * @returns {string} A string containing GLSL that clips and styles the current fragment.
  9. * @private
  10. */
  11. function getClipAndStyleCode(
  12. samplerUniformName,
  13. matrixUniformName,
  14. styleUniformName
  15. ) {
  16. //>>includeStart('debug', pragmas.debug);
  17. Check.typeOf.string("samplerUniformName", samplerUniformName);
  18. Check.typeOf.string("matrixUniformName", matrixUniformName);
  19. Check.typeOf.string("styleUniformName", styleUniformName);
  20. //>>includeEnd('debug');
  21. const shaderCode =
  22. ` float clipDistance = clip(gl_FragCoord, ${samplerUniformName}, ${matrixUniformName}); \n` +
  23. ` vec4 clippingPlanesEdgeColor = vec4(1.0); \n` +
  24. ` clippingPlanesEdgeColor.rgb = ${styleUniformName}.rgb; \n` +
  25. ` float clippingPlanesEdgeWidth = ${styleUniformName}.a; \n` +
  26. ` if (clipDistance > 0.0 && clipDistance < clippingPlanesEdgeWidth) \n` +
  27. ` { \n` +
  28. ` out_FragColor = clippingPlanesEdgeColor;\n` +
  29. ` } \n`;
  30. return shaderCode;
  31. }
  32. export default getClipAndStyleCode;