@@ -4,10 +4,10 @@ function ThreeJsViewer(){
44 this . UNSELECTED_COLOR = 0xFF0000 ;
55 this . meshes = { } ;
66
7- this . init = function ( modelUrl ) {
7+ this . init = function ( container ) {
88
99 this . projector = new THREE . Projector ( ) ;
10- this . size = { width : window . innerWidth - 1 , height : window . innerHeight - 1 }
10+ this . size = { width : container . width ( ) , height : container . height ( ) } ;
1111
1212 this . renderer = new THREE . WebGLRenderer ( ) ;
1313 this . renderer . sortObjects = false ;
@@ -45,6 +45,8 @@ function ThreeJsViewer(){
4545 this . camera . screen . height = this . size . height ;
4646
4747 this . scene = new THREE . Scene ( ) ;
48+ this . root = new THREE . Object3D ( ) ;
49+ this . scene . addObject ( this . root ) ;
4850
4951 var light1 = new THREE . DirectionalLight ( 0xffffff , 2 ) ;
5052 light1 . position . x = .5 ;
@@ -60,10 +62,14 @@ function ThreeJsViewer(){
6062 light2 . position . normalize ( ) ;
6163 this . scene . addLight ( light2 ) ;
6264
65+ container . click ( this . onMouseDown . bind ( this ) ) ;
66+ container . append ( this . renderer . domElement ) ;
67+ this . container = container ;
68+ this . onclick = function ( ) { } ;
69+ } ;
6370
71+ this . loadModel = function ( modelUrl ) {
6472 var geometryLoader = new THREE . JSONLoader ( true ) ;
65- this . root = new THREE . Object3D ( ) ;
66- this . scene . addObject ( this . root ) ;
6773
6874 var callback = function ( partId ) { return function ( geometry ) {
6975 var material = new THREE . MeshPhongMaterial ( { color : this . UNSELECTED_COLOR } ) ;
@@ -94,12 +100,13 @@ function ThreeJsViewer(){
94100 } . bind ( this ) ;
95101 geometryLoader . onLoadStart ( ) ;
96102 worker . postMessage ( new Date ( ) . getTime ( ) ) ;
97-
98- this . renderer . domElement . addEventListener ( 'mousedown' , this . onMouseDown . bind ( this ) , false ) ;
99- document . body . appendChild ( this . renderer . domElement ) ;
100- this . onclick = function ( ) { } ;
101- } ;
102-
103+ } ;
104+ this . clearModel = function ( ) {
105+ this . scene . removeChild ( this . root ) ;
106+ this . root = new THREE . Object3D ( ) ;
107+ this . scene . addObject ( this . root ) ;
108+ this . meshes = { } ;
109+ } ;
103110 this . computeBoundingBox = function ( ) {
104111 this . root . children [ 0 ] . geometry . computeBoundingBox ( ) ;
105112 var initialBB = this . root . children [ 0 ] . geometry . boundingBox ;
@@ -119,8 +126,8 @@ function ThreeJsViewer(){
119126 event . preventDefault ( ) ;
120127
121128 var mouse = new THREE . Vector3 ( 0 , 0 , 0 ) ;
122- mouse . x = ( event . clientX / this . size . width ) * 2 - 1 ;
123- mouse . y = - ( event . clientY / this . size . height ) * 2 + 1 ;
129+ mouse . x = ( event . pageX - this . container . offset ( ) . left ) / this . size . width * 2 - 1 ; // ( event.clientX / this.size.width ) * 2 - 1;
130+ mouse . y = - ( event . pageY - this . container . offset ( ) . top ) / this . size . height * 2 + 1 ; // - ( event.clientY / this.size.height ) * 2 + 1;
124131
125132 this . projector . unprojectVector ( mouse , this . camera ) ;
126133
0 commit comments