I’m trying to create a plane mesh with more than 2 triangles and attach a texture at multiple points. I’m trying to do this so I can break off some of the triangles so the object looks like it’s breaking apart.
When I have just 2 triangles, it works fine, but when I add more, my texture doesn’t render properly - it renders the correct shape, but it’s just semi-transparent black. I know my vertices, uvs and triangles are correct, because when I render any part of the plane that just has 2 triangles it looks correct (i.e. I can render the top half of the texture, or the bottom half fine, but when I try to render both halves it doesn’t work).
Here’s a sample script for what I’m trying to do. I think if you create any GameObject with a MeshFilter and MeshRenderer and attach a Material to it, you can reproduce it. If you comment out the bottom half (separated by the space) of each array, you’ll see the top half of the texture rendered correctly (same if you comment out the top half of each array - you’ll see the bottom half rendered correctly), but if you run it as is, the texture gets messed up. I’m porting this from another project I was working on, and I really have no idea what the problem is.
var mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.subMeshCount = 2;
mesh.vertices = new Vector3[]
{
new Vector3(0, 0, 0),
new Vector3(0, 0.5f, 0),
new Vector3(1, 0.5f, 0),
new Vector3(1, 0, 0),
new Vector3(0, 0.5f, 0),
new Vector3(0, 1, 0),
new Vector3(1, 1, 0),
new Vector3(1, 0.5f, 0),
};
mesh.uv = new Vector2[]
{
new Vector2(0, 0),
new Vector2(0, 0.5f),
new Vector2(1, 0.5f),
new Vector2(1, 0),
new Vector2(0, 0.5f),
new Vector2(0, 1),
new Vector2(1, 1),
new Vector2(1, 0.5f),
};
mesh.triangles = new int[]
{
0,
1,
2,
2,
3,
0,
4,
5,
6,
6,
7,
4,
};
mesh.RecalculateNormals();
UPDATE
I’ve worked more on this and the issue seems to be with the shader I’m using on my material. My texture is a sprite with transparency, so I was trying initially with the Sprite->Default and Sprite->Diffuse shaders. Both had the same issue with the mesh rendering like this (this is a bit different from the code posted above as there are more pieces which are offset away from each other, but it’s the same idea). It’s interesting to note that it looks fine the first time I run it in the Game tab in Unity after Unity is started, but looks like this every subsequent time I run it. It is also always ok when I build the full standalone game and run that. It seems to be an issue with how Unity is rendering it in the Game tab.
I changed the shader on my material to Unlit->Transparent, and it correctly looks like this all the time now.
I can live with this for now, but it would be nice to be able to use a shader that handles lighting at some point and have it look ok while I’m developing it. I wonder if there’s some call I can make to reset something so Unity will always render it properly.
Update: It looks like this actually works the first time it's run after Unity starts. The second time you run it, it looks corrupted, however. It always works fine when I build and launch the full game, but when I run it in the Unity editor it looks broken.
– haXadecimal