- Home /
Using GetPixel to instantiate blocks of different pixel colors
Code help needed please!
I instantiate cube prefabs that have a colorful texture, i want to use material.SetTextureScale to zoom the entire texture to one pixel, and then use GetPixel to offset the material color to different pixels for different cubes.
is it possible with this instantiation? please help me with the code!
var bullitPrefab : Transform;
function Update ()
{
if(Input.GetButton("Fire1"))
{
var bullit = Instantiate(bullitPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward *8000);
}
}
P.S. i already tried material.color, material.SetTextureOffset,sharedMaterial.SetTextureOffset, mainTextureOffset, etc to color instances without extra draw calls, they dont work! i get 1024 draw calls.
It's for procedural graphics(see pics), here's what i am doing, i simply cant set colors efficiently in unity, it's taken me hours and days of trying!!! learning very slowly :z
Hi, SetTextureOffset works vaguely to make objects of different colors - But it seems to always invoke a new copy of the texture, so if i do 1000'ds of instantiations for some procedural work SetTextureOffset does 1000ds of draw calls!!! i just want the same but with batch working!
that just sounds weird, i offset texture all the time w/ no woes! sorry can't help
Answer by ThermalFusion · Sep 06, 2012 at 08:39 PM
You could use a vertex colored material, and set each models vertex colors to a specific color, this should let them batch, although may be very slow.
Thanks ThermalFusion, I'd like to learn that trick although i have to figure out the right vertex colored shader, whether i have to copy the vertex lit one to my own version in resources, which commands will set the vertex colors from the reference, it will take me another 10 hours of newbie fumbling! i wish there was a working example of these things on the forums ;D. well i asked a guy for an example file in the unity forum that he did to find the max batch number of 15999, maybe he saw me struggling! i only took an hour and a bit to understand the solution you mention i think i would need another 10 15 hours by myself! :\
First off you would need a proper shader, this should do the trick: http://wiki.unity3d.com/index.php/VertexColor Then you would need to edit the meshes of the gameobjects ins$$anonymous$$d of the materials. http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$esh.html Then this page should tell you how to change the colors: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$esh-colors.html
I must also point this out to you before you dive too far into it: http://docs.unity3d.com/Documentation/$$anonymous$$anual/DrawCallBatching.html The dynamic batching doesn't do wonders, for example there's the 900 vertex attribute limit, and probably important for you, the scale limitations is probably also of importance to you.
Thanks ThermalFusion that info is very welcome, it save so much fumbling in the dark. i'll also do a performance comparison with coloring by offsetting the UVs some day.
found an amazing code to do uv offset ins$$anonymous$$d of a texture trick, nice it works with most shaders. and you can set palettes from pics to use as colorpalettes. the code is 4 lines somewherein this website.
Your answer
Follow this Question
Related Questions
Combination of Instance and random in a network game 2 Answers
How to get pixel color from MovieTexture? 2 Answers
How to instantiate a prefab and change its color? 5 Answers
Instantiated prefab color change 2 Answers
Compare getPixel() to a Color 1 Answer