What is active mesh / freezeActiveMeshes

Hi guys,

I am looking to optimize my 3D scene and apply what i found in the documentation.
but on the point with : freezeActiveMeshes i have issues.

When i am applying this on my scene some meshes don’t display and sometimes juste disapear when my camera view just looks elsewhere.

So i dont understand what is considering as “active” meshes and how to apply this feature without to have the display of the half of my scene.

1 Like

Depending on what attributes are applied to your mesh, freezeActiveMeshes will not compute elements such as particle systems, decals, actions, and many other calculations. So depending on the materials and attributes of your mesh, it may not appear when you use freezeActiveMeshes. If you still have questions, then create a playground scene and we’ll help you better understand your specific issues.

Galen

Technically freezeActiveMeshes will not evalaute the frustum clipping anymore and just keep the current list of visibile meshes.

Maybe you can repro on the playground with your setup to see what is going wrong?

1 Like

Hi, thx for replying.
Hum i tried to create a PG, but the PG refuse to load the babylon file from my website :frowning:
Is there a way to uplaod them with texture in the PG ?

This is my code and a picture.

var canvas = document.getElementById("canvas");
var engine = new BABYLON.Engine(canvas, false, null, false);
var main = new BABYLON.Scene(engine);       // global var scene for external function

window.addEventListener('DOMContentLoaded', function(){
    if (BABYLON.Engine.isSupported()){ 
       BABYLON.SceneLoader.Load("3d-appart/", "appart.babylon", engine, function (scene) {
            main.activeCamera = scene.activeCamera;     // to avoid error
            //
            // ---------------- OPTIMISATION
            // Options (target 60fps (which is not possible) with a check every 500ms)
            var options = new BABYLON.SceneOptimizerOptions(30, 500);
            options.addOptimization(new BABYLON.HardwareScalingOptimization(0, 1));
            options.addOptimization(new BABYLON.SceneOptimizerOptions.ModerateDegradationAllowed());
            var optimizer = new BABYLON.SceneOptimizer(scene, options);     // Optimizer
            
            //scene.debugLayer.show();          // for debuging
            
            // Reducing Shaders Overhead by freezing material (cant change them anymore
            var nb = scene.meshes.length;
            for (let i = 0; i < nb; i++) {
                if( scene.meshes[i].material ){ 
                   //scene.meshes[i].material.freeze();     // generate error on console : Reducing shaders checking and updating if matreial are static
                }
                scene.meshes[i].freezeWorldMatrix();        // Reducing World Matrices Computation ((position / rotation / scaling of a mesh ))
            }
            // Reducing calls to gl.clear() :  stop clearing the color, depth, and stencil buffers before rendering the scene
            scene.autoClear = false;                        // Color buffer
            scene.autoClearDepthAndStencil = false;         // Depth and stencil, obviously
            
            scene.blockMaterialDirtyMechanism = true;       // Blocking the dirty mechanism
            scene.cleanCachedTextureBuffer();               // Removing cached texture buffers
            engine.doNotHandleContextLost = true;           // Turning off support for context lost / contest restore
            engine.enableOfflineSupport = false;            // Dont require a manifest file
            
            // Freeze active meshes after 10 seconds => issues now, to test
            //setTimeout(function(){ scene.freezeActiveMeshes(); }, 5000);
            
            // ---------------- SCENE
            var gravtity    = new BABYLON.Vector3(0, -.15, 0);
            scene.clearColor    = new BABYLON.Color3(.6, .8, 1);
            scene.ambientColor  = new BABYLON.Color3(1,1,0.98);
            scene.gravity       = new BABYLON.Vector3(0, -.15, 0);            
            
            // ---------------- LIGHT
            var light = new BABYLON.PointLight("light", new BABYLON.Vector3(5, 5, 1), scene); 
            light.intensity = 0.7;

            // ---------------- CAMERA             
            var camera = new BABYLON.UniversalCamera('camera', scene.activeCamera.position, scene);
            camera.speed = .1;                      // vitesse deplacement camera
            camera.angularSensibility = 3500;       // vitesse rotation camera            

            var postProcess = new BABYLON.FxaaPostProcess("fxaa", 1.0, camera);     // anti-alising for more performance
            
            // --- BLENDER SET UP WITH Babylon exporter
            camera.checkCollisions = scene.activeCamera.checkCollisions;
            camera.applyGravity = scene.activeCamera.applyGravity;
            camera.ellipsoid = scene.activeCamera.ellipsoid;
            camera.rotation = scene.activeCamera.rotation;
            camera.minZ = 0;                        // min deep of camera view. With 0 and an elipsoid collision system we can avoid to see through/inside meshs.

            // --- MOVEMENT
            camera.keysUp.push(87);    //W
            camera.keysDown.push(83);  //D
            camera.keysLeft.push(65);  //A
            camera.keysRight.push(68); //S

            // apply camera setup to active camera on canvas
            scene.activeCamera = camera;
            scene.activeCamera.attachControl(canvas, true);
            // ---------------- MIRROR
            var glass = scene.getMeshByName("o_bathroom_cabinet-mirror");

            //Obtain normals for plane and assign one of them as the normal
            var glass_vertexData = glass.getVerticesData("normal");
            var glassNormal = new BABYLON.Vector3(glass_vertexData[0], glass_vertexData[1], glass_vertexData[2]);	

            //Create reflecting surface for mirror surface
            var reflector = new BABYLON.Plane.FromPositionAndNormal(glass.position, glassNormal.scale(-1));

            //Create the mirror material
            var mirrorMaterial = new BABYLON.StandardMaterial("mirror", scene);
            mirrorMaterial.reflectionTexture = new BABYLON.MirrorTexture("mirror", 1024, scene, true);
            mirrorMaterial.reflectionTexture.mirrorPlane = reflector;
            mirrorMaterial.reflectionTexture.renderList = [ scene.getMeshByName("o_bathroom_door"), scene.getMeshByName("o_entrance-2_radiator"), scene.getMeshByName("o_baathroom"), 
                scene.getMeshByName("o_bathroom_shower"), scene.getMeshByName("o_bathroom_shower.001"), scene.getMeshByName("o_bathroom_toilet"),
                scene.getMeshByName("o_baathroom_wall-motif.002"),scene.getMeshByName("o_bathroom_ventilation"),scene.getMeshByName("o_bathroom_interruptor.002"),scene.getMeshByName("o_bathroom_interruptor.001"),
                scene.getMeshByName("o_bathroom_tap"),scene.getMeshByName("o_baathroom_wall-motif.003"),scene.getMeshByName("o_bathroom_interruptor.003"),                    
                scene.getMeshByName("o_living_walls"),scene.getMeshByName("o_living_plainte"),scene.getMeshByName("o_living.001"),
                scene.getMeshByName("o_entrance-2_door"),scene.getMeshByName("o_living_support-wall-light"),scene.getMeshByName("o_living.003")
            ];
            mirrorMaterial.reflectionTexture.level = 0.8;
            mirrorMaterial.reflectionTexture.adaptiveBlurKernel = 15;
            mirrorMaterial.needDepthPrePass = true;         // Using depth pre-pass
            
            glass.material = mirrorMaterial;
            
            light.excludedMeshes.push(scene.getMeshByName("o_living-yellow"), glass);
           
            // ---------------- ACTIONS EVENT 
            actions(scene);
            
            // ---------------- RENDER
            scene.createDefaultEnvironment({
                createGround:false
            });

            // Wait for textures and shaders to be ready
            //var fpsLabel = document.getElementById("fpsLabel");
            scene.executeWhenReady(function () {
                engine.runRenderLoop(function() {
                    scene.render();
                    
            //       fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
                });
            });
            window.addEventListener("resize", function () {
                engine.resize();
            }); 
            
            main = scene;
        },
        // ---------------- LOADING SCREEN
        function (evt) {
            if (evt.lengthComputable) { 
                engine.loadingUIText = "<br>Loading, please wait..." + (evt.loaded * 100 / evt.total).toFixed() + "%";} 
            else {
                dlCount = evt.loaded / (1024 * 1024);engine.loadingUIText = "Loading, please wait..." + Math.floor(dlCount * 100.0) / 100.0 + " MB already loaded.";
            }
        });
    }
});

Here is a doc on how to reference external assets on PG:
https://doc.babylonjs.com/resources/external_pg_assets

Thanks for the link, thats great to know.

I solved my issue by adding : alwaysSelectAsActiveMesh = true on all my meshes.
But now an other issue, my mirror dosent work anymore if i use freezeActiveMeshes before or even after the mirror creation.

hum issues is endless :frowning:

PS. “Active Mesh” means : meshes that is display or it has an other meaning?

1 Like

If you do not repro it in the PG I can only guess. This would be far more efficient with a repro

activeMeshes are the list of meshes that the camera will render

I finllay have a PG for that but i cant figure out how to load texture :frowning:

https://www.babylonjs-playground.com/#XYDFLV#1

Or you can see the result here : SM - 3D
The mirror is in the bathroom straight ahead (actually with freezeActivaMeshes enabled)
If I desactivate freezActiveMeshes, the mirror has the good behavior

I guess you need the alwaysSelectAsActiveMesh cause some of the meshes are first clipped so not in the active list so by doing so they appear in the list before freezing it :slight_smile:

i see, thats why it solved my issues, thx
last thing to understand why mirror (reflection) dosent work :frowning:

In case someone finds this topic at some point, for the possible solutions for non-working mirror please check this topic Mirror texture with freezing active meshes

2 Likes

Thanks to you alvov, is nice to answer it.
Actually I stoped BJS to Unity, but it still good to know :slight_smile:

1 Like