Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leak Fix #13616

Merged
merged 2 commits into from
Mar 10, 2023
Merged

Leak Fix #13616

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/dev/core/src/Meshes/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4604,15 +4604,15 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
const getVertexDataFromMesh = (mesh: Mesh) => {
const wm = mesh.computeWorldMatrix(true);
const vertexData = VertexData.ExtractFromMesh(mesh, false, false);
return [vertexData, wm] as const;
return { vertexData, transform: wm };
};

const [sourceVertexData, sourceTransform] = getVertexDataFromMesh(source);
const { vertexData: sourceVertexData, transform: sourceTransform } = getVertexDataFromMesh(source);
if (isAsync) {
yield;
}

const meshVertexDatas = new Array<readonly [VertexData, Matrix]>(meshes.length - 1);
const meshVertexDatas = new Array<{ vertexData: VertexData; transform?: Matrix }>(meshes.length - 1);
for (let i = 1; i < meshes.length; i++) {
meshVertexDatas[i - 1] = getVertexDataFromMesh(meshes[i]);
if (isAsync) {
Expand Down
40 changes: 22 additions & 18 deletions packages/dev/core/src/Meshes/mesh.vertexData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ export class VertexData {
* @returns the modified VertexData
*/
public merge(others: VertexData | VertexData[], use32BitsIndices = false, forceCloneIndices = false) {
const vertexDatas: [vertexData: VertexData, transform?: Matrix][] = Array.isArray(others) ? others.map((other) => [other, undefined]) : [[others, undefined]];
const vertexDatas: { vertexData: VertexData; transform?: Matrix }[] = Array.isArray(others)
? others.map((other) => {
return { vertexData: other };
})
: [{ vertexData: others }];
return runCoroutineSync(this._mergeCoroutine(undefined, vertexDatas, use32BitsIndices, false, forceCloneIndices));
}

Expand All @@ -547,14 +551,14 @@ export class VertexData {
*/
public *_mergeCoroutine(
transform: Matrix | undefined,
vertexDatas: (readonly [vertexData: VertexData, transform?: Matrix])[],
vertexDatas: { vertexData: VertexData; transform?: Matrix }[],
use32BitsIndices = false,
isAsync: boolean,
forceCloneIndices: boolean
): Coroutine<VertexData> {
this._validate();

const others = vertexDatas.map((vertexData) => vertexData[0]);
const others = vertexDatas.map((vertexData) => vertexData.vertexData);

for (const other of others) {
other._validate();
Expand Down Expand Up @@ -603,7 +607,7 @@ export class VertexData {
}

let positionsOffset = this.positions ? this.positions.length / 3 : 0;
for (const [other, transform] of vertexDatas) {
for (const { vertexData: other, transform } of vertexDatas) {
if (other.indices) {
for (let index = 0; index < other.indices.length; index++) {
indices[indicesOffset + index] = other.indices[index] + positionsOffset;
Expand All @@ -630,7 +634,7 @@ export class VertexData {
VertexBuffer.PositionKind,
this.positions,
transform,
vertexDatas.map((other) => [other[0].positions, other[1]])
vertexDatas.map((other) => [other.vertexData.positions, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -639,7 +643,7 @@ export class VertexData {
VertexBuffer.NormalKind,
this.normals,
transform,
vertexDatas.map((other) => [other[0].normals, other[1]])
vertexDatas.map((other) => [other.vertexData.normals, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -648,7 +652,7 @@ export class VertexData {
VertexBuffer.TangentKind,
this.tangents,
transform,
vertexDatas.map((other) => [other[0].tangents, other[1]])
vertexDatas.map((other) => [other.vertexData.tangents, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -657,7 +661,7 @@ export class VertexData {
VertexBuffer.UVKind,
this.uvs,
transform,
vertexDatas.map((other) => [other[0].uvs, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -666,7 +670,7 @@ export class VertexData {
VertexBuffer.UV2Kind,
this.uvs2,
transform,
vertexDatas.map((other) => [other[0].uvs2, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs2, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -675,7 +679,7 @@ export class VertexData {
VertexBuffer.UV3Kind,
this.uvs3,
transform,
vertexDatas.map((other) => [other[0].uvs3, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs3, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -684,7 +688,7 @@ export class VertexData {
VertexBuffer.UV4Kind,
this.uvs4,
transform,
vertexDatas.map((other) => [other[0].uvs4, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs4, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -693,7 +697,7 @@ export class VertexData {
VertexBuffer.UV5Kind,
this.uvs5,
transform,
vertexDatas.map((other) => [other[0].uvs5, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs5, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -702,7 +706,7 @@ export class VertexData {
VertexBuffer.UV6Kind,
this.uvs6,
transform,
vertexDatas.map((other) => [other[0].uvs6, other[1]])
vertexDatas.map((other) => [other.vertexData.uvs6, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -711,7 +715,7 @@ export class VertexData {
VertexBuffer.ColorKind,
this.colors,
transform,
vertexDatas.map((other) => [other[0].colors, other[1]])
vertexDatas.map((other) => [other.vertexData.colors, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -720,7 +724,7 @@ export class VertexData {
VertexBuffer.MatricesIndicesKind,
this.matricesIndices,
transform,
vertexDatas.map((other) => [other[0].matricesIndices, other[1]])
vertexDatas.map((other) => [other.vertexData.matricesIndices, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -729,7 +733,7 @@ export class VertexData {
VertexBuffer.MatricesWeightsKind,
this.matricesWeights,
transform,
vertexDatas.map((other) => [other[0].matricesWeights, other[1]])
vertexDatas.map((other) => [other.vertexData.matricesWeights, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -738,7 +742,7 @@ export class VertexData {
VertexBuffer.MatricesIndicesExtraKind,
this.matricesIndicesExtra,
transform,
vertexDatas.map((other) => [other[0].matricesIndicesExtra, other[1]])
vertexDatas.map((other) => [other.vertexData.matricesIndicesExtra, other.transform])
);
if (isAsync) {
yield;
Expand All @@ -747,7 +751,7 @@ export class VertexData {
VertexBuffer.MatricesWeightsExtraKind,
this.matricesWeightsExtra,
transform,
vertexDatas.map((other) => [other[0].matricesWeightsExtra, other[1]])
vertexDatas.map((other) => [other.vertexData.matricesWeightsExtra, other.transform])
);

return this;
Expand Down
10 changes: 9 additions & 1 deletion packages/dev/core/src/Morph/morphTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class MorphTargetManager implements IDisposable {
this._scene = scene;

if (this._scene) {
this._scene.morphTargetManagers.push(this);
this._scene.addMorphTargetManager(this);

this._uniqueId = this._scene.getUniqueId();

Expand Down Expand Up @@ -234,6 +234,10 @@ export class MorphTargetManager implements IDisposable {
target._onDataLayoutChanged.remove(this._targetDataLayoutChangedObservers.splice(index, 1)[0]);
this._syncActiveTargets(true);
}

if (this._scene) {
this._scene.stopAnimation(target);
}
}

/**
Expand Down Expand Up @@ -481,6 +485,10 @@ export class MorphTargetManager implements IDisposable {
}
this._parentContainer = null;
}

for (const morph of this._targets) {
this._scene.stopAnimation(morph);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/dev/core/src/assetContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,11 @@ export class AssetContainer extends AbstractScene {
});
this.reflectionProbes.length = 0;

this.morphTargetManagers.slice(0).forEach((o) => {
o.dispose();
});
this.morphTargetManagers.length = 0;

if (this.environmentTexture) {
this.environmentTexture.dispose();
this.environmentTexture = null;
Expand Down
6 changes: 5 additions & 1 deletion packages/dev/loaders/src/glTF/2.0/glTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,11 @@ export class GLTFLoader implements IGLTFLoader {

const targetNames = mesh.extras ? mesh.extras.targetNames : null;

babylonMesh.morphTargetManager = new MorphTargetManager(babylonMesh.getScene());
this._babylonScene._blockEntityCollection = !!this._assetContainer;
babylonMesh.morphTargetManager = new MorphTargetManager(this._babylonScene);
babylonMesh.morphTargetManager._parentContainer = this._assetContainer;
this._babylonScene._blockEntityCollection = false;

babylonMesh.morphTargetManager.areUpdatesFrozen = true;

for (let index = 0; index < primitive.targets.length; index++) {
Expand Down
9 changes: 9 additions & 0 deletions packages/dev/loaders/src/glTF/glTFFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type { LoadFileError } from "core/Misc/fileTools";
import { DecodeBase64UrlToBinary } from "core/Misc/fileTools";
import { RuntimeError, ErrorCodes } from "core/Misc/error";
import type { TransformNode } from "core/Meshes/transformNode";
import type { MorphTargetManager } from "core/Morph/morphTargetManager";

interface IFileRequestInfo extends IFileRequest {
_lengthComputable?: boolean;
Expand Down Expand Up @@ -713,6 +714,13 @@ export class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISc
cameras.push(camera);
});

const morphTargetManagers: Array<MorphTargetManager> = [];
this.onMeshLoadedObservable.add((mesh) => {
if (mesh.morphTargetManager) {
morphTargetManagers.push(mesh.morphTargetManager);
}
});

return this._loader.importMeshAsync(null, scene, container, data, rootUrl, onProgress, fileName).then((result) => {
Array.prototype.push.apply(container.geometries, result.geometries);
Array.prototype.push.apply(container.meshes, result.meshes);
Expand All @@ -724,6 +732,7 @@ export class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISc
Array.prototype.push.apply(container.lights, result.lights);
Array.prototype.push.apply(container.transformNodes, result.transformNodes);
Array.prototype.push.apply(container.cameras, cameras);
Array.prototype.push.apply(container.morphTargetManagers, morphTargetManagers);
return container;
});
});
Expand Down