- Home /
Bottom-texture of code generated cube does not draw
Heya, I am generating cubes with my code (actually these are walls, but should not make a difference here) and I can't find the reason why the bottom is not drawn. While it is not crucial for the project itself (you won't be able to see the bottom of the wall anyways) I'd like to know if there is an mistake in my code which I can't find. I marked the spot where I suspect the error with "NEEDS CORRECTION!"
vertices[tileCounter*24+00] = leftBtmFront;
vertices[tileCounter*24+01] = rightBtmFront;
vertices[tileCounter*24+02] = leftTopFront;
vertices[tileCounter*24+03] = leftBtmBack;
vertices[tileCounter*24+04] = rightBtmBack;
vertices[tileCounter*24+05] = rightTopFront;
vertices[tileCounter*24+06] = leftTopBack;
vertices[tileCounter*24+07] = rightTopBack;
vertices[tileCounter*24+08] = leftBtmFront;
vertices[tileCounter*24+09] = rightBtmFront;
vertices[tileCounter*24+10] = leftTopFront;
vertices[tileCounter*24+11] = leftBtmBack;
vertices[tileCounter*24+12] = rightBtmBack;
vertices[tileCounter*24+13] = rightTopFront;
vertices[tileCounter*24+14] = leftTopBack;
vertices[tileCounter*24+15] = rightTopBack;
vertices[tileCounter*24+16] = leftBtmFront;
vertices[tileCounter*24+17] = rightBtmFront;
vertices[tileCounter*24+18] = leftTopFront;
vertices[tileCounter*24+19] = leftBtmBack;
vertices[tileCounter*24+20] = rightBtmBack;
vertices[tileCounter*24+21] = rightTopFront;
vertices[tileCounter*24+22] = leftTopBack;
vertices[tileCounter*24+23] = rightTopBack;
//front and back
uv[tileCounter*24+00] =new Vector2(0,0);//linksUntenVorn
uv[tileCounter*24+01] =new Vector2(1,0);//rechtsUntenVorn
uv[tileCounter*24+02] =new Vector2(0,1);//linksObenVorn
uv[tileCounter*24+03] =new Vector2(0,0);//linksUntenHinten
uv[tileCounter*24+04] =new Vector2(1,0);//rechtsUntenHinten
uv[tileCounter*24+05] =new Vector2(1,1);//rechtsObenVorn
uv[tileCounter*24+06] =new Vector2(0,1);//linksObenHinten
uv[tileCounter*24+07] =new Vector2(1,1);//rechtsObenHinten
//left and right
uv[tileCounter*24+08] =new Vector2(1,0);//rechtsuntenvorn1,0
uv[tileCounter*24+09] =new Vector2(1,0);//rechtsuntenhinten1,0
uv[tileCounter*24+10] =new Vector2(1,1);//rechtsobenvorn1,1
uv[tileCounter*24+11] =new Vector2(0,0);//linksuntenvorn0,0
uv[tileCounter*24+12] =new Vector2(0,0);//linksuntenhinten0,0
uv[tileCounter*24+13] =new Vector2(1,1);//rechtsobenhinten1,1
uv[tileCounter*24+14] =new Vector2(0,1);//linksobenvorn0,1
uv[tileCounter*24+15] =new Vector2(0,1);//linksobenhinten0,1
//top and bottom
//************************NEEDS CORRECTION!*************bottom not displayed correctly
uv[tileCounter*24+16] =new Vector2(0,0);//linksUntenHinten-0,0
uv[tileCounter*24+17] =new Vector2(0,1);//rechtsUntenhinten-0,1
uv[tileCounter*24+19] =new Vector2(1,0);//linksObenhinten-1,0
uv[tileCounter*24+20] =new Vector2(1,1);//rechtsObenHinten-1,1
//************************NEEDS CORRECTION!*************
uv[tileCounter*24+18] =new Vector2(0,0);//linksUntenVorn0,0
uv[tileCounter*24+21] =new Vector2(1,0);//rechtsUntenVorn1,0
uv[tileCounter*24+22] =new Vector2(0,1);//linksObenVorn0,1
uv[tileCounter*24+23] =new Vector2(1,1);//rechtsObenVorn1,1
triangles[tileCounter*36+00] =00+tileCounter*24;//leftBtmFront
triangles[tileCounter*36+01] =02+tileCounter*24;//leftTopFront
triangles[tileCounter*36+02] =01+tileCounter*24;//rightBtmFront
triangles[tileCounter*36+03] =01+tileCounter*24;//rightBtmFront
triangles[tileCounter*36+04] =02+tileCounter*24;//leftTopFront
triangles[tileCounter*36+05] =05+tileCounter*24;//rightTopFront;
triangles[tileCounter*36+06] =03+tileCounter*24;//leftBtmBack;
triangles[tileCounter*36+07] =00+tileCounter*24;//leftBtmFront
triangles[tileCounter*36+08] =01+tileCounter*24;//rightBtmFront
triangles[tileCounter*36+09] =01+tileCounter*24;//rightBtmFront
triangles[tileCounter*36+10] =04+tileCounter*24;//rightBtmBack;
triangles[tileCounter*36+11] =03+tileCounter*24;//leftBtmBack;
triangles[tileCounter*36+12] =08+tileCounter*24;//leftBtmFront
triangles[tileCounter*36+13] =11+tileCounter*24;//leftBtmBack
triangles[tileCounter*36+14] =10+tileCounter*24;//leftTopFront
triangles[tileCounter*36+15] =10+tileCounter*24;//leftTopFront
triangles[tileCounter*36+16] =11+tileCounter*24;//leftBtmBack;
triangles[tileCounter*36+17] =14+tileCounter*24;//leftTopBack
triangles[tileCounter*36+18] =09+tileCounter*24;//rightBtmFront
triangles[tileCounter*36+19] =13+tileCounter*24;//rightTopFront;
triangles[tileCounter*36+20] =12+tileCounter*24;//rightBtmBack;
triangles[tileCounter*36+21] =13+tileCounter*24;//rightTopBack
triangles[tileCounter*36+22] =15+tileCounter*24;//rightTopBack
triangles[tileCounter*36+23] =12+tileCounter*24;//rightBtmBack;
triangles[tileCounter*36+24] =22+tileCounter*24;//leftTopBack
triangles[tileCounter*36+25] =19+tileCounter*24;//leftBtmBack;
triangles[tileCounter*36+26] =20+tileCounter*24;//rightBtmBack;
triangles[tileCounter*36+27] =22+tileCounter*24;//leftTopBack
triangles[tileCounter*36+28] =20+tileCounter*24;//rightBtmBack;
triangles[tileCounter*36+29] =23+tileCounter*24;//rightTopBack
triangles[tileCounter*36+30] =22+tileCounter*24;//leftTopBack
triangles[tileCounter*36+31] =21+tileCounter*24;//rightTopFront;
triangles[tileCounter*36+32] =18+tileCounter*24;//leftTopFront
triangles[tileCounter*36+33] =23+tileCounter*24;//rightTopBack
triangles[tileCounter*36+34] =21+tileCounter*24;//rightTopFront;
triangles[tileCounter*36+35] =22+tileCounter*24;//leftTopBack
For now I am only using a little image and not an image map which will be added later. So it does not matter if the image is displayed mirrored. For now I only want that it is displayed at all.
In unity it looks like this:
Is it not drawing at all, or is the UV messed up? I cannot tell from the image. Typically if a side not showing at all is due to you walking the triangles backwards. The direction you walk the vertices for a triangle deter$$anonymous$$es the normal, and you likely have your normal pointing inwards.
The model draws perfectly, but the texture is missing. Look on the lower picture first. There you see the left, the right and the top of the "cube" - all with a texture. Then look on the upper picture which shows the bottom of the "cube". This should actually look exactly like the top as it uses the same picture and the same uv range (0x0 to 1x1).
I understand. Assu$$anonymous$$g the UV is correct, like I indicated in my previous comment you need to wind your triangles for the bottom in the opposite direction. If the winding is backwards, then the surface normal for those two triangles will be up inside the box rather than down so the surface will not show.
Yes, I understand what you mean, but this is not the case here, as the bottom is shown (in the upper picture), just without the texture. I changed the UV map a couple of times to try out different things. But it always stays as it can be seen in the picture.
All the code can be found in my initial post (besides of the exact positions and the object creation stuff).
Answer by robertbu · Feb 24, 2013 at 08:43 PM
I started with a cube with a diffuse shader, a texture and a point light positioned to the "front" left. I then used the script below to apply your mesh to the cube. If your code is correct, then the cube should remain unchanged. What I found was problems in four of the six sides. Only the problems in the bottom and back are obvious in flat lighting. I thought maybe you had a different definition of front, but the best result was with the vertices defined below (and it matched fairly well with your image).
Mesh MakeMesh()
{
Mesh mesh = new Mesh();
Vector3 leftBtmFront = new Vector3(-.5f, -.5f, -.5f);
Vector3 rightBtmFront = new Vector3( .5f, -.5f, -.5f);
Vector3 leftTopFront = new Vector3(-.5f, .5f, -.5f);
Vector3 rightTopFront = new Vector3( .5f, .5f, -.5f);
Vector3 leftBtmBack = new Vector3(-.5f, -.5f, .5f);
Vector3 rightBtmBack = new Vector3( .5f, -.5f, .5f);
Vector3 leftTopBack = new Vector3(-.5f, .5f, .5f);
Vector3 rightTopBack = new Vector3( .5f, .5f, .5f);
int tileCounter = 0;
Vector3[] vertices = new Vector3[24];
Vector2[] uv = new Vector2[24];
int[] triangles = new int[36];
// Your code here
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;
mesh.RecalculateNormals();
return mesh;
}
An approach is to take this code and with a simple cube build one face at a time until you get each right.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to count Tiles in a sourinding Area around an Object? 1 Answer
Can isometric tiles be rotated? 0 Answers
How to fix glitch between walls? With pic. 1 Answer
sliding rows of tiles in 3D 0 Answers