@@ -8,21 +8,21 @@ import {
88 WebGLRenderer ,
99 PerspectiveCamera ,
1010 MeshPhongMaterial ,
11- Box3Helper ,
1211 Box3 ,
1312 Sphere ,
14- SphereGeometry ,
13+ AxesHelper ,
1514 Mesh ,
15+ SphereGeometry ,
16+ BoxGeometry ,
17+ LineSegments ,
18+ EdgesGeometry ,
1619} from 'three' ;
1720import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' ;
1821import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js' ;
1922
2023let camera , controls , scene , renderer , group ;
2124let dirLight ;
22- let sphereHelper , boxHelper ;
23- let helper , ghostHelper , edges , boxGroup ;
24- const pointsArray = [ ] ;
25- let pointsGroup ;
25+ let helper , ghostHelper , edges , boxGroup , sphereGroup ;
2626
2727const params = {
2828
@@ -57,9 +57,6 @@ function init() {
5757 group . rotation . x = - Math . PI / 2 ;
5858 scene . add ( group ) ;
5959
60- pointsGroup = new Group ( ) ;
61- group . add ( pointsGroup ) ;
62-
6360 // add ellipsoid helper
6461 helper = new EllipsoidRegionHelper ( ) ;
6562 helper . material = new MeshPhongMaterial ( {
@@ -89,24 +86,41 @@ function init() {
8986 edges . material . color . set ( 0x151c1f ) . convertSRGBToLinear ( ) ;
9087
9188 // add sphere helper
92- sphereHelper = new SphereHelper ( new Sphere ( ) ) ;
89+ const sphereHelper = new SphereHelper ( new Sphere ( ) ) ;
90+ sphereHelper . sphere . center . set ( 0 , 0 , 0 ) ;
91+ sphereHelper . sphere . radius = 1 ;
92+
93+ const sphereMesh = new Mesh ( new SphereGeometry ( ) , new MeshPhongMaterial ( {
94+ transparent : true ,
95+ depthWrite : false ,
96+ opacity : 0.35 ,
97+ color : 0xffff00
98+ } ) ) ;
99+
100+ sphereGroup = new Group ( ) ;
101+ sphereGroup . add ( sphereMesh , sphereHelper ) ;
102+
93103
94104 // add box helper
95- boxGroup = new Group ( ) ;
105+ const boxHelper = new LineSegments ( new EdgesGeometry ( new BoxGeometry ( ) ) ) ;
106+ boxHelper . material . color . set ( 0xffff00 ) ;
107+ // const boxHelper = new Box3Helper( new Box3() );
96108
97- boxHelper = new Box3Helper ( new Box3 ( ) ) ;
98- boxGroup . add ( boxHelper ) ;
99109
100- group . add ( helper , ghostHelper , edges , sphereHelper , boxGroup ) ;
110+ const boxMesh = new Mesh ( new BoxGeometry ( ) , sphereMesh . material ) ;
111+ boxGroup = new Group ( ) ;
112+ boxGroup . add ( boxHelper , boxMesh , new AxesHelper ( ) ) ;
101113
102- updateHelper ( ) ;
114+ group . add ( helper , ghostHelper , edges , boxGroup , sphereGroup ) ;
103115
104116 // controls
105117 controls = new OrbitControls ( camera , renderer . domElement ) ;
106118 controls . screenSpacePanning = false ;
107- controls . minDistance = 1 ;
119+ controls . minDistance = 0 ;
108120 controls . maxDistance = 2000 ;
109121
122+ updateHelper ( ) ;
123+
110124 // lights
111125 dirLight = new DirectionalLight ( 0xffffff , 1.25 ) ;
112126 dirLight . position . set ( 1 , 2 , 3 ) . multiplyScalar ( 40 ) ;
@@ -118,7 +132,6 @@ function init() {
118132 const gui = new GUI ( ) ;
119133 gui . add ( params , 'displayBoxHelper' ) ;
120134 gui . add ( params , 'displaySphereHelper' ) ;
121- gui . add ( params , 'displayPoints' ) ;
122135
123136 const radiusFolder = gui . addFolder ( 'radius' ) ;
124137 radiusFolder . add ( helper . ellipsoidRegion . radius , 'x' , 0.1 , 2 ) . onChange ( updateHelper ) ;
@@ -149,30 +162,25 @@ function updateHelper() {
149162 edges . update ( ) ;
150163
151164 // update the bounds helpers
152- helper . ellipsoidRegion . getBoundingSphere ( sphereHelper . sphere ) ;
153- helper . ellipsoidRegion . getBoundingBox ( boxHelper . box , boxGroup . matrix ) ;
165+ const sphere = new Sphere ( ) ;
166+ helper . ellipsoidRegion . getBoundingSphere ( sphere ) ;
167+ sphereGroup . position . copy ( sphere . center ) ;
168+ sphereGroup . scale . setScalar ( sphere . radius ) ;
169+
170+ const box = new Box3 ( ) ;
171+ helper . ellipsoidRegion . getBoundingBox ( box , boxGroup . matrix ) ;
154172 boxGroup . matrix . decompose (
155173 boxGroup . position ,
156174 boxGroup . quaternion ,
157175 boxGroup . scale ,
158176 ) ;
177+ box . getSize ( boxGroup . scale ) ;
178+ box . getCenter ( boxGroup . position ) . applyMatrix4 ( boxGroup . matrix ) ;
179+ scene . updateMatrixWorld ( true ) ;
159180
160- const points = helper . ellipsoidRegion . _getPoints ( ) ;
161- pointsArray . forEach ( o => o . visible = false ) ;
162- for ( let i = 0 ; i < points . length ; i ++ ) {
163-
164- if ( ! pointsArray [ i ] ) {
165-
166- pointsArray . push ( new Mesh ( new SphereGeometry ( 0.01 ) ) ) ;
167- pointsArray [ i ] . material . color . set ( 0xff0000 ) ;
168- pointsGroup . add ( pointsArray [ i ] ) ;
181+ controls . target . set ( 0 , 0 , 0 ) . applyMatrix4 ( boxGroup . matrixWorld ) ;
182+ camera . position . add ( controls . target ) ;
169183
170- }
171-
172- pointsArray [ i ] . position . copy ( points [ i ] ) ;
173- pointsArray [ i ] . visible = true ;
174-
175- }
176184
177185}
178186
@@ -195,9 +203,10 @@ function animate() {
195203
196204function render ( ) {
197205
198- sphereHelper . visible = params . displaySphereHelper ;
199- boxHelper . visible = params . displayBoxHelper ;
200- pointsGroup . visible = params . displayPoints ;
206+ sphereGroup . visible = params . displaySphereHelper ;
207+
208+ boxGroup . visible = params . displayBoxHelper ;
209+
201210 renderer . render ( scene , camera ) ;
202211
203212}
0 commit comments