@@ -69,6 +69,21 @@ export class SimplePlane implements Disposable, Hideable {
6969 visible : false ,
7070 } ) ;
7171
72+ private _sizeMultiplier = 5 ;
73+
74+ private _autoScale = true ;
75+
76+ get autoScale ( ) {
77+ return this . _autoScale ;
78+ }
79+
80+ set autoScale ( value : boolean ) {
81+ this . _autoScale = value ;
82+ if ( value ) {
83+ this . updateScale ( ) ;
84+ }
85+ }
86+
7287 /**
7388 * Getter for the enabled state of the clipping plane.
7489 * @returns {boolean } The current enabled state.
@@ -136,12 +151,17 @@ export class SimplePlane implements Disposable, Hideable {
136151
137152 /** The size of the clipping plane representation. */
138153 get size ( ) {
139- return this . _planeMesh . scale . x ;
154+ return this . _sizeMultiplier ;
140155 }
141156
142157 /** Sets the size of the clipping plane representation. */
143158 set size ( size : number ) {
144- this . _planeMesh . scale . set ( size , size , size ) ;
159+ this . _sizeMultiplier = size ;
160+ if ( this . autoScale ) {
161+ this . updateScale ( ) ;
162+ } else {
163+ this . _planeMesh . scale . set ( size , size , size ) ;
164+ }
145165 }
146166
147167 /**
@@ -183,6 +203,7 @@ export class SimplePlane implements Disposable, Hideable {
183203
184204 this . normal = normal ;
185205 this . origin = origin ;
206+ this . _sizeMultiplier = size ;
186207
187208 world . renderer . setPlane ( true , this . three ) ;
188209
@@ -194,6 +215,11 @@ export class SimplePlane implements Disposable, Hideable {
194215 if ( activateControls ) {
195216 this . toggleControls ( true ) ;
196217 }
218+
219+ this . updateScale ( ) ;
220+ if ( world . camera ?. controls ) {
221+ world . camera . controls . addEventListener ( "update" , this . updateScale ) ;
222+ }
197223 }
198224
199225 private notifyManager = ( ) => {
@@ -231,11 +257,32 @@ export class SimplePlane implements Disposable, Hideable {
231257 this . normal ,
232258 this . _helper . position ,
233259 ) ;
260+ this . updateScale ( ) ;
261+ } ;
262+
263+ private updateScale = ( ) => {
264+ if ( ! this . autoScale ) return ;
265+ const camera = this . world . camera ?. three ;
266+ if ( ! camera ) return ;
267+ if ( ! ( camera instanceof THREE . PerspectiveCamera ) ) return ;
268+ const cameraPosition = new THREE . Vector3 ( ) ;
269+ camera . getWorldPosition ( cameraPosition ) ;
270+ const distance = cameraPosition . distanceTo ( this . _helper . position ) ;
271+ const scale = ( distance / 7 ) * this . _sizeMultiplier ;
272+ this . _planeMesh . scale . set ( scale , scale , scale ) ;
234273 } ;
235274
236275 /** {@link Disposable.dispose } */
237276 dispose ( ) {
238277 this . _enabled = false ;
278+
279+ if ( this . world . camera ?. controls ) {
280+ this . world . camera . controls . removeEventListener (
281+ "update" ,
282+ this . updateScale ,
283+ ) ;
284+ }
285+
239286 this . onDraggingStarted . reset ( ) ;
240287 this . onDraggingEnded . reset ( ) ;
241288 this . _helper . removeFromParent ( ) ;
0 commit comments