• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iTowns / itowns / 5691913681

pending completion
5691913681

push

github

mgermerie
chore: update three to r154

3933 of 5864 branches covered (67.07%)

Branch coverage included in aggregate %.

7778 of 9456 relevant lines covered (82.25%)

1619.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/src/Utils/ThreeUtils.js
1
/**
2
 * Find threejs textures from a material
3
 * @param {Material} material the threejs material holding textures
4
 * @returns {Array} an array of textures in the material
5
 */
6
function findTextures(material) {
7
    const textures = [];
1✔
8
    if (material.alphaMap) {
1!
9
        textures.push(material.map);
1✔
10
    }
11
    if (material.aoMap) {
1!
12
        textures.push(material.map);
1✔
13
    }
14
    if (material.bumpMap) {
1!
15
        textures.push(material.bumpMap);
1✔
16
    }
17
    if (material.displacementMap) {
1!
18
        textures.push(material.bumpMap);
1✔
19
    }
20
    if (material.emissiveMap) {
1!
21
        textures.push(material.emissiveMap);
1✔
22
    }
23
    if (material.envMap) {
1!
24
        textures.push(material.envMap);
1✔
25
    }
26
    if (material.lightMap) {
1!
27
        textures.push(material.envMap);
1✔
28
    }
29
    if (material.map) {
1!
30
        textures.push(material.map);
1✔
31
    }
32
    if (material.metalnessMap) {
1!
33
        textures.push(material.map);
1✔
34
    }
35
    if (material.normalMap) {
1!
36
        textures.push(material.map);
1✔
37
    }
38
    if (material.roughnessMap) {
1!
39
        textures.push(material.map);
1✔
40
    }
41
    if (material.specularMap) {
1!
42
        textures.push(material.specularMap);
1✔
43
    }
44
    return textures;
1✔
45
}
46

47
/**
48
 * Removes a material and its textures, memory will be freed.
49
 * IMPORTANT NOTE: the material and the texture must not be referenced by other threejs objects, otherwise the memory
50
 * won't be freed.
51
 * @param {Material} material the material to remove
52
 */
53
export default function disposeThreeMaterial(material) {
54
    const textures = findTextures(material);
1✔
55
    // Remove material
56
    if (Array.isArray(material)) {
1!
57
        for (const m of material) {
×
58
            m.dispose();
×
59
        }
×
60
    } else {
61
        material.dispose();
1✔
62
    }
63
    // Remove textures
64
    for (let i = 0; i < textures.length; i++) {
1✔
65
        textures[i].dispose();
12✔
66
    }
67
}
68

69
/**
70
 * Merge groups of an object3D when it can to reduce number of them + remove unused materials
71
 * Reduce draw call https://threejs.org/docs/index.html?q=geometry#api/en/core/BufferGeometry.groups
72
 *
73
 * @param {THREE.Object3D} object3D - object to get optimize
74
 */
75
export function optimizeGeometryGroups(object3D) {
76
    if (!object3D.geometry) {
20!
77
        return;
×
78
    }
79

80
    object3D.geometry.groups.sort((a, b) => (a.start - b.start) * -1); // [5,10,7] => [10,7,5]
35,944✔
81
    const lastIndex = object3D.geometry.groups.length - 1;
20✔
82
    let currentMaterialIndex = object3D.geometry.groups[lastIndex].materialIndex; // initialized with the lastest group
20✔
83
    const usedIndexMaterials = [currentMaterialIndex]; // compute materials actually used by group
20✔
84
    // for loop descendant to be able to splice in loop without modifying group index
85
    for (let index = lastIndex - 1; index >= 0; index--) { // begin at lastIndex - 1 because intialized with lastIndex
20✔
86
        const group = object3D.geometry.groups[index];
5,422✔
87
        if (group.materialIndex !== currentMaterialIndex) {
5,422✔
88
            // this is another group (!= materialIndex) take its material as ref to continue
89
            currentMaterialIndex = group.materialIndex;
1,623✔
90
            usedIndexMaterials.push(currentMaterialIndex);
1,623✔
91
            continue;
1,623✔
92
        } else {
93
            // indeed same group merge with previous group
94
            const previousGroup = object3D.geometry.groups[index + 1];
3,799✔
95
            previousGroup.count += group.count; // previous group wrap the current one
3,799✔
96
            object3D.geometry.groups.splice(index, 1); // remove group
3,799✔
97
        }
98
    }
99

100
    // clean unused material
20✔
101
    for (let index = object3D.material.length - 1; index >= 0; index--) {
54✔
102
        if (!usedIndexMaterials.includes(index)) {
54!
103
            // update all materialIndex in groups
104
            object3D.geometry.groups.forEach((group) => {
×
105
                if (group.materialIndex > index) {
×
106
                    // only materialIndex > at index are modified
107
                    group.materialIndex -= 1;
×
108
                }
109
            });
110

111
            // remove
112
            object3D.material.splice(index, 1);
×
113
        }
114
    }
115
}
116

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc