Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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 })
Expand Down Expand Up @@ -141,8 +142,13 @@ export class StlModelViewerComponent implements OnInit, OnDestroy {
}
}

async createMesh(path: string, meshOptions: MeshOptions = {}): Promise<THREE.Mesh> {
const geometry: THREE.BufferGeometry = await this.stlLoader.loadAsync(path)
async createMesh(path: string, meshOptions: MeshOptions = {}, parse: boolean = false): Promise<THREE.Mesh> {
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']
Expand Down Expand 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<THREE.Mesh>[] = [];
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))
Expand Down