暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CustomTexture.hlsl 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Ported from CGinc version
  2. #ifndef UNITY_CUSTOM_TEXTURE_INCLUDED
  3. #define UNITY_CUSTOM_TEXTURE_INCLUDED
  4. #ifndef UNITY_PI
  5. #define UNITY_PI 3.14159265358979323846
  6. #endif
  7. // Keep in sync with CustomRenderTexture.h
  8. #define kCustomTextureBatchSize 16
  9. #define CRT_DIMENSION_2D 0.0
  10. #define CRT_DIMENSION_3D 1.0
  11. #define CRT_DIMENSION_CUBE 2.0
  12. struct appdata_customrendertexture
  13. {
  14. uint vertexID : SV_VertexID;
  15. };
  16. // User facing vertex to fragment shader structure
  17. struct v2f_customrendertexture
  18. {
  19. float4 vertex : SV_POSITION;
  20. float3 localTexcoord : TEXCOORD0; // Texcoord local to the update zone (== globalTexcoord if no partial update zone is specified)
  21. float3 globalTexcoord : TEXCOORD1; // Texcoord relative to the complete custom texture
  22. uint primitiveID : TEXCOORD2; // Index of the update zone (correspond to the index in the updateZones of the Custom Texture)
  23. float3 direction : TEXCOORD3; // For cube textures, direction of the pixel being rendered in the cubemap
  24. };
  25. float2 CustomRenderTextureRotate2D(float2 pos, float angle)
  26. {
  27. float sn = sin(angle);
  28. float cs = cos(angle);
  29. return float2( pos.x * cs - pos.y * sn, pos.x * sn + pos.y * cs);
  30. }
  31. // Built-in unity functions and matrices:
  32. float4 _Time, _SinTime, _CosTime, unity_DeltaTime;
  33. // ================================
  34. // PER FRAME CONSTANTS
  35. // ================================
  36. #if defined(USING_STEREO_MATRICES)
  37. float4x4 unity_StereoMatrixP[2];
  38. float4x4 unity_StereoMatrixV[2];
  39. float4x4 unity_StereoMatrixInvV[2];
  40. float4x4 unity_StereoMatrixVP[2];
  41. float4x4 unity_StereoCameraProjection[2];
  42. float4x4 unity_StereoCameraInvProjection[2];
  43. float4x4 unity_StereoWorldToCamera[2];
  44. float4x4 unity_StereoCameraToWorld[2];
  45. #define glstate_matrix_projection unity_StereoMatrixP[unity_StereoEyeIndex]
  46. #define unity_MatrixV unity_StereoMatrixV[unity_StereoEyeIndex]
  47. #define unity_MatrixInvV unity_StereoMatrixInvV[unity_StereoEyeIndex]
  48. #define unity_MatrixVP unity_StereoMatrixVP[unity_StereoEyeIndex]
  49. #define unity_CameraProjection unity_StereoCameraProjection[unity_StereoEyeIndex]
  50. #define unity_CameraInvProjection unity_StereoCameraInvProjection[unity_StereoEyeIndex]
  51. #define unity_WorldToCamera unity_StereoWorldToCamera[unity_StereoEyeIndex]
  52. #define unity_CameraToWorld unity_StereoCameraToWorld[unity_StereoEyeIndex]
  53. #else
  54. float4x4 glstate_matrix_projection;
  55. float4x4 unity_MatrixV;
  56. float4x4 unity_MatrixInvV;
  57. float4x4 unity_MatrixVP;
  58. float4x4 unity_ObjectToWorld;
  59. float4 unity_StereoScaleOffset;
  60. #endif
  61. // Internal
  62. float4 CustomRenderTextureCenters[kCustomTextureBatchSize];
  63. float4 CustomRenderTextureSizesAndRotations[kCustomTextureBatchSize];
  64. float CustomRenderTexturePrimitiveIDs[kCustomTextureBatchSize];
  65. float4 CustomRenderTextureParameters;
  66. #define CustomRenderTextureUpdateSpace CustomRenderTextureParameters.x // Normalized(0)/PixelSpace(1)
  67. #define CustomRenderTexture3DTexcoordW CustomRenderTextureParameters.y
  68. #define CustomRenderTextureIs3D (CustomRenderTextureParameters.z == CRT_DIMENSION_3D)
  69. #define CustomRenderTextureDimension CustomRenderTextureParameters.z
  70. // User facing uniform variables
  71. float4 _CustomRenderTextureInfo; // x = width, y = height, z = depth, w = face/3DSlice
  72. // Helpers
  73. #define _CustomRenderTextureWidth _CustomRenderTextureInfo.x
  74. #define _CustomRenderTextureHeight _CustomRenderTextureInfo.y
  75. #define _CustomRenderTextureDepth _CustomRenderTextureInfo.z
  76. // Those two are mutually exclusive so we can use the same slot
  77. #define _CustomRenderTextureCubeFace _CustomRenderTextureInfo.w
  78. #define _CustomRenderTexture3DSlice _CustomRenderTextureInfo.w
  79. float _CustomRenderTextureMipLevel;
  80. TEXTURE2D(_SelfTexture2D);
  81. float4 _SelfTexture2D_TexelSize;
  82. SAMPLER(sampler_SelfTexture2D);
  83. TEXTURECUBE(_SelfTextureCube);
  84. float4 _SelfTextureCube_TexelSize;
  85. SAMPLER(sampler_SelfTextureCube);
  86. TEXTURE3D(_SelfTexture3D);
  87. float4 _SelfTexture3D_TexelSize;
  88. SAMPLER(sampler_SelfTexture3D);
  89. float3 ComputeCubemapDirectionFromUV(float2 uv, int cubeFace)
  90. {
  91. float2 xy = uv * 2.0 - 1.0;
  92. float3 direction;
  93. if(cubeFace == 0.0)
  94. {
  95. direction = normalize(float3(1.0, -xy.y, -xy.x));
  96. }
  97. else if(cubeFace == 1.0)
  98. {
  99. direction = normalize(float3(-1.0, -xy.y, xy.x));
  100. }
  101. else if(cubeFace == 2.0)
  102. {
  103. direction = normalize(float3(xy.x, 1.0, xy.y));
  104. }
  105. else if(cubeFace == 3.0)
  106. {
  107. direction = normalize(float3(xy.x, -1.0, -xy.y));
  108. }
  109. else if(cubeFace == 4.0)
  110. {
  111. direction = normalize(float3(xy.x, -xy.y, 1.0));
  112. }
  113. else if(cubeFace == 5.0)
  114. {
  115. direction = normalize(float3(-xy.x, -xy.y, -1.0));
  116. }
  117. else
  118. {
  119. direction = float3(0, 0, 0);
  120. }
  121. return direction;
  122. }
  123. float3 CustomRenderTextureComputeCubeDirection(float2 globalTexcoord)
  124. {
  125. return ComputeCubemapDirectionFromUV(globalTexcoord, _CustomRenderTextureCubeFace);
  126. }
  127. // standard custom texture vertex shader that should always be used
  128. v2f_customrendertexture CustomRenderTextureVertexShader(appdata_customrendertexture IN)
  129. {
  130. v2f_customrendertexture OUT;
  131. #if UNITY_UV_STARTS_AT_TOP
  132. const float2 vertexPositions[6] =
  133. {
  134. { -1.0f, 1.0f },
  135. { -1.0f, -1.0f },
  136. { 1.0f, -1.0f },
  137. { 1.0f, 1.0f },
  138. { -1.0f, 1.0f },
  139. { 1.0f, -1.0f }
  140. };
  141. const float2 texCoords[6] =
  142. {
  143. { 0.0f, 0.0f },
  144. { 0.0f, 1.0f },
  145. { 1.0f, 1.0f },
  146. { 1.0f, 0.0f },
  147. { 0.0f, 0.0f },
  148. { 1.0f, 1.0f }
  149. };
  150. #else
  151. const float2 vertexPositions[6] =
  152. {
  153. { 1.0f, 1.0f },
  154. { -1.0f, -1.0f },
  155. { -1.0f, 1.0f },
  156. { -1.0f, -1.0f },
  157. { 1.0f, 1.0f },
  158. { 1.0f, -1.0f }
  159. };
  160. const float2 texCoords[6] =
  161. {
  162. { 1.0f, 1.0f },
  163. { 0.0f, 0.0f },
  164. { 0.0f, 1.0f },
  165. { 0.0f, 0.0f },
  166. { 1.0f, 1.0f },
  167. { 1.0f, 0.0f }
  168. };
  169. #endif
  170. uint primitiveID = IN.vertexID / 6;
  171. uint vertexID = IN.vertexID % 6;
  172. float3 updateZoneCenter = CustomRenderTextureCenters[primitiveID].xyz;
  173. float3 updateZoneSize = CustomRenderTextureSizesAndRotations[primitiveID].xyz;
  174. float rotation = CustomRenderTextureSizesAndRotations[primitiveID].w * UNITY_PI / 180.0f;
  175. #if !UNITY_UV_STARTS_AT_TOP
  176. rotation = -rotation;
  177. #endif
  178. // Normalize rect if needed
  179. if (CustomRenderTextureUpdateSpace > 0.0) // Pixel space
  180. {
  181. // Normalize xy because we need it in clip space.
  182. updateZoneCenter.xy /= _CustomRenderTextureInfo.xy;
  183. updateZoneSize.xy /= _CustomRenderTextureInfo.xy;
  184. }
  185. else // normalized space
  186. {
  187. // Un-normalize depth because we need actual slice index for culling
  188. updateZoneCenter.z *= _CustomRenderTextureInfo.z;
  189. updateZoneSize.z *= _CustomRenderTextureInfo.z;
  190. }
  191. // Compute rotation
  192. // Compute quad vertex position
  193. float2 clipSpaceCenter = updateZoneCenter.xy * 2.0 - 1.0;
  194. float2 pos = vertexPositions[vertexID] * updateZoneSize.xy;
  195. pos = CustomRenderTextureRotate2D(pos, rotation);
  196. pos.x += clipSpaceCenter.x;
  197. #if UNITY_UV_STARTS_AT_TOP
  198. pos.y += clipSpaceCenter.y;
  199. #else
  200. pos.y -= clipSpaceCenter.y;
  201. #endif
  202. // For 3D texture, cull quads outside of the update zone
  203. // This is neeeded in additional to the preliminary minSlice/maxSlice done on the CPU because update zones can be disjointed.
  204. // ie: slices [1..5] and [10..15] for two differents zones so we need to cull out slices 0 and [6..9]
  205. if (CustomRenderTextureIs3D)
  206. {
  207. int minSlice = (int)(updateZoneCenter.z - updateZoneSize.z * 0.5);
  208. int maxSlice = minSlice + (int)updateZoneSize.z;
  209. if (_CustomRenderTexture3DSlice < minSlice || _CustomRenderTexture3DSlice >= maxSlice)
  210. {
  211. pos.xy = float2(1000.0, 1000.0); // Vertex outside of ncs
  212. }
  213. }
  214. OUT.vertex = float4(pos, 0.0, 1.0);
  215. OUT.primitiveID = asuint(CustomRenderTexturePrimitiveIDs[primitiveID]);
  216. OUT.localTexcoord = float3(texCoords[vertexID], CustomRenderTexture3DTexcoordW);
  217. OUT.globalTexcoord = float3(pos.xy * 0.5 + 0.5, CustomRenderTexture3DTexcoordW);
  218. #if UNITY_UV_STARTS_AT_TOP
  219. OUT.globalTexcoord.y = 1.0 - OUT.globalTexcoord.y;
  220. #endif
  221. OUT.direction = CustomRenderTextureComputeCubeDirection(OUT.globalTexcoord.xy);
  222. return OUT;
  223. }
  224. struct appdata_init_customrendertexture
  225. {
  226. float4 vertex : POSITION;
  227. float2 texcoord : TEXCOORD0;
  228. };
  229. // User facing vertex to fragment structure for initialization materials
  230. struct v2f_init_customrendertexture
  231. {
  232. float4 vertex : SV_POSITION;
  233. float3 texcoord : TEXCOORD0;
  234. float3 direction : TEXCOORD1;
  235. };
  236. // standard custom texture vertex shader that should always be used for initialization shaders
  237. v2f_init_customrendertexture InitCustomRenderTextureVertexShader (appdata_init_customrendertexture v)
  238. {
  239. v2f_init_customrendertexture o;
  240. o.vertex = v.vertex;
  241. o.texcoord = float3(v.texcoord.xy, CustomRenderTexture3DTexcoordW);
  242. o.direction = CustomRenderTextureComputeCubeDirection(v.texcoord.xy);
  243. return o;
  244. }
  245. #endif // UNITY_CUSTOM_TEXTURE_INCLUDED