Create an offset set of points or an offset mesh from an STL file using the three.js library.
https://threejs-offset.netlify.app/
npm install threejs-offset
or
yarn add threejs-offset
This library provides two algorithms for offsetting STL geometry and one helper to build a mesh:
processGeometry→ offset pointscreateOffsetMesh→ offset a full meshcreateMeshFromObject→ convert an offset mesh object into a Three.js Mesh
import { createOffsetMesh, createMeshFromObject } from "threejs-offset"
// Example: parse STL data and apply an offset
const stlData = "... your STL file contents ..."
const offset = 2.0
// Create an offset mesh object
const objects = createOffsetMesh(stlData, offset)
// Convert the object into a Three.js Mesh
const mesh = createMeshFromObject(objects)
scene.add(mesh)import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";
import { processGeometry } from "threejs-offset";
// If you have an STL file (ArrayBuffer):
const geometry = new STLLoader().parse(arrayBuffer); // THREE.BufferGeometry
const offset = 2;
// Ensure normals are present (STLLoader usually provides them; otherwise compute)
if (!geometry.attributes.normal) geometry.computeVertexNormals?.();
const points = processGeometry(geometry, offset); // returns THREE.Points
scene.add(points);-
processGeometryreturns aTHREE.Pointsinstance with materialPointsMaterial({ color: 0xff0000, size: 0.7 })andname = "offset". -
createOffsetMeshexpects an ASCII STL string (not aBufferGeometry). In practice the workflow in the demo is: export a mesh to ASCII (viaSTLExporter), callcreateOffsetMesh, thenawait createMeshFromObject(...). -
If your geometry lacks normals, call
geometry.computeVertexNormals()before usingprocessGeometry.
The library provides two algorithms:
- Point offset – creates a new set of vertices displaced by the given offset.
- Mesh offset – recalculates faces, normals, and vertices to generate a new offset mesh.
The meshed offset is still experimental: results are not perfect, but it’s a good starting point for further refinement.
- Performance may degrade with very large meshes.
- Some geometries (e.g. spheres) are not handled correctly yet.
- For heavy processing, consider moving parts of the algorithm (offsetObjectHash, hashTable) to a Node.js backend.
If you want to start the application locally:
- Clone the project
git clone [email protected]:AngyDev/threejs-offset.git - With your terminal go to the folder where you cloned the project
- Run the command
npm installto install the project's dependencies - Run the command
npm run buildthat creates the dist folder - Run the server with the command
npm run start - If all is correct the browser will be open to the
localhost:9000url
Contributions are welcome! If you have suggestions or bug reports, please open an issue.
