Jump to content

How to create a cube mesh?


bakslash
 Share

Recommended Posts

I want to build a cube (excluding the bottom). So I started to create a mesh with the following vertices (I'll just post the actual code since it's not much overhead)

 

The problem I have is that all the 'sides' (but top) are only drawing one triangle.

 

So my question is: What do I have to do to get the second triangle visible?

 

WZR2zq3.png

geometry = new THREE.Geometry();// build top: alwaysgeometry.vertices.push(new THREE.Vector3(0, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.vertices.push(new THREE.Vector3(0, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.vertices.push(new THREE.Vector3(0, 1, 1));geometry.faces.push(new THREE.Face3(0, 1, 2));geometry.faces.push(new THREE.Face3(3, 4, 5));f = 5;// build left: (-x)if (!chunk.isSolid(x - 1, y)) {geometry.vertices.push(new THREE.Vector3(0, 0, 0));geometry.vertices.push(new THREE.Vector3(0, 0, 1));geometry.vertices.push(new THREE.Vector3(0, 1, 1));geometry.vertices.push(new THREE.Vector3(0, 0, 0));geometry.vertices.push(new THREE.Vector3(0, 1, 0));geometry.vertices.push(new THREE.Vector3(0, 1, 1));geometry.faces.push(new THREE.Face3(f + 1, f + 2, f + 3));geometry.faces.push(new THREE.Face3(f + 4, f + 5, f + 6));f += 6;}// build right: (+x)if (!chunk.isSolid(x + 1, y)) {geometry.vertices.push(new THREE.Vector3(1, 0, 0));geometry.vertices.push(new THREE.Vector3(1, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.vertices.push(new THREE.Vector3(1, 0, 0));geometry.vertices.push(new THREE.Vector3(1, 1, 0));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.faces.push(new THREE.Face3(f + 1, f + 2, f + 3));geometry.faces.push(new THREE.Face3(f + 4, f + 5, f + 6));f += 6;}//build front: (+y)if (!chunk.isSolid(x, y + 1)) {geometry.vertices.push(new THREE.Vector3(0, 1, 0));geometry.vertices.push(new THREE.Vector3(0, 1, 1));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.vertices.push(new THREE.Vector3(0, 1, 0));geometry.vertices.push(new THREE.Vector3(1, 1, 0));geometry.vertices.push(new THREE.Vector3(1, 1, 1));geometry.faces.push(new THREE.Face3(f + 1, f + 2, f + 3));geometry.faces.push(new THREE.Face3(f + 4, f + 5, f + 6));f += 6;}// build back: (-y)if (!chunk.isSolid(x, y - 1)) {geometry.vertices.push(new THREE.Vector3(0, 0, 0));geometry.vertices.push(new THREE.Vector3(0, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 0, 1));geometry.vertices.push(new THREE.Vector3(0, 0, 0));geometry.vertices.push(new THREE.Vector3(1, 0, 1));geometry.vertices.push(new THREE.Vector3(1, 0, 0));geometry.faces.push(new THREE.Face3(f + 1, f + 2, f + 3));geometry.faces.push(new THREE.Face3(f + 4, f + 5, f + 6));f += 6;}cube = new THREE.Mesh(geometry, material);cube.position.x = x + (cx * Chunk.dimension);cube.position.y = y + (cy * Chunk.dimension);scene.add(cube);}
Link to comment
Share on other sites

  • 1 month later...

identify which of the sides it is that is correct, then invert the order of the indices for the second triangle. So either invert the order of the first three or the last three.

 

This is usually the problem since the faces are only drawn into one direction and as far as I know, that is due to the order of the vertices put into the geometry info

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...