threejs-ply-loader
Version:
Node.js wrapper for three.js PLYLoader
72 lines (47 loc) • 2.45 kB
Markdown
  [](http://badge.fury.io/js/threejs-ply-loader) 
Node.js wrapper for [three.js][THREEJS-github-link] PLYLoader (currently three.js v0.137.0).
Original PLYLoader source code can be found [here][PLYLoader-source-link].
Beside some minor edits, I added an additional helper function for converting Node Buffer to ArrayBuffer (convenience-wise) and I put in place tests.
This library was developed as a [JavaScript Module][javascript-module-url]. The recommended use with CommonJS files is through [dynamic imports][dynamic-import-url].
## Usage
```javascript
const fs = require("fs");
const { join } = require("path");
const THREE = require("three");
(async () => {
// Import PLYLoader Factory
const { PLYLoaderFactory } = await import("threejs-ply-loader");
// Initialize PLYLoader Class
const PLYLoader = PLYLoaderFactory(THREE);
// Instantiate PLYLoader object
const plyLoader = new PLYLoader();
// Read 3D Model as PLY file format
const sourceFilepath = join(__dirname, "assets/cube.ply");
const fileBuffer = fs.readFileSync(sourceFilepath);
// Convert node file Buffer to ArrayBuffer
const fileArrayBuffer = plyLoader.bufferToArrayBuffer(fileBuffer);
// Parse 3D model into THREE geometry
const geometry = plyLoader.parse(fileArrayBuffer);
})();
```
An example is present in the examples folder. It will load a PLY file model and output its rendering to a PNG file. It can be run from a shell:
```bash
npm install
npm run example:cube-to-png
```
The output image is going to located in `examples/temp/vertex-colored-cube.png`
They can be run from a shell:
```bash
npm install
npm test
```
Developed with Node v16.13.2
[]: https://github.com/mrdoob/three.js
[]: https://github.com/mrdoob/three.js/blob/r137/examples/js/loaders/PLYLoader.js
[]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[]: https://v8.dev/features/dynamic-import#dynamic