SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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.

150 lines
4.6 KiB

4 years ago
  1. Shader "Unlit/ARKitBackground"
  2. {
  3. Properties
  4. {
  5. _textureY ("TextureY", 2D) = "white" {}
  6. _textureCbCr ("TextureCbCr", 2D) = "black" {}
  7. }
  8. SubShader
  9. {
  10. Tags
  11. {
  12. "Queue" = "Background"
  13. "RenderType" = "Background"
  14. "ForceNoShadowCasting" = "True"
  15. }
  16. Pass
  17. {
  18. Cull Off
  19. ZTest Always
  20. ZWrite Off
  21. Lighting Off
  22. LOD 100
  23. Tags
  24. {
  25. "LightMode" = "Always"
  26. }
  27. HLSLPROGRAM
  28. #pragma vertex vert
  29. #pragma fragment frag
  30. #pragma multi_compile_local __ ARKIT_BACKGROUND_URP ARKIT_BACKGROUND_LWRP
  31. #if ARKIT_BACKGROUND_URP
  32. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  33. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  34. #define ARKIT_TEXTURE2D_HALF(texture) TEXTURE2D(texture)
  35. #define ARKIT_SAMPLER_HALF(sampler) SAMPLER(sampler)
  36. #define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) SAMPLE_TEXTURE2D(texture,sampler,texcoord)
  37. #elif ARKIT_BACKGROUND_LWRP
  38. #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
  39. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  40. #define ARKIT_TEXTURE2D_HALF(texture) TEXTURE2D(texture)
  41. #define ARKIT_SAMPLER_HALF(sampler) SAMPLER(sampler)
  42. #define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) SAMPLE_TEXTURE2D(texture,sampler,texcoord)
  43. #else // Legacy RP
  44. #include "UnityCG.cginc"
  45. #define real4 half4
  46. #define real4x4 half4x4
  47. #define TransformObjectToHClip UnityObjectToClipPos
  48. #define FastSRGBToLinear GammaToLinearSpace
  49. #define ARKIT_TEXTURE2D_HALF(texture) UNITY_DECLARE_TEX2D_HALF(texture)
  50. #define ARKIT_SAMPLER_HALF(sampler)
  51. #define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) UNITY_SAMPLE_TEX2D(texture,texcoord)
  52. #endif
  53. struct appdata
  54. {
  55. float3 position : POSITION;
  56. float2 texcoord : TEXCOORD0;
  57. };
  58. struct v2f
  59. {
  60. float4 position : SV_POSITION;
  61. float2 texcoord : TEXCOORD0;
  62. };
  63. struct fragment_output
  64. {
  65. real4 color : SV_Target;
  66. };
  67. CBUFFER_START(UnityPerFrame)
  68. // Device display transform is provided by the AR Foundation camera background renderer.
  69. float4x4 _UnityDisplayTransform;
  70. CBUFFER_END
  71. v2f vert (appdata v)
  72. {
  73. // Transform the position from object space to clip space.
  74. float4 position = TransformObjectToHClip(v.position);
  75. // Remap the texture coordinates based on the device rotation.
  76. float2 texcoord = mul(float3(v.texcoord, 1.0f), _UnityDisplayTransform).xy;
  77. v2f o;
  78. o.position = position;
  79. o.texcoord = texcoord;
  80. return o;
  81. }
  82. CBUFFER_START(ARKitColorTransformations)
  83. static const real4x4 s_YCbCrToSRGB = real4x4(
  84. real4(1.0h, 0.0000h, 1.4020h, -0.7010h),
  85. real4(1.0h, -0.3441h, -0.7141h, 0.5291h),
  86. real4(1.0h, 1.7720h, 0.0000h, -0.8860h),
  87. real4(0.0h, 0.0000h, 0.0000h, 1.0000h)
  88. );
  89. CBUFFER_END
  90. ARKIT_TEXTURE2D_HALF(_textureY);
  91. ARKIT_SAMPLER_HALF(sampler_textureY);
  92. ARKIT_TEXTURE2D_HALF(_textureCbCr);
  93. ARKIT_SAMPLER_HALF(sampler_textureCbCr);
  94. fragment_output frag (v2f i)
  95. {
  96. // Sample the video textures (in YCbCr).
  97. real4 ycbcr = real4(ARKIT_SAMPLE_TEXTURE2D(_textureY, sampler_textureY, i.texcoord).r,
  98. ARKIT_SAMPLE_TEXTURE2D(_textureCbCr, sampler_textureCbCr, i.texcoord).rg,
  99. 1.0h);
  100. // Convert from YCbCr to sRGB.
  101. real4 videoColor = mul(s_YCbCrToSRGB, ycbcr);
  102. #if !UNITY_COLORSPACE_GAMMA
  103. // If rendering in linear color space, convert from sRGB to RGB.
  104. videoColor.xyz = FastSRGBToLinear(videoColor.xyz);
  105. #endif // !UNITY_COLORSPACE_GAMMA
  106. fragment_output o;
  107. o.color = videoColor;
  108. return o;
  109. }
  110. ENDHLSL
  111. }
  112. }
  113. }