Skip to content

Commit

Permalink
Only enable shadowMap if an entity uses the shadow component and the …
Browse files Browse the repository at this point in the history
…shadow system is enabled
  • Loading branch information
mrxz committed Nov 29, 2023
1 parent 8ae062f commit 2f0f0ab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/systems/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ module.exports.System = registerSystem('shadow', {

this.shadowMapEnabled = false;

if (!sceneEl.renderer) { return; } // For tests.

sceneEl.renderer.shadowMap.type = SHADOW_MAP_TYPE_MAP[data.type];
sceneEl.renderer.shadowMap.autoUpdate = data.autoUpdate;
this.setShadowMapEnabled(this.shadowMapEnabled);
},

update: function (prevData) {
if (prevData.enabled !== this.data.enabled) {
this.setShadowMapEnabled(this.data.enabled);
this.setShadowMapEnabled(this.shadowMapEnabled);
}
},

Expand All @@ -44,10 +41,25 @@ module.exports.System = registerSystem('shadow', {
* @param {boolean} enabled
*/
setShadowMapEnabled: function (enabled) {
var sceneEl = this.sceneEl;
var renderer = this.sceneEl.renderer;
this.shadowMapEnabled = this.data.enabled && enabled;
if (renderer) {
renderer.shadowMap.enabled = this.shadowMapEnabled;

this.shadowMapEnabled = enabled;
var newEnabledState = this.data.enabled && this.shadowMapEnabled;
if (renderer && newEnabledState !== renderer.shadowMap.enabled) {
renderer.shadowMap.enabled = newEnabledState;

// If scene has already rendered, materials must be updated.
if (sceneEl.hasLoaded) {
sceneEl.object3D.traverse(function (node) {
if (node.material) {
var materials = Array.isArray(node.material) ? node.material : [node.material];
for (var i = 0; i < materials.length; i++) {
materials[i].needsUpdate = true;
}
}
});
}
}
}
});
41 changes: 39 additions & 2 deletions tests/systems/shadow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ suite('shadow system', function () {
});

suite('init', function () {
test('enabled by default', function (done) {
test('shadowMap disabled by default', function (done) {
var el = this.el;
this.el.addEventListener('loaded', function () {
var sceneEl = el.sceneEl;
var renderer = sceneEl.renderer;
assert.ok(renderer.shadowMap.enabled);
assert.notOk(renderer.shadowMap.enabled);
done();
});
});
Expand All @@ -32,6 +32,43 @@ suite('shadow system', function () {

document.body.appendChild(div);
});

test('shadwMap automatically enables', function (done) {
var el = this.el;
el.setAttribute('shadow', 'receive: false');
el.addEventListener('loaded', function () {
var renderer = el.sceneEl.renderer;
assert.ok(renderer.shadowMap.enabled);
done();
});
});
});

suite('enabled', function () {
test('shadowMap remains disabled when system is explicitly disabled', function (done) {
var el = this.el;
el.setAttribute('shadow', 'receive: false');
el.sceneEl.setAttribute('shadow', 'enabled: false');
el.addEventListener('loaded', function () {
var renderer = el.sceneEl.renderer;
assert.notOk(renderer.shadowMap.enabled);
done();
});
});

test('shadowMap enables when system is (re)enabled', function (done) {
var el = this.el;
el.setAttribute('shadow', 'receive: false');
el.sceneEl.setAttribute('shadow', 'enabled: false');
el.addEventListener('loaded', function () {
var renderer = el.sceneEl.renderer;
assert.notOk(renderer.shadowMap.enabled);

el.sceneEl.setAttribute('shadow', 'enabled: true');
assert.ok(renderer.shadowMap.enabled);
done();
});
});
});

suite('setShadowMapEnabled', function () {
Expand Down

0 comments on commit 2f0f0ab

Please sign in to comment.