Skip to content

Commit b46c7e5

Browse files
mvaligurskyMartin Valigursky
andauthored
Render handles refouncing of meshes (playcanvas#3328)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 67cd715 commit b46c7e5

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/resources/parser/glb-parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ const createMesh = function (device, gltfMesh, accessors, bufferViews, callback,
767767
mesh.primitive[0].count = vertexBuffer.numVertices;
768768
}
769769

770+
// TODO: Refactor, we should not store temporary data on the mesh.
771+
// The container should store some mapping table instead.
770772
mesh.materialIndex = primitive.material;
771773

772774
let accessor = accessors[primitive.attributes.POSITION];

src/resources/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function onContainerAssetRemoved(containerAsset) {
3535
renderAsset.registry.off('load:' + containerAsset.id, onContainerAssetLoaded, renderAsset);
3636

3737
if (renderAsset.resource) {
38-
renderAsset.resource.meshes = null;
38+
renderAsset.resource.destroy();
3939
}
4040
}
4141

src/scene/render.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,58 @@ import { EventHandler } from '../core/event-handler.js';
2020
class Render extends EventHandler {
2121
constructor() {
2222
super();
23+
24+
// meshes are reference counted, and this class owns the references and is responsible
25+
// for releasing the meshes when they are no longer referenced
2326
this._meshes = null;
2427
}
2528

29+
destroy() {
30+
this.meshes = null;
31+
}
32+
33+
// decrement references to meshes, destroy the ones with zero references
34+
decRefMeshes() {
35+
if (this._meshes) {
36+
const count = this._meshes.length;
37+
for (let i = 0; i < count; i++) {
38+
const mesh = this._meshes[i];
39+
if (mesh) {
40+
mesh.decRefCount();
41+
if (mesh.getRefCount() < 1) {
42+
mesh.destroy();
43+
this._meshes[i] = null;
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
// increments ref count on all meshes
51+
incRefMeshes() {
52+
if (this._meshes) {
53+
const count = this._meshes.length;
54+
for (let i = 0; i < count; i++) {
55+
if (this._meshes[i]) {
56+
this._meshes[i].incRefCount();
57+
}
58+
}
59+
}
60+
}
61+
2662
get meshes() {
2763
return this._meshes;
2864
}
2965

3066
set meshes(value) {
67+
68+
// decrement references on the existing meshes
69+
this.decRefMeshes();
70+
71+
// assign new meshes
3172
this._meshes = value;
73+
this.incRefMeshes();
74+
3275
this.fire('set:meshes', value);
3376
}
3477
}

0 commit comments

Comments
 (0)