New ray offset for vxgi

This commit is contained in:
luboslenco 2018-03-26 00:30:24 +02:00
parent 977301a819
commit e868b0f614
8 changed files with 57 additions and 78 deletions

View file

@ -154,7 +154,7 @@ void main() {
#else
vec3 voxpos = p / voxelgiHalfExtents;
#endif
if (dotNL > 0.0) visibility = max(0, 1.0 - traceShadow(voxels, voxpos, l, 0.1, length(lp)));
if (dotNL > 0.0) visibility = max(0, 1.0 - traceShadow(voxels, voxpos, l, 0.1, length(lp), n));
#endif

View file

@ -118,7 +118,7 @@ void main() {
#else
vec3 voxpos = p / voxelgiHalfExtents;
#endif
if (dotNL > 0.0) visibility = max(0, 1.0 - traceShadow(voxels, voxpos, l, 0.1, 10.0));
if (dotNL > 0.0) visibility = max(0, 1.0 - traceShadow(voxels, voxpos, l, 0.1, 10.0, n));
#endif
// Per-light

View file

@ -42,11 +42,12 @@ vec3 tangent(const vec3 n) {
// dir.z * textureLod(voxels[indices.z], pos, lod);
// }
vec4 traceCone(sampler3D voxels, const vec3 origin, vec3 dir, const float aperture, const float maxDist, const float offset) {
vec4 traceCone(sampler3D voxels, vec3 origin, vec3 dir, const float aperture, const float maxDist, const vec3 normal) {
dir = normalize(dir);
// origin -= dir * dot(dir, normal) * VOXEL_SIZE;
// uvec3 indices = faceIndices(dir);
vec4 sampleCol = vec4(0.0);
float dist = offset;
float dist = 0.04 * voxelgiOffset;
float diam = dist * aperture;
vec3 samplePos;
// Step until alpha > 1 or out of bounds
@ -68,66 +69,61 @@ vec4 traceCone(sampler3D voxels, const vec3 origin, vec3 dir, const float apertu
}
vec4 traceDiffuse(const vec3 origin, const vec3 normal, sampler3D voxels) {
const float TAN_22_5 = 0.55785173935;
const float angleMix = 0.5f;
const float aperture = TAN_22_5;
const float aperture = 0.55785173935;
vec3 o1 = normalize(tangent(normal));
vec3 o2 = normalize(cross(o1, normal));
vec3 c1 = 0.5f * (o1 + o2);
vec3 c2 = 0.5f * (o1 - o2);
const float offset = 1.5 * VOXEL_SIZE * voxelgiOffsetDiff;
#ifdef _VoxelCones1
return traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
return traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, normal);
#endif
#ifdef _VoxelCones3
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, offset);
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, normal);
return col / (3.0 * blendFac);
#endif
#ifdef _VoxelCones5
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, offset);
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, normal);
return col / (5.0 * blendFac);
#endif
#ifdef _VoxelCones9
// Normal direction
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
vec4 col = traceCone(voxels, origin, normal, aperture, MAX_DISTANCE, normal);
// 4 side cones
col += traceCone(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, normal);
// 4 corners
col += traceCone(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceCone(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, normal);
col += traceCone(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE, normal);
return col / (9.0 * blendFac);
#endif
return vec4(0.0);
}
float traceShadow(sampler3D voxels, const vec3 origin, const vec3 dir, const float aperture, const float targetDistance) {
const float offset = 2 * VOXEL_SIZE * voxelgiOffsetShadow;
return traceCone(voxels, origin, dir, aperture, targetDistance, offset).a;
float traceShadow(sampler3D voxels, const vec3 origin, const vec3 dir, const float aperture, const float targetDistance, const vec3 normal) {
return traceCone(voxels, origin, dir, aperture, targetDistance, normal).a;
}
vec3 traceSpecular(sampler3D voxels, const vec3 pos, const vec3 normal, const vec3 viewDir, const float roughness) {
float rough = max(roughness, 0.15);
float specularAperture = clamp(tan((3.14159265 / 2) * rough * 0.75), 0.0174533, 3.14159265);
vec3 specularDir = normalize(reflect(-viewDir, normal));
// Clamp to 1 grad and pi, exponent is angle of cone in radians
const float offset = 3 * VOXEL_SIZE * voxelgiOffsetSpec;
return traceCone(voxels, pos, specularDir, specularAperture, MAX_DISTANCE, offset).xyz;
return traceCone(voxels, pos, specularDir, specularAperture, MAX_DISTANCE, normal).xyz;
}
vec3 traceRefraction(sampler3D voxels, const vec3 pos, const vec3 normal, const vec3 viewDir, const float roughness) {
@ -136,14 +132,13 @@ vec3 traceRefraction(sampler3D voxels, const vec3 pos, const vec3 normal, const
vec3 refraction = refract(viewDir, normal, 1.0 / ior);
float rough = max(roughness, 0.03);
float specularAperture = clamp(tan((3.14159265 / 2) * rough), 0.0174533, 3.14159265);
const float offset = 1.5 * VOXEL_SIZE * voxelgiOffsetRefract;
return transmittance * traceCone(voxels, pos, refraction, specularAperture, MAX_DISTANCE, offset).xyz;
return transmittance * traceCone(voxels, pos, refraction, specularAperture, MAX_DISTANCE, normal).xyz;
}
float traceConeAO(sampler3D voxels, const vec3 origin, vec3 dir, const float aperture, const float maxDist, const float offset) {
float traceConeAO(sampler3D voxels, const vec3 origin, vec3 dir, const float aperture, const float maxDist) {
dir = normalize(dir);
float sampleCol = 0.0;
float dist = offset;
float dist = 1.5 * VOXEL_SIZE * voxelgiOffset;
float diam = dist * aperture;
vec3 samplePos;
while (sampleCol < 1.0 && dist < maxDist) {
@ -158,46 +153,44 @@ float traceConeAO(sampler3D voxels, const vec3 origin, vec3 dir, const float ape
}
float traceAO(const vec3 origin, const vec3 normal, sampler3D voxels) {
const float TAN_22_5 = 0.55785173935;
const float angleMix = 0.5f;
const float aperture = TAN_22_5;
const float aperture = 0.55785173935;
vec3 o1 = normalize(tangent(normal));
vec3 o2 = normalize(cross(o1, normal));
vec3 c1 = 0.5f * (o1 + o2);
vec3 c2 = 0.5f * (o1 - o2);
const float offset = 1.5 * VOXEL_SIZE * voxelgiOffsetDiff;
#ifdef _VoxelCones1
return traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
return traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE);
#endif
#ifdef _VoxelCones3
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE, offset);
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE);
return col / (3.0 * blendFac);
#endif
#ifdef _VoxelCones5
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE, offset);
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE);
return col / (5.0 * blendFac);
#endif
#ifdef _VoxelCones9
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE, offset);
float col = traceConeAO(voxels, origin, normal, aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, o1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, o2, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -c1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -c2, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE, offset);
col += traceConeAO(voxels, origin, mix(normal, -o1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, -o2, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, c1, angleMix), aperture, MAX_DISTANCE);
col += traceConeAO(voxels, origin, mix(normal, c2, angleMix), aperture, MAX_DISTANCE);
return col / (9.0 * blendFac);
#endif

View file

@ -34,15 +34,17 @@ void main() {
const vec3 hres = voxelgiResolution / 2;
vec3 wposition = ((gl_GlobalInvocationID.xyz - hres) / hres) * voxelgiHalfExtents;
uint unor = imageLoad(voxelsNor, ivec3(gl_GlobalInvocationID.xyz)).r;
vec3 wnormal = decNor(unor);
wposition -= wnormal * 0.01; // Offset
float visibility;
vec3 lp = lightPos - wposition;
vec3 l;
if (lightType == 0) { l = lightDir; visibility = 1.0; }
else { l = normalize(lp); visibility = attenuate(distance(wposition, lightPos)); }
uint unor = imageLoad(voxelsNor, ivec3(gl_GlobalInvocationID.xyz)).r;
vec3 wnormal = decNor(unor);
float dotNL = max(dot(wnormal, l), 0.0);
if (dotNL == 0.0) return;

View file

@ -327,7 +327,6 @@ class Inc {
kha.compute.Compute.setShader(voxel_sh);
kha.compute.Compute.setTexture(voxel_ta, rts.get("voxelsOpac").image, kha.compute.Access.Read);
kha.compute.Compute.setTexture(voxel_tb, rts.get("voxelsNor").image, kha.compute.Access.Read);
// kha.compute.Compute.setTexture(voxel_tc, rts.get("voxels").image, kha.compute.Access.Write);
kha.compute.Compute.setTexture(voxel_tc, rts.get("voxels").image, kha.compute.Access.Write);
#if (rp_shadowmap)

View file

@ -531,10 +531,7 @@ class ArmRPListItem(bpy.types.PropertyGroup):
arm_voxelgi_occ = FloatProperty(name="Occlusion", description="", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_env = FloatProperty(name="Env Map", description="Contribute light from environment map", default=0.0, update=assets.invalidate_shader_cache)
arm_voxelgi_step = FloatProperty(name="Step", description="Step size", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_offset_diff = FloatProperty(name="Diffuse Offset", description="Offset size", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_offset_spec = FloatProperty(name="Specular Offset", description="Step size", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_offset_shadow = FloatProperty(name="Shadow Offset", description="Step size", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_offset_refract = FloatProperty(name="Refract Offset", description="Step size", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_offset = FloatProperty(name="Offset", description="Ray offset", default=1.0, update=assets.invalidate_shader_cache)
arm_voxelgi_range = FloatProperty(name="Range", description="Maximum range", default=0.5, update=assets.invalidate_shader_cache)
arm_sss_width = FloatProperty(name="Width", description="SSS blur strength", default=1.0, update=assets.invalidate_shader_cache)
arm_clouds_density = FloatProperty(name="Density", default=1.0, min=0.0, max=10.0, update=assets.invalidate_shader_cache)

View file

@ -1029,16 +1029,7 @@ class ArmRenderPathPanel(bpy.types.Panel):
row.alignment = 'EXPAND'
row.prop(rpdat, 'arm_voxelgi_step')
row.prop(rpdat, 'arm_voxelgi_range')
col.label("Offset")
row = col.row(align=True)
row.alignment = 'EXPAND'
row.prop(rpdat, 'arm_voxelgi_offset_diff', text="Diffuse")
row.prop(rpdat, 'arm_voxelgi_offset_spec', text="Specular")
row = col.row(align=True)
row.alignment = 'EXPAND'
row.prop(rpdat, 'arm_voxelgi_offset_shadow', "Shadow")
row.prop(rpdat, 'arm_voxelgi_offset_refract', "Refracr")
col.prop(rpdat, 'arm_voxelgi_offset');
layout.label('World')
box = layout.box().column()

View file

@ -582,10 +582,7 @@ const float voxelgiOcc = """ + str(round(rpdat.arm_voxelgi_occ * 100) / 100) + "
const float voxelgiEnv = """ + str(round(rpdat.arm_voxelgi_env * 100) / 100) + """ / 10.0;
const float voxelgiStep = """ + str(round(rpdat.arm_voxelgi_step * 100) / 100) + """;
const float voxelgiRange = """ + str(round(rpdat.arm_voxelgi_range * 100) / 100) + """;
const float voxelgiOffsetDiff = """ + str(round(rpdat.arm_voxelgi_offset_diff * 100) / 100) + """;
const float voxelgiOffsetSpec = """ + str(round(rpdat.arm_voxelgi_offset_spec * 100) / 100) + """;
const float voxelgiOffsetShadow = """ + str(round(rpdat.arm_voxelgi_offset_shadow * 100) / 100) + """;
const float voxelgiOffsetRefract = """ + str(round(rpdat.arm_voxelgi_offset_refract * 100) / 100) + """;
const float voxelgiOffset = """ + str(round(rpdat.arm_voxelgi_offset * 100) / 100) + """;
""")
if rpdat.rp_sss_state == 'On':