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
- Inputs: Java scene code, LMCode schemas and annotated handlers, TeaVM JSO canvas references, geometry parameters, glTF files, colors, light intensities, and browser events.
- Build outputs: the lm3d JAR and target/demo/ containing demo assets, models/ glTF examples, and lmcode/scenes.lms with its schema.
- Runtime outputs: WebGL frames, Java or LMCode-created scene roots, mesh/vertex/triangle counts, transforms, and application-defined DOM status.
- Geometry input: float XYZ positions, matching float XYZ normals, and char triangle indices. Arrays are defensively copied. glTF supports FLOAT VEC3 attributes and unsigned indices within the 16-bit renderer limit.
- Renderer output: the one-argument constructor uses a non-preserved buffer; the two-argument constructor can preserve it for screenshots and readback.
Safety rules
- Treat Java, LMCode, and TeaVM build inputs as trusted code. Do not compile or execute unreviewed LMCode or generated JavaScript.
- Keep the LMCode schema and Java Registry signatures identical and preserve the exact schema bytes used by the compiler. Expose only required host capabilities.
- Treat LMCode node handles as scoped to one Lm3dHost. Do not reuse handles across host instances or assume handle values.
- Validate external numeric geometry data before allocating arrays. Limit vertices and indices, reject malformed lengths, and keep each index within the vertex array.
- Treat downloaded glTF and buffer files as untrusted input. Enforce HTTP origin and size limits appropriate to the application, and handle loader errors without replacing the last valid scene.
- Use innerText for untrusted labels or errors. lm3d does not sanitize application HTML.
- Call renderer.dispose when permanently replacing a renderer to release GPU buffers and its program. Replacing many immutable geometries in one renderer retains their cache entries until disposal.
- Preserve drawing buffers only when readback is required; normal rendering should use the default non-preserved buffer.
- Handle unavailable WebGL and shader errors visibly. Test on the target browser and GPU, including software WebGL in CI.
Worked examples
- Create a Scene and PerspectiveCamera, position the camera, and call lookAt.
- Create a Mesh with BoxGeometry and MeshPhongMaterial, set its position, and add it to the scene.
- Add AmbientLight and DirectionalLight. A directional light points from its position toward its target, whose default position is the origin.
- Construct WebGLRenderer with the canvas, call setSize, and render the scene and camera.
- For hierarchy, call scene.add(group) and group.add(mesh) as separate statements. Object3D.add returns its receiver.
- For animation, update Java transforms inside Window.requestAnimationFrame and render again. For camera input, create OrbitControls with the same camera and canvas.
- 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.
- 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.
- 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.
- 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.
- 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
- Run mvn clean verify to execute the JVM tests and generate the TeaVM site.
- Run npm test with Chromium installed. The test starts its own local server.
- Exercise actual geometry selection and pointer/wheel events, not just Java compilation.
- Load each glTF example and verify its .gltf and expected external .bin requests, imported mesh counts, and distinct rendered frames.
- Run every LMCode handler and verify its mesh/vertex counts, Java object transforms and materials, and distinct WebGL pixels.
- Read WebGL pixels and check gl.getError. A DOM canvas element alone does not prove a scene rendered.
- Check animation frames differ, pause makes them stable, resume restarts motion, and controls still redraw while object motion is paused.
- Check desktop and narrow mobile layouts, external requests, console errors, and page errors. Set DEMO_URL to repeat the suite against production.