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.

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’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.

YOURGAMEOBJECTNAME.renderer.material.SetTextureOffset(“_MainTex”,offset);
YOURGAMEOBJECTNAME.renderer.material.SetTextureScale(“_MainTex”, _size);
HOPE THIS WILL WORK FOR YOU…(<")