render a Texture2D with Graphics.DrawMesh?

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.

No takers? Can this not be done?

I cant be done. The command you specified draws a “Mesh” which has nothing to do with texture 2D. do somthing like:

var texture : Texture2D 

//Instantiate the var texture 2d at a specific point in space

or you can do:

// Load a .jpg or .png file by adding .txt extensions to the file
// and dragging it on the imageTextAsset
var imageTextAsset : TextAsset;
function Start () {
var tex = new Texture2D (4, 4);
tex.LoadImage(imageTextAsset.bytes);
renderer.material.mainTexture = tex;
}

possibly

function Start  () {
// Create a new texture and assign it to the renderer's material
var texture = new Texture2D (128, 128);
renderer.material.mainTexture = texture;
}

reply to me if any of these work and please do help me in my thread:
http://forum.unity3d.com/viewtopic.php?t=57834
thanks!!!

Is this c# code? because it dosent look like JS.

Thanks for your reply. Yes, this is C# code. I’ve already implemented a solution similar to what you describe but I was hoping for something lighter weight. All I want to do is control the rotation of a texture simliar to the way RotateAroundPivot works, but in all axis. I guess it will have to be a game object though.

No problem.

http://forum.unity3d.com/viewtopic.php?t=57834

Why jump through all those hoops to create a procedural mesh? If a rotating texture is all you need then why not simply create a prefab of a Unity built-in plane and attach your texture to that?

Well, that’s what I’m doing now. I’m new to unity and still have lots to learn, and I’m working on the iPhone where cycles are still important. In OpenGL I could bind the texture and then do this with a single matrix multiply but I honestly don’t know how that compares with manipulating the transform on a mesh in a prefab.

Well, that’s what I’m doing now. I’m new to unity and still have lots to learn, and I’m working on the iPhone where cycles are still important. In OpenGL I could bind the texture and then do this with a single matrix multiply but I honestly don’t know how that compares with manipulating the transform on a mesh in a prefab.