Skip to content

Commit

Permalink
Disable lights for objects with baked lighting
Browse files Browse the repository at this point in the history
Don't apply lighting to objects when they have a lightmap texture and
the light is set to BAKE_ALL. This prevents applying the same direct
light twice on the same object and makes setting up scenes with mixed
lighting much easier.
  • Loading branch information
JFonS committed Oct 11, 2020
1 parent 18fc857 commit fcb00ca
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 16 deletions.
19 changes: 18 additions & 1 deletion doc/classes/VisualServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,17 @@
Sets whether to use a dual paraboloid or a cubemap for the shadow map. Dual paraboloid is faster but may suffer from artifacts. Equivalent to [member OmniLight.omni_shadow_mode].
</description>
</method>
<method name="light_set_bake_mode">
<return type="void">
</return>
<argument index="0" name="light" type="RID">
</argument>
<argument index="1" name="bake_mode" type="int" enum="VisualServer.LightBakeMode">
</argument>
<description>
Sets the bake mode for this light, see [enum LightBakeMode] for options. The bake mode affects how the light will be baked in [BakedLightmap]s and [GIProbe]s.
</description>
</method>
<method name="light_set_color">
<return type="void">
</return>
Expand Down Expand Up @@ -2250,7 +2261,7 @@
<argument index="1" name="enabled" type="bool">
</argument>
<description>
Sets whether GI probes capture light information from this light.
Sets whether GI probes capture light information from this light. [i]Deprecated method.[/i] Use [method light_set_bake_mode] instead. This method is only kept for compatibility reasons and calls [method light_set_bake_mode] internally, setting the bake mode to [constant LIGHT_BAKE_DISABLED] or [constant LIGHT_BAKE_INDIRECT] depending on the given parameter.
</description>
</method>
<method name="lightmap_capture_create">
Expand Down Expand Up @@ -4517,6 +4528,12 @@
<constant name="LIGHT_PARAM_MAX" value="15" enum="LightParam">
Represents the size of the [enum LightParam] enum.
</constant>
<constant name="LIGHT_BAKE_DISABLED" value="0" enum="LightBakeMode">
</constant>
<constant name="LIGHT_BAKE_INDIRECT" value="1" enum="LightBakeMode">
</constant>
<constant name="LIGHT_BAKE_ALL" value="2" enum="LightBakeMode">
</constant>
<constant name="LIGHT_OMNI_SHADOW_DUAL_PARABOLOID" value="0" enum="LightOmniShadowMode">
Use a dual paraboloid shadow map for omni lights.
</constant>
Expand Down
2 changes: 2 additions & 0 deletions drivers/dummy/rasterizer_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ class RasterizerStorageDummy : public RasterizerStorage {
void light_set_cull_mask(RID p_light, uint32_t p_mask) {}
void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {}
void light_set_use_gi(RID p_light, bool p_enabled) {}
void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) {}

void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {}
void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) {}
Expand All @@ -511,6 +512,7 @@ class RasterizerStorageDummy : public RasterizerStorage {
float light_get_param(RID p_light, VS::LightParam p_param) { return 0.0; }
Color light_get_color(RID p_light) { return Color(); }
bool light_get_use_gi(RID p_light) { return false; }
VS::LightBakeMode light_get_bake_mode(RID p_light) { return VS::LightBakeMode::LIGHT_BAKE_DISABLED; }
uint64_t light_get_version(RID p_light) const { return 0; }

/* PROBE API */
Expand Down
3 changes: 3 additions & 0 deletions drivers/gles2/rasterizer_scene_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,9 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,

if (!unshaded && e->light_index < RenderList::MAX_LIGHTS) {
light = render_light_instances[e->light_index];
if (e->light_mode == LightMode::LIGHTMODE_LIGHTMAP && light->light_ptr->bake_mode == VS::LIGHT_BAKE_ALL) {
light = NULL; // Don't use this light, it is already included in the lightmap
}
}

if (light != prev_light) {
Expand Down
17 changes: 13 additions & 4 deletions drivers/gles2/rasterizer_storage_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3816,7 +3816,7 @@ RID RasterizerStorageGLES2::light_create(VS::LightType p_type) {
light->directional_blend_splits = false;
light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
light->reverse_cull = false;
light->use_gi = true;
light->bake_mode = VS::LIGHT_BAKE_INDIRECT;
light->version = 0;

return light_owner.make_rid(light);
Expand Down Expand Up @@ -3905,10 +3905,15 @@ void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool
}

void RasterizerStorageGLES2::light_set_use_gi(RID p_light, bool p_enabled) {
WARN_DEPRECATED_MSG("'VisualServer.light_set_use_gi' is deprecated and will be removed in a future version. Use 'VisualServer.light_set_bake_mode' instead.");
light_set_bake_mode(p_light, p_enabled ? VS::LightBakeMode::LIGHT_BAKE_INDIRECT : VS::LightBakeMode::LIGHT_BAKE_DISABLED);
}

void RasterizerStorageGLES2::light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) {
Light *light = light_owner.getornull(p_light);
ERR_FAIL_COND(!light);

light->use_gi = p_enabled;
light->bake_mode = p_bake_mode;

light->version++;
light->instance_change_notify(true, false);
Expand Down Expand Up @@ -4010,10 +4015,14 @@ Color RasterizerStorageGLES2::light_get_color(RID p_light) {
}

bool RasterizerStorageGLES2::light_get_use_gi(RID p_light) {
return light_get_bake_mode(p_light) != VS::LightBakeMode::LIGHT_BAKE_DISABLED;
}

VS::LightBakeMode RasterizerStorageGLES2::light_get_bake_mode(RID p_light) {
Light *light = light_owner.getornull(p_light);
ERR_FAIL_COND_V(!light, false);
ERR_FAIL_COND_V(!light, VS::LightBakeMode::LIGHT_BAKE_DISABLED);

return light->use_gi;
return light->bake_mode;
}

bool RasterizerStorageGLES2::light_has_shadow(RID p_light) const {
Expand Down
4 changes: 3 additions & 1 deletion drivers/gles2/rasterizer_storage_gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,10 @@ class RasterizerStorageGLES2 : public RasterizerStorage {
bool shadow;
bool negative;
bool reverse_cull;
bool use_gi;

uint32_t cull_mask;

VS::LightBakeMode bake_mode;
VS::LightOmniShadowMode omni_shadow_mode;
VS::LightOmniShadowDetail omni_shadow_detail;

Expand All @@ -960,6 +960,7 @@ class RasterizerStorageGLES2 : public RasterizerStorage {
virtual void light_set_cull_mask(RID p_light, uint32_t p_mask);
virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
virtual void light_set_use_gi(RID p_light, bool p_enabled);
virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode);

virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode);
virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail);
Expand All @@ -980,6 +981,7 @@ class RasterizerStorageGLES2 : public RasterizerStorage {
virtual float light_get_param(RID p_light, VS::LightParam p_param);
virtual Color light_get_color(RID p_light);
virtual bool light_get_use_gi(RID p_light);
virtual VS::LightBakeMode light_get_bake_mode(RID p_light);

virtual AABB light_get_aabb(RID p_light) const;
virtual uint64_t light_get_version(RID p_light) const;
Expand Down
9 changes: 7 additions & 2 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,8 +1849,13 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform

for (int i = 0; i < lc; i++) {
LightInstance *li = light_instance_owner.getornull(lights[i]);
if (!li || li->last_pass != render_pass) //not visible
continue;
if (!li || li->last_pass != render_pass) {
continue; // Not visible
}

if (e->instance->baked_light && li->light_ptr->bake_mode == VS::LightBakeMode::LIGHT_BAKE_ALL) {
continue; // This light is already included in the lightmap
}

if (li && li->light_ptr->type == VS::LIGHT_OMNI) {
if (omni_count < maxobj && e->instance->layer_mask & li->light_ptr->cull_mask) {
Expand Down
18 changes: 14 additions & 4 deletions drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5479,7 +5479,7 @@ RID RasterizerStorageGLES3::light_create(VS::LightType p_type) {
light->directional_blend_splits = false;
light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
light->reverse_cull = false;
light->use_gi = true;
light->bake_mode = VS::LIGHT_BAKE_INDIRECT;
light->version = 0;

return light_owner.make_rid(light);
Expand Down Expand Up @@ -5572,14 +5572,20 @@ void RasterizerStorageGLES3::light_set_reverse_cull_face_mode(RID p_light, bool
}

void RasterizerStorageGLES3::light_set_use_gi(RID p_light, bool p_enabled) {
WARN_DEPRECATED_MSG("'VisualServer.light_set_use_gi' is deprecated and will be removed in a future version. Use 'VisualServer.light_set_bake_mode' instead.");
light_set_bake_mode(p_light, p_enabled ? VS::LightBakeMode::LIGHT_BAKE_INDIRECT : VS::LightBakeMode::LIGHT_BAKE_DISABLED);
}

void RasterizerStorageGLES3::light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) {
Light *light = light_owner.getornull(p_light);
ERR_FAIL_COND(!light);

light->use_gi = p_enabled;
light->bake_mode = p_bake_mode;

light->version++;
light->instance_change_notify(true, false);
}

void RasterizerStorageGLES3::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {

Light *light = light_owner.getornull(p_light);
Expand Down Expand Up @@ -5686,10 +5692,14 @@ Color RasterizerStorageGLES3::light_get_color(RID p_light) {
}

bool RasterizerStorageGLES3::light_get_use_gi(RID p_light) {
return light_get_bake_mode(p_light) != VS::LightBakeMode::LIGHT_BAKE_DISABLED;
}

VS::LightBakeMode RasterizerStorageGLES3::light_get_bake_mode(RID p_light) {
Light *light = light_owner.getornull(p_light);
ERR_FAIL_COND_V(!light, false);
ERR_FAIL_COND_V(!light, VS::LightBakeMode::LIGHT_BAKE_DISABLED);

return light->use_gi;
return light->bake_mode;
}

bool RasterizerStorageGLES3::light_has_shadow(RID p_light) const {
Expand Down
4 changes: 3 additions & 1 deletion drivers/gles3/rasterizer_storage_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
bool shadow;
bool negative;
bool reverse_cull;
bool use_gi;
VS::LightBakeMode bake_mode;
uint32_t cull_mask;
VS::LightOmniShadowMode omni_shadow_mode;
VS::LightOmniShadowDetail omni_shadow_detail;
Expand All @@ -962,6 +962,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
virtual void light_set_cull_mask(RID p_light, uint32_t p_mask);
virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
virtual void light_set_use_gi(RID p_light, bool p_enabled);
virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode);

virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode);
virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail);
Expand All @@ -982,6 +983,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
virtual float light_get_param(RID p_light, VS::LightParam p_param);
virtual Color light_get_color(RID p_light);
virtual bool light_get_use_gi(RID p_light);
virtual VS::LightBakeMode light_get_bake_mode(RID p_light);

virtual AABB light_get_aabb(RID p_light) const;
virtual uint64_t light_get_version(RID p_light) const;
Expand Down
2 changes: 1 addition & 1 deletion scene/3d/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ PoolVector<Face3> Light::get_faces(uint32_t p_usage_flags) const {

void Light::set_bake_mode(BakeMode p_mode) {
bake_mode = p_mode;
VS::get_singleton()->light_set_use_gi(light, p_mode != BAKE_DISABLED);
VS::get_singleton()->light_set_bake_mode(light, VS::LightBakeMode(bake_mode));
}

Light::BakeMode Light::get_bake_mode() const {
Expand Down
2 changes: 2 additions & 0 deletions servers/visual/rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ class RasterizerStorage {
virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) = 0;
virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) = 0;
virtual void light_set_use_gi(RID p_light, bool p_enable) = 0;
virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) = 0;

virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) = 0;
virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) = 0;
Expand All @@ -399,6 +400,7 @@ class RasterizerStorage {
virtual float light_get_param(RID p_light, VS::LightParam p_param) = 0;
virtual Color light_get_color(RID p_light) = 0;
virtual bool light_get_use_gi(RID p_light) = 0;
virtual VS::LightBakeMode light_get_bake_mode(RID p_light) = 0;
virtual uint64_t light_get_version(RID p_light) const = 0;

/* PROBE API */
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class VisualServerRaster : public VisualServer {
BIND2(light_set_cull_mask, RID, uint32_t)
BIND2(light_set_reverse_cull_face_mode, RID, bool)
BIND2(light_set_use_gi, RID, bool)
BIND2(light_set_bake_mode, RID, LightBakeMode)

BIND2(light_omni_set_shadow_mode, RID, LightOmniShadowMode)
BIND2(light_omni_set_shadow_detail, RID, LightOmniShadowDetail)
Expand Down
4 changes: 2 additions & 2 deletions servers/visual/visual_server_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3120,7 +3120,7 @@ bool VisualServerScene::_check_gi_probe(Instance *p_gi_probe) {

for (List<Instance *>::Element *E = p_gi_probe->scenario->directional_lights.front(); E; E = E->next()) {

if (!VSG::storage->light_get_use_gi(E->get()->base))
if (VSG::storage->light_get_bake_mode(E->get()->base) == VS::LightBakeMode::LIGHT_BAKE_DISABLED)
continue;

InstanceGIProbeData::LightCache lc;
Expand All @@ -3143,7 +3143,7 @@ bool VisualServerScene::_check_gi_probe(Instance *p_gi_probe) {

for (Set<Instance *>::Element *E = probe_data->lights.front(); E; E = E->next()) {

if (!VSG::storage->light_get_use_gi(E->get()->base))
if (VSG::storage->light_get_bake_mode(E->get()->base) == VS::LightBakeMode::LIGHT_BAKE_DISABLED)
continue;

InstanceGIProbeData::LightCache lc;
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class VisualServerWrapMT : public VisualServer {
FUNC2(light_set_cull_mask, RID, uint32_t)
FUNC2(light_set_reverse_cull_face_mode, RID, bool)
FUNC2(light_set_use_gi, RID, bool)
FUNC2(light_set_bake_mode, RID, LightBakeMode)

FUNC2(light_omni_set_shadow_mode, RID, LightOmniShadowMode)
FUNC2(light_omni_set_shadow_detail, RID, LightOmniShadowDetail)
Expand Down
5 changes: 5 additions & 0 deletions servers/visual_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ void VisualServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("light_set_cull_mask", "light", "mask"), &VisualServer::light_set_cull_mask);
ClassDB::bind_method(D_METHOD("light_set_reverse_cull_face_mode", "light", "enabled"), &VisualServer::light_set_reverse_cull_face_mode);
ClassDB::bind_method(D_METHOD("light_set_use_gi", "light", "enabled"), &VisualServer::light_set_use_gi);
ClassDB::bind_method(D_METHOD("light_set_bake_mode", "light", "bake_mode"), &VisualServer::light_set_bake_mode);

ClassDB::bind_method(D_METHOD("light_omni_set_shadow_mode", "light", "mode"), &VisualServer::light_omni_set_shadow_mode);
ClassDB::bind_method(D_METHOD("light_omni_set_shadow_detail", "light", "detail"), &VisualServer::light_omni_set_shadow_detail);
Expand Down Expand Up @@ -2154,6 +2155,10 @@ void VisualServer::_bind_methods() {
BIND_ENUM_CONSTANT(LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE);
BIND_ENUM_CONSTANT(LIGHT_PARAM_MAX);

BIND_ENUM_CONSTANT(LIGHT_BAKE_DISABLED);
BIND_ENUM_CONSTANT(LIGHT_BAKE_INDIRECT);
BIND_ENUM_CONSTANT(LIGHT_BAKE_ALL);

BIND_ENUM_CONSTANT(LIGHT_OMNI_SHADOW_DUAL_PARABOLOID);
BIND_ENUM_CONSTANT(LIGHT_OMNI_SHADOW_CUBE);
BIND_ENUM_CONSTANT(LIGHT_OMNI_SHADOW_DETAIL_VERTICAL);
Expand Down
10 changes: 10 additions & 0 deletions servers/visual_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ class VisualServer : public Object {
virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) = 0;
virtual void light_set_use_gi(RID p_light, bool p_enable) = 0;

// bake mode
enum LightBakeMode {
LIGHT_BAKE_DISABLED,
LIGHT_BAKE_INDIRECT,
LIGHT_BAKE_ALL
};

virtual void light_set_bake_mode(RID p_light, LightBakeMode p_bake_mode) = 0;

// omni light
enum LightOmniShadowMode {
LIGHT_OMNI_SHADOW_DUAL_PARABOLOID,
Expand Down Expand Up @@ -1098,6 +1107,7 @@ VARIANT_ENUM_CAST(VisualServer::Features);
VARIANT_ENUM_CAST(VisualServer::MultimeshTransformFormat);
VARIANT_ENUM_CAST(VisualServer::MultimeshColorFormat);
VARIANT_ENUM_CAST(VisualServer::MultimeshCustomDataFormat);
VARIANT_ENUM_CAST(VisualServer::LightBakeMode);
VARIANT_ENUM_CAST(VisualServer::LightOmniShadowMode);
VARIANT_ENUM_CAST(VisualServer::LightOmniShadowDetail);
VARIANT_ENUM_CAST(VisualServer::LightDirectionalShadowMode);
Expand Down

0 comments on commit fcb00ca

Please sign in to comment.