As you can see in the image below, there are 5 different rooms and each room has 5 different contents.
I create these rooms first in rows and then randomly with the following spawn algorithm.
var Roomcreate = pc.createScript('roomcreate');
Roomcreate.attributes.add('Tile1', { type: 'entity' });
// initialize code called once per entity
Roomcreate.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
this.lastTile = new pc.Vec3(0, 0, 25, 0);
this.timer = 0;
this.cam = this.app.root.findByName("Camera");
};
Roomcreate.prototype.onTriggerEnter = function (triggerEntity) {
//Don't destroy when you hit big obstacles
if (triggerEntity.tags.has('roomcreation')) {
this.spawngo();
//this.collision=0;
triggerEntity.destroy();
//console.log("Check up");
}
};
Roomcreate.prototype.onCollisionStart = function (result) {
if (result.other && result.other.tags.has('Ball')) {
this.spawngo();
this.entity.destroy();
//console.log("Check up");
}
};
Roomcreate.prototype.spawngo = function(){
this.december();
//Spacing of chambers
var mid = new pc.Vec3(0,0,this.entity.getPosition().z -this.cam.script.controller.roomcreaterange);
this.spawn(mid);
};
Roomcreate.prototype.spawn = function (position) {
/* remember the root to local variable - for better performance */
this.root = this.root || this.app.root.findByName('Root');
if(this.cam.script.controller.icheck > 4){
this.cam.script.controller.roomcreatecheck = 1;
}
/* select random parkur index */
randomChildIndex = Math.floor(Math.random() * this.Tile1.children.length);
// const randomChildIndex = Math.floor(Math.random() * this.Tile1.children.length);
if(this.cam.script.controller.roomcreatecheck == 0){
/* select the child from the Tile1 by this random index and clone it*/
this.tile = this.Tile1.children[this.cam.script.controller.icheck].clone();
if(this.cam.script.controller.jcheck <= 4){
console.log(this.cam.script.controller.icheck+". door"+ this.cam.script.controller.jcheck+". mataryal");
this.tile.children[0].children[this.cam.script.controller.jcheck].enabled = true;
this.cam.script.controller.jcheck++;
if(this.cam.script.controller.jcheck > 4)
{
this.cam.script.controller.icheck++;
this.cam.script.controller.jcheck = 0;
}
}}
else if (this.cam.script.controller.roomcreatecheck == 1){
this.tile = this.Tile1.children[Math.floor(Math.random()* this.Tile1.children.length)].clone();
this.tile.children[0].children[Math.floor(Math.random()* this.tile.children[0].children.length)].enabled = true;
}
//this.cam.script.controller.randomicheck = Math.floor(Math.random()* this.Tile1.children.length);
this.tile.enabled = false;
this.tile.name = "newTilee";
this.root.addChild(this.tile);
this.tile.setLocalEulerAngles(0, 0, 0);
this.tile.setPosition(position);
this.tile.enabled = true;
};
The rough draft of this algorithm works like this. He copies the room first. It determines which material will appear in the copied room and performs enabled = true and adds it to a certain location in the root object.
There is one problem. There is a momentary freezing problem when the triggering process required to create these rooms occurs. I think this is because there are too many objects in the contents of the rooms.
How can I optimize or is there a better rendering algorithm to fix this freezing issue?
Thanks in advance for your help and ideas.