Dispose

Dispose a parent (and not children), the children still have a reference of the parent.

by MaxenceBrasselet

HTML

<script src="https://rawgithub.com/BabylonJS/Babylon.js/master/babylon.1.8.0.js"></script>
<div id="rootDiv">
   <button id="dispose">dispose</button>
  <canvas id="renderCanvas"></canvas>
</div>

CSS

html, body, div, canvas {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

JavaScript

// Get the Canvas element from our HTML below
var canvas = document.getElementById("renderCanvas");
// Load BABYLON 3D engine
var engine = new BABYLON.Engine(canvas, true);

window.scene = new BABYLON.Scene(engine);
var scene = window.scene;

// Creating a camera looking to the zero point (0,0,0), a light, and a sphere of size 1
var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 30, new BABYLON.Vector3(0, 0, 0), scene);
var light0 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 0, 10), scene);
var material = new BABYLON.StandardMaterial("material01", scene);
material.emissiveColor = new BABYLON.Color3(0, 1, 0);
var material2 = new BABYLON.StandardMaterial("material02", scene);
material2.emissiveColor = new BABYLON.Color3(1, 0, 0);

var cube2 = BABYLON.Mesh.CreateBox("Box2", 2, scene);
cube2.position.x = 0.0;
cube2.position.y = 4;
cube2.position.z = 4;
cube2.material = material2

var cube = BABYLON.Mesh.CreateBox("Box", 2, scene);
cube.position.x = 0.0;
cube.position.y = 3.0;
cube.position.z = 3.0;
cube.material = material;
cube.parent = cube2;

// Attach the camera to the scene
scene.activeCamera.attachControl(canvas);

// Once the scene is loaded, just register a render loop to render it
engine.runRenderLoop(function () {
    scene.render();
});

jQuery('#dispose').click(function() {
    cube2.dispose(true);
    console.log("meshes:", scene.meshes);
});