Hi,
I’m trying to set the proper aspect ratio for a texture mapped to an object (here a plane)!
With (the new) VideoPlayer, this is an easy thing to achieve!
…but I can’t see where to set it for a bitmap!
How do I please have to proceed for an image that type
to display with the proper aspect ration on a square object like this plane?
(so, NOT like this )
Thanks!
Your mesh / uvs need to be the correct aspect ratio. This is the same problem as texturing with a normal texture. Try adjusting material tiling first.
Hi hippocoder,
Thanks for the reply!
The object is immuable (a tv screen)…so the UV’s are fixed one for all!
What would you do from there?
hippocoder,
OK, I’ve now written some code to adjust UV’s tiling and offset!
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class MainCameraScript : MonoBehaviour
{
public GameObject screen;
public Material bmp;
void Start()
{
SetTexture("C:\\testimage.png");
}
void SetTexture(string path)
{
// Create new texture2d
Texture2D tex = LoadPNG(path);
if (tex == null)
{
return;
}
// Set width/height scale/offset for UV's
float twidth = 1.0f;
float theight = 1.0f;
float owidth = 0.0f;
float oheight = 0.0f;
if (tex.width >= tex.height)
{
theight = (float)tex.width / tex.height; // Tiling
oheight = oheight = (theight -1) / -2; // Offset
}
else
{
twidth = (float)tex.height / tex.width;
owidth = (twidth -1) / -2;
}
// Set texture to material albedo
bmp.mainTexture = tex;
// Scale object UVs to fit texture
bmp.mainTextureScale = new Vector2(twidth, theight);
bmp.mainTextureOffset = new Vector2(owidth, oheight);
}
public static Texture2D LoadPNG(string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2, TextureFormat.ARGB32, false);
tex.filterMode = FilterMode.Point;
tex.wrapMode = TextureWrapMode.Clamp;
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
return tex;
}
}
It’s now OK for aspect ratio and centering
…but as you can see I would now need to set the pixels to black when outside of UV coordinates!
Could you (one) please help me on that Shader part?
How to set pixels to black when outside of UV coordinates? (btw, is this the way to proceed)
I saw on this page (http://metalbyexample.com/textures-and-samplers/ ) that this would be known as “Clamp-to-zero addressing”!?
Thanks for your help!