ChunkIntersection.hlsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #define minPos(x, y) min(x>0?x:y,y>0?y:x)
  13. #define minPosV(v1, v2) float3(minPos(v1.x, v2.x), minPos(v1.y, v2.y), minPos(v1.z, v2.z))
  14. #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)
  15. float GetEntry(float3 minp, float3 maxp, float3 invDir, float3 origin)
  16. {
  17. if (between(origin, minp, maxp))
  18. {
  19. return 0;
  20. }
  21. float3 t1 = (minp - origin) * invDir;
  22. float3 t2 = (maxp - origin) * invDir;
  23. float3 tmin = min(t1, t2);
  24. return max(max(tmin.x, tmin.y), tmin.z);
  25. }
  26. [shader("intersection")]
  27. void ChunkIntersection()
  28. {
  29. float THit = RayTCurrent();
  30. Attributes intersectionAttributes;
  31. intersectionAttributes.texCoord = float2(0.5, 0.5);
  32. intersectionAttributes.textureId = 2;
  33. float3 origin = WorldRayOrigin();
  34. float3 direction = WorldRayDirection();
  35. float minDistance = RayTMin();
  36. float3 invDirection = 1.0 / direction;
  37. float3 chunkMin = float3(pos.x - CHUNK_SIZE / 2, pos.y - CHUNK_SIZE / 2, 0);
  38. float3 chunkMax = float3(pos.x + CHUNK_SIZE / 2, pos.y + CHUNK_SIZE / 2, WORLD_HEIGHT);
  39. float distance = 0;
  40. float3 step = sign(direction);
  41. int hitSide = -1;
  42. float3 currentPos = origin;
  43. if (!between(origin, chunkMin, chunkMax))
  44. {
  45. float3 t1 = (chunkMin - origin) * invDirection;
  46. float3 t2 = (chunkMax - origin) * invDirection;
  47. float3 tmin = min(t1, t2);
  48. if (tmin.x > tmin.y && tmin.x > tmin.z)
  49. {
  50. hitSide = step.x > 0 ? 3 : 2;
  51. distance = tmin.x;
  52. }
  53. else if (tmin.y > tmin.z)
  54. {
  55. hitSide = step.y > 0 ? 0 : 1;
  56. distance = tmin.y;
  57. }
  58. else
  59. {
  60. hitSide = step.z > 0 ? 5 : 4;
  61. distance = tmin.z;
  62. }
  63. currentPos = origin + direction * distance;
  64. }
  65. float3 chunkPos = currentPos - chunkMin;
  66. float3 sum = step + currentPos;
  67. 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));
  68. int3 block = floor(chunkPos);
  69. int3 blockMin = int3(0, 0, 0);
  70. int3 blockMax = int3(CHUNK_SIZE - 1, CHUNK_SIZE - 1, WORLD_HEIGHT - 1);
  71. switch (hitSide)
  72. {
  73. case 0: // front
  74. block.y = blockMin.y;
  75. nextBorder.y = blockMin.y + 1 + chunkMin.y;
  76. break;
  77. case 1: // back
  78. block.y = blockMax.y;
  79. nextBorder.y = blockMax.y + chunkMin.y;
  80. break;
  81. case 2: // left
  82. block.x = blockMax.x;
  83. nextBorder.x = blockMax.x + chunkMin.x;
  84. break;
  85. case 3: // right
  86. block.x = blockMin.x;
  87. nextBorder.x = blockMin.x + 1 + chunkMin.x;
  88. break;
  89. case 4: // top
  90. block.z = blockMax.z;
  91. nextBorder.z = blockMax.z + chunkMin.z;
  92. break;
  93. case 5: // bottom
  94. block.z = blockMin.z;
  95. nextBorder.z = blockMin.z + 1 + chunkMin.z;
  96. break;
  97. }
  98. while (between(block, blockMin, blockMax) && distance < THit)
  99. {
  100. int index = (block.x * CHUNK_SIZE + block.y) * WORLD_HEIGHT + block.z;
  101. if (indexBuffer[index] >= 0 && distance > minDistance && hitSide >= 0)
  102. {
  103. switch (hitSide)
  104. {
  105. case 0: // front
  106. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.z - floor(currentPos.z)));
  107. break;
  108. case 1: // back
  109. intersectionAttributes.texCoord = float2(currentPos.x - floor(currentPos.x), 1 - (currentPos.z - floor(currentPos.z)));
  110. break;
  111. case 2: // left
  112. intersectionAttributes.texCoord = float2(1 - (currentPos.y - floor(currentPos.y)), 1 - (currentPos.z - floor(currentPos.z)));
  113. break;
  114. case 3: // right
  115. intersectionAttributes.texCoord = float2(currentPos.y - floor(currentPos.y), 1 - (currentPos.z - floor(currentPos.z)));
  116. break;
  117. case 4: // top
  118. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.y - floor(currentPos.y)));
  119. break;
  120. case 5: // bottom
  121. intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), currentPos.y - floor(currentPos.y));
  122. break;
  123. }
  124. intersectionAttributes.textureId = textureIdBuffer[indexBuffer[index] + hitSide];
  125. ReportHit(distance, hitSide, intersectionAttributes);
  126. if (RayTCurrent() < THit)
  127. {
  128. return; // since we go in direction of the ray we will not find a nearer hit in this intersection shader
  129. }
  130. }
  131. float3 stepDist = (nextBorder - origin) * invDirection;
  132. if (stepDist.x < stepDist.y && stepDist.x < stepDist.z)
  133. {
  134. hitSide = step.x > 0 ? 3 : 2;
  135. block.x += step.x;
  136. nextBorder.x += step.x;
  137. distance = stepDist.x;
  138. }
  139. else if (stepDist.y < stepDist.z)
  140. {
  141. hitSide = step.y > 0 ? 0 : 1;
  142. block.y += step.y;
  143. nextBorder.y += step.y;
  144. distance = stepDist.y;
  145. }
  146. else
  147. {
  148. hitSide = step.z > 0 ? 5 : 4;
  149. block.z += step.z;
  150. nextBorder.z += step.z;
  151. distance = stepDist.z;
  152. }
  153. currentPos = origin + direction * distance;
  154. }
  155. }