I’m struggling trying to render a Texture2D created from a PNG and attached to a simple plane mesh. Here is the essence of the code:
public Texture2D image1;
private Mesh mesh;
private float angle;
void Start ()
{
Vector3[] verts = new Vector3[4];
Vector3[] normals = new Vector3[4];
Vector2[] uv = new Vector2[4];
int[] tri = new int[6];
verts[0] = new Vector3(0, 0, 0);
verts[1] = new Vector3(1, 0, 0);
verts[2] = new Vector3(0, 1, 0);
verts[3] = new Vector3(1, 1, 0);
for (int i = 0; i < normals.Length; i++)
{
normals[i] = Vector3.up;
}
tri[0] = 0;
tri[1] = 2;
tri[2] = 3;
tri[3] = 0;
tri[4] = 3;
tri[5] = 1;
mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
mesh.uv[0] = new Vector2(0, 0);
mesh.uv[1] = new Vector2(1, 0);
mesh.uv[2] = new Vector2(0, 1);
mesh.uv[3] = new Vector2(1, 1);
mesh.RecalculateNormals();
mesh.Optimize();
renderer.material.mainTexture = image1;
renderer.material.shader = Shader.Find("Particles/Alpha Blended");
}
void LateUpdate()
{
Vector3 pos = new Vector3(0, 0, 0);
Vector3 scale = new Vector3(1, 1, 1);
Quaternion rot = Quaternion.identity;
angle += .1f;
rot.eulerAngles = new Vector3(0, angle, 0);
Graphics.DrawMesh(mesh, Matrix4x4.TRS(pos, rot, scale), renderer.material, 0);
}
This will rotate a solid color PNG around the Y-axix just fine but not a PNG with an image. The image doesn’t show up - just a colored plane is displayed. If I create 2 PNGS, one solid red and the second also red but with a black letter ‘A’ on top only the red is shown. I’m guessing it has something to do with the UV mapping being wrong, but I don’t see it in the code. The image file works fine if drawn in a GUI.DrawTexture call.
What am I doing wrong?? Unity iPhone basic.