Hello Babylon.js forum!
So, I’m beginning my journey with Babylon and I thought that I’ll make my first game to not only better understand processes of making them but also get better at writing javascript. My game is supposed to be a isometric rpg/shooter type with modifiable terrain (because that will be a crucial part of my main mechanic - which will be - using terrain blocks to use them against your opponents), that last thing is where my problem begins.
Here is the code that I made for Babylon playground:
var createScene = function ()
{
let isPickedUp = false;
const scene = new BABYLON.Scene(engine, {useGeometryUniqueIdsMap: true});
canvas.focus();
var camera = new BABYLON.ArcRotateCamera("FollowCam", 2.35, 0.3, 50, BABYLON.Vector3.Zero(), scene);
const light = new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 2, 0), scene);
light.intensity = 0.7;
//Terrain
var ground = BABYLON.MeshBuilder.CreateBox("Ground", {width: 50, height: 25, depth: 50}); //Main terrain
ground.position.y = -12.5;
ground.checkCollisions = true;
var telekinesis_hole = BABYLON.MeshBuilder.CreateBox("Hole", {width: 2, height: 2, depth: 2}); //Cut out shape
telekinesis_hole.isVisible = false;
//Terrain
//CSG pick function
scene.onPointerDown = function (evt, pickresult)
{
var pickResult = scene.pick(scene.pointerX, scene.pointerY); //Koordynaty 2D danego miejsca w scenie
//If mesh is not picked up yet
if (pickresult.hit & isPickedUp === false)
{
//isPickedUp = true; //If picked up
isPickedUp = false; //If not picked up (here just for testing)
var targetPoint = pickResult.pickedPoint;
var ground_csg = BABYLON.CSG.FromMesh(ground);
var telekinesis_hole_clone = telekinesis_hole.clone("Hole");
telekinesis_hole_clone.position = targetPoint;
ground_csg.subtractInPlace(BABYLON.CSG.FromMesh(telekinesis_hole_clone));
var csgmesh = ground_csg.toMesh();
//Disposing
ground.dispose();
ground = csgmesh; //Overriding terrain to save changes
telekinesis_hole_clone.dispose();
}
if(pickresult.hit & isPickedUp === true)
{
//If mesh is picked up (not using yet)
//isPickedUp = false;
}
}
return scene;
};
I made one big box mesh which is simulating what will happen with terrain in my game, and made another box (set to invisible) which is my cut-out shape which I’ll use with CSG. In my game I want to use pointer position to substract those smaller box shapes from my main terrain mesh (so in short - making holes wherever I click). It does work BUT after like 30 clicks/substractions my FPS is starting to slowly drop to a point where single click takes like 500/600ms. I suspect that it may be some problem with GarbageCollector and variables that I try to dispose but I am totally unable to understand what exactly is causing those FPS drops/how to prevent them, so I would like to ask if anyone knows what is happening here?