-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Unify texture loading and map property fixes #5453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,19 +53,17 @@ module.exports.Shader = registerShader('phong', { | |
| */ | ||
| init: function (data) { | ||
| this.materialData = { color: new THREE.Color(), specular: new THREE.Color(), emissive: new THREE.Color() }; | ||
| this.textureSrc = null; | ||
| getMaterialData(data, this.materialData); | ||
| this.material = new THREE.MeshPhongMaterial(this.materialData); | ||
| utils.material.updateMap(this, data); | ||
| }, | ||
|
|
||
| update: function (data) { | ||
| this.updateMaterial(data); | ||
| utils.material.updateMap(this, data); | ||
| if (data.normalMap) { utils.material.updateDistortionMap('normal', this, data); } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checks no needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for |
||
| if (data.displacementMap) { utils.material.updateDistortionMap('displacement', this, data); } | ||
| if (data.ambientOcclusionMap) { utils.material.updateDistortionMap('ambientOcclusion', this, data); } | ||
| if (data.bump) { utils.material.updateDistortionMap('bump', this, data); } | ||
| utils.material.updateDistortionMap('normal', this, data); | ||
| utils.material.updateDistortionMap('displacement', this, data); | ||
| utils.material.updateDistortionMap('ambientOcclusion', this, data); | ||
| utils.material.updateDistortionMap('bump', this, data); | ||
| this.updateEnvMap(data); | ||
| }, | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,11 +69,11 @@ module.exports.Shader = registerShader('standard', { | |
| update: function (data) { | ||
| this.updateMaterial(data); | ||
| utils.material.updateMap(this, data); | ||
| if (data.normalMap) { utils.material.updateDistortionMap('normal', this, data); } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This guards no longer needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The guards prevented these textures from being removed. Similar to how the main (diffuse) map is handled, the underlying implementation of |
||
| if (data.displacementMap) { utils.material.updateDistortionMap('displacement', this, data); } | ||
| if (data.ambientOcclusionMap) { utils.material.updateDistortionMap('ambientOcclusion', this, data); } | ||
| if (data.metalnessMap) { utils.material.updateDistortionMap('metalness', this, data); } | ||
| if (data.roughnessMap) { utils.material.updateDistortionMap('roughness', this, data); } | ||
| utils.material.updateDistortionMap('normal', this, data); | ||
| utils.material.updateDistortionMap('displacement', this, data); | ||
| utils.material.updateDistortionMap('ambientOcclusion', this, data); | ||
| utils.material.updateDistortionMap('metalness', this, data); | ||
| utils.material.updateDistortionMap('roughness', this, data); | ||
| this.updateEnvMap(data); | ||
| }, | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,30 +16,40 @@ function setTextureProperties (texture, data) { | |
| var offset = data.offset || {x: 0, y: 0}; | ||
| var repeat = data.repeat || {x: 1, y: 1}; | ||
| var npot = data.npot || false; | ||
| var anisotropy = data.anisotropy || 0; | ||
| var anisotropy = data.anisotropy || THREE.Texture.DEFAULT_ANISOTROPY; | ||
| var wrapS = texture.wrapS; | ||
| var wrapT = texture.wrapT; | ||
| var magFilter = texture.magFilter; | ||
| var minFilter = texture.minFilter; | ||
|
|
||
| // To support NPOT textures, wrap must be ClampToEdge (not Repeat), | ||
| // and filters must not use mipmaps (i.e. Nearest or Linear). | ||
| if (npot) { | ||
| texture.wrapS = THREE.ClampToEdgeWrapping; | ||
| texture.wrapT = THREE.ClampToEdgeWrapping; | ||
| texture.magFilter = THREE.LinearFilter; | ||
| texture.minFilter = THREE.LinearFilter; | ||
| wrapS = THREE.ClampToEdgeWrapping; | ||
| wrapT = THREE.ClampToEdgeWrapping; | ||
| magFilter = THREE.LinearFilter; | ||
| minFilter = THREE.LinearFilter; | ||
| } | ||
|
|
||
| // Don't bother setting repeat if it is 1/1. Power-of-two is required to repeat. | ||
| // Set wrap mode to repeat only if repeat isn't 1/1. Power-of-two is required to repeat. | ||
| if (repeat.x !== 1 || repeat.y !== 1) { | ||
| texture.wrapS = THREE.RepeatWrapping; | ||
| texture.wrapT = THREE.RepeatWrapping; | ||
| texture.repeat.set(repeat.x, repeat.y); | ||
| } | ||
| // Don't bother setting offset if it is 0/0. | ||
| if (offset.x !== 0 || offset.y !== 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guard no longer needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The guard actually prevented offset from being set back to 0/0 after being set to something else. On top of that, changing the offset doesn't require a texture update, so there's nothing gained by skipping this. |
||
| texture.offset.set(offset.x, offset.y); | ||
| wrapS = THREE.RepeatWrapping; | ||
| wrapT = THREE.RepeatWrapping; | ||
| } | ||
|
|
||
| // Only set anisotropy if it isn't 0, which indicates that the default value should be used. | ||
| if (anisotropy !== 0) { | ||
| // Apply texture properties | ||
| texture.offset.set(offset.x, offset.y); | ||
| texture.repeat.set(repeat.x, repeat.y); | ||
|
|
||
| if (texture.wrapS !== wrapS || texture.wrapT !== wrapT || | ||
| texture.magFilter !== magFilter || texture.minFilter !== minFilter || | ||
| texture.anisotropy !== anisotropy) { | ||
| texture.wrapS = wrapS; | ||
| texture.wrapT = wrapT; | ||
| texture.magFilter = magFilter; | ||
| texture.minFilter = minFilter; | ||
| texture.anisotropy = anisotropy; | ||
| texture.needsUpdate = true; | ||
| } | ||
| } | ||
| module.exports.setTextureProperties = setTextureProperties; | ||
|
|
@@ -127,43 +137,15 @@ module.exports.updateMap = function (shader, data) { | |
| module.exports.updateDistortionMap = function (longType, shader, data) { | ||
| var shortType = longType; | ||
| if (longType === 'ambientOcclusion') { shortType = 'ao'; } | ||
| var el = shader.el; | ||
| var material = shader.material; | ||
| var rendererSystem = el.sceneEl.systems.renderer; | ||
| var src = data[longType + 'Map']; | ||
|
|
||
| var info = {}; | ||
| info.src = src; | ||
| info.src = data[longType + 'Map']; | ||
|
|
||
| // Pass through the repeat and offset to be handled by the material loader. | ||
| info.offset = data[longType + 'TextureOffset']; | ||
| info.repeat = data[longType + 'TextureRepeat']; | ||
| info.wrap = data[longType + 'TextureWrap']; | ||
|
|
||
| if (src) { | ||
| if (src === shader[longType + 'TextureSrc']) { return; } | ||
|
|
||
| // Texture added or changed. | ||
| shader[longType + 'TextureSrc'] = src; | ||
| el.sceneEl.systems.material.loadTexture(src, info, setMap); | ||
| return; | ||
| } | ||
|
|
||
| // Texture removed. | ||
| if (!material.map) { return; } | ||
| setMap(null); | ||
|
|
||
| function setMap (texture) { | ||
| var slot = shortType + 'Map'; | ||
| material[slot] = texture; | ||
| if (texture && COLOR_MAPS.has(slot)) { | ||
| rendererSystem.applyColorCorrection(texture); | ||
| } | ||
| if (texture) { | ||
| setTextureProperties(texture, data); | ||
| } | ||
| material.needsUpdate = true; | ||
| handleTextureEvents(el, texture); | ||
| } | ||
| return module.exports.updateMapMaterialFromData(shortType + 'Map', 'src', shader, info); | ||
| }; | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove because of redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this is in the
initmethod, which is followed byupdatewhich also callsupdateMap. Bothflatandstandardlet the update handle this andphongwas the odd one out.