Upgrading from 3.1 alpha5 to 4.03, take 12, multiple issues

Progress on #4 @Deltakosh – Remember this issue Performance optimization for activeMeshes / memory leak issue with freezeActiveMeshes

?

I noticed this while debugging today, I thought it had gone away but memory leaking was back when trying to freeze. I assumed these two issues were linked before, as they are executing same code path, but I am not so sure anymore, because I figured out that issue and removed the leak, but freezeActiveMeshes still makes my scene disappear. Here is playground reproducing the memory leak/calling _activate even though scene is frozen issue. (It is godrays, so I removed them for now and it’s fine).

https://playground.babylonjs.com/indexStable.html#W2ZA5S#12

The meshes disappearing is still an issue, that I cannot reproduce outside my main scene, but I’ve been now debugging this for over a week and a half, not to mention the other 2+ weeks of time I spent trying to upgrade previously, so I could really use some help even though I can’t repro via playground. I need ideas on things to try or understanding as to why a mesh would stop rendering even though it didn’t leave the activeMeshes array. Stepping through it alone is very difficult because there are so many different render related methods in different classes/places, so it’s hard to know if I’m even looking in the right place.

The things I’ve narrowed down (which may or may not be totally accurate) are:

It seems to be occurring in _activate method of instanced mesh.

    InstancedMesh.prototype._activate = function (renderId, intermediateRendering) {
        if (this._currentLOD) {
            this._currentLOD._registerInstanceForRenderId(this, renderId);
        }
        if (intermediateRendering) {
            if (!this._currentLOD._internalAbstractMeshDataInfo._isActiveIntermediate) {
                this._currentLOD._internalAbstractMeshDataInfo._onlyForInstancesIntermediate = true;
                return true;
            }
        }
        else {
            //I THINK THIS IS CULPRIT, it's already marked as active so it returns false, and never render again?
            if (!this._currentLOD._internalAbstractMeshDataInfo._isActive) {
                this._currentLOD._internalAbstractMeshDataInfo._onlyForInstances = true;
                return true;
            }
        }
        return false;
    };

Which if I’m understanding it correctly, is to prevent a mesh that is already in activeMeshes array, from being rendered again? But that also seems to be what is causing it not to render if so, as if I mark the instanced mesh mesh._currentLOD._internalAbstractMeshDataInfo._isActive=false, it will render again when I unfreeze active meshes. However: It will only render itself, not any children, even though they should be active as well.

I was able to “fix” the issue (make things worse, but at least get meshes to show up again), by overriding this in _evaluateActivemeshes

        if (mesh.isVisible && mesh.visibility > 0 && ((mesh.layerMask & this.activeCamera.layerMask) !== 0) && (mesh.alwaysSelectAsActiveMesh || mesh.isInFrustum(this._frustumPlanes))) {
            this._activeMeshes.push(mesh);
            this.activeCamera._activeMeshes.push(mesh);
            if (meshToRender !== mesh) {
                meshToRender._activate(this._renderId, false);
            }
            if (mesh._activate(this._renderId, false)) {
                if (!mesh.isAnInstance) {
                    meshToRender._internalAbstractMeshDataInfo._onlyForInstances = false;
                }
               //forcing to false causes meshes to show up again. But it created 200+ more draw calls in the process
                meshToRender._internalAbstractMeshDataInfo._isActive = false;
                // meshToRender._internalAbstractMeshDataInfo._isActive = true;
                this._activeMesh(mesh, meshToRender);
            }
            mesh._postActivate();
        }

Any new ideas based on this?