Create an offset set of points or an offset mesh from an STL file using the three.js library.
https://threejs-offset.netlify.app/
This simple application gives you a basic idea of an offset on a mesh. You can try the functionality with different files and different offset values.
With this application is possible to try adding an offset to your mesh and obtain a list of points or a meshed offset. We have two different algorithms, one to manipulate the point offset and another for the meshed offset.
The logic of the first algorithm is based on getting the vertices and the normals of the geometry of the selected mesh and creating an array of new vertices with the given offset and then creating a new 3D object, with points.
The logic of the second one is more complicated and the results are not perfect, but it's a starting point. Let's look at the steps to obtain the meshed offset.
A mesh has a geometry with a list of vertices and normals. We want to get the faces of the mesh, calculate a new position with the passed offset and the normal of the faces and then reposition the faces in the correct place.
- Exports the mesh as a not binary file and creates an object with the number of the face, the normal of this face and the three vertices of the face
- Creates a hash table with the vertex in common on more faces as the key and the value is a list of objects with the face, the normal and the position of the vertex in the face.
- Calculates the sum of the normals of the faces with the same vertex in common
- Normalizes the normal
- Calculates the new position with the offset, the normal and the vertex
- Adds the new position in the correct position of the new object (this is a clone of the initial object)
- At the end calculates the new normals with the new positions and completes the new object
- Creates a mesh from the new objects
- The process can be slow when trying to load relatively complex models with a lot of faces. If you want, you can move the files
offsetObjectHash
andhashTable
to a node.js server. - The process does not work in some cases, for example, with a sphere. I'm working on it. If you have some ideas on how to improve it, please open an issue, I appreciate that.
If you want to start the application locally:
- Clone the project
git clone git@github.com:AngyDev/threejs-offset.git
- With your terminal go to the folder where you cloned the project
- Run the command
npm install
to install the project's dependencies - Run the command
npm run build
that 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:8080
url