- Home /
Batching object with script that chooses random diffuse texture?
Would this work? Assigning a few diffuse textures, and then have a script attached that tells it to randomly choose between them for diffuse. Just trying to think of a way to give variety to batched meshes of the same geometry.
Answer by Kiloblargh · Jul 15, 2013 at 07:09 PM
changing the texture of the material creates a duplicate material. If you need copies of an object with different textures to batch then you need to put all the textures on a texture atlas , create multiple meshes with different uv coordinates, give them all the same sharedMaterial but randomly pick a different mesh.
different UV coordinates wouldn't cause a separate draw call for the duplicate mesh?
$$anonymous$$y models are broken up into modular pieces though, I wouldn't want some parts to choose one set of UV coordinates and some to choose another, within the the complete object. Want them to choose uniformly for that complete object, but randomly for copies of that complete object.
I guess I could randomly choose the complete object. So as long as it has same geometry, the geometry is batched together then eh?
Not as long as it has the same geometry- as long as it has the same material. You can in theory have every mesh in the game use the same material. But you have all their textures in one big texturemap like a patchwork quilt and map their UV's to their little corner of it ins$$anonymous$$d of the whole thing. To change sets of meshes at once, you just need to create arrays or lists, like this:
var myHead$$anonymous$$F : $$anonymous$$eshFilter; //assign each part in the inspector
var myTorso$$anonymous$$F : $$anonymous$$eshFilter;
var myLegs$$anonymous$$F : $$anonymous$$eshFilter;
var head$$anonymous$$eshes : List.<$$anonymous$$esh>; // drag on your different meshes in the inspector
var torso$$anonymous$$eshes : List.<$$anonymous$$esh>; // in the same order for each list
var leg$$anonymous$$eshes : List.<$$anonymous$$esh>; // so all the same index will give a matching set
var r : int = Random.Range (0, head$$anonymous$$eshes.Count);
myHead$$anonymous$$F.mesh = head$$anonymous$$eshes [r];
myTorso$$anonymous$$F.mesh = torso$$anonymous$$eshes [r];
myLegs$$anonymous$$F.mesh = leg$$anonymous$$eshes [r];
Your answer
![](https://koobas.hobune.stream/wayback/20220613110130im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with Batching (diffuse shader) 1 Answer
Static batching not reducing draw calls but is creating batches 0 Answers
How do you get proper static batching? 1 Answer
(Solved) Batching sprites in a randomly generated dungeon with Diffuse sprites 1 Answer
Static batching not working (HDRP) 0 Answers