Skip to content

Commit

Permalink
got proper voxels refraction working with ssrefr in deferred. TODO: i…
Browse files Browse the repository at this point in the history
…mplement voxels refraction for translucent.
  • Loading branch information
e2002e committed Jul 29, 2024
1 parent dc94830 commit aea321a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 61 deletions.
5 changes: 4 additions & 1 deletion Shaders/deferred_light/deferred_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ uniform sampler2D gbuffer1;
#ifdef _VoxelGI
uniform sampler2D voxels_diffuse;
uniform sampler2D voxels_specular;
#ifdef _VoxelRefract
uniform sampler2D voxels_refraction;
uniform sampler2D gbuffer_refraction;
#endif
#endif
#ifdef _VoxelAOvar
uniform sampler2D voxels_ao;
Expand Down Expand Up @@ -506,6 +510,5 @@ void main() {
);
}
#endif // _Clusters

fragColor.a = 1.0; // Mark as opaque
}
13 changes: 13 additions & 0 deletions Shaders/ssrefr_pass/ssrefr_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform sampler2D gbufferD1;
uniform sampler2D gbuffer_refraction; // ior\opacity
uniform sampler2D voxels_refraction;
uniform sampler2D voxels_specular;
uniform sampler2D voxels_diffuse;
uniform sampler2D voxels_shadows;
uniform mat4 P;
uniform mat3 V3;
uniform vec2 cameraProj;
Expand Down Expand Up @@ -110,5 +114,14 @@ void main() {
refractionCol = clamp(refractionCol, 0.0, 1.0);
refractionCol *= intensity;
vec3 color = textureLod(tex, texCoord.xy, 0.0).rgb;
#ifdef _VoxelGI
vec3 diffuse = textureLod(voxels_diffuse, texCoord, 0.0).rgb;
vec3 specular = textureLod(voxels_specular, texCoord, 0.0).rgb;
color = (color + diffuse + specular) * textureLod(voxels_shadows, texCoord, 0.0).r;
#ifdef _VoxelRefract
vec3 voxelsRefr = textureLod(voxels_refraction, texCoord, 0.0).rgb;
refractionCol = (refractionCol + voxelsRefr) / 2;
#endif
#endif
fragColor.rgb = mix(refractionCol, color, opac);
}
18 changes: 2 additions & 16 deletions Sources/armory/renderpath/Inc.hx
Original file line number Diff line number Diff line change
Expand Up @@ -537,19 +537,6 @@ class Inc {
}
#end

#if (rp_voxels == "Voxel AO")
path.bindTarget("voxels_ao", "voxels_ao");
#else
path.bindTarget("voxels_diffuse", "voxels_diffuse");
path.bindTarget("voxels_specular", "voxels_specular");
#if arm_voxelgi_refract
path.bindTarget("voxels_refraction", "voxels_refraction");
#end
#end
#if arm_voxelgi_shadows
path.bindTarget("voxels_shadows", "voxels_shadows");
#end

path.drawMeshes("translucent");

#if rp_render_to_texture
Expand All @@ -561,7 +548,6 @@ class Inc {
path.setTarget("");
}
#end

path.bindTarget("accum", "gbuffer0");
path.bindTarget("revealage", "gbuffer1");
path.drawShader("shader_datas/translucent_resolve/translucent_resolve");
Expand Down Expand Up @@ -1309,9 +1295,9 @@ class Inc {
kha.compute.Compute.setSampledTexture(voxel_ta6, rts.get("voxelsOut").image);
kha.compute.Compute.setSampledTexture(voxel_tb6, rts.get("half").image);
#if arm_deferred
kha.compute.Compute.setSampledTexture(voxel_tc6, rts.get("gbuffer0").image);
kha.compute.Compute.setSampledTexture(voxel_tc6, rts.get("gbuffer0_refr").image);
#else
kha.compute.Compute.setSampledTexture(voxel_tc6, rts.get("lbuffer1").image);
kha.compute.Compute.setSampledTexture(voxel_tc6, rts.get("lbuffer1_refr").image);
#end
kha.compute.Compute.setSampledTexture(voxel_td6, rts.get("voxelsSDF").image);
kha.compute.Compute.setTexture(voxel_te6, rts.get("voxels_refraction").image, kha.compute.Access.Write);
Expand Down
41 changes: 19 additions & 22 deletions Sources/armory/renderpath/RenderPathDeferred.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RenderPathDeferred {
"gbuffer1",
#if rp_gbuffer2 "gbuffer2", #end
#if rp_gbuffer_emission "gbuffer_emission", #end
#if (rp_ssrefr || arm_voxelgi_refract) "gbuffer_refraction" #end
#if (arm_voxelgi_refract || rp_ssrefr) "gbuffer_refraction" #end
]);
}

Expand Down Expand Up @@ -357,7 +357,7 @@ class RenderPathDeferred {
t.width = 0;
t.height = 0;
t.displayp = Inc.getDisplayp();
t.depth_buffer = "refraction";
t.depth_buffer = "main";
t.format = "RGBA64";
t.scale = Inc.getSuperSampling();
path.createRenderTarget(t);
Expand Down Expand Up @@ -491,26 +491,18 @@ class RenderPathDeferred {
}
#end

#if (rp_ssrefr || arm_voxelgi_refract)
path.setTarget("gbuffer_refraction");
#if (rp_background == "Clear")
{
path.clearTarget(-1, 1.0);
}
#else
{
path.clearTarget(null, 1.0);
}
#end
#end

#if rp_gbuffer2
{
path.setTarget("gbuffer2");
path.clearTarget(0xff000000);
}
#end

#if (arm_voxelgi_refract || rp_ssrefr)
path.setTarget("gbuffer_refraction");
path.clearTarget(0xff000000);
#end

RenderPathCreator.setTargetMeshes();

#if rp_dynres
Expand Down Expand Up @@ -674,6 +666,7 @@ class RenderPathDeferred {
}
}
#end

// ---
// Deferred light
// ---
Expand Down Expand Up @@ -723,9 +716,6 @@ class RenderPathDeferred {
Inc.resolveSpecular();
path.bindTarget("voxels_diffuse", "voxels_diffuse");
path.bindTarget("voxels_specular", "voxels_specular");
#if arm_voxelgi_refract
Inc.resolveRefraction();
#end
#end
#if arm_voxelgi_shadows
Inc.resolveShadows();
Expand Down Expand Up @@ -880,6 +870,16 @@ class RenderPathDeferred {
path.drawShader("shader_datas/copy_pass/copy_pass");

path.setTarget("gbuffer1_refr", ["gbuffer0_refr", "gbuffer_refraction"]);
path.drawMeshes("refraction");

#if arm_voxelgi_refract
path.setTarget("half");
path.bindTarget("_main", "texdepth");
path.drawShader("shader_datas/downsample_depth/downsample_depth");
Inc.resolveRefraction();
#end
path.setTarget("tex");

#if (rp_voxels == "Voxel AO")
path.bindTarget("voxels_ao", "voxels_ao");
#else
Expand All @@ -892,16 +892,13 @@ class RenderPathDeferred {
#if arm_voxelgi_shadows
path.bindTarget("voxels_shadows", "voxels_shadows");
#end

path.drawMeshes("refraction");

path.setTarget("tex");
path.bindTarget("refr", "tex1");
path.bindTarget("gbuffer1_refr", "tex");
path.bindTarget("_main", "gbufferD");
path.bindTarget("gbufferD1", "gbufferD1");
path.bindTarget("gbuffer0_refr", "gbuffer0");
path.bindTarget("gbuffer_refraction", "gbuffer_refraction");

path.drawShader("shader_datas/ssrefr_pass/ssrefr_pass");
}
}
Expand Down
26 changes: 7 additions & 19 deletions blender/arm/material/make_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,7 @@ def make_deferred(con_mesh, rpasses):
frag.write('#endif')

if '_SSRefraction' in wrd.world_defs or '_VoxelRefract' in wrd.world_defs:
if parse_opacity:
frag.write('fragColor[GBUF_IDX_REFRACTION] = vec4(ior, opacity, 0.0, 0.0);')
else:
frag.write('fragColor[GBUF_IDX_REFRACTION] = vec4(1.0, 1.0, 0.0, 0.0);')
frag.write('fragColor[GBUF_IDX_REFRACTION] = vec4(1.0, 1.0, 0.0, 0.0);')

return con_mesh

Expand Down Expand Up @@ -560,10 +557,7 @@ def make_forward(con_mesh):
frag.write('fragColor[0] = vec4(direct + indirect, packFloat2(occlusion, specular));')
frag.write('fragColor[1] = vec4(n.xy, roughness, metallic);')
if rpdat.rp_ss_refraction or rpdat.arm_voxelgi_refract:
if parse_opacity:
frag.write(f'fragColor[2] = vec4(1.0, 1.0, 0.0, 0.0);')
else:
frag.write(f'fragColor[2] = vec4(ior, opacity, 0.0, 0.0);')
frag.write(f'fragColor[2] = vec4(1.0, 1.0, 0.0, 0.0);')
else:
frag.add_out('vec4 fragColor[1]')
frag.write('fragColor[0] = vec4(direct + indirect, 1.0);')
Expand Down Expand Up @@ -674,15 +668,13 @@ def make_forward_base(con_mesh, parse_opacity=False, transluc_pass=False):
frag.write('vec3 indirect = envl;')

if '_VoxelAOvar' in wrd.world_defs:
frag.add_uniform("sampler2D voxels_ao");
frag.add_include("std/conetrace.glsl")
frag.add_uniform("sampler3D voxels")
if '_VoxelShadow' in wrd.world_defs:
frag.add_uniform("sampler3D voxelsSDF")
frag.add_uniform('float clipmaps[10 * voxelgiClipmapCount]', '_clipmaps');
frag.write('indirect *= textureLod(voxels_ao, gl_FragCoord.xy, 0.0).r;')

if '_VoxelGI' in wrd.world_defs:
frag.add_uniform("sampler2D voxels_diffuse")
frag.add_uniform("sampler2D voxels_specular")
frag.write("indirect = textureLod(voxels_diffuse, gl_FragCoord.xy, 0.0).rgb * albedo * voxelgiDiff;")
frag.write("indirect += textureLod(voxels_specular, gl_FragCoord.xy, 0.0).rgb * specular * voxelgiRefl;")

frag.write('vec3 direct = vec3(0.0);')

if '_Sun' in wrd.world_defs:
Expand Down Expand Up @@ -765,10 +757,6 @@ def make_forward_base(con_mesh, parse_opacity=False, transluc_pass=False):
frag.write('direct = vec3(0.0);')
frag.write('indirect += emissionCol;')

if '_VoxelRefract' in wrd.world_defs and parse_opacity:
frag.add_uniform("sampler2D voxels_refraction");
frag.write('direct = mix(textureLod(voxels_refraction, gl_FragCoord.xy, 0.0).rgb * voxelgiRefr, direct, opacity);')
frag.write('indirect = mix(textureLod(voxels_refraction, gl_FragCoord.xy, 0.0).rgb * voxelgiRefr, indirect, opacity);')

def _write_material_attribs_default(frag: shader.Shader, parse_opacity: bool):
frag.write('vec3 basecol;')
Expand Down
15 changes: 13 additions & 2 deletions blender/arm/material/make_refraction_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import arm
import arm.material.cycles as cycles
import arm.material.mat_state as mat_state
import arm.material.mat_utils as mat_utils
import arm.material.make_mesh as make_mesh
import arm.material.make_finalize as make_finalize
import arm.assets as assets
Expand All @@ -19,7 +20,13 @@

def make(context_id):
con_refraction_buffer = mat_state.data.add_context({ 'name': context_id, 'depth_write': True, 'compare_mode': 'less', 'cull_mode': 'clockwise' })
make_mesh.make_base(con_refraction_buffer, True)

arm_discard = mat_state.material.arm_discard
blend = mat_state.material.arm_blending
is_transluc = mat_utils.is_transluc(mat_state.material)
parse_opacity = (blend and is_transluc) or arm_discard

make_mesh.make_base(con_refraction_buffer, parse_opacity)

vert = con_refraction_buffer.vert
frag = con_refraction_buffer.frag
Expand All @@ -34,7 +41,11 @@ def make(context_id):

wrd = bpy.data.worlds['Arm']

frag.write('fragColor = vec4(ior, opacity, 0.0, 0.0);')
if parse_opacity:
frag.write('fragColor = vec4(ior, opacity, 0.0, 0.0);')
else:
frag.write('fragColor = vec4(1.0, 1.0, 0.0, 0.0);')

make_finalize.make(con_refraction_buffer)

# assets.vs_equal(con_refract, assets.shader_cons['transluc_vert']) # shader_cons has no transluc yet
Expand Down
1 change: 0 additions & 1 deletion blender/arm/material/make_shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import arm.material.make_transluc as make_transluc
import arm.material.make_refract as make_refract
import arm.material.make_voxel as make_voxel
import arm.material.make_refraction_buffer as make_refraction_buffer
import arm.material.mat_state as mat_state
import arm.material.mat_utils as mat_utils
from arm.material.shader import Shader, ShaderContext, ShaderData
Expand Down

0 comments on commit aea321a

Please sign in to comment.