|
| 1 | +import { RefCountedCache } from '../core/ref-counted-cache.js'; |
1 | 2 | import { BoundingBox } from '../shape/bounding-box.js';
|
2 | 3 | import { BoundingSphere } from '../shape/bounding-sphere.js';
|
3 | 4 |
|
@@ -168,33 +169,17 @@ class MeshInstance {
|
168 | 169 | static lightmapParamNames = ["texture_lightMap", "texture_dirLightMap"];
|
169 | 170 |
|
170 | 171 | // cache of lightmaps internally created by baking using Lightmapper
|
171 |
| - // the key is the lightmap (texture), the value is reference count .. when it reaches 0, the lightmap gets destroyed. |
172 | 172 | // this allows us to automatically release realtime baked lightmaps when mesh instances using them are destroyed
|
173 |
| - static _lightmapCache = new Map(); |
| 173 | + static _lightmapCache = new RefCountedCache(); |
174 | 174 |
|
175 | 175 | // add texture reference to lightmap cache
|
176 | 176 | static incRefLightmap(texture) {
|
177 |
| - let refCount = MeshInstance._lightmapCache.get(texture) || 0; |
178 |
| - refCount++; |
179 |
| - MeshInstance._lightmapCache.set(texture, refCount); |
| 177 | + this._lightmapCache.incRef(texture); |
180 | 178 | }
|
181 | 179 |
|
182 | 180 | // remove texture reference from lightmap cache
|
183 | 181 | static decRefLightmap(texture) {
|
184 |
| - if (texture) { |
185 |
| - let refCount = MeshInstance._lightmapCache.get(texture); |
186 |
| - if (refCount) { |
187 |
| - refCount--; |
188 |
| - if (refCount === 0) { |
189 |
| - // destroy texture and remove it from cache |
190 |
| - MeshInstance._lightmapCache.delete(texture); |
191 |
| - texture.destroy(); |
192 |
| - } else { |
193 |
| - // update new ref count in the cache |
194 |
| - MeshInstance._lightmapCache.set(texture, refCount); |
195 |
| - } |
196 |
| - } |
197 |
| - } |
| 182 | + this._lightmapCache.decRef(texture); |
198 | 183 | }
|
199 | 184 |
|
200 | 185 | get renderStyle() {
|
|
0 commit comments