ChunkIntersection.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "Common.hlsl"
  2. #define WORLD_HEIGHT 500
  3. #define CHUNK_SIZE 16
  4. #define EPSILON 1e-6f
  5. cbuffer ChunkShaderInfo : register(b0, space2)
  6. {
  7. int2 pos;
  8. unsigned int blockCount;
  9. };
  10. StructuredBuffer<int> textureIdBuffer : register(t1, space2);
  11. StructuredBuffer<int> indexBuffer : register(t1, space3);
  12. ByteAddressBuffer lightBuffer : register(t1, space4);
  13. #define minPos(x, y) min(x>0?x:y,y>0?y:x)
  14. #define minPosV(v1, v2) float3(minPos(v1.x, v2.x), minPos(v1.y, v2.y), minPos(v1.z, v2.z))
  15. #define between(v1, vmin, vmax) (v1.x >= vmin.x && v1.x <= vmax.x && v1.y >= vmin.y && v1.y <= vmax.y && v1.z >= vmin.z && v1.z <= vmax.z)
  16. float3 getLight(int index)
  17. {
  18. uint raw = lightBuffer.Load((index / 4) * 4);
  19. uint shift = (index % 4) * 8;
  20. float r = ((raw >> shift) & 0xFF) / 255.0;
  21. shift += 8;
  22. if (shift == 32)
  23. {
  24. raw = lightBuffer.Load((index / 4) * 4 + 4);
  25. shift = 0;
  26. }
  27. float g = ((raw >> shift) & 0xFF) / 255.0;
  28. shift += 8;
  29. if (shift == 32)
  30. {
  31. raw = lightBuffer.Load((index / 4) * 4 + 4);
  32. shift = 0;
  33. }
  34. float b = ((raw >> shift) & 0xFF) / 255.0;
  35. return float3(r, g, b);
  36. }
  37. float3 interpolateLight(float3 topLeft, float3 topRight, float3 bottomLeft, float3 bottomRight, float2 coords)
  38. {
  39. return (topLeft * (1 - coords.x) + topRight * coords.x) * (1 - coords.y) + (bottomLeft * (1 - coords.x) + bottomRight * coords.x) * coords.y;
  40. }
  41. [shader("intersection")]
  42. void ChunkIntersection()
  43. {
  44. float THit = RayTCurrent();
  45. ChunkHitAttributes intersectionAttributes;
  46. intersectionAttributes.texCoord = float2(0.5, 0.5);
  47. intersectionAttributes.textureId = 2;
  48. float3 origin = WorldRayOrigin();
  49. float3 direction = WorldRayDirection();
  50. float minDistance = RayTMin();
  51. float3 invDirection = 1.0 / direction;
  52. float3 chunkMin = float3(pos.x - CHUNK_SIZE / 2, pos.y - CHUNK_SIZE / 2, 0);
  53. float3 chunkMax = float3(pos.x + CHUNK_SIZE / 2, pos.y + CHUNK_SIZE / 2, WORLD_HEIGHT);
  54. float distance = 0;
  55. float3 step = sign(direction);
  56. int hitSide = -1;
  57. float3 currentPos = origin;
  58. if (!between(origin, chunkMin, chunkMax))
  59. {
  60. float3 t1 = (chunkMin - origin) * invDirection;
  61. float3 t2 = (chunkMax - origin) * invDirection;
  62. float3 tmin = min(t1, t2);
  63. if (tmin.x > tmin.y && tmin.x > tmin.z)
  64. {
  65. hitSide = step.x > 0 ? 3 : 2;
  66. distance = tmin.x;
  67. }
  68. else if (tmin.y > tmin.z)
  69. {
  70. hitSide = step.y > 0 ? 0 : 1;
  71. distance = tmin.y;
  72. }
  73. else
  74. {
  75. hitSide = step.z > 0 ? 5 : 4;
  76. distance = tmin.z;
  77. }
  78. currentPos = origin + direction * distance;
  79. }
  80. float3 chunkPos = currentPos - chunkMin;
  81. float3 sum = step + currentPos;
  82. float3 nextBorder = float3(step.x > 0 ? floor(sum.x) : ceil(sum.x), step.y > 0 ? floor(sum.y) : ceil(sum.y), step.z > 0 ? floor(sum.z) : ceil(sum.z));
  83. int3 block = floor(chunkPos);
  84. int3 blockMin = int3(0, 0, 0);
  85. int3 blockMax = int3(CHUNK_SIZE - 1, CHUNK_SIZE - 1, WORLD_HEIGHT - 1);
  86. switch (hitSide)
  87. {
  88. case 0: // front | NORTH
  89. block.y = blockMin.y;
  90. nextBorder.y = blockMin.y + 1 + chunkMin.y;
  91. break;
  92. case 1: // back | SOUTH
  93. block.y = blockMax.y;
  94. nextBorder.y = blockMax.y + chunkMin.y;
  95. break;
  96. case 2: // left | EAST
  97. block.x = blockMax.x;
  98. nextBorder.x = blockMax.x + chunkMin.x;
  99. break;
  100. case 3: // right | WEST
  101. block.x = blockMin.x;
  102. nextBorder.x = blockMin.x + 1 + chunkMin.x;
  103. break;
  104. case 4: // top
  105. block.z = blockMax.z;
  106. nextBorder.z = blockMax.z + chunkMin.z;
  107. break;
  108. case 5: // bottom
  109. block.z = blockMin.z;
  110. nextBorder.z = blockMin.z + 1 + chunkMin.z;
  111. break;
  112. }
  113. while (between(block, blockMin, blockMax) && distance < THit)
  114. {
  115. int index = (block.x * CHUNK_SIZE + block.y) * WORLD_HEIGHT + block.z;
  116. if (indexBuffer[index] >= 0 && distance > minDistance && hitSide >= 0)
  117. {
  118. int lightOffset = indexBuffer[index] * 8;
  119. float3 topLeftDayLight;
  120. float3 topRightDayLight;
  121. float3 bottomLeftDayLight;
  122. float3 bottomRightDayLight;
  123. float3 topLeftDynamicLight;
  124. float3 topRightDynamicLight;
  125. float3 bottomLeftDynamicLight;
  126. float3 bottomRightDynamicLight;
  127. /**
  128. Light indices are stored in the following order:
  129. lightOffset = TOP | NORTH | EAST
  130. lightOffset + 6 = TOP | SOUTH | EAST
  131. lightOffset + 12 = TOP | SOUTH | WEST
  132. lightOffset + 18 = TOP | NORTH | WEST
  133. lightOffset + 24 = BOTTOM | NORTH | EAST
  134. lightOffset + 30 = BOTTOM | SOUTH | EAST
  135. lightOffset + 36 = BOTTOM | SOUTH | WEST
  136. lightOffset + 42 = BOTTOM | NORTH | WEST
  137. **/
  138. switch (hitSide)
  139. {
  140. case 0: // front | NORTH
  141. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.z - floor(currentPos.z)));
  142. topLeftDayLight = getLight(lightOffset + 0);
  143. topLeftDynamicLight = getLight(lightOffset + 3);
  144. topRightDayLight = getLight(lightOffset + 18);
  145. topRightDynamicLight = getLight(lightOffset + 21);
  146. bottomLeftDayLight = getLight(lightOffset + 24);
  147. bottomLeftDynamicLight = getLight(lightOffset + 27);
  148. bottomRightDayLight = getLight(lightOffset + 42);
  149. bottomRightDynamicLight = getLight(lightOffset + 45);
  150. break;
  151. case 1: // back | SOUTH
  152. intersectionAttributes.texCoord = float2(currentPos.x - floor(currentPos.x), 1 - (currentPos.z - floor(currentPos.z)));
  153. topLeftDayLight = getLight(lightOffset + 12);
  154. topLeftDynamicLight = getLight(lightOffset + 15);
  155. topRightDayLight = getLight(lightOffset + 6);
  156. topRightDynamicLight = getLight(lightOffset + 9);
  157. bottomLeftDayLight = getLight(lightOffset + 36);
  158. bottomLeftDynamicLight = getLight(lightOffset + 39);
  159. bottomRightDayLight = getLight(lightOffset + 30);
  160. bottomRightDynamicLight = getLight(lightOffset + 33);
  161. break;
  162. case 2: // left | EAST
  163. intersectionAttributes.texCoord = float2(1 - (currentPos.y - floor(currentPos.y)), 1 - (currentPos.z - floor(currentPos.z)));
  164. topLeftDayLight = getLight(lightOffset + 6);
  165. topLeftDynamicLight = getLight(lightOffset + 9);
  166. topRightDayLight = getLight(lightOffset + 0);
  167. topRightDynamicLight = getLight(lightOffset + 3);
  168. bottomLeftDayLight = getLight(lightOffset + 30);
  169. bottomLeftDynamicLight = getLight(lightOffset + 33);
  170. bottomRightDayLight = getLight(lightOffset + 24);
  171. bottomRightDynamicLight = getLight(lightOffset + 27);
  172. break;
  173. case 3: // right | WEST
  174. intersectionAttributes.texCoord = float2(currentPos.y - floor(currentPos.y), 1 - (currentPos.z - floor(currentPos.z)));
  175. topLeftDayLight = getLight(lightOffset + 18);
  176. topLeftDynamicLight = getLight(lightOffset + 21);
  177. topRightDayLight = getLight(lightOffset + 12);
  178. topRightDynamicLight = getLight(lightOffset + 15);
  179. bottomLeftDayLight = getLight(lightOffset + 42);
  180. bottomLeftDynamicLight = getLight(lightOffset + 45);
  181. bottomRightDayLight = getLight(lightOffset + 36);
  182. bottomRightDynamicLight = getLight(lightOffset + 39);
  183. break;
  184. case 4: // top
  185. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.y - floor(currentPos.y)));
  186. topLeftDayLight = getLight(lightOffset + 6);
  187. topLeftDynamicLight = getLight(lightOffset + 9);
  188. topRightDayLight = getLight(lightOffset + 12);
  189. topRightDynamicLight = getLight(lightOffset + 15);
  190. bottomLeftDayLight = getLight(lightOffset + 0);
  191. bottomLeftDynamicLight = getLight(lightOffset + 3);
  192. bottomRightDayLight = getLight(lightOffset + 18);
  193. bottomRightDynamicLight = getLight(lightOffset + 21);
  194. break;
  195. case 5: // bottom
  196. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), currentPos.y - floor(currentPos.y));
  197. topLeftDayLight = getLight(lightOffset + 24);
  198. topLeftDynamicLight = getLight(lightOffset + 27);
  199. topRightDayLight = getLight(lightOffset + 42);
  200. topRightDynamicLight = getLight(lightOffset + 45);
  201. bottomLeftDayLight = getLight(lightOffset + 30);
  202. bottomLeftDynamicLight = getLight(lightOffset + 33);
  203. bottomRightDayLight = getLight(lightOffset + 36);
  204. bottomRightDynamicLight = getLight(lightOffset + 39);
  205. break;
  206. }
  207. intersectionAttributes.dayLight = packLight(interpolateLight(topLeftDayLight, topRightDayLight, bottomLeftDayLight, bottomRightDayLight, intersectionAttributes.texCoord));
  208. intersectionAttributes.dynamicLight = packLight(interpolateLight(topLeftDynamicLight, topRightDynamicLight, bottomLeftDynamicLight, bottomRightDynamicLight, intersectionAttributes.texCoord));
  209. intersectionAttributes.textureId = textureIdBuffer[indexBuffer[index] + hitSide];
  210. ReportHit(distance, hitSide, intersectionAttributes);
  211. if (RayTCurrent() < THit)
  212. {
  213. return; // since we go in direction of the ray we will not find a nearer hit in this intersection shader
  214. }
  215. }
  216. float3 stepDist = (nextBorder - origin) * invDirection;
  217. if (stepDist.x < stepDist.y && stepDist.x < stepDist.z)
  218. {
  219. hitSide = step.x > 0 ? 3 : 2;
  220. block.x += step.x;
  221. nextBorder.x += step.x;
  222. distance = stepDist.x;
  223. }
  224. else if (stepDist.y < stepDist.z)
  225. {
  226. hitSide = step.y > 0 ? 0 : 1;
  227. block.y += step.y;
  228. nextBorder.y += step.y;
  229. distance = stepDist.y;
  230. }
  231. else
  232. {
  233. hitSide = step.z > 0 ? 5 : 4;
  234. block.z += step.z;
  235. nextBorder.z += step.z;
  236. distance = stepDist.z;
  237. }
  238. currentPos = origin + direction * distance;
  239. }
  240. }