I have setup an example PG here https://www.babylonjs-playground.com/#BGS59P#4 , I have 2 materials grass1 and grass2 and was only able to use grass1. I read about custom attributes and was able to create in PG, I used different colours on each sphere, just like colours can we also use different material per sphere? if yes, how?
Unfortunately no, different materials are not possible with instances. You can use clones in this case but you will end up with far less efficient rendering
Thank you @Deltakosh for clearing this, I have been searching like everywhere about it.
The issue is I have a model with 29 meshes, 4 of them have textures/material which are different on each instance. what could be good approach to create instance of this model? I need like 500 instances of same model. here is PG of model https://www.babylonjs-playground.com/#YAWQ2W#1
I was thinking to create thinInstance of 25 static meshes and 4 simple instances, which I can add different textures material, is this approach good or anything better to create 500 copies?
a huge thank you @Deltakosh, I successfully increase FPS from 10 to 60 by combining thinInstance for static meshes and normal createInstance for materials, I have updated PG if someone in future find it useful its here https://www.babylonjs-playground.com/#YAWQ2W#2
just one issue, I had glow effect on 1 mesh which is just showing now plain white colour without glow, is it a bug or just glow effect isnāt supported with thinInstance ?
p.s if glow effect isnāt supported can I apply some kind of lights? if you see in PG I have 3 lamp lights and having 500X3 lights is huge issue in scene.
ok gotcha. The think is that thinInstances cannot be treated independantly that their root (ie only having some thininstances and not the root do apply glow for instance)
They are the fastest but also the less flexible. In you case you need to use instances for the glow
Now 4 meshes I am creating using createInstance , each has unique texture/materials, that means 500 X 4 = 2000 unique images/materials. How should I approach this. I am thinking maybe I should load those materials on movement of camera. so when camera is in X distance from instance model or in X radius of camera , then load 4 textures of that model dynamically, or if there is any better way?
This is a great idea. Another approach depending on the texture resolution is to pack them. Like having a big 2048x2048 texture that hosts 64 256x256 textures. That will reduce the load a lot
but if you can implement your solution this is great as well
Thank you @Deltakosh , for my approach I will work on this, but for some textures I could use the pack technique. For that if I understand correctly I will need to set offset of image, right? so if I create a material like this, which has 64 textures as you said:
var tvImage = new BABYLON.StandardMaterial("tvImage", scene);
tvImage.diffuseTexture = new BABYLON.Texture("./assets/tvImage.jpg",scene);
tvImage.diffuseTexture.uOffset = 0;
tvImage.diffuseTexture.vOffset = 0;
how will I change offsets in loop?
task.loadedMeshes.forEach(function (e, index) {
e.material = tvImage;
for (i = 0; i < totalStands; i++) {
var newStand = e.createInstance("stand_" + i);
// uv offset for next stand?
}
}
It doesnāt still clears me, see in my code I am creating multiple instance, so I have a new variable newStand which holds the instance, so on what I need to set offset?
for example newStand.vOffset ?
so each stand has a unique offset in my loop above
I think what @Deltakosh wanted to say is that you would have to use a custom instance attribute given you the offset (x,y) in the atlas to use for mapping. That would also mean you need to use a custom material / shader to read this offset and apply to the texture coordinates before reading inside the atlas.