I need help with ovulation algorithm

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')) {
            //console.log("Check up");

Roomcreate.prototype.onCollisionStart = function (result) {
    if (result.other && result.other.tags.has('Ball')) {
        //console.log("Check up");


Roomcreate.prototype.spawngo = function(){
    //Spacing of chambers
    var mid = new pc.Vec3(0,0,this.entity.getPosition().z -this.cam.script.controller.roomcreaterange);

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;
          if(this.cam.script.controller.jcheck > 4)
               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.tile.setLocalEulerAngles(0, 0, 0);
     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. :pray:

Hi @Onur_Ozturk,

What kind of objects do your levels have? The most common cause for freezing is material generation, and also generating mesh colliders for rigid bodies.

  • To fix the material generation freezing the most common way is to have models rendering all of your material library for at least a frame, when your game starts. That way the engine will be forced to compile all shaders. You can disable those objects after that. Later on there will be no freezing when other models use the same materials (the shader compiles only once and it’s cached).

  • If you use rigid bodies with mesh colliders you need to follow a similar strategy. Have entities with each mesh collider enabled at your game start, so the shapes are generated and cached internally, then disable them.

1 Like

Thank you for your answer. Objects are drawn in 3D and I output them in fbx format and render them in mesh type on the editor (solid objects[rigidbody: true]). How can I render these objects for at least 1 frame while the game is loading. Are there any documentation sources or a sample code block?

There is a number of ways to do that. Add them in your scene hierarchy under a parent entity.

Add a script on that parent entity that disables those entities just a frame after the application starts:

// runs this in the initialize method of a script
window.setTimeout( () => this.entity.enabled = false, 0);

Thank you very much again. I will try this on the project. :pray:

1 Like