11function ThreeJsViewer ( ) {
22
3- this . SELECTED_COLOR = 0xffff00 ;
4- this . UNSELECTED_COLOR = 0xFF0000 ;
5- this . meshes = { } ;
3+ this . selectedMaterial = new THREE . MeshPhongMaterial ( { color : 0xffff00 } ) ;
4+ this . unselectedMaterial = new THREE . MeshPhongMaterial ( { color : 0xFF0000 } ) ;
65
76 this . init = function ( container ) {
87
@@ -11,23 +10,22 @@ function ThreeJsViewer(){
1110
1211 this . renderer = new THREE . WebGLRenderer ( ) ;
1312 this . renderer . sortObjects = false ;
13+ this . renderer . setClearColor ( 0xcceeee ) ;
1414 this . renderer . setSize ( this . size . width , this . size . height ) ;
1515
1616 this . scene = new THREE . Scene ( ) ;
1717
1818 this . camera = new THREE . PerspectiveCamera ( 50 , this . size . width / this . size . height , 1 , 100000 ) ; // fov, aspect, near, far
19- this . camera . up = new THREE . Vector3 ( 0 , 0 , 1 ) ;
20- this . camera . position = new THREE . Vector3 ( 1 , 1 , 1 ) ;
19+ this . camera . up . set ( 0 , 0 , 1 ) ;
20+ this . camera . position . set ( 1 , 1 , 1 ) ;
2121
2222 this . controls = new THREE . TrackballControls ( this . camera , this . renderer . domElement ) ;
2323 this . controls . minDistance = 0.1 ;
2424 this . controls . maxDistance = 100000 ;
2525 this . controls . target . position = new THREE . Vector3 ( 0 , 0 , 0 ) ;
2626 this . controls . screen . width = this . size . width ;
2727 this . controls . screen . height = this . size . height ;
28-
29- this . scene . add ( this . camera ) ;
30-
28+
3129 this . root = new THREE . Object3D ( ) ;
3230 this . scene . add ( this . root ) ;
3331
@@ -48,72 +46,40 @@ function ThreeJsViewer(){
4846 container . click ( { viewer : this } , this . onMouseDown ) ;
4947 container . append ( this . renderer . domElement ) ;
5048 this . container = container ;
51- this . onclick = function ( ) { } ;
52- } ;
53-
54- this . registerGeometryFunc = function ( ) {
55- var viewer = this ;
56- return function ( partId ) {
57- var material = new THREE . MeshPhongMaterial ( { color : viewer . UNSELECTED_COLOR } ) ;
58- return function ( geometry ) {
59- var mesh = new THREE . Mesh ( geometry , material ) ;
60- mesh . doubleSided = false ;
61- viewer . root . add ( mesh ) ;
62- viewer . meshes [ mesh . geometry . id ] = partId ;
63- } ;
64- } ;
49+ this . onclick = function ( ) { } ;
6550 } ;
6651
67- this . finishScene = function ( ) {
68- var bb = this . computeBoundingBox ( ) ;
69- var ext = { x : bb . x [ 1 ] - bb . x [ 0 ] , y : bb . y [ 1 ] - bb . y [ 0 ] , z : bb . z [ 1 ] - bb . z [ 0 ] } ;
70- // center mesh
71- this . root . position . x = ext . x * - .5 - bb . x [ 0 ] ;
72- this . root . position . y = ext . y * - .5 - bb . y [ 0 ] ;
73- this . root . position . z = ext . z * - .5 - bb . z [ 0 ] ;
74-
52+ this . centerScene = function ( ) {
53+ var bb = new THREE . Box3 ( )
54+ var material = this . unselectedMaterial ;
55+ $ . each ( this . root . children , function ( i , object ) {
56+ bb . union ( new THREE . Box3 ( ) . setFromObject ( object ) ) ;
57+ object . material = material ;
58+ } ) ;
59+ var ext = { x : bb . max . x - bb . min . x , y : bb . max . y - bb . min . y , z : bb . max . z - bb . min . z } ;
60+ this . root . position . set ( ext . x * - .5 - bb . min . x , ext . y * - .5 - bb . min . y , ext . z * - .5 - bb . min . z ) ;
7561 var maxExtent = Math . max ( ext . x , ext . y , ext . z ) ;
76- this . camera . position = new THREE . Vector3 ( maxExtent , maxExtent , maxExtent ) ;
62+ this . camera . position . set ( maxExtent , maxExtent , maxExtent ) ;
63+ this . camera . lookAt ( new THREE . Vector3 ( 0 , 0 , 0 ) ) ;
7764 // TODO: adjust clipping
7865 } ;
7966
8067 this . loadSerializedModel = function ( serializedModel ) {
81- var geometryLoader = new JSONListLoader ( true ) ;
68+ this . clearModel ( ) ;
8269 var model = JSON . parse ( serializedModel ) ;
83- geometryLoader . onLoadStart ( ) ;
84- geometryLoader . createModelFull ( model , this . registerGeometryFunc ( ) , 'localhost' ) ; // jsonObject, modelPartCallback, texture_path
85- geometryLoader . onLoadComplete ( ) ;
86- this . finishScene ( ) ;
87- } ;
88-
89- this . loadModel = function ( modelUrl ) {
90- var geometryLoader = new JSONListLoader ( true ) ;
91- var texture_path = geometryLoader . extractUrlbase ( modelUrl ) ;
92- geometryLoader . onLoadStart ( ) ;
93- geometryLoader . loadAjaxJSON ( modelUrl , this . registerGeometryFunc ( ) , texture_path ) ;
94- this . finishScene ( ) ;
70+ var loader = new THREE . ObjectLoader ( ) ;
71+ var loadedScene = loader . parse ( model , function ( object ) {
72+ // object.material=material;
73+ } ) ;
74+ this . root . add . apply ( this . root , loadedScene . children ) ;
75+ this . centerScene ( ) ;
76+ this . renderer . render ( this . scene , this . camera ) ;
9577 } ;
9678
9779 this . clearModel = function ( ) {
9880 this . scene . remove ( this . root ) ;
9981 this . root = new THREE . Object3D ( ) ;
10082 this . scene . add ( this . root ) ;
101- this . meshes = { } ;
102- } ;
103-
104- this . computeBoundingBox = function ( ) {
105- this . root . children [ 0 ] . geometry . computeBoundingBox ( ) ;
106- var initialBB = this . root . children [ 0 ] . geometry . boundingBox ;
107- var bb = { x : [ initialBB . x [ 0 ] , initialBB . x [ 1 ] ] , y : [ initialBB . y [ 0 ] , initialBB . y [ 1 ] ] , z : [ initialBB . z [ 0 ] , initialBB . z [ 1 ] ] } ;
108- THREE . SceneUtils . traverseHierarchy ( this . root , function ( object ) {
109- object . geometry . computeBoundingBox ( ) ;
110- $ . each ( [ 'x' , 'y' , 'z' ] , function ( index , dimension ) {
111- bb [ dimension ] [ 0 ] = Math . min ( bb [ dimension ] [ 0 ] , object . geometry . boundingBox [ dimension ] [ 0 ] ) ;
112- bb [ dimension ] [ 1 ] = Math . max ( bb [ dimension ] [ 1 ] , object . geometry . boundingBox [ dimension ] [ 1 ] ) ;
113- } ) ;
114-
115- } ) ;
116- return bb ;
11783 } ;
11884
11985 this . onMouseDown = function ( event ) {
@@ -127,28 +93,27 @@ function ThreeJsViewer(){
12793
12894 viewer . projector . unprojectVector ( mouse , viewer . camera ) ;
12995
130- var ray = new THREE . Ray ( viewer . camera . position , mouse . subSelf ( viewer . camera . position ) . normalize ( ) ) ;
96+ var ray = new THREE . Raycaster ( viewer . camera . position , mouse . sub ( viewer . camera . position ) . normalize ( ) ) ;
13197
132- var intersects = ray . intersectScene ( viewer . scene ) ;
98+ var intersects = ray . intersectObjects ( viewer . root . children ) ;
13399 if ( intersects . length > 0 ) {
134100 if ( viewer . selected != intersects [ 0 ] . object ) {
135- if ( viewer . selected ) viewer . selected . material . color . setHex ( viewer . UNSELECTED_COLOR ) ;
101+ if ( viewer . selected ) viewer . selected . material = viewer . unselectedMaterial ;
136102 viewer . selected = intersects [ 0 ] . object ;
137- viewer . selected . material . color . setHex ( viewer . SELECTED_COLOR ) ;
103+ viewer . selected . material = viewer . selectedMaterial ;
138104 }
139105 } else {
140- if ( viewer . selected ) viewer . selected . material . color . setHex ( viewer . UNSELECTED_COLOR ) ;
106+ if ( viewer . selected ) viewer . selected . material = viewer . unselectedMaterial ;
141107 viewer . selected = null ;
142108 }
143- viewer . onClick ( viewer . selected ? viewer . meshes [ viewer . selected . geometry . id ] : null ) ;
109+ viewer . onClick ( viewer . selected ? viewer . selected . uuid : null ) ;
144110 } ;
145111
146112 this . animate = function ( ) {
147113 this . _animate ( ) ( ) ;
148114 } ;
149115
150116 this . _animate = function ( ) {
151- // Include examples/js/RequestAnimationFrame.js for cross-browser compatibility.
152117 var viewer = this ;
153118 return function ( ) {
154119 requestAnimationFrame ( viewer . _animate ( ) ) ;
@@ -158,7 +123,6 @@ function ThreeJsViewer(){
158123
159124 this . render = function ( ) {
160125 this . controls . update ( ) ;
161- this . renderer . clear ( ) ;
162126 this . renderer . render ( this . scene , this . camera ) ;
163127 } ;
164128}
0 commit comments