- Home /
How do I map a texture to a primitive instance?
I have 2 primitive cubes, one twice as big as the other and with the same material applied (with defuse shader) - Without duplicating the material, How do I set the UV of the bigger one different to the smaller one?
I assume duplicating the material just to have different Tiling settings is a)possible b)a hit to the engine unnecessarily c)a really clumsy, long-way-round method of achieving goal.
I've taken a look around this and other sites and whilst I seem to be on the cusp of finding the answer, it eludes me! I'm a novice coder, so please be gentle/obvious. THANKS!
Answer by testure · May 26, 2011 at 09:21 PM
You can do this using the built in material scale vector: renderer.material.mainTextureScale = new Vector2(x,y);
More info: http://unity3d.com/support/documentation/ScriptReference/Material.SetTextureScale.html
Answer by Eric5h5 · May 26, 2011 at 09:29 PM
You must duplicate the material in order to have different tiling settings. It's impossible for one material to have different settings. The only way to have different UV mapping on different objects while using one material is to change the UVs of the meshes themselves, using the Mesh class.
do you mean I need to create meshes in an external package?
No, use the $$anonymous$$esh class as I mentioned. It's easier to use multiple materials though.
Answer by Martin 11 · May 27, 2011 at 05:35 PM
So testures answer led me to the web page where I took their code example for animated texture, removed the animated bit and created this
var scaleX : float;
var scaleY : float;
function Start () {
renderer.material.SetTextureScale ("_MainTex", Vector2(scaleX,scaleY));
}
This allows me to, at run time, have the texture scaled as needed. Of course the downside of this is that in the editor it looks highly stretched. Given this step towards the right answer can anyone suggest how I'd get the editor to look correct? Better still, how to have the script auto-adjust based on the primitive's actual scale?
Using SetTextureScale at runtime just results in Unity creating a duplicate of the material for you. If you look in the editor inspector while running, you can see the material name now has "(Instance)" appended to it, because it's a new copy of the material and not the original. As I said in my answer, it's impossible to do what you want using one material.
well, yes, technically it's 'impossible' (if you say so) - but if I hadn't dug around, I wouldn't have found this auto-fixing solution using a script which does exactly what I need it to, see$$anonymous$$gly.
Sure, that's fine if you're only concerned with the runtime, and you let Unity generate the necessary material duplicates for you. I'm replying to your question about the editor...you won't be able to have multiple objects using different UV scales look correct using one material in the editor.
Answer by Martin 11 · May 27, 2011 at 05:46 PM
So, figured this out:
function Start () {
renderer.material.SetTextureScale ("_MainTex", Vector2((transform.localScale.z/4),(transform.localScale.y/4)));
}
The above code will grab the ZY values and autoscale the texture appropriately. If I want to see the North/South facing quads then it's a XY script I need (am applying this to a cube.
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Get a UV texture of a models surface 2 Answers
Does Unity support multi-tile texture workflows? 0 Answers