Jump to content

Create a mesh from a list of vertices and faces


emilbusman
 Share

Recommended Posts

I'm a newbie going through the basics here. In the section about creating a mesh from a list of vertices, I can't get the code to work. It seems to crash when constructing the mesh in the line

var plane = new BABYLON.Mesh("plane", [3, 3, 2], scene);

Any thoughts? I am trying to create a hexagon (once I get the sample code working anyways :)

Link to comment
Share on other sites

THe correct line is:

var plane = BABYLON.Mesh.CreatePlane("plane", 3, scene);

 

Thanks for the reply, but that doesn't seem to be what I need either.

 

The section of the walkthrough I am referring to states:

You can also create a mesh from a list of vertices and faces:.var plane = new BABYLON.Mesh("plane", [3, 3, 2], scene);var indices = [];var vertices = [];// Verticesvar halfSize = 0.5;vertices.push(-halfSize, -halfSize, 0, 0, 1.0, 0, 0.0, 0.0);vertices.push(halfSize, -halfSize, 0, 0, 1.0, 0, 1.0, 0.0);vertices.push(halfSize, halfSize, 0, 0, 1.0, 0, 1.0, 1.0);vertices.push(-halfSize, halfSize, 0, 0, 1.0, 0, 0.0, 1.0);// Indicesindices.push(0);indices.push(1);indices.push(2);indices.push(0);indices.push(2);indices.push(3);plane.setVertices(vertices, 1);plane.setIndices(indices);return plane;You have to create a blank new mesh and call setVertices and setIndices.

using the line you gave me breaks the lines plane.setVertices(vertices, 1); and plane.setIndices(indices);

 

I don't see anything like those two functions in the documentation either.

Link to comment
Share on other sites

Oh sorry I misunderstood.

 

Here is a working sample for a box:

BABYLON.Mesh.CreateBox = function (name, size, scene, updatable) {        var box = new BABYLON.Mesh(name, scene);        var normalsSource = [            new BABYLON.Vector3(0, 0, 1),            new BABYLON.Vector3(0, 0, -1),            new BABYLON.Vector3(1, 0, 0),            new BABYLON.Vector3(-1, 0, 0),            new BABYLON.Vector3(0, 1, 0),            new BABYLON.Vector3(0, -1, 0)        ];        var indices = [];        var positions = [];        var normals = [];        var uvs = [];        // Create each face in turn.        for (var index = 0; index < normalsSource.length; index++) {            var normal = normalsSource[index];            // Get two vectors perpendicular to the face normal and to each other.            var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);            var side2 = BABYLON.Vector3.Cross(normal, side1);            // Six indices (two triangles) per face.            var verticesLength = positions.length / 3;            indices.push(verticesLength);            indices.push(verticesLength + 1);            indices.push(verticesLength + 2);            indices.push(verticesLength);            indices.push(verticesLength + 2);            indices.push(verticesLength + 3);            // Four vertices per face.            var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);            positions.push(vertex.x, vertex.y, vertex.z);            normals.push(normal.x, normal.y, normal.z);            uvs.push(1.0, 1.0);            vertex = normal.subtract(side1).add(side2).scale(size / 2);            positions.push(vertex.x, vertex.y, vertex.z);            normals.push(normal.x, normal.y, normal.z);            uvs.push(0.0, 1.0);            vertex = normal.add(side1).add(side2).scale(size / 2);            positions.push(vertex.x, vertex.y, vertex.z);            normals.push(normal.x, normal.y, normal.z);            uvs.push(0.0, 0.0);            vertex = normal.add(side1).subtract(side2).scale(size / 2);            positions.push(vertex.x, vertex.y, vertex.z);            normals.push(normal.x, normal.y, normal.z);            uvs.push(1.0, 0.0);        }        box.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);        box.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);        box.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);        box.setIndices(indices);        return box;    };
Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...

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...