Jump to content

To clone or not to clone (that is the question)


Recommended Posts

Hi everybody.

Here I come again with a new question for this incredible community. By the way, I think it will be "super easy" for someone which a good knowledge of the BJS internals.

The case is that, following with my current development (tap simulator) I'm now focused on the performance.

My doubts are mainly about the proper use of the method "clone" of the "Texture" object.


Doubt 1) Suppose you have a Standard Material, you create it following the usual way, something like this:

matFoo=new BABYLON.StandardMaterial("Foo_Material",myScene);
matFoo.diffuseColor=new BABYLON.Color3(0,0,0);

Note that we are using the texture (textFoo) on two channels, opacity and emissive. Is this effective in terms of resource economy or I must follow this other approach?:

matFoo=new BABYLON.StandardMaterial("Foo_Material",myScene);
matFoo.diffuseColor=new BABYLON.Color3(0,0,0);


 Doubt 2) Now we have a similar scenario, when we must assign a previously created texture, but this time we want to assign it to a series of Particles:

textSprite=new BABYLON.Texture("assets/textures/flarealpha.png",myScene);
myParticles=new BABYLON.ParticleSystem("Particles",10000,myScene);
myParticles.emitter=new BABYLON.Vector3(0,0,0);

This time, as we have a lot of particles chances are that it is better to use textSprite.clone() in order to assign the sprite image to each particle, at last line of shown code. Isn´t it?


Doubt 3) Last but not least. When you (as me) arrives at the conclusion that PBR materials are the way-to-go (in order to achieve a decent look with your CGs), you are going to use, among others, the environment channel  (dds files in current 3.0 version of BJS). Here I have certainly a doubt and a thought. Going first with the last, I think the environment should not be a PBR Material property (as currently is), but a scene one, as certainly is very weird to think in a scene with more than one environment (?). Anyway, as at the moment this is not the case, and we have to assign the same value of "environment" for each PBR Material, we find ourselves setting time after time the same texture (dds file) in this way (please focus only on the reflectionTexture property):

matTube=new BABYLON.PBRMaterial("MaterialPBR_Tube",myScene);
matTube.albedoTexture=new BABYLON.Texture("assets/models/Tubo_BaseColor.png",myScene);
matTube.metallicTexture=new BABYLON.Texture("assets/models/Tubo_Metalico_PBR.png",myScene);
matTube.bumpTexture=new BABYLON.Texture("assets/models/Tubo_Normal.png",myScene);
matTap=new BABYLON.PBRMaterial("MaterialPBR_Tap",_Scene);
matTap.albedoTexture=new BABYLON.Texture("assets/models/mono/Manetas_BaseColor.png",myScene);
matTap.metallicTexture=new BABYLON.Texture("assets/models/mono/Manetas_Metallic_PBR.png",myScene);
matTap.bumpTexture=new BABYLON.Texture("assets/models/mono/Manetas_Normal.png",myScene);
matSprinkler=new BABYLON.PBRMaterial("MaterialPBR_Sprinkler",myScene);
matSprinkler.albedoTexture=new BABYLON.Texture("assets/model/mono/Resto_BaseColor.png",myScene);
matSprinkler.metallicTexture=new BABYLON.Texture("assets/models/mono/Resto_Metallic_PBR.png",myScene);
matSprinkler.bumpTexture=new BABYLON.Texture("assets/models/mono/Resto_Normal.png",myScene);

Here, as in the case of my first doubt, I'm thinking, obviously in using "matXXX.reflectionTexture=textEnvironment.clone();", but I don´t know if that is a way to enhance performance, and by the way if dds files are buffered in a way they can benefit of texture cloning mechanism.


Ok, this is all, sorry for the length of the question and, as always, thanks in advance for your time!


Link to comment
Share on other sites


so :

1. The option 1 is better. But cloning is not a big deal as only some number values will be duplicated. The internal texture will be shared

2. see #1. No need to clone either

3. scene.environmentTexture is exactly here for that. Only set this value and nothing in the material itself. The value exists at both levels because you may want to specify a specific one for a given material


Link to comment
Share on other sites

Ok RaananW, I see.

I don´t know now why , but the case is that I thought assigning the same texture to different material "channels" (diffuse, ) of the same or a different mesh, had a performance hit when not done by means of a clone of that texture.

Misunderstandings  happen!

Thanks for your time guys... I think this question is solved.

Link to comment
Share on other sites


As for your last question:

On 10/26/2017 at 10:36 PM, paleRider said:

Based on your explanation, what would be an appropriate case to use texture.clone?

I definitely clone meshes which are imported, along with the materials which I apply in my babylon.js script when I am creating thousands (even hundreds)  of objects to apply procedural animation. Especially if the materials are 20 lines of code with multiple shaders and textures. In testing, I find this reduced my memory usage, especially garbage collection. and in cloning vs. instancing - I find cloning provides me with far more control over properties of individual meshes and their attributes.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...