- Home /
Change material tiling according to loaded texture size in CS
Hi, I have a project in unity4, where U can browse your computer and upload an image from it into a model in runtime. I´m using UniFileBrowser in cs. The problem is when the uploaded image does not match 1x1 aspect ratio it gets distorted because of tiling. I can let the user change it manually with sliders or input fields but since they use it mostly on fair presentations - it shold be less fuss / more action. It seems so basic - but i just can´t get my head around it. Thanks in advance. void OpenFile (string pathToFile) { var Tagged_1 = GameObject.FindWithTag("kuubik");
var fileIndex = pathToFile.LastIndexOf (pathChar);
message = "You selected file: " + pathToFile.Substring (fileIndex+1, pathToFile.Length-fileIndex-1);
Fade();
string[] filePaths = Directory.GetFiles(@"" + pathToFile);
WWW www = new WWW("file://" + filePaths[0]);
Texture2D new_texture = new Texture2D(512,512);
www.LoadImageIntoTexture(new_texture);
Tagged_1.renderer.material.mainTexture = new_texture;
}
Why are you using a new_texture rather than just use www.texture?
As for material tiling, you can set the material scale according to the aspect ratio of the texture;
Textur2D tex = www.texture;
Tagged_1.renderer.material.mainTexture = tex;
Tagged_1.renderer.material.mainTextureScale = new Vector2(1, tex.height / tex.width);
Hi, thanks for Your reply. For whatever reason - when i use www.texture, the material seems faded but using new_texture gives more precise result of the texture used ( right sided used www.texture). Another thing is - (1, tex.height / tex.width) gives "1,0" as a result. But when i use (tex.height, tex.width) the numbers are accurate (image height pixels, image witdth pixels).
Your answer
Follow this Question
Related Questions
Same material, different meshes, different tiling 2 Answers
Effects Of Tiling Textures 1 Answer
Texture Tiling Stopped Working in Unity 4? 2 Answers
How would you texture a road 2 Answers