lm3d Java 3D application skill

Use lm3d to build a browser scene graph and WebGL renderer in Java, compiled by TeaVM without a JavaScript 3D runtime.

Purpose

Create Java scenes with meshes, indexed geometry, basic or Phong materials, lights, cameras, orbit controls, and glTF 2.x JSON models. Expose the same Java scene operations to annotated LMCode handlers through typed host functions. Start with the quick reference, the working demo, and the public source.

When to use it

Use lm3d when Java should own scene objects, rendering, glTF loading, and browser events, or when LMCode should compose 3D scenes through an explicit Java capability boundary. It is suitable for focused visualizations and teaching examples. It is not a drop-in Three.js replacement and currently has no binary GLB container support, textures, skins, glTF animation, shadows, post-processing, or transparent-object sorting.

Install

Install Java 21, Maven, Node 24.15 or newer, lmcode-java commit b88d3401ee10210a0a6583991d6a81427d5f112a in the local Maven repository, and @lmctl-ai/lmctl 0.2.482. The runtime commit and compiler version are pinned for reproducibility. Set LMCODE_COMPILER_MODULE to the compiler package's dist/dsl.js.

git clone https://github.com/lmctl-ai/lm3d.git
git clone https://github.com/lmctl-ai/lmcode-java.git
(cd lmcode-java && git checkout b88d3401ee10210a0a6583991d6a81427d5f112a && mvn -DskipTests install)
cd lm3d
npm install --prefix compiler @lmctl-ai/lmctl@0.2.482
export LMCODE_COMPILER_MODULE="$PWD/compiler/node_modules/@lmctl-ai/lmctl/dist/dsl.js"
mvn clean install

Commands

Build the library and static demo, or override demo.output to generate the deployable assets elsewhere:

mvn clean package
mvn -Ddemo.output=/absolute/site/path clean package

Run the JVM and real-browser suites:

mvn clean verify
npm ci
npx playwright install --with-deps chromium
npm test

Inputs and outputs

Safety rules

Worked examples

  1. Create a Scene and PerspectiveCamera, position the camera, and call lookAt.
  2. Create a Mesh with BoxGeometry and MeshPhongMaterial, set its position, and add it to the scene.
  3. Add AmbientLight and DirectionalLight. A directional light points from its position toward its target, whose default position is the origin.
  4. Construct WebGLRenderer with the canvas, call setSize, and render the scene and camera.
  5. For hierarchy, call scene.add(group) and group.add(mesh) as separate statements. Object3D.add returns its receiver.
  6. For animation, update Java transforms inside Window.requestAnimationFrame and render again. For camera input, create OrbitControls with the same camera and canvas.
  7. For glTF, call GltfLoader.load with a .gltf URL, add model.root() only in the success callback, and render. Use model.meshCount(), vertexCount(), and triangleCount() for imported statistics.
  8. Try the demo's embedded cube, external-buffer hierarchy, and interleaved torus options to see base64 buffers, external .bin files, generated normals, quaternion rotation, byte strides, and node matrices.
  9. For LMCode, register Lm3dHost with the scenes schema, create a root with lm3d_group, add mesh handles, apply transforms, and finish with lm3d_use. Resolve cube, hierarchy, or materials in program.entries() and call Program.call.
  10. Compare the LMCode handlers in lmcode/scenes.lms: a single cube, a three-mesh hierarchy, Phong customization, and failure after lm3d_use. The host rejects cycles; the demo swaps scenes only after successful completion.
  11. For screenshot or pixel tests, construct WebGLRenderer(canvas, true). Read renderer.triangles() and renderer.drawCalls() after rendering.

The complete compilable scene and imports are in the quick reference and repository README.

Errors and exit codes

LMCode compiler diagnostics and Maven or JVM failures produce a nonzero build exit. Program.load reports schema mismatches and unbound imports. npm test returns nonzero for browser launch, assertion, HTTP, console, page, or WebGL errors. Constructors throw IllegalArgumentException for invalid dimensions, arrays, indices, projections, handles, or null required objects. Matrix4f.invert throws IllegalStateException for a singular matrix. WebGLRenderer throws IllegalStateException when WebGL is unavailable or shader compilation/linking fails. GltfLoader reports HTTP, network, JSON, buffer, accessor, hierarchy, and unsupported-feature errors through its error callback. Include compiler and loader messages in an accessible status.

How to verify

  1. Run mvn clean verify to execute the JVM tests and generate the TeaVM site.
  2. Run npm test with Chromium installed. The test starts its own local server.
  3. Exercise actual geometry selection and pointer/wheel events, not just Java compilation.
  4. Load each glTF example and verify its .gltf and expected external .bin requests, imported mesh counts, and distinct rendered frames.
  5. Run every LMCode handler and verify its mesh/vertex counts, Java object transforms and materials, and distinct WebGL pixels.
  6. Read WebGL pixels and check gl.getError. A DOM canvas element alone does not prove a scene rendered.
  7. Check animation frames differ, pause makes them stable, resume restarts motion, and controls still redraw while object motion is paused.
  8. Check desktop and narrow mobile layouts, external requests, console errors, and page errors. Set DEMO_URL to repeat the suite against production.