Begin shadow index

This commit is contained in:
luboslenco 2018-12-10 23:29:04 +01:00
parent bd03fd0465
commit d8f78638bd
13 changed files with 102 additions and 95 deletions

View file

@ -105,18 +105,21 @@ uniform vec2 cameraPlane;
#endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
//!uniform vec2 lightProj;
// uniform samplerCubeShadow shadowMap0; //arm_dev
//!uniform samplerCube shadowMap0;
#else
// uniform sampler2DShadow shadowMap0; //arm_dev
//!uniform sampler2D shadowMap0;
//!uniform mat4 LWVP0;
#endif
//!uniform samplerCube shadowMap1;
//!uniform samplerCube shadowMap2;
//!uniform samplerCube shadowMap3;
#ifdef _Spot
//!uniform sampler2D shadowMapSpot0;
//!uniform sampler2D shadowMapSpot1;
//!uniform sampler2D shadowMapSpot2;
//!uniform sampler2D shadowMapSpot3;
//!uniform mat4 LWVPSpot0;
//!uniform mat4 LWVPSpot1;
//!uniform mat4 LWVPSpot2;
//!uniform mat4 LWVPSpot3;
#endif
#endif
@ -357,7 +360,7 @@ void main() {
fragColor.rgb += sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, metrough.y, occspec.y, f0
#ifdef _ShadowMap
, pointBias
, 0, pointBias
#endif
#ifdef _Spot
, true, spotData.x, spotData.y, spotDir
@ -393,10 +396,10 @@ void main() {
occspec.y,
f0
#ifdef _ShadowMap
, lightsArray[li * 2].w // bias
, li, lightsArray[li * 2].w // bias
#endif
#ifdef _Spot
, i > numPoints - 1
, li > numPoints - 1
, lightsArray[li * 2 + 1].w // cutoff
, lightsArraySpot[li].w // cutoff - exponent
, lightsArraySpot[li].xyz // spotDir

View file

@ -171,7 +171,7 @@
{
"name": "lightProj",
"link": "_lightPlaneProj",
"ifdef": ["_ShadowMapCube"]
"ifdef": ["_ShadowMap"]
},
{
"name": "pointPos",

View file

@ -44,18 +44,21 @@ uniform vec2 cameraPlane;
#endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
//!uniform vec2 lightProj;
// uniform samplerCubeShadow shadowMap0; //arm_dev
//!uniform samplerCube shadowMap0;
#else
// uniform sampler2DShadow shadowMap0; //arm_dev
//!uniform sampler2D shadowMap0;
//!uniform mat4 LWVP0;
#endif
//!uniform samplerCube shadowMap1;
//!uniform samplerCube shadowMap2;
//!uniform samplerCube shadowMap3;
#ifdef _Spot
//!uniform sampler2D shadowMapSpot0;
//!uniform sampler2D shadowMapSpot1;
//!uniform sampler2D shadowMapSpot2;
//!uniform sampler2D shadowMapSpot3;
//!uniform mat4 LWVPSpot0;
//!uniform mat4 LWVPSpot1;
//!uniform mat4 LWVPSpot2;
//!uniform mat4 LWVPSpot3;
#endif
#endif
@ -175,7 +178,7 @@ void main() {
fragColor.rgb += sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, metrough.y, occspec.y, f0
#ifdef _ShadowMap
, pointBias
, 0, pointBias
#endif
#ifdef _Spot
, true, spotData.x, spotData.y, spotDir
@ -211,10 +214,10 @@ void main() {
occspec.y,
f0
#ifdef _ShadowMap
, lightsArray[li * 2].w // bias
, li, lightsArray[li * 2].w // bias
#endif
#ifdef _Spot
, i > numPoints - 1
, li > numPoints - 1
, lightsArray[li * 2 + 1].w // cutoff
, lightsArraySpot[li].w // cutoff - exponent
, lightsArraySpot[li].xyz // spotDir

View file

@ -110,7 +110,7 @@
{
"name": "lightProj",
"link": "_lightPlaneProj",
"ifdef": ["_ShadowMapCube"]
"ifdef": ["_ShadowMap"]
},
{
"name": "pointPos",

View file

@ -9,25 +9,28 @@
#endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
uniform vec2 lightProj;
// uniform samplerCubeShadow shadowMap0; //arm_dev
uniform samplerCube shadowMap0;
#else
// uniform sampler2DShadow shadowMap0; //arm_dev
uniform sampler2D shadowMap0;
uniform mat4 LWVP0;
#endif
uniform samplerCube shadowMap1;
uniform samplerCube shadowMap2;
uniform samplerCube shadowMap3;
#ifdef _Spot
uniform sampler2D shadowMapSpot0;
uniform sampler2D shadowMapSpot1;
uniform sampler2D shadowMapSpot2;
uniform sampler2D shadowMapSpot3;
uniform mat4 LWVPSpot0;
uniform mat4 LWVPSpot1;
uniform mat4 LWVPSpot2;
uniform mat4 LWVPSpot3;
#endif
#endif
vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol,
const vec3 albedo, const float rough, const float spec, const vec3 f0
#ifdef _ShadowMap
, float bias
, int index, float bias
#endif
#ifdef _Spot
, bool isSpot, float spotA, float spotB, vec3 spotDir
@ -55,9 +58,7 @@ vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
}
#ifdef _ShadowMap
vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
if (lPos.w > 0.0) {
direct *= shadowTest(shadowMapSpot0, lPos.xyz / lPos.w, bias, shadowmapSize);
}
if (lPos.w > 0.0) direct *= shadowTest(shadowMapSpot0, lPos.xyz / lPos.w, bias, shadowmapSize);
#endif
return direct;
}
@ -86,19 +87,11 @@ vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
// #endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
direct *= PCFCube(shadowMap0, ld, -l, bias, lightProj, n);
#else
vec4 lPos = LWVP0 * vec4(p + n * bias * 10, 1.0);
if (lPos.w > 0.0) {
#ifdef _SMSizeUniform
vec2 smSize = smSizeUniform;
#else
vec2 smSize = shadowmapSize;
#endif
direct *= shadowTest(shadowMap0, lPos.xyz / lPos.w, bias, smSize);
}
#endif
// Oh well..
if (index == 0) direct *= PCFCube(shadowMap0, ld, -l, bias, lightProj, n);
else if (index == 1) direct *= PCFCube(shadowMap1, ld, -l, bias, lightProj, n);
else if (index == 2) direct *= PCFCube(shadowMap2, ld, -l, bias, lightProj, n);
else if (index == 3) direct *= PCFCube(shadowMap3, ld, -l, bias, lightProj, n);
#endif
return direct;

View file

@ -8,25 +8,28 @@
#endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
uniform vec2 lightProj;
// uniform samplerCubeShadow shadowMap0; //arm_dev
uniform samplerCube shadowMap0;
#else
// uniform sampler2DShadow shadowMap0; //arm_dev
uniform sampler2D shadowMap0;
uniform mat4 LWVP0;
#endif
uniform samplerCube shadowMap1;
uniform samplerCube shadowMap2;
uniform samplerCube shadowMap3;
#ifdef _Spot
uniform sampler2D shadowMapSpot0;
uniform sampler2D shadowMapSpot1;
uniform sampler2D shadowMapSpot2;
uniform sampler2D shadowMapSpot3;
uniform mat4 LWVPSpot0;
uniform mat4 LWVPSpot1;
uniform mat4 LWVPSpot2;
uniform mat4 LWVPSpot3;
#endif
#endif
vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol,
const vec3 albedo, const float rough, const float spec, const vec3 f0
#ifdef _ShadowMap
, float bias
, int index, float bias
#endif
#ifdef _Spot
, bool isSpot, float spotA, float spotB, vec3 spotDir
@ -63,15 +66,10 @@ vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, co
#endif
#ifdef _ShadowMap
#ifdef _ShadowMapCube
direct *= PCFCube(shadowMap0, ld, -l, bias, lightProj, n);
#else
vec4 lPos = LWVP0 * vec4(p + n * bias * 10, 1.0);
if (lPos.w > 0.0) {
vec2 smSize = shadowmapSize;
direct *= shadowTest(shadowMap0, lPos.xyz / lPos.w, bias, smSize);
}
#endif
if (index == 0) direct *= PCFCube(shadowMap0, ld, -l, bias, lightProj, n);
else if (index == 1) direct *= PCFCube(shadowMap1, ld, -l, bias, lightProj, n);
else if (index == 2) direct *= PCFCube(shadowMap2, ld, -l, bias, lightProj, n);
else if (index == 3) direct *= PCFCube(shadowMap3, ld, -l, bias, lightProj, n);
#endif
return direct;

View file

@ -32,6 +32,9 @@ class Inc {
#end
public static var superSample = 1.0;
static var pointIndex = 0;
static var spotIndex = 0;
public static function init(_path:RenderPath) {
path = _path;
@ -48,8 +51,17 @@ class Inc {
public static function bindShadowMap() {
for (l in iron.Scene.active.lights) {
if (!l.visible || !l.data.raw.cast_shadow) continue;
var n = shadowMapName(l);
if (!l.visible || !l.data.raw.cast_shadow || l.data.raw.type != "sun") continue;
var n = "shadowMap";
path.bindTarget(n, n);
break;
}
for (i in 0...pointIndex) {
var n = "shadowMap" + i;
path.bindTarget(n, n);
}
for (i in 0...spotIndex) {
var n = "shadowMapSpot" + i;
path.bindTarget(n, n);
}
@ -72,8 +84,8 @@ class Inc {
static function shadowMapName(l:iron.object.LightObject):String {
if (l.data.raw.type == "sun") return "shadowMap";
if (l.data.raw.type == "point") return "shadowMap0";
else return "shadowMapSpot0";
if (l.data.raw.type == "point") return "shadowMap" + pointIndex;
else return "shadowMapSpot" + spotIndex;
}
static function getShadowMap(l:iron.object.LightObject):String {
@ -112,18 +124,24 @@ class Inc {
public static function drawShadowMap() {
#if (rp_shadowmap)
pointIndex = 0;
spotIndex = 0;
for (l in iron.Scene.active.lights) {
if (!l.visible || !l.data.raw.cast_shadow) continue;
path.light = l;
var shadowmap = Inc.getShadowMap(l);
var faces = l.data.raw.shadowmap_cube ? 6 : 1;
for (i in 0...faces) {
if (faces > 1) path.currentFace = i;
path.setTarget(Inc.getShadowMap(l));
path.setTarget(shadowmap);
path.clearTarget(null, 1.0);
path.drawMeshes("shadowmap");
}
path.currentFace = -1;
if (l.data.raw.type == "point") pointIndex++;
else if (l.data.raw.type == "spot") spotIndex++;
}
// One light at a time for now, precompute all lights for tiled

View file

@ -1760,7 +1760,7 @@ class ArmoryExporter:
o['shadows_bias'] *= 1 / (o['shadowmap_size'] / 1024) # Less bias for bigger maps
if (objtype == 'POINT' or objtype == 'SPOT') and objref.shadow_soft_size > 0.1:
o['light_size'] = objref.shadow_soft_size * 10 # Match to Cycles
if objtype == 'POINT' and rpdat.arm_shadows_cubemap:
if objtype == 'POINT':
o['fov'] = 1.5708 # pi/2
o['shadowmap_cube'] = True
o['shadows_bias'] *= 2.0

View file

@ -52,8 +52,6 @@ def add_world_defs():
else:
wrd.world_defs += '_CSM'
assets.add_khafile_def('arm_csm')
if rpdat.arm_shadows_cubemap:
wrd.world_defs += '_ShadowMapCube'
# SS
if rpdat.rp_ssgi == 'RTGI' or rpdat.rp_ssgi == 'RTAO':
if rpdat.rp_ssgi == 'RTGI':

View file

@ -12,6 +12,9 @@ def write(vert, frag):
if is_shadows:
frag.add_uniform('vec2 lightProj', link='_lightPlaneProj', included=True)
frag.add_uniform('samplerCube shadowMap0', included=True)
frag.add_uniform('samplerCube shadowMap1', included=True)
frag.add_uniform('samplerCube shadowMap2', included=True)
frag.add_uniform('samplerCube shadowMap3', included=True)
vert.add_out('vec4 wvpposition')
vert.write('wvpposition = gl_Position;')
# wvpposition.z / wvpposition.w
@ -42,9 +45,9 @@ def write(vert, frag):
frag.write(' specular,')
frag.write(' f0')
if is_shadows:
frag.write(' , lightsArray[li * 2].w') # bias
frag.write(' , li, lightsArray[li * 2].w') # bias
if '_Spot' in wrd.world_defs:
frag.write(' , i > numPoints - 1')
frag.write(' , li > numPoints - 1')
frag.write(' , lightsArray[li * 2 + 1].w') # cutoff
frag.write(' , lightsArraySpot[li].w') # cutoff - exponent
frag.write(' , lightsArraySpot[li].xyz') # spotDir

View file

@ -509,25 +509,23 @@ def make_forward_mobile(con_mesh):
frag.add_uniform('vec3 pointPos', '_pointPosition')
frag.add_uniform('vec3 pointCol', '_pointColor')
frag.write('float visibility = 1.0;')
frag.write('float dotNL = max(dot(n, pointPos - wposition), 0.0);')
frag.write('vec3 ld = pointPos - wposition;')
frag.write('vec3 l = normalize(ld);')
frag.write('float dotNL = max(dot(n, l), 0.0);')
# if '_Spot' in wrd.world_defs:
# frag.add_uniform('vec3 spotDir', link='_spotDirection')
# frag.add_uniform('vec2 spotData', link='_spotData')
# if is_shadows:
# frag.add_uniform('sampler2D shadowMapSpot0', included=True)
if is_shadows:
vert.add_out('vec4 lightPosition')
vert.add_uniform('mat4 LWVP', '_biasLightWorldViewProjectionMatrix')
vert.write('lightPosition = LWVP * spos;')
frag.add_uniform('sampler2D shadowMap0')
frag.add_uniform('float shadowsBias', '_lightShadowsBias')
frag.write('if (lightPosition.w > 0.0) {')
frag.write(' vec3 lPos = lightPosition.xyz / lightPosition.w;')
frag.write(' const float texelSize = 1.0 / shadowmapSize.x;')
frag.write(' visibility = 0.0;')
frag.write(' visibility += float(texture(shadowMap0, lPos.xy).r + shadowsBias > lPos.z);')
frag.write(' visibility += float(texture(shadowMap0, lPos.xy + vec2(texelSize, 0.0)).r + shadowsBias > lPos.z) * 0.5;')
frag.write(' visibility += float(texture(shadowMap0, lPos.xy + vec2(-texelSize, 0.0)).r + shadowsBias > lPos.z) * 0.25;')
frag.write(' visibility += float(texture(shadowMap0, lPos.xy + vec2(0.0, texelSize)).r + shadowsBias > lPos.z) * 0.5;')
frag.write(' visibility += float(texture(shadowMap0, lPos.xy + vec2(0.0, -texelSize)).r + shadowsBias > lPos.z) * 0.25;')
frag.write(' visibility /= 2.5;')
frag.write(' visibility = max(visibility, 0.2);')
frag.write('}')
frag.add_include('std/shadows.glsl')
frag.add_uniform('vec2 lightProj', link='_lightPlaneProj')
frag.add_uniform('samplerCube shadowMap0')
frag.add_uniform('float pointBias', link='_pointShadowsBias')
frag.write('const float s = shadowmapCubePcfSize;') # TODO: incorrect...
frag.write('float compare = lpToDepth(ld - n * pointBias * 80, lightProj);')
frag.write('visibility = step(compare, texture(shadowMap0, -l + n * pointBias * 80).r);')
frag.write('direct += basecol * dotNL * pointCol * attenuate(distance(wposition, pointPos)) * visibility;')
if '_Clusters' in wrd.world_defs:
@ -736,7 +734,7 @@ def make_forward_base(con_mesh, parse_opacity=False):
frag.write('direct += sampleLight(')
frag.write(' wposition, n, vVec, dotNV, pointPos, pointCol, albedo, roughness, specular, f0')
if is_shadows:
frag.write(' , pointBias')
frag.write(' , 0, pointBias')
if '_Spot' in wrd.world_defs:
frag.write(' , true, spotData.x, spotData.y, spotDir')
frag.write(');')

View file

@ -41,7 +41,6 @@ def update_preset(self, context):
rpdat.arm_irradiance = True
rpdat.arm_radiance = True
rpdat.arm_radiance_sky = True
rpdat.arm_shadows_cubemap = True
elif self.rp_preset == 'VR':
rpdat.rp_renderer = 'Forward'
rpdat.rp_depthprepass = False
@ -76,7 +75,6 @@ def update_preset(self, context):
rpdat.arm_irradiance = True
rpdat.arm_radiance = True
rpdat.arm_radiance_sky = True
rpdat.arm_shadows_cubemap = True
elif self.rp_preset == 'Mobile':
rpdat.rp_renderer = 'Forward'
rpdat.rp_depthprepass = False
@ -111,7 +109,6 @@ def update_preset(self, context):
rpdat.arm_irradiance = True
rpdat.arm_radiance = False
rpdat.arm_radiance_sky = False
rpdat.arm_shadows_cubemap = False
elif self.rp_preset == 'Max':
rpdat.rp_renderer = 'Deferred'
rpdat.rp_shadows = True
@ -150,7 +147,6 @@ def update_preset(self, context):
rpdat.arm_irradiance = True
rpdat.arm_radiance = True
rpdat.arm_radiance_sky = True
rpdat.arm_shadows_cubemap = True
elif self.rp_preset == '2D/Baked':
rpdat.rp_renderer = 'Forward'
rpdat.rp_depthprepass = False
@ -185,7 +181,6 @@ def update_preset(self, context):
rpdat.arm_irradiance = False
rpdat.arm_radiance = False
rpdat.arm_radiance_sky = False
rpdat.arm_shadows_cubemap = False
update_renderpath(self, context)
def update_renderpath(self, context):
@ -394,7 +389,6 @@ class ArmRPListItem(bpy.types.PropertyGroup):
name="Soft Shadows", description="Soft shadows with variable penumbra (spot and non-cascaded sun light supported)", default='Off', update=assets.invalidate_shader_cache)
arm_soft_shadows_penumbra = IntProperty(name="Penumbra", description="Variable penumbra scale", default=1, min=0, max=10, update=assets.invalidate_shader_cache)
arm_soft_shadows_distance = FloatProperty(name="Distance", description="Variable penumbra distance", default=1.0, min=0, max=10, update=assets.invalidate_shader_cache)
arm_shadows_cubemap = BoolProperty(name="Cubemap", description="Use cubemap to capture point light shadows", default=True)
arm_ssrs = BoolProperty(name="SSRS", description="Screen-space ray-traced shadows", default=False, update=assets.invalidate_shader_cache)
arm_texture_filter = EnumProperty(
items=[('Anisotropic', 'Anisotropic', 'Anisotropic'),

View file

@ -798,7 +798,6 @@ class ArmRenderPathPanel(bpy.types.Panel):
row = self.row(col2, align=True, alignment='EXPAND')
self.prop(row, rpdat, 'arm_soft_shadows_penumbra')
self.prop(row, rpdat, 'arm_soft_shadows_distance')
self.prop(col, rpdat, 'arm_shadows_cubemap')
self.prop(col, rpdat, 'arm_pcfsize')