- Home /
How to stretch a texture applied to a material
Hi,
I'm working on a simple viewer with WebPlayer that displays a texture, contained into a URL, on an specific material of a displayed object.
Actually the texture is loaded and displayed on the object into the correct material, but the texture is not resized properly and a small part of it is displayed, this is the code that I'm using:
function loadTexture (url : String) {
// Start a download of the given URL
var www : WWW = new WWW (url);
// Wait for download to complete
yield www;
// assign texture
var texture : Texture2D = www.texture;
texture.Compress(true);
renderer.materials[0].mainTexture = texture;
renderer.materials[0].mainTextureScale = Vector2(1,1);
}
The object has already a texture on the same material that is displayed properly.
Thanks in advance for your help.
Right.. a mesh, as in something 3d, not just a simple plane, correct?
And the texture was not created for the object, correct?
Answer by Seth-Bergman · Nov 18, 2012 at 12:43 PM
Sounds like a mapping issue..
3D objects use a 3D coordinate system, we refer to as UVW. In order to add textures to them, we do something called UNWRAP it... Basically, we peel off pieces, flatten them, and arrange them into a square "tile", we call this a UV tile. The position of each vertex in the tile determines where the texture will map to..
In other words, you can't just add any texture to a 3d model, it actually would need to correspond to the model..
To give you an idea, here is a UV tile I have for a character in one of my projects:
EDIT: To stretch the texture, just say
renderer.material.mainTextureScale = Vector2(scaleX,scaleY);
http://docs.unity3d.com/Documentation/ScriptReference/Material-mainTextureScale.html
in your example, you are using (1,1) as the scale, which is where it starts at.. just lower the numbers, such as (.5,.5), to increase the size.. or raise them to decrease the size..
I know how UV coordinates works. But in this case the material is assegned to a simple rect. The texture must be simply stretched. There is a way to stretch it?
Thank you Seth. But if you take some moment to see the code i've already posted, there's already the row that you've edited. It simply doesn't works. I will try some other way.
YOUR LINE sets the scale to (1,1)
this is where it starts already
you need to use something else, such as (2,2)
Uhm... I need to do a proportion before establish the scale, but it could works. I will try and let you know.
Answer by htsoft · Nov 27, 2012 at 09:20 AM
I've found this solution:
1 - In Maya i've duplicated the polygon which will receive the texture and made an UV on it
2 - I've assigned a material to this polygon
3 - I've removed the original polygon and saved the mesh
4 - In Unity i've simply assigned a texture to the material and Unity made the correct work.
I've tried with a complex structure and it worked fine.
Thank you.
Answer by Maulik2208 · Nov 27, 2012 at 12:45 PM
YOURGAMEOBJECTNAME.renderer.material.SetTextureOffset("_MainTex",offset);
YOURGAMEOBJECTNAME.renderer.material.SetTextureScale("_MainTex", _size);
HOPE THIS WILL WORK FOR YOU.......(<")
Your answer
Follow this Question
Related Questions
where to call wwwLoadImageIntoTexture 1 Answer
Error display when loading a 6000 x 3000 jpg by using WWW 0 Answers
Where is downloaded things from editor script being saved? 0 Answers
Textures are not tiling properly on my wall model! 3 Answers
Converting downloaded texture into PVRTC on iPhone 3 Answers