Skip to content

Commit cd525b1

Browse files
ngokevinJustin Rogers
authored andcommitted
also apply newly set materials to obj-model recursively (aframevr#4062)
1 parent 7082bd9 commit cd525b1

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

‎src/components/obj-model.js‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ module.exports.Component = registerComponent('obj-model', {
1111
},
1212

1313
init: function () {
14+
var self = this;
15+
1416
this.model = null;
1517
this.objLoader = new THREE.OBJLoader();
1618
this.mtlLoader = new THREE.MTLLoader(this.objLoader.manager);
1719
// Allow cross-origin images to be loaded.
1820
this.mtlLoader.crossOrigin = '';
21+
22+
this.el.addEventListener('componentinitialized', function (evt) {
23+
if (!self.model) { return; }
24+
if (evt.detail.name !== 'material') { return; }
25+
self.applyMaterial();
26+
});
1927
},
2028

2129
update: function () {
@@ -70,19 +78,23 @@ module.exports.Component = registerComponent('obj-model', {
7078

7179
// .OBJ only.
7280
objLoader.load(objUrl, function loadObjOnly (objModel) {
73-
// Apply material.
74-
var material = el.components.material;
75-
if (material) {
76-
objModel.traverse(function (child) {
77-
if (child instanceof THREE.Mesh) {
78-
child.material = material.material;
79-
}
80-
});
81-
}
82-
8381
self.model = objModel;
82+
self.applyMaterial();
8483
el.setObject3D('mesh', objModel);
8584
el.emit('model-loaded', {format: 'obj', model: objModel});
8685
});
86+
},
87+
88+
/**
89+
* Apply material from material component recursively.
90+
*/
91+
applyMaterial: function () {
92+
var material = this.el.components.material;
93+
if (!material) { return; }
94+
this.model.traverse(function (child) {
95+
if (child instanceof THREE.Mesh) {
96+
child.material = material.material;
97+
}
98+
});
8799
}
88100
});

‎tests/components/obj-model.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,18 @@ suite('obj-model', function () {
8686
});
8787
el.setAttribute('obj-model', 'obj', '#obj');
8888
});
89+
90+
test('can load .OBJ with reset material', function (done) {
91+
var el = this.el;
92+
el.setAttribute('material', 'color', 'red');
93+
el.addEventListener('object3dset', () => {
94+
el.removeAttribute('material');
95+
el.setAttribute('material', 'color', 'blue');
96+
setTimeout(() => {
97+
assert.equal(el.getObject3D('mesh').children[0].material.color.b, 1);
98+
done();
99+
});
100+
});
101+
el.setAttribute('obj-model', 'obj', '#obj');
102+
});
89103
});

0 commit comments

Comments
 (0)