설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CanvasPass.hlsl 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #if SHADERPASS != SHADERPASS_CUSTOM_UI
  2. #error SHADERPASS_CUSTOM_UI_is_not_correctly_defined
  3. #endif
  4. // Get Homogeneous normalized device coordinates
  5. float4 GetVertexPositionNDC(float3 positionCS)
  6. {
  7. float3 ndc = positionCS * 0.5f;
  8. float4 positionNDC;
  9. positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x);// + ndc.w;
  10. positionNDC.zw = float2(positionCS.z, 1.0);
  11. return positionNDC;
  12. }
  13. // Transforms position from object space to homogenous space
  14. float4 TransformModelToHClip(float3 positionOS)
  15. {
  16. return mul(unity_MatrixVP, mul(unity_ObjectToWorld, float4(positionOS, 1.0)));
  17. }
  18. Varyings BuildVaryings(Attributes input)
  19. {
  20. Varyings output = (Varyings)0;
  21. UNITY_SETUP_INSTANCE_ID(input);
  22. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  23. // Returns the camera relative position (if enabled)
  24. float3 positionWS = TransformObjectToWorld(input.positionOS);
  25. output.positionCS = TransformWorldToHClip(positionWS);
  26. #if UNITY_UV_STARTS_AT_TOP
  27. //When UI is set to render in Overlay it writes to GFXdevice
  28. //the same as if it was writing to a full screen back buffer
  29. //Work around to catch Matrices not supplied by Camera,
  30. //So Clipspace is recalculated, using raw unity_ObjectToWorld & unity_MatrixVP
  31. output.positionCS = TransformModelToHClip(input.positionOS);
  32. output.texCoord0.y = 1.0 - output.texCoord0.y;
  33. #endif
  34. #ifdef ATTRIBUTES_NEED_NORMAL
  35. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  36. #else
  37. // Required to compile ApplyVertexModification that doesn't use normal.
  38. float3 normalWS = float3(0.0, 0.0, 0.0);
  39. #endif
  40. #ifdef ATTRIBUTES_NEED_TANGENT
  41. float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
  42. #endif
  43. // TODO: Change to inline ifdef
  44. // Do vertex modification in camera relative space (if enabled)
  45. #if defined(HAVE_VERTEX_MODIFICATION)
  46. ApplyVertexModification(input, normalWS, positionWS, _TimeParameters.xyz);
  47. #endif
  48. #ifdef VARYINGS_NEED_POSITION_WS
  49. output.positionWS = positionWS;
  50. #endif
  51. #ifdef VARYINGS_NEED_NORMAL_WS
  52. output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal()
  53. #endif
  54. #ifdef VARYINGS_NEED_TANGENT_WS
  55. output.tangentWS = tangentWS; // normalized in TransformObjectToWorldDir()
  56. #endif
  57. #if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
  58. output.texCoord0 = input.uv0;
  59. #endif
  60. //UI "Mask"
  61. #if defined(VARYINGS_NEED_TEXCOORD1) || defined(VARYINGS_DS_NEED_TEXCOORD1)
  62. #ifdef UNITY_UI_CLIP_RECT
  63. float2 pixelSize = output.positionCS.w;
  64. pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  65. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  66. float2 maskUV = (input.positionOS.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  67. output.texCoord1 = float4(input.positionOS.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
  68. #endif
  69. #endif
  70. #if defined(VARYINGS_NEED_TEXCOORD2) || defined(VARYINGS_DS_NEED_TEXCOORD2)
  71. output.texCoord2 = input.uv2;
  72. #endif
  73. #if defined(VARYINGS_NEED_TEXCOORD3) || defined(VARYINGS_DS_NEED_TEXCOORD3)
  74. output.texCoord3 = input.uv3;
  75. #endif
  76. #if defined(VARYINGS_NEED_COLOR) || defined(VARYINGS_DS_NEED_COLOR)
  77. output.color = input.color;
  78. #endif
  79. #ifdef VARYINGS_NEED_SCREENPOSITION
  80. output.screenPosition = GetVertexPositionNDC(output.positionCS); // vertexInput.positionNDC;
  81. #endif
  82. return output;
  83. }
  84. PackedVaryings vert(Attributes input)
  85. {
  86. Varyings output = BuildVaryings(input);
  87. PackedVaryings packedOutput = PackVaryings(output);
  88. return packedOutput;
  89. }
  90. half4 frag(PackedVaryings packedInput) : SV_TARGET
  91. {
  92. Varyings unpacked = UnpackVaryings(packedInput);
  93. UNITY_SETUP_INSTANCE_ID(unpacked);
  94. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
  95. SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(unpacked);
  96. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
  97. //Round up the alpha color coming from the interpolator (to 1.0/256.0 steps)
  98. //The incoming alpha could have numerical instability, which makes it very sensible to
  99. //HDR color transparency blend, when it blends with the world's texture.
  100. const half alphaPrecision = half(0xff);
  101. const half invAlphaPrecision = half(1.0/alphaPrecision);
  102. unpacked.color.a = round(unpacked.color.a * alphaPrecision)*invAlphaPrecision;
  103. half alpha = surfaceDescription.Alpha;
  104. half4 color = half4(surfaceDescription.BaseColor + surfaceDescription.Emission, alpha) ;
  105. #if !defined(HAVE_VFX_MODIFICATION) && !defined(_DISABLE_COLOR_TINT)
  106. color *= unpacked.color;
  107. #endif
  108. #ifdef UNITY_UI_CLIP_RECT
  109. //mask = Uv2
  110. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(unpacked.texCoord1.xy)) * unpacked.texCoord1.zw);
  111. color.a *= m.x * m.y;
  112. #endif
  113. #if _ALPHATEST_ON
  114. clip(alpha - surfaceDescription.AlphaClipThreshold);
  115. #endif
  116. color.rgb *= color.a;
  117. return color;
  118. }