// Assuming a isoceles right angled triangle of height "triangleHeight" (as drawn below). // This function return the area of the triangle above the first texel. // // |\ <-- 45 degree slop isosceles right angled triangle // | \ // ---- <-- length of this side is "triangleHeight" // _ _ _ _ <-- texels real SampleShadow_GetTriangleTexelArea(real triangleHeight) { return triangleHeight - 0.5; }
// Assuming a isoceles triangle of 1.5 texels height and 3 texels wide lying on 4 texels. // This function return the area of the triangle above each of those texels. // | <-- offset from -0.5 to 0.5, 0 meaning triangle is exactly in the center // / \ <-- 45 degree slop isosceles triangle (ie tent projected in 2D) // / \ // _ _ _ _ <-- texels // X Y Z W <-- result indices (in computedArea.xyzw and computedAreaUncut.xyzw) voidSampleShadow_GetTexelAreas_Tent_3x3(real offset, out real4 computedArea, out real4 computedAreaUncut) { // Compute the exterior areas real offset01SquaredHalved = (offset + 0.5) * (offset + 0.5) * 0.5; computedAreaUncut.x = computedArea.x = offset01SquaredHalved - offset; computedAreaUncut.w = computedArea.w = offset01SquaredHalved;
// Compute the middle areas // For Y : We find the area in Y of as if the left section of the isoceles triangle would // intersect the axis between Y and Z (ie where offset = 0). computedAreaUncut.y = SampleShadow_GetTriangleTexelArea(1.5 - offset); // This area is superior to the one we are looking for if (offset < 0) thus we need to // subtract the area of the triangle defined by (0,1.5-offset), (0,1.5+offset), (-offset,1.5). real clampedOffsetLeft = min(offset,0); // 偏移值为负值时才需要减去三角形的面积 real areaOfSmallLeftTriangle = clampedOffsetLeft * clampedOffsetLeft; computedArea.y = computedAreaUncut.y - areaOfSmallLeftTriangle;
// We do the same for the Z but with the right part of the isoceles triangle computedAreaUncut.z = SampleShadow_GetTriangleTexelArea(1.5 + offset); real clampedOffsetRight = max(offset,0); real areaOfSmallRightTriangle = clampedOffsetRight * clampedOffsetRight; computedArea.z = computedAreaUncut.z - areaOfSmallRightTriangle; }
// Assuming a isoceles triangle of 1.5 texels height and 3 texels wide lying on 4 texels. // This function return the weight of each texels area relative to the full triangle area. voidSampleShadow_GetTexelWeights_Tent_3x3(real offset, out real4 computedWeight) { real4 dummy; SampleShadow_GetTexelAreas_Tent_3x3(offset, computedWeight, dummy); computedWeight *= 0.44444;//0.44 == 1/(the triangle area) }
// Assuming a isoceles triangle of 2.5 texel height and 5 texels wide lying on 6 texels. // This function return the weight of each texels area relative to the full triangle area. // / \ // _ _ _ _ _ _ <-- texels // 0 1 2 3 4 5 <-- computed area indices (in texelsWeights[]) voidSampleShadow_GetTexelWeights_Tent_5x5(real offset, out real3 texelsWeightsA, out real3 texelsWeightsB) { // See _UnityInternalGetAreaPerTexel_3TexelTriangleFilter for details. real4 computedArea_From3texelTriangle; real4 computedAreaUncut_From3texelTriangle; SampleShadow_GetTexelAreas_Tent_3x3(offset, computedArea_From3texelTriangle, computedAreaUncut_From3texelTriangle);
// Triangle slope is 45 degree thus we can almost reuse the result of the 3 texel wide computation. // the 5 texel wide triangle can be seen as the 3 texel wide one but shifted up by one unit/texel. // 0.16 is 1/(the triangle area) texelsWeightsA.x = 0.16 * (computedArea_From3texelTriangle.x); texelsWeightsA.y = 0.16 * (computedAreaUncut_From3texelTriangle.y); texelsWeightsA.z = 0.16 * (computedArea_From3texelTriangle.y + 1); texelsWeightsB.x = 0.16 * (computedArea_From3texelTriangle.z + 1); texelsWeightsB.y = 0.16 * (computedAreaUncut_From3texelTriangle.z); texelsWeightsB.z = 0.16 * (computedArea_From3texelTriangle.w); }
// 3x3 Tent filter (45 degree sloped triangles in U and V) voidSampleShadow_ComputeSamples_Tent_3x3(real4 shadowMapTexture_TexelSize, real2 coord, out real fetchesWeights[4], out real2 fetchesUV[4]) { // tent base is 3x3 base thus covering from 9 to 12 texels, thus we need 4 bilinear PCF fetches real2 tentCenterInTexelSpace = coord.xy * shadowMapTexture_TexelSize.zw; real2 centerOfFetchesInTexelSpace = floor(tentCenterInTexelSpace + 0.5); real2 offsetFromTentCenterToCenterOfFetches = tentCenterInTexelSpace - centerOfFetchesInTexelSpace;
// find the weight of each texel based real4 texelsWeightsU, texelsWeightsV; SampleShadow_GetTexelWeights_Tent_3x3(offsetFromTentCenterToCenterOfFetches.x, texelsWeightsU); SampleShadow_GetTexelWeights_Tent_3x3(offsetFromTentCenterToCenterOfFetches.y, texelsWeightsV);
// each fetch will cover a group of 2x2 texels, the weight of each group is the sum of the weights of the texels real2 fetchesWeightsU = texelsWeightsU.xz + texelsWeightsU.yw; real2 fetchesWeightsV = texelsWeightsV.xz + texelsWeightsV.yw;