Spine-Outline-Common.cginc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef SPINE_OUTLINE_COMMON_INCLUDED
  2. #define SPINE_OUTLINE_COMMON_INCLUDED
  3. float4 computeOutlinePixel(sampler2D mainTexture, float2 mainTextureTexelSize,
  4. float2 uv, float vertexColorAlpha,
  5. float OutlineWidth, float OutlineReferenceTexWidth, float OutlineMipLevel,
  6. float OutlineSmoothness, float ThresholdEnd, float4 OutlineColor) {
  7. float4 texColor = fixed4(0, 0, 0, 0);
  8. float outlineWidthCompensated = OutlineWidth / (OutlineReferenceTexWidth * mainTextureTexelSize.x);
  9. float xOffset = mainTextureTexelSize.x * outlineWidthCompensated;
  10. float yOffset = mainTextureTexelSize.y * outlineWidthCompensated;
  11. float xOffsetDiagonal = mainTextureTexelSize.x * outlineWidthCompensated * 0.7;
  12. float yOffsetDiagonal = mainTextureTexelSize.y * outlineWidthCompensated * 0.7;
  13. float pixelCenter = tex2D(mainTexture, uv).a;
  14. float4 uvCenterWithLod = float4(uv, 0, OutlineMipLevel);
  15. float pixelTop = tex2Dlod(mainTexture, uvCenterWithLod + float4(0, yOffset, 0, 0)).a;
  16. float pixelBottom = tex2Dlod(mainTexture, uvCenterWithLod + float4(0, -yOffset, 0, 0)).a;
  17. float pixelLeft = tex2Dlod(mainTexture, uvCenterWithLod + float4(-xOffset, 0, 0, 0)).a;
  18. float pixelRight = tex2Dlod(mainTexture, uvCenterWithLod + float4(xOffset, 0, 0, 0)).a;
  19. #if _USE8NEIGHBOURHOOD_ON
  20. float numSamples = 8;
  21. float pixelTopLeft = tex2Dlod(mainTexture, uvCenterWithLod + float4(-xOffsetDiagonal, yOffsetDiagonal, 0, 0)).a;
  22. float pixelTopRight = tex2Dlod(mainTexture, uvCenterWithLod + float4(xOffsetDiagonal, yOffsetDiagonal, 0, 0)).a;
  23. float pixelBottomLeft = tex2Dlod(mainTexture, uvCenterWithLod + float4(-xOffsetDiagonal, -yOffsetDiagonal, 0, 0)).a;
  24. float pixelBottomRight = tex2Dlod(mainTexture, uvCenterWithLod + float4(xOffsetDiagonal, -yOffsetDiagonal, 0, 0)).a;
  25. float average = (pixelTop + pixelBottom + pixelLeft + pixelRight +
  26. pixelTopLeft + pixelTopRight + pixelBottomLeft + pixelBottomRight)
  27. * vertexColorAlpha / numSamples;
  28. #else // 4 neighbourhood
  29. float numSamples = 1;
  30. float average = (pixelTop + pixelBottom + pixelLeft + pixelRight) * vertexColorAlpha / numSamples;
  31. #endif
  32. float thresholdStart = ThresholdEnd * (1.0 - OutlineSmoothness);
  33. float outlineAlpha = saturate((average - thresholdStart) / (ThresholdEnd - thresholdStart)) - pixelCenter;
  34. return lerp(texColor, OutlineColor, outlineAlpha);
  35. }
  36. #endif