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
|
float3 SampleSHVoxel( in float4 worldPos, in float3 albedo, in float3 normal, in StructuredBuffer<int> _coefficientVoxel, in float _coefficientVoxelGridSize, in float4 _coefficientVoxelCorner, in float4 _coefficientVoxelSize ) { int3 probeIndex3 = GetProbeIndex3DFromWorldPos(worldPos, _coefficientVoxelSize, _coefficientVoxelGridSize, _coefficientVoxelCorner); int3 offset[8] = { int3(0, 0, 0), int3(0, 0, 1), int3(0, 1, 0), int3(0, 1, 1), int3(1, 0, 0), int3(1, 0, 1), int3(1, 1, 0), int3(1, 1, 1), };
float3 c[9]; float3 Lo[8] = { float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), float3(0, 0, 0), }; float3 BRDF = albedo / PI; float weight = 0.0005;
for (int i = 0; i < 8; i++) { int3 idx3 = probeIndex3 + offset[i]; bool isInsideVoxel = IsIndex3DInsideVoxel(idx3, _coefficientVoxelSize); if (!isInsideVoxel) { Lo[i] = float3(0, 0, 0); continue; }
float3 probePos = GetProbePositionFromIndex3D(idx3, _coefficientVoxelGridSize, _coefficientVoxelCorner); float3 dir = normalize(probePos - worldPos.xyz); float normalWeight = saturate(dot(dir, normal)); weight += normalWeight;
int probeIndex = GetProbeIndex1DFromIndex3D(idx3, _coefficientVoxelSize); DecodeSHCoefficientFromVoxel(c, _coefficientVoxel, probeIndex); Lo[i] = IrradianceSH9(c, normal) * BRDF * normalWeight; }
float3 minCorner = GetProbePositionFromIndex3D(probeIndex3, _coefficientVoxelGridSize, _coefficientVoxelCorner); float3 maxCorner = minCorner + float3(1, 1, 1) * _coefficientVoxelGridSize; float3 rate = (worldPos - minCorner) / _coefficientVoxelGridSize; float3 color = TrilinearInterpolationFloat3(Lo, rate) / weight;
return color; }
float3 history = SampleSHVoxel( float4(surfel.position, 1.0), surfel.albedo, surfel.normal, _lastFrameCoefficientVoxel, _coefficientVoxelGridSize, _coefficientVoxelCorner, _coefficientVoxelSize ); radiance += history * _giIntensity;
|