Goals
IntroductionMT4j provides several classes for common geometric shapes. These geometric shapes are defined by vertices, which are points in 3D space describing the shape's geometry. All shapes in the shapes package extend the AbstractShape class which provides the basic funcitonality for shapes. Currently, MT4j offers several shape classes:
3D:
Shapes provide following functionality:
Creating and destroying shapesAs shapes extend the MTComponent class the instructions for creating and destroying components in the components tutorial also apply to shapes. Example:
In this example we use the MTPolygon class to specify a triangle shape. The vertices used in the shape are defined by a Vertex array. As we can see, besides the position we can also specify a different color for each vertex. This will create a gradient effect. We also notice that the outline stroke doesent draw the last line. If we want the outline stroke to close the path we have to specify the first vertex one more time at the end of the vertex array. Example:
This would create an ellipse with the center point at (100,100,0). The shape will calculate all the vertices needed for the ellipse itself. Changing a shape's geometryThe geometry, and with it the shape itself can be changed by changing the shape's vertices. Therefore, shapes offer the setVertices() and the setGeometryInfo() methods. The setVertices() method takes an array of vertices as an argument which will then define the shape. Internally, the setVertices() method creates a new GeometryInfo object which encapsulates all the information needed to describe the shapes geometry. We can also provide a new GeometryInfo object directly to the shape using the setGeometryInfo() method. For the creation of a geometryInfo object we need to specify an array of vertices as well. Besides the vertices, we can also specify an array of normals used for lightning calculations and an array of indices used for indexed geometry (mostly used at triangle meshes). Relevant Methods:
Setting and changing a shape's appearanceEvery shape has a StyleInfo object with contains and defines most of the shape's appearance such as:
We can change these settings individually by using the corresponding method or we can change all settings at once by providing a new StyleInfo object using setStyleInfo(). ColorsWhen setting colors, we have to specify MTColor objects. MT4j uses the RGBA color model which is an additive color model in which red, green, and blue are added together resulting in the final color. Additionally we can set an alpha value (=transparency). The allowed value range is from 0 to 255 for each component. The default color for shapes is white. FillingThe interior of a shape can be painted with a solid color using the setFillColor() method of the shape. Example:
If we dont want the shape to be filled at all, we can invoke the shape's setNoFill() method with "true". Example:
Outline StrokeMost shapes allow to draw their countour as a stroked outline. The color of the outline can be set using the setStrokeColor() method while the width of the stroke is controlled by the setStrokeWeight() method.
We can also omit drawing the outline completely by using the setNoStroke() method.
Now, only the filling will be drawn. Note: If we set both noStroke and noFill to true, the shape will draw nothing at all! TexturesShapes also allow to use a texture image as the filling by mapping an image onto the shape. The information of how to map the image onto the shape is contained in the shapes vertices and can be changed on a vertex using the setTextureCoordinateU() and setTextureCoordinateV() methods. MT4j shapes like the MTRectangle, MTRoundRectangle and MTEllipse already contain default texture coordinates so we just have to set the texture using the setTexture() method. General information on texture mapping can be found here on wikipedia. Example:
This shows how to load an image from file and use it as a texture on a MT4j shape. We notice that the fill color setting still influences the texture and tints it blue. If we want to display the texture un-tinted we have to change the fill color to white. Relevant Methods:
Positioning shapesAll shapes provide methods for positioning them in space by taking their center point as reference. As usual, we have to specify which coordinate space we want the position to be relative to. Note: The center point is calculated using a bounding shape of the real shape so the result of a positioning might sometimes be slightly different than expected. Example: mtEllipse.setPositionGlobal(new Vector3D(200,250,100)); The ellipse's center will now be at P=(200,250,100) relative to the global space. Relevant Methods:
Size of shapesAll provided shapes offer methods to measure their width and height. The calculation is done, using the shape's bounding shape. If the shape has no bounding shape, a temporary bounding shape is created. The precision of the calculated dimensions depend on how well the bounding shape fits the real shape. The TransformSpace parameter specifies which coordinate space the dimensions should be relative to. Relevant Methods:
Bounding ShapesBounding shapes are geometric shapes that fully contain another shape. The geometry of the bounding shape is usually less complex than the geometry of the "real" shape to facilitate and speed up calculations like intersection tests for example. MT4j allows to create and assign following bounding shapes to shapes:
When assigning bounding shapes we should consider which bounding shape will fit the real shape best. If we know that a shape will only be used on the Z=0 plane in space (usually the case with 2D shapes) for example, we would assign the oriented bounding rectangle to the shape. This can significantly speed up picking and intersection tests. For 3D shapes (=> MTTriangleMesh shapes or 2D shapes that dont lie in the z=0 plane) we can choose between the bounding sphere and bounding box shapes. For all MT4j shapes, a default 3D bounding shape is computed automatically at initialization (usually a oriented bounding box or bounding sphere). So setting a bounding shape is only necessary if the default bounding shape isn't ideal. Bounding shapes are used in following calculcations:
How and in which calculations the bounding shape is used can be set using the setBoundsPickingBehaviour() method. It accepts one of the three constant values:
Relevant Methods:
|