Cloning Entities with Scripts

Hello all, I’m having some difficulty with cloned entities in my code. I have a “PreFabs” that is simply a plane with a material and a script, it’s purpose is for billboarding text during development. My issues is, I set the text to be the index number of the element, but for some reason, that all get assigned the value of the last one created. I even set window.root to app.root in the initialize method to find each individual entity and they all have their unique text values, but they are only rendering the last created entity’s value. Feel free to peruse the project: project_001

And thank you for any help :smile:

What I suspect is:

  • When you clone an entity, you create an instance of it, not a new one.

What this means is that cloned entities:

  • Share Materials
  • Share UV co-ordinates
  • Share any mesh deformations
  • Share vertex colors
  • Etc…

Because they are just planes with the numbers on them, the solution is possibly to create the planes from primitives using:

var box = new pc.Entity();
    box.addComponent("model", {
        type: "plane",

I’m not sure how you’d map the texture onto it though.

sdfgeoff, thank you for the reply…

I tried that method as well but couldn’t set the plane’s entity.text value after creation. I will try that method again.

Thanks again…

This is exactly what is going on.

The Billboard.js script is editing the material attached to the model. When you clone an Entity you do not create a new material.

After cloning the entity you could try creating (or cloning) a new material for the model.

Something like this:

var e = this.entity.clone();
e.model.material = e.model.material.clone();
1 Like

Now I feel like an idiot… that was so obvious - lol

One more thing: I used code from a user named will for the billboards, but my camera is different and the rotation isn’t correct, everything is slightly skewed, any suggestions?

Thanks dave :smile:

Never mind about the rotation skewing, it was part of the material issue…

Thanks, both of you!

1 Like

I am trying to clone an apple model from chrome console. Does anyone know how to do this?

Editor -
Launch -

Get the apple with this -
var appleVariable =“apple1”);

I have tried the docs-

//Asset docs
var appleVariable =“apple1”);; //fail

// CLone docs
var appleVariable =“apple1”);
var e = appleVariable.entity.clone();// fail

Ignore… I found the answer here … [SOLVED] _aabVer clone prototype

var cloned =“apple1”);
var e = cloned.clone(); // Clone Entity
cloned.parent.addChild(e); // Add it as a sibling to the original