Skip to content

Commit bf92363

Browse files
committed
Flag texture as needsUpdate when texture properties change
1 parent c0bc685 commit bf92363

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/utils/material.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,40 @@ function setTextureProperties (texture, data) {
1616
var offset = data.offset || {x: 0, y: 0};
1717
var repeat = data.repeat || {x: 1, y: 1};
1818
var npot = data.npot || false;
19-
var anisotropy = data.anisotropy || 0;
19+
var anisotropy = data.anisotropy || THREE.Texture.DEFAULT_ANISOTROPY;
20+
var wrapS = texture.wrapS;
21+
var wrapT = texture.wrapT;
22+
var magFilter = texture.magFilter;
23+
var minFilter = texture.minFilter;
24+
2025
// To support NPOT textures, wrap must be ClampToEdge (not Repeat),
2126
// and filters must not use mipmaps (i.e. Nearest or Linear).
2227
if (npot) {
23-
texture.wrapS = THREE.ClampToEdgeWrapping;
24-
texture.wrapT = THREE.ClampToEdgeWrapping;
25-
texture.magFilter = THREE.LinearFilter;
26-
texture.minFilter = THREE.LinearFilter;
28+
wrapS = THREE.ClampToEdgeWrapping;
29+
wrapT = THREE.ClampToEdgeWrapping;
30+
magFilter = THREE.LinearFilter;
31+
minFilter = THREE.LinearFilter;
2732
}
2833

29-
// Don't bother setting repeat if it is 1/1. Power-of-two is required to repeat.
34+
// Set wrap mode to repeat only if repeat isn't 1/1. Power-of-two is required to repeat.
3035
if (repeat.x !== 1 || repeat.y !== 1) {
31-
texture.wrapS = THREE.RepeatWrapping;
32-
texture.wrapT = THREE.RepeatWrapping;
33-
texture.repeat.set(repeat.x, repeat.y);
34-
}
35-
// Don't bother setting offset if it is 0/0.
36-
if (offset.x !== 0 || offset.y !== 0) {
37-
texture.offset.set(offset.x, offset.y);
36+
wrapS = THREE.RepeatWrapping;
37+
wrapT = THREE.RepeatWrapping;
3838
}
3939

40-
// Only set anisotropy if it isn't 0, which indicates that the default value should be used.
41-
if (anisotropy !== 0) {
40+
// Apply texture properties
41+
texture.offset.set(offset.x, offset.y);
42+
texture.repeat.set(repeat.x, repeat.y);
43+
44+
if (texture.wrapS !== wrapS || texture.wrapT !== wrapT ||
45+
texture.magFilter !== magFilter || texture.minFilter !== minFilter ||
46+
texture.anisotropy !== anisotropy) {
47+
texture.wrapS = wrapS;
48+
texture.wrapT = wrapT;
49+
texture.magFilter = magFilter;
50+
texture.minFilter = minFilter;
4251
texture.anisotropy = anisotropy;
52+
texture.needsUpdate = true;
4353
}
4454
}
4555
module.exports.setTextureProperties = setTextureProperties;

tests/components/material.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ suite('material', function () {
9393
});
9494
});
9595

96+
test('updates texture when repeat changes', function (done) {
97+
var imageUrl = 'base/tests/assets/test.png';
98+
el.setAttribute('material', '');
99+
assert.notOk(el.components.material.material.texture);
100+
el.setAttribute('material', 'src: url(' + imageUrl + ')');
101+
el.addEventListener('materialtextureloaded', function (evt) {
102+
var loadedTexture = evt.detail.texture;
103+
assert.ok(el.components.material.material.map === loadedTexture);
104+
var version = loadedTexture.version;
105+
el.setAttribute('material', 'repeat', '2 2');
106+
assert.ok(el.components.material.material.map.version > version);
107+
done();
108+
});
109+
});
110+
96111
test('removes texture when src attribute removed', function (done) {
97112
var imageUrl = 'base/tests/assets/test.png';
98113
el.setAttribute('material', '');

0 commit comments

Comments
 (0)