ChunkIntersection.hlsl 11 KB

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