public class ShapeRenderer extends Object implements Disposable
By default a 2D orthographic projection with the origin in the lower left corner is used and units are specified in screen
pixels. This can be changed by configuring the projection matrix, usually using the Camera.combined
matrix. If the
screen resolution changes, the projection matrix may need to be updated.
Shapes are rendered in batches to increase performance. Standard usage pattern looks as follows:
camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(1, 1, 0, 1);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.rect(x, y, width, height);
shapeRenderer.circle(x, y, radius);
shapeRenderer.end();
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(0, 1, 0, 1);
shapeRenderer.rect(x, y, width, height);
shapeRenderer.circle(x, y, radius);
shapeRenderer.end();
ShapeRenderer has a second matrix called the transformation matrix which is used to rotate, scale and translate shapes in a
more flexible manner. The following example shows how to rotate a rectangle around its center using the z-axis as the rotation
axis and placing it's center at (20, 12, 2):
shapeRenderer.begin(ShapeType.Line); shapeRenderer.identity(); shapeRenderer.translate(20, 12, 2); shapeRenderer.rotate(0, 0, 1, 90); shapeRenderer.rect(-width / 2, -height / 2, width, height); shapeRenderer.end();Matrix operations all use postmultiplication and work just like glTranslate, glScale and glRotate. The last transformation specified will be the first that is applied to a shape (rotate then translate in the above example).
The projection and transformation matrices are a state of the ShapeRenderer, just like the color, and will be applied to all shapes until they are changed.
Modifier and Type | Class and Description |
---|---|
static class |
ShapeRenderer.ShapeType
Shape types to be used with
begin(ShapeType) . |
Constructor and Description |
---|
ShapeRenderer() |
ShapeRenderer(int maxVertices) |
ShapeRenderer(int maxVertices,
ShaderProgram defaultShader) |
Modifier and Type | Method and Description |
---|---|
void |
arc(float x,
float y,
float radius,
float start,
float degrees)
Calls
arc(float, float, float, float, float, int) by estimating the number of segments needed for a smooth arc. |
void |
arc(float x,
float y,
float radius,
float start,
float degrees,
int segments)
Draws an arc using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
begin()
Begins a new batch without specifying a shape type.
|
void |
begin(ShapeRenderer.ShapeType type)
Starts a new batch of shapes.
|
void |
box(float x,
float y,
float z,
float width,
float height,
float depth)
Draws a cube using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
circle(float x,
float y,
float radius)
Calls
circle(float, float, float, int) by estimating the number of segments needed for a smooth circle. |
void |
circle(float x,
float y,
float radius,
int segments)
Draws a circle using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
cone(float x,
float y,
float z,
float radius,
float height)
Calls
cone(float, float, float, float, float, int) by estimating the number of segments needed for a smooth
circular base. |
void |
cone(float x,
float y,
float z,
float radius,
float height,
int segments)
Draws a cone using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
curve(float x1,
float y1,
float cx1,
float cy1,
float cx2,
float cy2,
float x2,
float y2,
int segments)
Draws a curve using
ShapeRenderer.ShapeType.Line . |
void |
dispose()
Releases all resources of this object.
|
void |
ellipse(float x,
float y,
float width,
float height)
Calls
ellipse(float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse. |
void |
ellipse(float x,
float y,
float width,
float height,
float rotation)
Calls
ellipse(float, float, float, float, float, int) by estimating the number of segments needed for a smooth ellipse. |
void |
ellipse(float x,
float y,
float width,
float height,
float rotation,
int segments)
Draws an ellipse using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
ellipse(float x,
float y,
float width,
float height,
int segments)
Draws an ellipse using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
end()
Finishes the batch of shapes and ensures they get rendered.
|
void |
flush() |
Color |
getColor() |
ShapeRenderer.ShapeType |
getCurrentType()
Returns the current shape type.
|
Matrix4 |
getProjectionMatrix()
If the matrix is modified,
updateMatrices() must be called. |
ImmediateModeRenderer |
getRenderer() |
Matrix4 |
getTransformMatrix()
If the matrix is modified,
updateMatrices() must be called. |
void |
identity()
Sets the transformation matrix to identity.
|
boolean |
isDrawing() |
void |
line(float x,
float y,
float x2,
float y2) |
void |
line(float x,
float y,
float x2,
float y2,
Color c1,
Color c2) |
void |
line(float x,
float y,
float z,
float x2,
float y2,
float z2)
Draws a line using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
line(float x,
float y,
float z,
float x2,
float y2,
float z2,
Color c1,
Color c2)
Draws a line using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
line(Vector2 v0,
Vector2 v1) |
void |
line(Vector3 v0,
Vector3 v1) |
void |
point(float x,
float y,
float z)
Draws a point using
ShapeRenderer.ShapeType.Point , ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
polygon(float[] vertices) |
void |
polygon(float[] vertices,
int offset,
int count)
Draws a polygon in the x/y plane using
ShapeRenderer.ShapeType.Line . |
void |
polyline(float[] vertices) |
void |
polyline(float[] vertices,
int offset,
int count)
Draws a polyline in the x/y plane using
ShapeRenderer.ShapeType.Line . |
void |
rect(float x,
float y,
float width,
float height)
Draws a rectangle in the x/y plane using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
rect(float x,
float y,
float width,
float height,
Color col1,
Color col2,
Color col3,
Color col4)
Draws a rectangle in the x/y plane using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
rect(float x,
float y,
float originX,
float originY,
float width,
float height,
float scaleX,
float scaleY,
float degrees)
Draws a rectangle in the x/y plane using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
rect(float x,
float y,
float originX,
float originY,
float width,
float height,
float scaleX,
float scaleY,
float degrees,
Color col1,
Color col2,
Color col3,
Color col4)
Draws a rectangle in the x/y plane using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
rectLine(float x1,
float y1,
float x2,
float y2,
float width)
Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.
|
void |
rectLine(float x1,
float y1,
float x2,
float y2,
float width,
Color c1,
Color c2)
Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.
|
void |
rectLine(Vector2 p1,
Vector2 p2,
float width) |
void |
rotate(float axisX,
float axisY,
float axisZ,
float degrees)
Multiplies the current transformation matrix by a rotation matrix.
|
void |
scale(float scaleX,
float scaleY,
float scaleZ)
Multiplies the current transformation matrix by a scale matrix.
|
void |
set(ShapeRenderer.ShapeType type) |
void |
setAutoShapeType(boolean autoShapeType)
If true, when drawing a shape cannot be performed with the current shape type, the batch is flushed and the shape type is
changed automatically.
|
void |
setColor(Color color)
Sets the color to be used by the next shapes drawn.
|
void |
setColor(float r,
float g,
float b,
float a)
Sets the color to be used by the next shapes drawn.
|
void |
setProjectionMatrix(Matrix4 matrix)
Sets the projection matrix to be used for rendering.
|
void |
setTransformMatrix(Matrix4 matrix) |
void |
translate(float x,
float y,
float z)
Multiplies the current transformation matrix by a translation matrix.
|
void |
triangle(float x1,
float y1,
float x2,
float y2,
float x3,
float y3)
Draws a triangle in x/y plane using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
triangle(float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
Color col1,
Color col2,
Color col3)
Draws a triangle in x/y plane with colored corners using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
updateMatrices() |
void |
x(float x,
float y,
float size)
Draws two crossed lines using
ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled . |
void |
x(Vector2 p,
float size) |
public ShapeRenderer()
public ShapeRenderer(int maxVertices)
public ShapeRenderer(int maxVertices, ShaderProgram defaultShader)
public void setColor(Color color)
public void setColor(float r, float g, float b, float a)
public Color getColor()
public void updateMatrices()
public void setProjectionMatrix(Matrix4 matrix)
Camera.combined
.matrix
- public Matrix4 getProjectionMatrix()
updateMatrices()
must be called.public void setTransformMatrix(Matrix4 matrix)
public Matrix4 getTransformMatrix()
updateMatrices()
must be called.public void identity()
public void translate(float x, float y, float z)
public void rotate(float axisX, float axisY, float axisZ, float degrees)
public void scale(float scaleX, float scaleY, float scaleZ)
public void setAutoShapeType(boolean autoShapeType)
public void begin()
IllegalStateException
- if autoShapeType
is false.public void begin(ShapeRenderer.ShapeType type)
end()
.setAutoShapeType(boolean)
public void set(ShapeRenderer.ShapeType type)
public void point(float x, float y, float z)
ShapeRenderer.ShapeType.Point
, ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public final void line(float x, float y, float z, float x2, float y2, float z2)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public final void line(float x, float y, float x2, float y2)
public void line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
. The line is drawn with two colors interpolated
between the start and end points.public void curve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, int segments)
ShapeRenderer.ShapeType.Line
.public void triangle(float x1, float y1, float x2, float y2, float x3, float y3)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void triangle(float x1, float y1, float x2, float y2, float x3, float y3, Color col1, Color col2, Color col3)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void rect(float x, float y, float width, float height)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void rect(float x, float y, float width, float height, Color col1, Color col2, Color col3, Color col4)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
. The x and y specify the lower
left corner.col1
- The color at (x, y).col2
- The color at (x + width, y).col3
- The color at (x + width, y + height).col4
- The color at (x, y + height).public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
. The x and y specify the lower
left corner. The originX and originY specify the point about which to rotate the rectangle.public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees, Color col1, Color col2, Color col3, Color col4)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
. The x and y specify the lower
left corner. The originX and originY specify the point about which to rotate the rectangle.col1
- The color at (x, y)col2
- The color at (x + width, y)col3
- The color at (x + width, y + height)col4
- The color at (x, y + height)public void rectLine(float x1, float y1, float x2, float y2, float width)
public void rectLine(float x1, float y1, float x2, float y2, float width, Color c1, Color c2)
public void box(float x, float y, float z, float width, float height, float depth)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
. The x, y and z specify the bottom, left, front corner
of the rectangle.public void x(float x, float y, float size)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void x(Vector2 p, float size)
x(float, float, float)
public void arc(float x, float y, float radius, float start, float degrees)
arc(float, float, float, float, float, int)
by estimating the number of segments needed for a smooth arc.public void arc(float x, float y, float radius, float start, float degrees, int segments)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void circle(float x, float y, float radius)
circle(float, float, float, int)
by estimating the number of segments needed for a smooth circle.public void circle(float x, float y, float radius, int segments)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void ellipse(float x, float y, float width, float height)
ellipse(float, float, float, float, int)
by estimating the number of segments needed for a smooth ellipse.public void ellipse(float x, float y, float width, float height, int segments)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void ellipse(float x, float y, float width, float height, float rotation)
ellipse(float, float, float, float, float, int)
by estimating the number of segments needed for a smooth ellipse.public void ellipse(float x, float y, float width, float height, float rotation, int segments)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void cone(float x, float y, float z, float radius, float height)
cone(float, float, float, float, float, int)
by estimating the number of segments needed for a smooth
circular base.public void cone(float x, float y, float z, float radius, float height, int segments)
ShapeRenderer.ShapeType.Line
or ShapeRenderer.ShapeType.Filled
.public void polygon(float[] vertices, int offset, int count)
ShapeRenderer.ShapeType.Line
. The vertices must contain at least 3 points (6 floats x,y).public void polygon(float[] vertices)
polygon(float[], int, int)
public void polyline(float[] vertices, int offset, int count)
ShapeRenderer.ShapeType.Line
. The vertices must contain at least 2 points (4 floats x,y).public void polyline(float[] vertices)
polyline(float[], int, int)
public void end()
public void flush()
public ShapeRenderer.ShapeType getCurrentType()
public ImmediateModeRenderer getRenderer()
public boolean isDrawing()
public void dispose()
Disposable
dispose
in interface Disposable
Copyright © 2021. All rights reserved.