Class ShapeBatch
- All Implemented Interfaces:
AutoCloseable
Call begin(float), add shapes, then end(). Shapes are drawn in the order added, in the
current model-view-projection space (GUI units on a Minecraft screen). A batch flushes when it is full, when an
image needs a different texture, and at end(). Each flush saves and restores every piece of GL state it
touches. Every edge is anti-aliased over one framebuffer pixel; the pixel passed to begin is the
size of that pixel in drawing units, and quads are grown by it so the outer half of the edge is not clipped.
Each shape is one quad of four vertices of 72 bytes: position, position relative to the shape
centre, shape (half size, radius, kind), parameters, texture coordinates and four corner colours. Shapes are drawn
as indexed triangles from the batch's own vertex array. Vertex data goes through a StreamBuffer, so a
frame's shapes land in one buffer; call endFrame() once per frame.
-
Field Summary
FieldsModifier and TypeFieldDescriptionAttribute names, in the order of theirlayout(location)inshape.vsh.static final intBytes per vertex. -
Constructor Summary
ConstructorsConstructorDescriptionShapeBatch(GlBackend gl, GlStateCache cache, ShaderLoader shaders, int capacity) Creates a batch. -
Method Summary
Modifier and TypeMethodDescriptionvoidalphaCutoff(float cutoff) Discards fragments whose alpha is at or below a cutoff, in the fragment shader.arc(float cx, float cy, float radius, float thickness, float startRadians, float sweepRadians, Paint paint) A ring segment with butt caps.begin(float pixel) Starts a batch.A filled circle.voidclose()Deletes the program, the vertex array and the buffers.intReturns how many draw calls this batch has issued, for tests and profiling.booleandrawing()Returns whether the batch is betweenbegin(float)andend().voidend()Draws everything added sincebegin(float).voidendFrame()Marks the end of a frame for the streamed vertex buffers.glyph(int textureId, float x0, float y0, float x1, float y1, float skewTop, float skewBottom, float u0, float v0, float u1, float v1, int argb, float pxRange, float weight, float softness, int atlasWidth, int atlasHeight) One glyph from a multi-channel signed distance field atlas.image(int textureId, float x, float y, float width, float height, float radius, float u0, float v0, float u1, float v1, int tint) A textured rounded rectangle, multiplied by a tint.innerGlow(float x, float y, float width, float height, float radius, float glowWidth, int argb) A glow inside the edge of a rounded rectangle, strongest at the edge.A straight line.The outline of a rounded rectangle, drawn inside its edge.floatpixel()Returns the pixel size given tobegin(float).Connected lines with round joins and caps.rect(float x, float y, float width, float height, float radius, int argb) A filled rounded rectangle in one colour.A filled rounded rectangle.shadow(float x, float y, float width, float height, float radius, float softness, int argb) A soft drop shadow for a rounded rectangle.
-
Field Details
-
STRIDE
public static final int STRIDEBytes per vertex.- See Also:
-
ATTRIBUTES
-
-
Constructor Details
-
ShapeBatch
Creates a batch.- Parameters:
gl- the backendcache- the host's state cacheshaders- the loader that resolves the built-in shaderscapacity- shapes per draw call
-
-
Method Details
-
begin
Starts a batch.- Parameters:
pixel- the size of one framebuffer pixel in drawing units, such as1 / guiScale- Returns:
- this batch
-
end
public void end()Draws everything added sincebegin(float). -
alphaCutoff
public void alphaCutoff(float cutoff) Discards fragments whose alpha is at or below a cutoff, in the fragment shader. Stencil masks use it so only the inside of a shape is written. 0 turns it off.- Parameters:
cutoff- the alpha below which fragments are dropped, in[0, 1)
-
pixel
public float pixel()Returns the pixel size given tobegin(float).- Returns:
- one framebuffer pixel in drawing units
-
drawing
public boolean drawing()Returns whether the batch is betweenbegin(float)andend().- Returns:
- true while drawing
-
glyph
public ShapeBatch glyph(int textureId, float x0, float y0, float x1, float y1, float skewTop, float skewBottom, float u0, float v0, float u1, float v1, int argb, float pxRange, float weight, float softness, int atlasWidth, int atlasHeight) One glyph from a multi-channel signed distance field atlas. Text layout lives inTextRenderer; this is the primitive it draws with.- Parameters:
textureId- the atlas texturex0- quad lefty0- quad topx1- quad righty1- quad bottomskewTop- horizontal offset of the top edge, for italicsskewBottom- horizontal offset of the bottom edgeu0- atlas leftv0- atlas topu1- atlas rightv1- atlas bottomargb- the colourpxRange- the atlas distance range in texelsweight- extra thickness in field units (range / 2 texels is 0.5); 0 for regularsoftness- edge softness in framebuffer pixels, for soft shadows; 0 for a crisp one-pixel edgeatlasWidth- atlas width in texelsatlasHeight- atlas height in texels- Returns:
- this batch
-
drawCalls
public int drawCalls()Returns how many draw calls this batch has issued, for tests and profiling.- Returns:
- the draw call count
-
rect
A filled rounded rectangle.- Parameters:
x- lefty- topwidth- widthheight- heightradius- corner radius, clamped to half the shorter sidepaint- the colour- Returns:
- this batch
-
rect
A filled rounded rectangle in one colour.- Parameters:
x- lefty- topwidth- widthheight- heightradius- corner radiusargb- the colour- Returns:
- this batch
-
outline
public ShapeBatch outline(float x, float y, float width, float height, float radius, float strokeWidth, Paint paint) The outline of a rounded rectangle, drawn inside its edge.- Parameters:
x- lefty- topwidth- widthheight- heightradius- corner radiusstrokeWidth- outline widthpaint- the colour- Returns:
- this batch
-
circle
A filled circle.- Parameters:
cx- centre xcy- centre yradius- the radiuspaint- the colour- Returns:
- this batch
-
arc
public ShapeBatch arc(float cx, float cy, float radius, float thickness, float startRadians, float sweepRadians, Paint paint) A ring segment with butt caps.- Parameters:
cx- centre xcy- centre yradius- outer radiusthickness- ring thickness, inward from the outer radiusstartRadians- start angle; 0 points along +x, angles grow clockwise on screensweepRadians- swept angle;2 * PIor more draws a full ringpaint- the colour; radial paints are not supported on arcs- Returns:
- this batch
-
line
public ShapeBatch line(float x0, float y0, float x1, float y1, float width, boolean roundCaps, Paint paint) A straight line.- Parameters:
x0- start xy0- start yx1- end xy1- end ywidth- line widthroundCaps- round ends reaching half the width past each end point, or square ends at the pointspaint- the colour; a horizontal paint runs from start to end- Returns:
- this batch
-
polyline
Connected lines with round joins and caps. Joins overlap, so a translucent colour is darker where segments meet.- Parameters:
points- x, y pairs; at least two pointswidth- line widthpaint- the colour of every segment- Returns:
- this batch
-
shadow
public ShapeBatch shadow(float x, float y, float width, float height, float radius, float softness, int argb) A soft drop shadow for a rounded rectangle. Draw it before the shape, usually offset down and right.- Parameters:
x- left of the shape casting ity- top of the shape casting itwidth- widthheight- heightradius- corner radiussoftness- half the width of the falloff band around the edgeargb- shadow colour; its alpha is the opacity inside- Returns:
- this batch
-
innerGlow
public ShapeBatch innerGlow(float x, float y, float width, float height, float radius, float glowWidth, int argb) A glow inside the edge of a rounded rectangle, strongest at the edge.- Parameters:
x- lefty- topwidth- widthheight- heightradius- corner radiusglowWidth- how far the glow reaches inwardargb- glow colour- Returns:
- this batch
-
image
public ShapeBatch image(int textureId, float x, float y, float width, float height, float radius, float u0, float v0, float u1, float v1, int tint) A textured rounded rectangle, multiplied by a tint.- Parameters:
textureId- the GL texturex- lefty- topwidth- widthheight- heightradius- corner radiusu0- texture coordinate at the leftv0- texture coordinate at the topu1- texture coordinate at the rightv1- texture coordinate at the bottomtint- colour the texture is multiplied by;0xFFFFFFFFfor none- Returns:
- this batch
-
endFrame
public void endFrame()Marks the end of a frame for the streamed vertex buffers.Helios.tick()calls it. -
close
public void close()Deletes the program, the vertex array and the buffers.- Specified by:
closein interfaceAutoCloseable
-