| 14 | import { Geometry } from "./geometry"; |
| 15 | |
| 16 | export class Preview{ |
| 17 | constructor(){ |
| 18 | this.scene = new Scene(); |
| 19 | this.scene.background = new Color( 0xAAAAAA ); |
| 20 | this.camera = new PerspectiveCamera( 40, 1, 0.1, 20 ); |
| 21 | this.camera.position.set( 0, 0, 3 ); |
| 22 | this.renderer = new WebGLRenderer( { antialias: true } ); |
| 23 | this.renderer.setSize( 512, 512 ); |
| 24 | this.light = new DirectionalLight( 0xFFFFFF, 3 ); |
| 25 | this.light.position.set( 1, 1, 1 ); |
| 26 | this.ambient = new HemisphereLight( 0xAAAAFF, 0x222255, 0.1 ); |
| 27 | this.scene.add( this.light ); |
| 28 | this.scene.add( this.ambient ); |
| 29 | this.container = document.getElementById("preview"); |
| 30 | this.container.appendChild( this.renderer.domElement ); |
| 31 | this.controls = new OrbitControls( this.camera, this.renderer.domElement ); |
| 32 | this.controls.addEventListener( "change", this.render.bind(this) ); |
| 33 | this.material = new MeshStandardMaterial( { color: 0x3333BB } );//, wireframe: true } ); |
| 34 | this.on = false; |
| 35 | } |
| 36 | |
| 37 | show( nodes, depth, holes ){ |
| 38 | const shape = this.nodesToShape( nodes ); |
| 39 | |
| 40 | if (holes){ |
| 41 | for( const [key, nodes] of Object.entries(holes)){ |
| 42 | const path = this.nodesToPath( nodes ); |
| 43 | shape.holes.push( path ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if ( this.mesh ){ |
| 48 | this.mesh.geometry.dispose(); |
| 49 | this.scene.remove( this.mesh ); |
| 50 | } |
| 51 | |
| 52 | const geometry = new ExtrudeGeometry( shape, { |
| 53 | depth, |
| 54 | bevelEnabled: false |
| 55 | }); |
| 56 | |
| 57 | this.mesh = new Mesh( geometry, this.material ); |
| 58 | this.scene.add( this.mesh ); |
| 59 | |
| 60 | const height = window.innerHeight; |
| 61 | const top = (height - 512) / 2; |
| 62 | this.container.style.top = `${top}px`; |
| 63 | |
| 64 | this.controls.target = this.getCenterPoint( this.mesh ); |
| 65 | this.camera.position.set( this.controls.target.x, this.controls.target.y, this.getCameraOffset( this.mesh ) ); |
| 66 | this.controls.update(); |
| 67 | |
| 68 | this.render(); |
| 69 | |
| 70 | this.on = true; |
| 71 | } |
| 72 | |
| 73 | hide(){ |
nothing calls this directly
no outgoing calls
no test coverage detected