3D models
A model node shows a 3D model inside its bounds, lit and textured, among the other nodes of a UI.
JOID reads models from Wavefront .obj files: a format that every 3D tool exports.
You will learn
- How to load a
.objmodel and its texture - How to show a model and turn it
- How to let the user rotate and zoom a model
- What JOID reads in a
.objfile
Load a model
Section titled “Load a model”ObjModel.of reads a model file and takes the texture that paints it:
private static final ObjModel CUBE = ObjModel.of("cube", "models/box.obj", Resource.of("models/box.png"));The first argument names the model in error messages.
The file is any input that Resource.of takes: a path in the classpath, a file, a stream or a URL (Custom formats explains how JOID finds it).
of reads the whole file at once, on the calling thread, and throws when a line is wrong: load a model once, here in a constant, and share it between nodes.
The texture is a Resource, loaded like any image (Images and media), and repeats when the texture coordinates go past its edges.
Show and turn a model
Section titled “Show and turn a model”A ModelNode draws its model centered in its bounds:
ModelNode.create(100, 100, 200, 200).rotationYaw(yaw).rotationPitch(20D).model(ModelsUI.CUBE).attach(this);RectNode.create(100, 340, 200, 50).color(ModelsUI.ACCENT).label(Text.create("Turn", label)).onClick(event -> yaw.set(yaw.peek() + 45D)).attach(this);| Property | Effect |
|---|---|
zoom | The size of the model: at 1D, the width of the model fills the width of the node. |
rotationYaw | The turn around the vertical axis, in degrees. |
rotationPitch | The tilt toward or away from you, in degrees. |
model | The model to draw, required: a node without one throws when it is attached. |
Each property takes a value or a Supplier, so a model can turn with a signal, as the yaw does here.
JOID draws the model with lighting and its own depth test, then clears the depth, so the model never cuts into the nodes drawn after it.
Both nodes extend BaseModelNode, which holds the model: a custom node that extends it turns the model by a rule of its own and calls drawModel(zoom, rotationYaw, rotationPitch) from its draw.
Draw a 3D model draws a model without a node.
Let the user rotate and zoom
Section titled “Let the user rotate and zoom”A ModelViewerNode turns its model while the user drags it with the left button, and zooms it with the mouse wheel:
final ModelViewerNode viewer = ModelViewerNode.create(400, 100, 200, 200).rotationPitch(20D).rotationPitchRange(-45D, 45D).zoomRange(0.5D, 1.5D).model(ModelsUI.CUBE).attach(this);- A drag of 5 design pixels turns the model by 1 degree; a notch of the wheel zooms by
0.04D. - The drawn model eases toward the requested rotation and zoom, so a drag stays smooth.
zoomRange(min, max)bounds the zoom,0.1Dto2Dby default;rotationYawRangeandrotationPitchRangebound the rotation, which is free by default. A range of0D, 0Dlocks an axis.
The rotation and the zoom are values that both the user and your code change, like the value of a control (Controls teaches the pattern):
zoom(...),rotationYaw(...)androtationPitch(...)set a start value, or bind a signal that follows each change of the user.zoomTo(zoom)androtate(yaw, pitch)change them as the user does.beforeZoomandbeforeRotatecan refuse a change, andonZoomandonRotatehear it.getZoom(),getRotationYaw()andgetRotationPitch()read them:
TextNode.create(400, 340).text(() -> Text.create("Yaw " + Math.round(viewer.getRotationYaw()), info)).attach(this);Know what JOID reads in a .obj file
Section titled “Know what JOID reads in a .obj file”| Line | JOID reads |
|---|---|
v x y z | A vertex position. The Y axis points up. |
vt u v | A texture coordinate, with v from the bottom of the texture, as every 3D tool writes it. |
vn x y z | A vertex normal, for smooth lighting; without normals, each face is lit flat. |
f v1 v2 v3 ... | A face of 3 vertices or more, as v, v/vt, v//vn or v/vt/vn, split into triangles. Indices start at 1, and negative indices count from the last line read. |
o name, g name | Starts a group. All faces of a group share one format. |
JOID skips every other line, such as materials (mtllib, usemtl) and smoothing groups (s): one texture paints the whole model.
A malformed line throws with the name of the model and the number of the line.
To draw a model of your own making, implement IDrawableModel and draw it with the model drawer of DrawUtils, DrawUtils.MODEL (Framebuffers and models builds geometry by hand).
ObjModel.of(name, file, texture)reads a.objfile at once: load it once and share it.- A
ModelNodedraws its model centered in its bounds, sized byzoomand turned byrotationYawandrotationPitch. - A
ModelViewerNodelets the user drag to rotate and scroll to zoom, within ranges you set. - Its rotation and zoom are interactive values: set them, bind them to signals, refuse or hear their changes.
- JOID reads positions, texture coordinates, normals, faces and groups; one texture paints the whole model.
Try it
Section titled “Try it”Look from the side only. Let the user turn the viewer from left to right, but never tilt it.
Solution
.rotationPitchRange(20D, 20D)The pitch stays at its start value of 20 degrees, and the yaw stays free.
Back to the front. Add a button that turns the viewer back to a yaw of 0, as if the user did it.
Solution
.onClick(event -> viewer.rotate(0D, viewer.getRotationPitch()))rotate goes through beforeRotate and onRotate, and the model eases back to the front.
