CustomRTUpdate.shader 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Shader /*ase_name*/"Hidden/Templates/CustomRTUpdate"/*end*/
  2. {
  3. Properties
  4. {
  5. /*ase_props*/
  6. }
  7. SubShader
  8. {
  9. Tags { }
  10. /*ase_all_modules*/
  11. /*ase_pass*/
  12. Pass
  13. {
  14. Name "Custom RT Update"
  15. CGPROGRAM
  16. #include "UnityCustomRenderTexture.cginc"
  17. #pragma vertex ASECustomRenderTextureVertexShader
  18. #pragma fragment frag
  19. #pragma target 3.5
  20. /*ase_pragma*/
  21. struct ase_appdata_customrendertexture
  22. {
  23. uint vertexID : SV_VertexID;
  24. /*ase_vdata:*/
  25. };
  26. struct ase_v2f_customrendertexture
  27. {
  28. float4 vertex : SV_POSITION;
  29. float3 localTexcoord : TEXCOORD0; // Texcoord local to the update zone (== globalTexcoord if no partial update zone is specified)
  30. float3 globalTexcoord : TEXCOORD1; // Texcoord relative to the complete custom texture
  31. uint primitiveID : TEXCOORD2; // Index of the update zone (correspond to the index in the updateZones of the Custom Texture)
  32. float3 direction : TEXCOORD3; // For cube textures, direction of the pixel being rendered in the cubemap
  33. /*ase_interp(4,):sp=sp.xyzw;uv0=tc0;uv1=tc1;uv2=tc2;uv3=tc3*/
  34. };
  35. /*ase_globals*/
  36. ase_v2f_customrendertexture ASECustomRenderTextureVertexShader(ase_appdata_customrendertexture IN /*ase_vert_input*/ )
  37. {
  38. ase_v2f_customrendertexture OUT;
  39. /*ase_vert_code:IN=ase_appdata_customrendertexture;OUT=ase_v2f_customrendertexture*/
  40. #if UNITY_UV_STARTS_AT_TOP
  41. const float2 vertexPositions[6] =
  42. {
  43. { -1.0f, 1.0f },
  44. { -1.0f, -1.0f },
  45. { 1.0f, -1.0f },
  46. { 1.0f, 1.0f },
  47. { -1.0f, 1.0f },
  48. { 1.0f, -1.0f }
  49. };
  50. const float2 texCoords[6] =
  51. {
  52. { 0.0f, 0.0f },
  53. { 0.0f, 1.0f },
  54. { 1.0f, 1.0f },
  55. { 1.0f, 0.0f },
  56. { 0.0f, 0.0f },
  57. { 1.0f, 1.0f }
  58. };
  59. #else
  60. const float2 vertexPositions[6] =
  61. {
  62. { 1.0f, 1.0f },
  63. { -1.0f, -1.0f },
  64. { -1.0f, 1.0f },
  65. { -1.0f, -1.0f },
  66. { 1.0f, 1.0f },
  67. { 1.0f, -1.0f }
  68. };
  69. const float2 texCoords[6] =
  70. {
  71. { 1.0f, 1.0f },
  72. { 0.0f, 0.0f },
  73. { 0.0f, 1.0f },
  74. { 0.0f, 0.0f },
  75. { 1.0f, 1.0f },
  76. { 1.0f, 0.0f }
  77. };
  78. #endif
  79. uint primitiveID = IN.vertexID / 6;
  80. uint vertexID = IN.vertexID % 6;
  81. float3 updateZoneCenter = CustomRenderTextureCenters[primitiveID].xyz;
  82. float3 updateZoneSize = CustomRenderTextureSizesAndRotations[primitiveID].xyz;
  83. float rotation = CustomRenderTextureSizesAndRotations[primitiveID].w * UNITY_PI / 180.0f;
  84. #if !UNITY_UV_STARTS_AT_TOP
  85. rotation = -rotation;
  86. #endif
  87. // Normalize rect if needed
  88. if (CustomRenderTextureUpdateSpace > 0.0) // Pixel space
  89. {
  90. // Normalize xy because we need it in clip space.
  91. updateZoneCenter.xy /= _CustomRenderTextureInfo.xy;
  92. updateZoneSize.xy /= _CustomRenderTextureInfo.xy;
  93. }
  94. else // normalized space
  95. {
  96. // Un-normalize depth because we need actual slice index for culling
  97. updateZoneCenter.z *= _CustomRenderTextureInfo.z;
  98. updateZoneSize.z *= _CustomRenderTextureInfo.z;
  99. }
  100. // Compute rotation
  101. // Compute quad vertex position
  102. float2 clipSpaceCenter = updateZoneCenter.xy * 2.0 - 1.0;
  103. float2 pos = vertexPositions[vertexID] * updateZoneSize.xy;
  104. pos = CustomRenderTextureRotate2D(pos, rotation);
  105. pos.x += clipSpaceCenter.x;
  106. #if UNITY_UV_STARTS_AT_TOP
  107. pos.y += clipSpaceCenter.y;
  108. #else
  109. pos.y -= clipSpaceCenter.y;
  110. #endif
  111. // For 3D texture, cull quads outside of the update zone
  112. // This is neeeded in additional to the preliminary minSlice/maxSlice done on the CPU because update zones can be disjointed.
  113. // ie: slices [1..5] and [10..15] for two differents zones so we need to cull out slices 0 and [6..9]
  114. if (CustomRenderTextureIs3D > 0.0)
  115. {
  116. int minSlice = (int)(updateZoneCenter.z - updateZoneSize.z * 0.5);
  117. int maxSlice = minSlice + (int)updateZoneSize.z;
  118. if (_CustomRenderTexture3DSlice < minSlice || _CustomRenderTexture3DSlice >= maxSlice)
  119. {
  120. pos.xy = float2(1000.0, 1000.0); // Vertex outside of ncs
  121. }
  122. }
  123. OUT.vertex = float4(pos, 0.0, 1.0);
  124. OUT.primitiveID = asuint(CustomRenderTexturePrimitiveIDs[primitiveID]);
  125. OUT.localTexcoord = float3(texCoords[vertexID], CustomRenderTexture3DTexcoordW);
  126. OUT.globalTexcoord = float3(pos.xy * 0.5 + 0.5, CustomRenderTexture3DTexcoordW);
  127. #if UNITY_UV_STARTS_AT_TOP
  128. OUT.globalTexcoord.y = 1.0 - OUT.globalTexcoord.y;
  129. #endif
  130. OUT.direction = CustomRenderTextureComputeCubeDirection(OUT.globalTexcoord.xy);
  131. return OUT;
  132. }
  133. float4 frag(ase_v2f_customrendertexture IN /*ase_frag_input*/) : COLOR
  134. {
  135. float4 finalColor;
  136. /*ase_frag_code:IN=ase_v2f_customrendertexture*/
  137. finalColor = /*ase_frag_out:Frag Color;Float4*/float4(1,1,1,1)/*end*/;
  138. return finalColor;
  139. }
  140. ENDCG
  141. }
  142. }
  143. }