diff --git a/projects/voxelization/resources/shaders/shader.frag b/projects/voxelization/resources/shaders/shader.frag index ff8968ca05d090703236c70bd2fef2f9f9784dde..7612025a0f5853f632e120a3ab2f442528541b2c 100644 --- a/projects/voxelization/resources/shaders/shader.frag +++ b/projects/voxelization/resources/shaders/shader.frag @@ -32,6 +32,13 @@ layout(set=0, binding=6) uniform VoxelInfoBuffer{ VoxelInfo voxelInfo; }; +layout(set=0, binding=7) uniform VolumetricSettings { + vec3 scatteringCoefficient; + float volumetricAmbientLight; + vec3 absorptionCoefficient; +}; + + vec3 cookTorrance(vec3 f0, float r, vec3 N, vec3 V, vec3 L){ vec3 H = normalize(L + V); @@ -78,10 +85,7 @@ vec3 volumetricLighting(vec3 colorIn, vec3 V, vec3 pos, float d){ int sampleCount = 48; float stepSize = d / sampleCount; - vec3 scatteringCoefficient = vec3(0.005); - vec3 absorptionCoefficient = vec3(0.01); vec3 extinctionCoefficient = scatteringCoefficient + absorptionCoefficient; - vec3 ambientLight = vec3(0.2); float noiseScale = 0.1; pos += V * noiseScale * interleavedGradientNoise(gl_FragCoord.xy); @@ -92,7 +96,7 @@ vec3 volumetricLighting(vec3 colorIn, vec3 V, vec3 pos, float d){ vec3 light = lightInfo.sunColor * lightInfo.sunStrength; float shadow = shadowTest(samplePoint, lightInfo, shadowMap, shadowMapSampler, vec2(0)); light *= shadow; - light += ambientLight; + light += volumetricAmbientLight; color += phase * light * scatteringCoefficient * stepSize; color *= exp(-stepSize * extinctionCoefficient); diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp index e67098693249c5070dde7f003f1810988269cf1c..fcdb4f2b60030e2822442d47be0d38ce458d5f38 100644 --- a/projects/voxelization/src/main.cpp +++ b/projects/voxelization/src/main.cpp @@ -444,12 +444,21 @@ int main(int argc, const char** argv) { vkcv::Buffer<glm::vec3> cameraPosBuffer = core.createBuffer<glm::vec3>(vkcv::BufferType::UNIFORM, 1); + struct VolumetricSettings { + glm::vec3 scatteringCoefficient; + float ambientLight; + glm::vec3 absorptionCoefficient; + }; + vkcv::Buffer<VolumetricSettings> volumetricSettingsBuffer + = core.createBuffer<VolumetricSettings>(vkcv::BufferType::UNIFORM ,1); + // write forward pass descriptor set vkcv::DescriptorWrites forwardDescriptorWrites; forwardDescriptorWrites.uniformBufferWrites = { vkcv::UniformBufferDescriptorWrite(0, shadowMapping.getLightInfoBuffer()), vkcv::UniformBufferDescriptorWrite(3, cameraPosBuffer.getHandle()), - vkcv::UniformBufferDescriptorWrite(6, voxelization.getVoxelInfoBufferHandle()) }; + vkcv::UniformBufferDescriptorWrite(6, voxelization.getVoxelInfoBufferHandle()), + vkcv::UniformBufferDescriptorWrite(7, volumetricSettingsBuffer.getHandle())}; forwardDescriptorWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(1, shadowMapping.getShadowMap()), vkcv::SampledImageDescriptorWrite(4, voxelization.getVoxelImageHandle()) }; @@ -470,6 +479,12 @@ int main(int argc, const char** argv) { bool msaaCustomResolve = true; + glm::vec3 scatteringColor = glm::vec3(1); + float scatteringDensity = 0.001; + glm::vec3 absorptionColor = glm::vec3(1); + float absorptionDensity = 0.001; + float volumetricAmbient = 0.2; + auto start = std::chrono::system_clock::now(); const auto appStartTime = start; while (window.isWindowOpen()) { @@ -567,6 +582,12 @@ int main(int argc, const char** argv) { mainPassMatrices.push_back({ viewProjectionCamera * m, m }); } + VolumetricSettings volumeSettings; + volumeSettings.scatteringCoefficient = scatteringColor * scatteringDensity; + volumeSettings.absorptionCoefficient = absorptionColor * absorptionDensity; + volumeSettings.ambientLight = volumetricAmbient; + volumetricSettingsBuffer.fill({ volumeSettings }); + const vkcv::PushConstantData pushConstantData((void*)mainPassMatrices.data(), 2 * sizeof(glm::mat4)); const std::vector<vkcv::ImageHandle> renderTargets = { colorBuffer, depthBuffer }; @@ -655,6 +676,12 @@ int main(int argc, const char** argv) { voxelizationExtent = std::max(voxelizationExtent, 1.f); voxelVisualisationMip = std::max(voxelVisualisationMip, 0); + ImGui::ColorEdit3("Scattering color", &scatteringColor.x); + ImGui::DragFloat("Scattering density", &scatteringDensity, 0.0001); + ImGui::ColorEdit3("Absorption color", &absorptionColor.x); + ImGui::DragFloat("Absorption density", &absorptionDensity, 0.0001); + ImGui::DragFloat("Volumetric ambient", &volumetricAmbient, 0.002); + if (ImGui::Button("Reload forward pass")) { vkcv::ShaderProgram newForwardProgram;