| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "Common.hlsl"
- #define WORLD_HEIGHT 500
- #define CHUNK_SIZE 16
- #define EPSILON 1e-6f
- cbuffer ChunkShaderInfo : register(b0, space2)
- {
- int2 pos;
- unsigned int blockCount;
- };
- StructuredBuffer<int> textureIdBuffer : register(t1, space2);
- StructuredBuffer<int> indexBuffer : register(t1, space3);
- #define minPos(x, y) min(x>0?x:y,y>0?y:x)
- #define minPosV(v1, v2) float3(minPos(v1.x, v2.x), minPos(v1.y, v2.y), minPos(v1.z, v2.z))
- #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)
- float GetEntry(float3 minp, float3 maxp, float3 invDir, float3 origin)
- {
- if (between(origin, minp, maxp))
- {
- return 0;
- }
- float3 t1 = (minp - origin) * invDir;
- float3 t2 = (maxp - origin) * invDir;
- float3 tmin = min(t1, t2);
- return max(max(tmin.x, tmin.y), tmin.z);
- }
- [shader("intersection")]
- void ChunkIntersection()
- {
- float THit = RayTCurrent();
- Attributes intersectionAttributes;
- intersectionAttributes.texCoord = float2(0.5, 0.5);
- intersectionAttributes.textureId = 2;
- float3 origin = WorldRayOrigin();
- float3 direction = WorldRayDirection();
- float minDistance = RayTMin();
- float3 invDirection = 1.0 / direction;
- float3 chunkMin = float3(pos.x - CHUNK_SIZE / 2, pos.y - CHUNK_SIZE / 2, 0);
- float3 chunkMax = float3(pos.x + CHUNK_SIZE / 2, pos.y + CHUNK_SIZE / 2, WORLD_HEIGHT);
- float distance = 0;
- float3 step = sign(direction);
- int hitSide = -1;
- float3 currentPos = origin;
- if (!between(origin, chunkMin, chunkMax))
- {
- float3 t1 = (chunkMin - origin) * invDirection;
- float3 t2 = (chunkMax - origin) * invDirection;
- float3 tmin = min(t1, t2);
- if (tmin.x > tmin.y && tmin.x > tmin.z)
- {
- hitSide = step.x > 0 ? 3 : 2;
- distance = tmin.x;
- }
- else if (tmin.y > tmin.z)
- {
- hitSide = step.y > 0 ? 0 : 1;
- distance = tmin.y;
- }
- else
- {
- hitSide = step.z > 0 ? 5 : 4;
- distance = tmin.z;
- }
- currentPos = origin + direction * distance;
- }
- float3 chunkPos = currentPos - chunkMin;
- float3 sum = step + currentPos;
- 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));
- int3 block = floor(chunkPos);
- int3 blockMin = int3(0, 0, 0);
- int3 blockMax = int3(CHUNK_SIZE - 1, CHUNK_SIZE - 1, WORLD_HEIGHT - 1);
- switch (hitSide)
- {
- case 0: // front
- block.y = blockMin.y;
- nextBorder.y = blockMin.y + 1 + chunkMin.y;
- break;
- case 1: // back
- block.y = blockMax.y;
- nextBorder.y = blockMax.y + chunkMin.y;
- break;
- case 2: // left
- block.x = blockMax.x;
- nextBorder.x = blockMax.x + chunkMin.x;
- break;
- case 3: // right
- block.x = blockMin.x;
- nextBorder.x = blockMin.x + 1 + chunkMin.x;
- break;
- case 4: // top
- block.z = blockMax.z;
- nextBorder.z = blockMax.z + chunkMin.z;
- break;
- case 5: // bottom
- block.z = blockMin.z;
- nextBorder.z = blockMin.z + 1 + chunkMin.z;
- break;
- }
- while (between(block, blockMin, blockMax) && distance < THit)
- {
- int index = (block.x * CHUNK_SIZE + block.y) * WORLD_HEIGHT + block.z;
- if (indexBuffer[index] >= 0 && distance > minDistance && hitSide >= 0)
- {
- switch (hitSide)
- {
- case 0: // front
- intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.z - floor(currentPos.z)));
- break;
- case 1: // back
- intersectionAttributes.texCoord = float2(currentPos.x - floor(currentPos.x), 1 - (currentPos.z - floor(currentPos.z)));
- break;
- case 2: // left
- intersectionAttributes.texCoord = float2(1 - (currentPos.y - floor(currentPos.y)), 1 - (currentPos.z - floor(currentPos.z)));
- break;
- case 3: // right
- intersectionAttributes.texCoord = float2(currentPos.y - floor(currentPos.y), 1 - (currentPos.z - floor(currentPos.z)));
- break;
- case 4: // top
- intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), 1 - (currentPos.y - floor(currentPos.y)));
- break;
- case 5: // bottom
- intersectionAttributes.texCoord = float2(1 - (currentPos.x - floor(currentPos.x)), currentPos.y - floor(currentPos.y));
- break;
- }
- intersectionAttributes.textureId = textureIdBuffer[indexBuffer[index] + hitSide];
- ReportHit(distance, hitSide, intersectionAttributes);
- if (RayTCurrent() < THit)
- {
- return; // since we go in direction of the ray we will not find a nearer hit in this intersection shader
- }
- }
- float3 stepDist = (nextBorder - origin) * invDirection;
- if (stepDist.x < stepDist.y && stepDist.x < stepDist.z)
- {
- hitSide = step.x > 0 ? 3 : 2;
- block.x += step.x;
- nextBorder.x += step.x;
- distance = stepDist.x;
- }
- else if (stepDist.y < stepDist.z)
- {
- hitSide = step.y > 0 ? 0 : 1;
- block.y += step.y;
- nextBorder.y += step.y;
- distance = stepDist.y;
- }
- else
- {
- hitSide = step.z > 0 ? 5 : 4;
- block.z += step.z;
- nextBorder.z += step.z;
- distance = stepDist.z;
- }
- currentPos = origin + direction * distance;
- }
- }
|