Skip to content

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 .obj model 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 .obj file

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"));

Full file: ModelsUI.java

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.

A ModelNode draws its model centered in its bounds:

ModelNode
.create(100, 100, 200, 200)
.rotationYaw(yaw)
.rotationPitch(20D)
.model(ModelsUI.CUBE)
.attach(this);

Full file: ModelsUI.java

RectNode
.create(100, 340, 200, 50)
.color(ModelsUI.ACCENT)
.label(Text.create("Turn", label))
.onClick(event -> yaw.set(yaw.peek() + 45D))
.attach(this);

Full file: ModelsUI.java

Both nodes share one model. The first one turns by 45 degrees on each click of the button; the second one turns under the mouse.
PropertyEffect
zoomThe size of the model: at 1D, the width of the model fills the width of the node.
rotationYawThe turn around the vertical axis, in degrees.
rotationPitchThe tilt toward or away from you, in degrees.
modelThe 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.

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);

Full file: ModelsUI.java

  • 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.1D to 2D by default; rotationYawRange and rotationPitchRange bound the rotation, which is free by default. A range of 0D, 0D locks 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(...) and rotationPitch(...) set a start value, or bind a signal that follows each change of the user.
  • zoomTo(zoom) and rotate(yaw, pitch) change them as the user does.
  • beforeZoom and beforeRotate can refuse a change, and onZoom and onRotate hear it.
  • getZoom(), getRotationYaw() and getRotationPitch() read them:
TextNode.create(400, 340).text(() -> Text.create("Yaw " + Math.round(viewer.getRotationYaw()), info)).attach(this);

Full file: ModelsUI.java

LineJOID reads
v x y zA vertex position. The Y axis points up.
vt u vA texture coordinate, with v from the bottom of the texture, as every 3D tool writes it.
vn x y zA 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 nameStarts 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 .obj file at once: load it once and share it.
  • A ModelNode draws its model centered in its bounds, sized by zoom and turned by rotationYaw and rotationPitch.
  • A ModelViewerNode lets 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.

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.

Report a problem with this page