|
| 1 | +/** |
| 2 | + * @author André Storhaug <[email protected]> |
| 3 | + */ |
| 4 | + |
| 5 | +import autoBind from 'auto-bind'; |
| 6 | +import { FileLoader, Loader, Vector3, LoadingManager } from 'three'; |
| 7 | +import { PointOctree } from "sparse-octree"; |
| 8 | +import { Parser } from 'binvox'; |
| 9 | +import { levelOfDetail } from '../mixins/levelOfDetail'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class for loading voxel data stored in BINVOX files. |
| 13 | + * @extends Loader |
| 14 | + */ |
| 15 | +class BINVOXLoader extends Loader { |
| 16 | + /** |
| 17 | + * Create a BINVOXLoader. |
| 18 | + * @param {LoadingManager} manager |
| 19 | + * @mixes levelOfDetail |
| 20 | + */ |
| 21 | + constructor(manager) { |
| 22 | + super(manager); |
| 23 | + autoBind(this); |
| 24 | + Object.assign(this, levelOfDetail); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Loads and parses a BINVOX file from a URL. |
| 29 | + * |
| 30 | + * @param {String} url - URL to the BINVOX file. |
| 31 | + * @param {Function} [onLoad] - Callback invoked with the loaded object. |
| 32 | + * @param {Function} [onProgress] - Callback for download progress. |
| 33 | + * @param {Function} [onError] - Callback for download errors. |
| 34 | + */ |
| 35 | + load(url, onLoad, onProgress, onError) { |
| 36 | + var scope = this; |
| 37 | + |
| 38 | + var loader = new FileLoader(this.manager); |
| 39 | + loader.setPath(this.path); |
| 40 | + loader.setResponseType('arraybuffer') |
| 41 | + loader.load(url, function (buffer) { |
| 42 | + |
| 43 | + scope.parse(buffer) |
| 44 | + .then(octree => onLoad(octree)) |
| 45 | + .catch(err => console.error(err)) |
| 46 | + |
| 47 | + }, onProgress, onError); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Parse BINVOX file data. |
| 52 | + * @param {Buffer} buffer Content of BINVOX file. |
| 53 | + * @return {Promise<PointOctree>} Promise with an octree filled with voxel data. |
| 54 | + */ |
| 55 | + parse(buffer) { |
| 56 | + |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + const parser = new Parser(); |
| 59 | + let data = parser.parse(buffer); |
| 60 | + console.log(data) |
| 61 | + const depth = data.dimension.depth; |
| 62 | + const width = data.dimension.width; |
| 63 | + const height = data.dimension.height; |
| 64 | + |
| 65 | + const min = new Vector3(-depth / 2, -height / 2, -width / 2); |
| 66 | + const max = new Vector3(depth / 2, height / 2, width / 2); |
| 67 | + |
| 68 | + const octree = new PointOctree(min, max, 0, this.LOD.maxPoints, this.LOD.maxDepth); |
| 69 | + let voxelData = {}; |
| 70 | + |
| 71 | + data.voxels.forEach(voxel => { |
| 72 | + let x, y, z; |
| 73 | + |
| 74 | + x = voxel.x - (depth / 2); |
| 75 | + y = voxel.y - (width / 2); |
| 76 | + z = voxel.z - (height / 2); |
| 77 | + |
| 78 | + let point = new Vector3(x, z, y); |
| 79 | + octree.insert(point, voxelData); |
| 80 | + }); |
| 81 | + |
| 82 | + resolve(octree); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +export { BINVOXLoader }; |
0 commit comments