Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add depth-of-field (Blurring rendering options) #1662

Merged
merged 12 commits into from
Oct 12, 2024
63 changes: 59 additions & 4 deletions avogadro/rendering/solid_first_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ uniform float offset;
uniform sampler2D inDepthTex;
// 1.0 if enabled, 0.0 if disabled
uniform float inAoEnabled;
// 1.0 if enabled, 0.0 if disabled
uniform float inDofEnabled;
// 0.0 if disabled
uniform float inFogStrength;
// Shadow strength for SSAO
uniform float inAoStrength;
// 1.0 if enabled, 0.0 if disabled
uniform float inEdStrength;
// offset for zoom-in and zoom-out
// amount of offset when zoom-in or zoom-out.
uniform float uoffset;
// Dof strength
perminder-17 marked this conversation as resolved.
Show resolved Hide resolved
uniform float inDofStrength;
// Dof position
uniform float inDofPosition;
// position for other molecules.
uniform float inFogPosition;
// Rendering surface dimensions, in pixels
Expand Down Expand Up @@ -79,6 +85,48 @@ float lerp(float a, float b, float f)
return a + f * (b - a);
}


float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
Copy link

@vinayakjeet vinayakjeet Apr 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float rand(vec2 co) {
return fract(sin(dot(co.xy ,vec2(12.9898,78.233)) + cos(dot(co.xy, vec2(93.9898,50.233)))) * 43758.5453);
}
//more complex pattern produced which helps in reducing the noise. @ghutchis your insights ?


float depthToZ(float depth) {
float eyeZ = ((height * 0.57735) /2.0);
float near = 2.0;
float far = 8000.0;
float depthNormalized = 2.0 * depth - 1.0;
return 2.0 * near * far / (far + near - depthNormalized * (far - near));
}

float calcBlur(float z, float pixelScale) {
return clamp(abs(z - 39.0), 0.0, 0.5*pixelScale);
}

vec4 applyBlur(vec2 texCoord) {
float pixelScale = max(width, height);
float origZ = depthToZ(texture2D(inDepthTex, texCoord).x);
float blurAmt = calcBlur(origZ, pixelScale);
// Skip blurring if the original depth is less than the threshold
if (origZ < uoffset * inDofPosition) {
return texture2D(inRGBTex, texCoord);
}
float total = 1.0;
vec4 color = texture2D(inRGBTex, texCoord);
for (int i = 0; i < 32; i++) {
float t = (float(i) / float(64));
float angle = (t * 4.0) * 6.28319;
float radius = (t * 2. - 1.);
angle += 1.0 * rand(gl_FragCoord.xy);
vec2 offset = (vec2(cos(angle), sin(angle)) * radius * 0.05 * inDofStrength) / pixelScale;
float z = depthToZ(texture2D(inDepthTex, texCoord + offset).x);
float sampleBlur = calcBlur(z, pixelScale);
float weight = float((z >= origZ) || (sampleBlur >= blurAmt * radius + 0.));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float weight = 1.0 - smoothstep(0.0, 1.0, abs(z - origZ) / blurAmt);
// using smoothstep to soften weight transitions
This modification uses smoothstep to create a more gradual transition in weights applied to sampled colors, potentially reducing artifacts like banding. Reviews @ghutchis

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the smoothstep() would be nice here, since it's also easier to read.

vec4 sample = texture2D(inRGBTex, texCoord + offset);
color += weight * sample;
total += weight;
}
return color / total;
}
vec4 applyFog(vec2 texCoord) {
vec4 finalColor = mix(
texture2D(inRGBTex, texCoord),
Expand Down Expand Up @@ -136,18 +184,25 @@ float computeEdgeLuminosity(vec3 normal)
}

void main() {
// Some cleanups required.
float luminosity = 1.0;
luminosity *= max(1.2 * (1.0 - inAoEnabled), computeSSAOLuminosity(getNormalNear(UV)));
luminosity *= max(1.0 - inEdStrength, computeEdgeLuminosity(getNormalAt(UV)));
vec4 color = texture2D(inRGBTex, UV);
if (inFogStrength == 0.0) {
if (inFogStrength == 0.0 && inDofEnabled == 0.0) {
gl_FragColor = vec4(color.xyz * luminosity, color.w);
}
else {
else if (inFogStrength != 0.0) { // apply fog since it's fast.
perminder-17 marked this conversation as resolved.
Show resolved Hide resolved
// Apply fog to the color texture
vec4 foggedColor = applyFog(UV);
vec4 fogColor = vec4(luminosity * foggedColor.xyz, foggedColor.w);
gl_FragColor = fogColor;
}
else {
// Apply blur to the color texture
vec4 blurredColor = applyBlur(UV);
vec4 blurColor = vec4(luminosity * blurredColor.xyz, blurredColor.w);
gl_FragColor = blurColor;
}
gl_FragDepth = texture2D(inDepthTex, UV).x;
}
}
12 changes: 8 additions & 4 deletions avogadro/rendering/solidpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "shader.h"
#include "camera.h"
#include "shaderprogram.h"
#include "camera.h"

#include "solid_vs.h"

Expand Down Expand Up @@ -88,9 +89,10 @@ void initializeFramebuffer(GLuint* outFBO, GLuint* texRGB, GLuint* texDepth)
}

SolidPipeline::SolidPipeline()
: m_pixelRatio(1.0f), m_aoEnabled(true), m_aoStrength(1.0f),
m_fogStrength(1.0f), m_fogPosition(1.0), m_fogEnabled(true), m_edEnabled(true), m_edStrength(1.0f),
m_width(0), m_height(0), d(new Private), m_backgroundColor(0,0,0,0)
: m_pixelRatio(1.0f), m_aoEnabled(true), m_dofEnabled(true), m_aoStrength(1.0f),
m_fogStrength(1.0f), m_fogPosition(1.0), m_fogEnabled(true), m_edEnabled(true),
m_edStrength(1.0f), m_width(0), m_height(0), m_dofStrength(1.0f),
m_dofPosition(1.0), m_backgroundColor(0,0,0,0), d(new Private)
{
}

Expand Down Expand Up @@ -159,6 +161,9 @@ void SolidPipeline::end()
d->attachStage(d->firstStageShaders, "inRGBTex", d->renderTexture, "inDepthTex",
d->depthTexture, m_width, m_height);
d->firstStageShaders.setUniformValue("inAoEnabled", m_aoEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inDofEnabled", m_dofEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inDofStrength", ((m_dofStrength) * 100.0f));
perminder-17 marked this conversation as resolved.
Show resolved Hide resolved
d->firstStageShaders.setUniformValue("inDofPosition", ((m_dofPosition) /10.0f));
d->firstStageShaders.setUniformValue("inAoStrength", m_aoStrength);
d->firstStageShaders.setUniformValue("inEdStrength", m_edStrength);
d->firstStageShaders.setUniformValue("inFogEnabled", m_fogEnabled ? 1.0f : 0.0f);
Expand All @@ -177,7 +182,6 @@ void SolidPipeline::adjustOffset(const Camera& cam) {
// They help define an offset with the projection-matrix
// to make the fog dynamic as the molecule moves away
// from the camera or come closer.

Eigen::Matrix4f projectView = cam.projection().matrix();

float project = ((((5000 + projectView(2,3) * 1000)/6) + 55) * 100);
Expand Down
25 changes: 23 additions & 2 deletions avogadro/rendering/solidpipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SolidPipeline
* @brief Begin solid geometry rendering.
*/
void begin();

void adjustOffset(const Camera& camera);
/**
* @brief End solid geometry rendering and apply screen-space shaders.
Expand All @@ -55,6 +55,12 @@ class SolidPipeline
bool getAoEnabled() { return m_aoEnabled; }
void setAoEnabled(bool enabled) { m_aoEnabled = enabled; }

/**
* @brief Get or set whether Depth-of-feild is enabled.
*/
bool getDofEnabled() { return m_dofEnabled; }
void setDofEnabled(bool enabled) { m_dofEnabled = enabled; }

/**
* @brief Get or set whether Fog is enabled.
*/
Expand Down Expand Up @@ -95,6 +101,18 @@ class SolidPipeline
m_edStrength = (m_edEnabled) ? 1.0 : 0.0;
}

/**
* @brief Get or set dof strength.
*/
float getDofStrength() { return m_dofStrength; }
void setDofStrength(float strength) { m_dofStrength = strength; }

/**
* @brief Set positon of dof
*/
float getDofPosition(){ return m_dofPosition;}
void setDofPosition(float position) { m_dofPosition = position; }

/**
* @brief Get or set the strength of the edge effect
*/
Expand All @@ -104,9 +122,12 @@ class SolidPipeline
private:
float m_pixelRatio;
bool m_aoEnabled;
Eigen::Affine3f modelView;
float m_dofStrength;
float m_dofPosition;
bool m_dofEnabled;
float m_fogPosition;
Vector4ub m_backgroundColor;
Eigen::Affine3f modelView;
bool m_fogEnabled;
float m_aoStrength;
float m_fogStrength;
Expand Down
Loading