How To > Create Custom Mesh Object |
MAXScript lets you create editable mesh objects by supplying lists of vertices and faces to the mesh constructor.
This topic shows how to write a simple mesh-generation script to create a circular 2D mesh object, similar to a sun symbol.
Creating Editable Mesh objects
First, we need some variables to define the geometry. The circular object has an inner and outer radius and a width value defining the outer size of the ray.
The two arrays store the vertex coordinates and the face definitions.
The user variable vert_count is set to 0 and will be incremented while creating the faces of the mesh.
The number of steps is related to the number of faces to create. It is used as step in the following loop.
The for a loop counts from 0 to 360 (degrees), minus one step to prevent overlap, with a user-defined step provided by the num_faces variable. This variable defines the number of "rays" in the mesh. For example, to get 10 rays we need 360/10 = 36 degrees per ray. The a value will be incremented nine times by the value of 36 to make a full turn in degrees.
v1 = [radius1*cos(a+width),radius1*sin(a+width),0] v2 = [radius1*cos(a-width),radius1*sin(a-width),0] v3 = [radius2*cos(a),radius2*sin(a),0]
Now we will define the 3 vertices for a single face.
The Vertices v1 and v2 are at the outer side of the object.
The Vertex v3 is closer to the center (at distance equal to radius2).
When radius3 is 0, all v3 vertices of all faces are coincident at the center.
We use the equation of the circle which is:
X = CenterX + Radius * Cos(A); Y = CenterY + Radius * Sin(A); Z = 0.0
where A is an angle changing from 0 to 2*Pi. In our case, CenterX and CenterY are 0.0.
By adding and subtracting the width value to/from the angle a , we get the outer two vertices offset from the actual angle used by the third vertex. The third coordinate Z is always 0 in our case – the mesh will reside on the ground XY plane.
We add all 3 new vertices to the vertex array.
Now we can store a face in the face array, listing the 3 vertex indices as a Point3 value in Counter-Clockwise order. This will create the face with a normal pointing up. Since vert_count is still 3 less than the number of vertices in the vert_array , we can add 1, 2 and 3 to its current value to get the correct indices needed for the face definition.
Now we increase the vertex counter by 3. Next face definition will use the correct indices again. The same could be expressed as vert_count = vert_count + 3
Now we can create a new Editable mesh by calling the mesh constructor and supplying the vertex and face arrays.
If you evaluate the script now, a new mesh object will appear in the scene. Changing the initial radius and width values lets you create multiple variations of the mesh.
This was just a sketch of a mesh-creating script. In a second step, we will enhance this code to generate a real geometry plug-in script operating interactively from within the Create tab.
How To ... Create Scripted Geometry Plug-in
Possible things to try yourself would be assigning different material IDs to the separate faces, assigning UVW coordinates to the mesh etc.