From d203a296cf0891af4d9a8e570b3c70f5be6bdc6d Mon Sep 17 00:00:00 2001 From: jose andres Date: Mon, 21 Dec 2020 10:35:35 +0100 Subject: [PATCH 1/3] New Configuration Add new Input Properties. stlModelFiles string[] - List of stl model files --- .../lib/angular-stl-model-viewer.component.ts | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) 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..45c4d57 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 = await this.stlLoader.loadAsync(path); + } else { + geometry = this.stlLoader.parse(path); + } const mesh = new THREE.Mesh(geometry, this.material) const vectorOptions = ['position', 'scale', 'up'] @@ -195,8 +201,14 @@ 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: any = null; + if (Array.isArray(this.stlModels) && + (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)) From fad72d0caa1275b6131d87ce87ae9a3ba61bff87 Mon Sep 17 00:00:00 2001 From: jose andres Date: Mon, 21 Dec 2020 15:33:35 +0100 Subject: [PATCH 2/3] Cosmetic changes --- .../src/lib/angular-stl-model-viewer.component.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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 45c4d57..16e29c7 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 @@ -144,10 +144,10 @@ export class StlModelViewerComponent implements OnInit, OnDestroy { async createMesh(path: string, meshOptions: MeshOptions = {}, parse: boolean = false): Promise { let geometry: THREE.BufferGeometry = null; - if (!parse) { - geometry = await this.stlLoader.loadAsync(path); - } else { + if (parse) { geometry = this.stlLoader.parse(path); + } else { + geometry = await this.stlLoader.loadAsync(path); } const mesh = new THREE.Mesh(geometry, this.material) @@ -201,9 +201,8 @@ export class StlModelViewerComponent implements OnInit, OnDestroy { } window.addEventListener('resize', this.onWindowResize, false) - let meshCreations: any = null; - if (Array.isArray(this.stlModels) && - (this.stlModels.length > 0)) { + let meshCreations: Promise[] = null; + if (this.stlModels.length > 0) { meshCreations = this.stlModels.map((modelPath, index) => this.createMesh(modelPath, this.meshOptions[index])); } else { From 83002f4047d5bf037384d08cb5b6115421f97802 Mon Sep 17 00:00:00 2001 From: jose andres Date: Tue, 22 Dec 2020 13:20:19 +0100 Subject: [PATCH 3/3] Cosmetic change Rocket --- .../src/lib/angular-stl-model-viewer.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 16e29c7..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 @@ -201,7 +201,7 @@ export class StlModelViewerComponent implements OnInit, OnDestroy { } window.addEventListener('resize', this.onWindowResize, false) - let meshCreations: Promise[] = null; + let meshCreations: Promise[] = []; if (this.stlModels.length > 0) { meshCreations = this.stlModels.map((modelPath, index) => this.createMesh(modelPath, this.meshOptions[index])); }