diff --git a/projects/angular-stl-model-viewer/src/lib/angular-stl-model-viewer.component.ts b/projects/angular-stl-model-viewer/src/lib/angular-stl-model-viewer.component.ts index 3311d6f..34557f7 100644 --- a/projects/angular-stl-model-viewer/src/lib/angular-stl-model-viewer.component.ts +++ b/projects/angular-stl-model-viewer/src/lib/angular-stl-model-viewer.component.ts @@ -24,7 +24,7 @@ export interface MeshOptions { receiveShadow?: boolean scale?: THREE.Vector3 up?: THREE.Vector3 - userData?: {[key: string]: any} + userData?: { [key: string]: any } visible?: boolean } @@ -38,7 +38,7 @@ const defaultMeshOptions = { const isWebGLAvailable = () => { try { const canvas = document.createElement('canvas') - return !! (window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext( 'experimental-webgl'))) + return !!(window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))) } catch (e) { return false } @@ -58,10 +58,11 @@ const isWebGLAvailable = () => { }) export class StlModelViewerComponent implements OnInit, OnDestroy { @Input() stlModels: string[] = [] + @Input() stlModelFiles: string[] = [] @Input() hasControls = true @Input() camera: THREE.PerspectiveCamera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 15) - @Input() cameraTarget: THREE.Vector3 = new THREE.Vector3( 0, 0, 0 ) - @Input() light: THREE.Light = new THREE.PointLight( 0xffffff ) + @Input() cameraTarget: THREE.Vector3 = new THREE.Vector3(0, 0, 0) + @Input() light: THREE.Light = new THREE.PointLight(0xffffff) @Input() material: THREE.Material = new THREE.MeshPhongMaterial({ color: 0xc4c4c4, shininess: 100, specular: 0x111111 }) @Input() scene: THREE.Scene = new THREE.Scene() @Input() renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer({ antialias: true }) @@ -141,8 +142,13 @@ export class StlModelViewerComponent implements OnInit, OnDestroy { } } - async createMesh(path: string, meshOptions: MeshOptions = {}): Promise { - const geometry: THREE.BufferGeometry = await this.stlLoader.loadAsync(path) + async createMesh(path: string, meshOptions: MeshOptions = {}, parse: boolean = false): Promise { + let geometry: THREE.BufferGeometry = null; + if (parse) { + geometry = this.stlLoader.parse(path); + } else { + geometry = await this.stlLoader.loadAsync(path); + } const mesh = new THREE.Mesh(geometry, this.material) const vectorOptions = ['position', 'scale', 'up'] @@ -195,8 +201,13 @@ export class StlModelViewerComponent implements OnInit, OnDestroy { } window.addEventListener('resize', this.onWindowResize, false) - - const meshCreations = this.stlModels.map((modelPath, index) => this.createMesh(modelPath, this.meshOptions[index])) + let meshCreations: Promise[] = []; + if (this.stlModels.length > 0) { + meshCreations = this.stlModels.map((modelPath, index) => this.createMesh(modelPath, this.meshOptions[index])); + } + else { + meshCreations = this.stlModelFiles.map((modelFile, index) => this.createMesh(modelFile, this.meshOptions[index], true)); + } const meshes: THREE.Object3D[] = await Promise.all(meshCreations) meshes.map((mesh) => this.meshGroup.add(mesh))