fix: Tweak dynamic shadows

This commit is contained in:
Vsevolod Kremianskii 2020-11-28 18:13:49 +07:00
parent 9619a8ec04
commit 1651d72b72
4 changed files with 28 additions and 13 deletions

View file

@ -258,6 +258,15 @@ float getShadowmapDepth(int index, vec2 texCoords) {
} else {
return texture(uShadowmaps[0], texCoords).r;
}
/*
if (index == 2) {
return texture(uShadowmaps[2], texCoords).r;
else if (index == 1) {
return texture(uShadowmaps[1], texCoords).r;
} else {
return texture(uShadowmaps[0], texCoords).r;
}
*/
}
void applyShadows(vec3 normal, inout vec3 color) {
@ -272,11 +281,7 @@ void applyShadows(vec3 normal, inout vec3 color) {
float closestDepth = getShadowmapDepth(i, projCoords.xy);
float currentDepth = projCoords.z;
vec3 surfaceToLight = uShadowLights[i].position.xyz - fragPosition;
vec3 lightDir = normalize(surfaceToLight);
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
if (currentDepth - bias > closestDepth) {
if (currentDepth > closestDepth) {
color *= 1.0 - shadow;
}
}

View file

@ -201,6 +201,7 @@ void WorldRenderPipeline::drawResult() const {
glActiveTexture(GL_TEXTURE0);
_geometry.bindColorBuffer(0);
//_shadows[0]->bindDepthBuffer();
glActiveTexture(GL_TEXTURE0 + TextureUniforms::bloom);
_verticalBlur.bindColorBuffer(0);

View file

@ -35,6 +35,8 @@ namespace reone {
namespace scene {
static const float kMaxLightDistance = 16.0f;
SceneGraph::SceneGraph(const GraphicsOptions &opts) : _opts(opts) {
}
@ -138,20 +140,27 @@ void SceneGraph::refreshShadowLights() {
if (!_refNode) return;
glm::vec3 refNodePos(_refNode->absoluteTransform()[3]);
vector<LightSceneNode *> lights;
getLightsAt(_refNode->absoluteTransform()[3], lights);
getLightsAt(refNodePos, lights);
for (auto &light : lights) {
if (!light->shadow()) continue;
if (_shadowLights.size() >= kMaxShadowLightCount) break;
glm::mat4 projection(getLightProjection(*light));
glm::mat4 view(light->absoluteTransformInverse());
glm::vec3 lightPos(light->absoluteTransform()[3]);
glm::vec3 lightToRefNode(lightPos - refNodePos);
glm::vec3 lightDir(glm::normalize(lightToRefNode));
float dist = glm::min(glm::length(lightToRefNode), kMaxLightDistance);
lightPos = refNodePos + dist * lightDir;
glm::mat4 view(glm::lookAt(lightPos, refNodePos, glm::vec3(0.0f, 0.0f, 1.0f)));
ShadowLight shadowLight;
shadowLight.position = light->absoluteTransform()[3];
shadowLight.position = glm::vec4(lightPos, 1.0f);
shadowLight.view = view;
shadowLight.projection = projection;
shadowLight.projection = getLightProjection();
_shadowLights.push_back(move(shadowLight));
}
}
@ -229,8 +238,8 @@ void SceneGraph::getLightsAt(const glm::vec3 &position, vector<LightSceneNode *>
}
}
glm::mat4 SceneGraph::getLightProjection(const LightSceneNode &light) const {
return glm::perspective(glm::radians(100.0f), 1.0f, 1.0f, light.radius());
glm::mat4 SceneGraph::getLightProjection() const {
return glm::perspective(glm::radians(90.0f), 1.0f, 1.0f, 1000.0f);
}
const glm::vec3 &SceneGraph::ambientLightColor() const {

View file

@ -82,7 +82,7 @@ private:
void refreshMeshesAndLights();
void refreshShadowLights();
glm::mat4 getLightProjection(const LightSceneNode &light) const;
glm::mat4 getLightProjection() const;
};
} // namespace scene