Drawing resources and transforms
Shapes and text are not all you draw by hand: images go on the canvas too, and any drawing can turn, grow or blend with what is under it. This page continues Drawing shapes and text with resources, transformations and the render state.
You will learn
- How to draw an image stretched, cropped or tinted
- How to rotate and scale a drawing around a point
- How to use the matrix stack directly
- How to change the render state without breaking the next draws
- How to blend a drawing with what is under it
Draw an image
Section titled “Draw an image”The RESOURCE drawer of DrawUtils, a DrawResource, draws a Resource: an image, a vector image, a frame of an animation or a video.
private final Resource image = Resource.of("images/placeholder.png");ContainerNode.create(80, 80, 1000, 200).layer((node, mouseX, mouseY) -> { DrawUtils.RESOURCE.drawResource(0D, 0D, 320D, 180D, this.image); DrawUtils.RESOURCE.drawResource(360D, 50D, 280D, 80D, this.image); DrawUtils.RESOURCE.drawResource(680D, 50D, 80D, 80D, 120D, 50D, 80D, 80D, this.image); final BoundingBox tint = BoundingBox.create(800D, 0D, 200D, 112D); TransformsUI.ACCENT.bind(() -> DrawUtils.RESOURCE.drawResource(800D, 0D, 200D, 112D, this.image), tint, true);}).attach(this);
color.bind(...).
drawResource(x, y, resource)draws the image at the size of the resource, in design pixels.drawResource(x, y, width, height, resource)stretches it over the box.drawResource(x, y, width, height, u, v, regionWidth, regionHeight, resource)draws only a region of the source, in pixels of the image from its top-left corner.
A resource with its own textureCoords(...) draws only that region; the region of the third form is then counted from the corner of that region (Images and media).
An image that is not loaded yet draws nothing and appears on the frame its content is ready.
Keep a resource in a field: created in drawing code, it would be created again on every frame.
An animated image or a video drawn this way plays on its own, and Images and media shows how to control it.
To tint a drawing, run it inside Color.bind: with color.bind(drawing, bounds, true), the image is multiplied by the color, and a gradient spreads over bounds, a BoundingBox.
Rotate and scale a drawing
Section titled “Rotate and scale a drawing”A Transformation is a list of operations that apply to everything you draw inside apply(...):
ContainerNode.create(1160, 80, 600, 240).layer((node, mouseX, mouseY) -> { Transformation .create() .rotate(20D, Rotation.ROLL, Vector.create(150D, 120D)) .apply(() -> DrawUtils.SHAPE.drawRect(80D, 80D, 140D, 80D, TransformsUI.ACCENT));
Transformation .create() .rotate(20D, Rotation.ROLL, Vector.create(380D, 80D)) .apply(() -> DrawUtils.SHAPE.drawRect(380D, 80D, 140D, 80D, TransformsUI.ACCENT));
DrawUtils.SHAPE.drawBorder(80D, 80D, 140D, 80D, TransformsUI.MUTED, 2D); DrawUtils.SHAPE.drawBorder(380D, 80D, 140D, 80D, TransformsUI.MUTED, 2D); DrawUtils.SHAPE.drawCircle(150D, 120D, TransformsUI.TEXT, 7D); DrawUtils.SHAPE.drawCircle(380D, 80D, TransformsUI.TEXT, 7D);}).attach(this);
With an angle that grows over time, each rectangle turns around its pivot:
| Operation | Effect |
|---|---|
translate(vector) | Moves what you draw by a Vector, Vector.create(x, y), in design pixels. |
rotate(angle, rotation, pivot) | Turns it by angle degrees around pivot: Rotation.ROLL in the plane of the screen (clockwise for a positive angle), YAW around the vertical axis, PITCH around the horizontal one. |
scale(scale, pivot) | Scales it around pivot by a Scale, Scale.create(width, height, depth): 1D keeps the size, -1D mirrors. |
Operations apply in the order you add them, each one in the space that the previous ones produced.
apply(drawing) saves the matrix, applies the operations, runs the drawing and restores the matrix, even when the drawing throws.
The angle of rotate, and each value of Vector, Scale and Rotation, can be a Supplier, which apply calls each time.
Build the transformation once in a field, and it follows your values:
private static final Supplier<Double> ANGLE = () -> BridgeHandler.CLOCK.get().currentTimeMillis() % 4000L * 0.09D;
private final Transformation spin = Transformation.create().rotate(TransformsUI.ANGLE, Rotation.ROLL, Vector.create(60D, 60D));Then apply it each time you draw, here in a layer:
ContainerNode.create(1640, 840, 120, 120).layer((node, mouseX, mouseY) -> { this.spin.apply(() -> DrawUtils.SHAPE.drawRect(20D, 20D, 80D, 80D, TransformsUI.ACCENT));}).attach(this);To move, turn or scale a whole node and its children, give it a TransformNodeEffect instead (Effects).
Use the matrix stack
Section titled “Use the matrix stack”A transformation is a shortcut for the model-view MatrixStack of the render bridge, which you can drive yourself:
ContainerNode.create(230, 500, 200, 100).layer((node, mouseX, mouseY) -> { BridgeHandler.RENDER.get().getModelView().with(matrix -> { matrix.translate(node.dw(2D), node.dh(2D), 0D); matrix.rotate(-10D, 0D, 0D, 1D); matrix.scale(1.5D, 1.5D, 1D); DrawUtils.SHAPE.drawRect(-node.dw(2D), -node.dh(2D), node.getWidth(), node.getHeight(), TransformsUI.ELEMENT); }); DrawUtils.SHAPE.drawBorder(0D, 0D, node.getWidth(), node.getHeight(), TransformsUI.ACCENT, 2D);}).attach(this);
with(body) saves the matrix, runs your code and restores the matrix, even when the code throws.
Inside, translate, rotate and scale change it.
The calls compose like in OpenGL: the last call applies first to what you draw.
Here the rectangle is drawn around the origin, scaled, rotated, then moved to the center of the node.
X goes right and Y goes down, in design pixels; rotate(angle, x, y, z) turns by angle degrees around the axis (x, y, z).
The render bridge, RENDER of BridgeHandler, holds a second stack, the projection, in getProjection(); drawing code rarely touches it.
Change the render state
Section titled “Change the render state”The render state holds the line settings, the blending, the current color, the texture and the shader.
Change it inside withState(...), so that what follows draws as before:
ContainerNode.create(700, 400, 500, 300).layer((node, mouseX, mouseY) -> { BridgeHandler.RENDER.get().withState(render -> { render.getState().lineSmooth(true).lineWidth(6F); final Point[] points = {Point.of(150D, 60D), Point.of(350D, 60D), Point.of(250D, 240D)}; DrawUtils.SHAPE.drawShape(DrawMode.LINE_LOOP, TransformsUI.ACCENT, points); });}).attach(this);
lineSmooth and lineWidth apply to the next lines, until withState ends.
IRenderBridge.withState saves the state, runs your code and restores the state, even when the code throws.
RenderState setters return the state, so they chain: lineWidth, lineSmooth, color, blend, texture, textureFilter, textureWrap and shader are the ones that drawing code uses.
withState does not save the matrices: combine it with with when you change both.
Blend with what is under
Section titled “Blend with what is under”The blending decides how a drawing combines with the pixels already drawn. A multiply blend darkens what is under it, like ink on paper:
private static final BlendState MULTIPLY = BlendState.of(Equation.ADD, Factor.DST_COLOR, Factor.ZERO);ContainerNode.create(1300, 400, 500, 300).layer((node, mouseX, mouseY) -> { final Tessellator tessellator = Tessellator.inst(); BridgeHandler.RENDER.get().withState(render -> { render.getState().blend(TransformsUI.MULTIPLY).texture(null).color(0.6F, 0.6F, 0.6F, 1F); tessellator.start(DrawMode.QUADS); for (final double offset : new double[] {0D, 80D}) { tessellator.addVertex(130D + offset, 200D + offset / 2D, 0D); tessellator.addVertex(290D + offset, 200D + offset / 2D, 0D); tessellator.addVertex(290D + offset, 60D + offset / 2D, 0D); tessellator.addVertex(130D + offset, 60D + offset / 2D, 0D); } tessellator.draw(); });}).attach(this);
BlendState has three presets: NORMAL, the usual transparency; PREMULTIPLIED, for colors already multiplied by their alpha; and DISABLED, which replaces the pixels.
BlendState.of(equation, source, destination) builds any other one from a BlendState.Equation and two BlendState.Factor values, the operands of the blend function of the graphics card.
The calls of DrawUtils set the blending they need.
A blend of yours applies to the vertices you send yourself with the Tessellator, as above; Framebuffers and models explains the tessellator.
DrawUtils.RESOURCEdraws a resource at its size, stretched, or as a region of the source.color.bind(...)tints what you draw inside it.- A
Transformationtranslates, rotates and scales a drawing around a pivot, in the order of its operations. - The
MatrixStackdoes the same by hand insidewith(...): the last call applies first. - Change the render state inside
withState(...). - A
BlendStatedecides how a drawing combines with what is under it; it applies to your own geometry.
Try it
Section titled “Try it”Mirror an image. Draw the placeholder image flipped horizontally, in the same 320 × 180 box.
Solution
Scale by -1D on X around the center of the box:
Transformation.create().scale(Scale.create(-1D, 1D, 1D), Vector.create(160D, 90D)).apply(() -> DrawUtils.RESOURCE.drawResource(0D, 0D, 320D, 180D, this.image));Swing instead of spin.
Make the square of spin swing between -15 and 15 degrees instead of turning.
Solution
Give the rotation a supplier that follows a sine of the time:
() -> 15D * Math.sin(BridgeHandler.CLOCK.get().currentTimeMillis() / 500D)