Tessellation.hlsl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #define TESSELLATION_INTERPOLATE_BARY(name, bary) output.name = input0.name * bary.x + input1.name * bary.y + input2.name * bary.z
  2. // p0, p1, p2 triangle world position
  3. // p0, p1, p2 triangle world vertex normal
  4. real3 PhongTessellation(real3 positionWS, real3 p0, real3 p1, real3 p2, real3 n0, real3 n1, real3 n2, real3 baryCoords, real shape)
  5. {
  6. real3 c0 = ProjectPointOnPlane(positionWS, p0, n0);
  7. real3 c1 = ProjectPointOnPlane(positionWS, p1, n1);
  8. real3 c2 = ProjectPointOnPlane(positionWS, p2, n2);
  9. real3 phongPositionWS = baryCoords.x * c0 + baryCoords.y * c1 + baryCoords.z * c2;
  10. return lerp(positionWS, phongPositionWS, shape);
  11. }
  12. // Reference: http://twvideo01.ubm-us.net/o1/vault/gdc10/slides/Bilodeau_Bill_Direct3D11TutorialTessellation.pdf
  13. // Compute both screen and distance based adaptation - return factor between 0 and 1
  14. real3 GetScreenSpaceTessFactor(real3 p0, real3 p1, real3 p2, real4x4 viewProjectionMatrix, real4 screenSize, real triangleSize)
  15. {
  16. // Get screen space adaptive scale factor
  17. real2 edgeScreenPosition0 = ComputeNormalizedDeviceCoordinates(p0, viewProjectionMatrix) * screenSize.xy;
  18. real2 edgeScreenPosition1 = ComputeNormalizedDeviceCoordinates(p1, viewProjectionMatrix) * screenSize.xy;
  19. real2 edgeScreenPosition2 = ComputeNormalizedDeviceCoordinates(p2, viewProjectionMatrix) * screenSize.xy;
  20. real EdgeScale = 1.0 / triangleSize; // Edge size in reality, but name is simpler
  21. real3 tessFactor;
  22. tessFactor.x = saturate(distance(edgeScreenPosition1, edgeScreenPosition2) * EdgeScale);
  23. tessFactor.y = saturate(distance(edgeScreenPosition0, edgeScreenPosition2) * EdgeScale);
  24. tessFactor.z = saturate(distance(edgeScreenPosition0, edgeScreenPosition1) * EdgeScale);
  25. return tessFactor;
  26. }
  27. real3 GetDistanceBasedTessFactor(real3 p0, real3 p1, real3 p2, real3 cameraPosWS, real tessMinDist, real tessMaxDist)
  28. {
  29. real3 edgePosition0 = 0.5 * (p1 + p2);
  30. real3 edgePosition1 = 0.5 * (p0 + p2);
  31. real3 edgePosition2 = 0.5 * (p0 + p1);
  32. // In case camera-relative rendering is enabled, 'cameraPosWS' is statically known to be 0,
  33. // so the compiler will be able to optimize distance() to length().
  34. real dist0 = distance(edgePosition0, cameraPosWS);
  35. real dist1 = distance(edgePosition1, cameraPosWS);
  36. real dist2 = distance(edgePosition2, cameraPosWS);
  37. // The saturate will handle the produced NaN in case min == max
  38. real fadeDist = tessMaxDist - tessMinDist;
  39. real3 tessFactor;
  40. tessFactor.x = saturate(1.0 - (dist0 - tessMinDist) / fadeDist);
  41. tessFactor.y = saturate(1.0 - (dist1 - tessMinDist) / fadeDist);
  42. tessFactor.z = saturate(1.0 - (dist2 - tessMinDist) / fadeDist);
  43. return tessFactor;
  44. }
  45. real4 CalcTriTessFactorsFromEdgeTessFactors(real3 triVertexFactors)
  46. {
  47. real4 tess;
  48. tess.x = triVertexFactors.x;
  49. tess.y = triVertexFactors.y;
  50. tess.z = triVertexFactors.z;
  51. tess.w = (triVertexFactors.x + triVertexFactors.y + triVertexFactors.z) / 3.0;
  52. return tess;
  53. }