(geometry)
| 25 | } |
| 26 | |
| 27 | ensureCached(geometry) { |
| 28 | const gid = geometry.gid; |
| 29 | if (!gid) { |
| 30 | throw new Error('The p5.Geometry you passed in has no gid property!'); |
| 31 | } |
| 32 | |
| 33 | if (this.isCached(gid)) return this.getCached(geometry); |
| 34 | |
| 35 | // Cache maintenance |
| 36 | this.freeBuffers(gid); |
| 37 | if (Object.keys(this.cache).length > 1000) { |
| 38 | const key = Object.keys(this.cache)[0]; |
| 39 | this.freeBuffers(key); |
| 40 | } |
| 41 | |
| 42 | const buffers = { geometry }; |
| 43 | this.cache[gid] = buffers; |
| 44 | |
| 45 | const indices = geometry.faces.length ? geometry.faces.flat() : null; |
| 46 | |
| 47 | // Determine index buffer type |
| 48 | let indexType = null; |
| 49 | if (indices) { |
| 50 | // If any face references a vertex with an index greater than the maximum |
| 51 | // un-singed 16 bit integer, then we need to use a Uint32Array instead of a |
| 52 | // Uint16Array |
| 53 | const hasVertexIndicesOverMaxUInt16 = indices.some(i => i > 65535); |
| 54 | indexType = hasVertexIndicesOverMaxUInt16 ? Uint32Array : Uint16Array; |
| 55 | } |
| 56 | |
| 57 | this.renderer._ensureGeometryBuffers(buffers, indices, indexType); |
| 58 | |
| 59 | return buffers; |
| 60 | } |
| 61 | |
| 62 | freeBuffers(gid) { |
| 63 | const buffers = this.cache[gid]; |
no test coverage detected