-
Notifications
You must be signed in to change notification settings - Fork 20
/
Visibility.compute
100 lines (78 loc) · 2.67 KB
/
Visibility.compute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma kernel ChunkRender
#define THREADS_CHUNK_RENDER 64
float4 viewFrustumPlanes[6];
float4 camPos;
float maxViewDistance;
float depthBias;
float4x4 vpMatrix;
struct Chunk {
float3 position;
int instanceStartIndex;
int instanceCount;
};
int instanceCount;
int chunkSize;
int numChunks;
RWStructuredBuffer<Chunk> chunkBuffer;
RWStructuredBuffer<float4x4> trsBuffer;
AppendStructuredBuffer<float4x4> visibleList;
Texture2D<float4> _DepthTexture;
SamplerState sampler_DepthTexture;
float planeDistance(float4 plane, float3 p) {
return dot(plane.xyz, p) + plane.w;
}
bool isInFrontOfPlane(float4 plane, float3 p) {
return planeDistance(plane, p) > 0;
}
bool isChunkInFrustum(float3 chunkPosition) {
float halfSize = chunkSize * 0.5;
float3 corners[8] = {
chunkPosition + float3(halfSize, halfSize, halfSize),
chunkPosition + float3(-halfSize, halfSize, halfSize),
chunkPosition + float3(halfSize, halfSize, -halfSize),
chunkPosition + float3(-halfSize, halfSize, -halfSize),
chunkPosition + float3(halfSize, -halfSize, halfSize),
chunkPosition + float3(-halfSize, -halfSize, halfSize),
chunkPosition + float3(halfSize, -halfSize, -halfSize),
chunkPosition + float3(-halfSize, -halfSize, -halfSize)
};
for (int i = 0; i < 8; i++) {
bool isInside = true;
for (int j = 0; j < 6; j++) {
if (!isInFrontOfPlane(viewFrustumPlanes[j], corners[i])) {
isInside = false;
break;
}
}
if (isInside) {
return true;
}
}
return false;
}
bool isChunkOccluded(float3 chunkPosition) {
float4 projectedPos = mul(vpMatrix, float4(chunkPosition, 1.0f));
float2 projectedUV = (projectedPos.xy / projectedPos.w + 1.0f) * 0.5f;
float sceneDepth = _DepthTexture.SampleLevel(sampler_DepthTexture, projectedUV, 0).r;
float chunkDepth = (projectedPos.z / projectedPos.w);
return chunkDepth < sceneDepth - depthBias;
}
[numthreads(THREADS_CHUNK_RENDER,1,1)]
void ChunkRender (uint3 id : SV_DispatchThreadID)
{
Chunk chunk = chunkBuffer[id.x];
//Order is important here for performance
if (distance(camPos.xyz, chunk.position) > maxViewDistance ||
!isChunkInFrustum(chunk.position) ||
isChunkOccluded(chunk.position))
{
return;
}
int start = chunk.instanceStartIndex;
int end = start + chunk.instanceCount;
for (int i = start; i < end; i++) {
float4x4 instanceTransform = trsBuffer[i];
float3 instancePosition = mul(instanceTransform, float4(0, 0, 0, 1)).xyz;
visibleList.Append(instanceTransform);
}
}