-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(3dtiles): fix and document 3d tiles material overriding
- Loading branch information
Showing
4 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Find threejs textures from a material | ||
* @param {Material} material the threejs material holding textures | ||
* @returns {Array} an array of textures in the material | ||
*/ | ||
function findTextures(material) { | ||
const textures = []; | ||
if (material.alphaMap) { | ||
textures.push(material.map); | ||
} | ||
if (material.aoMap) { | ||
textures.push(material.map); | ||
} | ||
if (material.bumpMap) { | ||
textures.push(material.bumpMap); | ||
} | ||
if (material.displacementMap) { | ||
textures.push(material.bumpMap); | ||
} | ||
if (material.emissiveMap) { | ||
textures.push(material.emissiveMap); | ||
} | ||
if (material.envMap) { | ||
textures.push(material.envMap); | ||
} | ||
if (material.lightMap) { | ||
textures.push(material.envMap); | ||
} | ||
if (material.map) { | ||
textures.push(material.map); | ||
} | ||
if (material.metalnessMap) { | ||
textures.push(material.map); | ||
} | ||
if (material.normalMap) { | ||
textures.push(material.map); | ||
} | ||
if (material.roughnessMap) { | ||
textures.push(material.map); | ||
} | ||
if (material.specularMap) { | ||
textures.push(material.specularMap); | ||
} | ||
return textures; | ||
} | ||
|
||
/** | ||
* Removes a material and its textures, memory will be freed. | ||
* IMPORTANT NOTE: the material and the texture must not be referenced by other threejs objects, otherwise the memory | ||
* won't be freed. | ||
* @param {Material} material the material to remove | ||
*/ | ||
export default function disposeThreeMaterial(material) { | ||
const textures = findTextures(material); | ||
// Remove material | ||
if (Array.isArray(material)) { | ||
for (const m of material) { | ||
m.dispose(); | ||
} | ||
} else { | ||
material.dispose(); | ||
} | ||
// Remove textures | ||
for (let i = 0; i < textures.length; i++) { | ||
textures[i].dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import assert from 'assert'; | ||
import * as THREE from 'three'; | ||
import disposeThreeMaterial from 'Utils/ThreeUtils'; | ||
|
||
describe('ThreeJS Utils', function () { | ||
it('Should dispose material and textures', function () { | ||
// Testing if material and textures are correctly disposed is not easy since dispose dispatches an event to | ||
// indicate that some WebGLRenderer GPU resources should be remove. A way to test that is to check the | ||
// WebGLRenderer.info value which lists used GPU resources but WebGLRenderer is not available for unit tests. | ||
// Another way, that is implemented here, is to create a material and a all possible textures and to count the | ||
// number of dispatched 'dispose' events | ||
var disposeCounter = { | ||
nb: 0, | ||
}; | ||
const increment = function () { | ||
this.nb++; | ||
}; | ||
|
||
const material = new THREE.MeshStandardMaterial(); | ||
material.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.alphaMap = new THREE.Texture(); | ||
material.alphaMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.aoMap = new THREE.Texture(); | ||
material.aoMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.bumpMap = new THREE.Texture(); | ||
material.bumpMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.displacementMap = new THREE.Texture(); | ||
material.displacementMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.emissiveMap = new THREE.Texture(); | ||
material.emissiveMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.envMap = new THREE.Texture(); | ||
material.envMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.lightMap = new THREE.Texture(); | ||
material.lightMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.map = new THREE.Texture(); | ||
material.map.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.metalnessMap = new THREE.Texture(); | ||
material.metalnessMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.normalMap = new THREE.Texture(); | ||
material.normalMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.roughnessMap = new THREE.Texture(); | ||
material.roughnessMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
material.specularMap = new THREE.Texture(); | ||
material.specularMap.addEventListener('dispose', increment.bind(disposeCounter)); | ||
|
||
disposeThreeMaterial(material); | ||
|
||
assert.equal(disposeCounter.nb, 13); | ||
}); | ||
}); |