11import * as THREE from "three" ;
2- import { Component , Disposable , Hideable , Event } from "../../base-types" ;
3- import { Disposer } from "../Disposer" ;
4- import { Components } from "../Components" ;
5- import { SimpleCamera } from "../SimpleCamera" ;
6- import { ToolComponent } from "../ToolsComponent" ;
2+ import { Component , Hideable , Event , World } from "../../Types" ;
3+ import { Components } from "../../Components" ;
4+ import { Disposer } from "../../Disposer" ;
5+ import { SimpleCamera } from "../../Worlds" ;
6+
7+ export interface GridConfig {
8+ color : THREE . Color ;
9+ size1 : number ;
10+ size2 : number ;
11+ distance : number ;
12+ }
713
814/**
915 * An infinite grid. Created by
1016 * [fyrestar](https://github.com/Fyrestar/THREE.InfiniteGridHelper)
1117 * and translated to typescript by
1218 * [dkaraush](https://github.com/dkaraush/THREE.InfiniteGridHelper/blob/master/InfiniteGridHelper.ts).
1319 */
14- export class SimpleGrid
15- extends Component < THREE . Mesh >
16- implements Hideable , Disposable
17- {
18- static readonly uuid = "d1e814d5-b81c-4452-87a2-f039375e0489" as const ;
19-
20+ export class SimpleGrid implements Hideable , Disposable {
2021 /** {@link Disposable.onDisposed } */
21- readonly onDisposed = new Event < string > ( ) ;
22+ readonly onDisposed = new Event ( ) ;
23+
24+ world : World ;
2225
23- /** {@link Component.enabled } */
24- enabled = true ;
26+ components : Components ;
2527
2628 /** {@link Hideable.visible } */
2729 get visible ( ) {
@@ -31,7 +33,7 @@ export class SimpleGrid
3133 /** {@link Hideable.visible } */
3234 set visible ( visible : boolean ) {
3335 if ( visible ) {
34- const scene = this . components . scene . get ( ) ;
36+ const scene = this . world . scene . three ;
3537 scene . add ( this . _grid ) ;
3638 } else {
3739 this . _grid . removeFromParent ( ) ;
@@ -63,19 +65,16 @@ export class SimpleGrid
6365 private readonly _grid : THREE . Mesh ;
6466 private _fade = 3 ;
6567
66- constructor (
67- components : Components ,
68- color = new THREE . Color ( 0xbbbbbb ) ,
69- size1 : number = 1 ,
70- size2 : number = 10 ,
71- distance : number = 500
72- ) {
73- super ( components ) ;
74- this . components . tools . add ( SimpleGrid . uuid , this ) ;
75-
68+ constructor ( components : Components , world : World , config : GridConfig ) {
7669 // Source: https://github.com/dkaraush/THREE.InfiniteGridHelper/blob/master/InfiniteGridHelper.ts
7770 // Author: Fyrestar https://mevedia.com (https://github.com/Fyrestar/THREE.InfiniteGridHelper)
7871
72+ this . world = world ;
73+
74+ const { color, size1, size2, distance } = config ;
75+
76+ this . components = components ;
77+
7978 const geometry = new THREE . PlaneGeometry ( 2 , 2 , 1 , 1 ) ;
8079
8180 const material = new THREE . ShaderMaterial ( {
@@ -174,29 +173,36 @@ export class SimpleGrid
174173
175174 this . _grid = new THREE . Mesh ( geometry , material ) ;
176175 this . _grid . frustumCulled = false ;
177- const scene = components . scene . get ( ) ;
178- scene . add ( this . _grid ) ;
176+ world . scene . three . add ( this . _grid ) ;
179177
180178 this . setupEvents ( true ) ;
181179 }
182180
181+ [ Symbol . dispose ] ( ) : void {
182+ throw new Error ( "Method not implemented." ) ;
183+ }
184+
183185 /** {@link Component.get } */
184186 get ( ) {
185187 return this . _grid ;
186188 }
187189
188190 /** {@link Disposable.dispose } */
189- async dispose ( ) {
191+ dispose ( ) {
190192 this . setupEvents ( false ) ;
191- const disposer = this . components . tools . get ( Disposer ) ;
193+ const disposer = this . components . get ( Disposer ) ;
192194 disposer . destroy ( this . _grid ) ;
193- await this . onDisposed . trigger ( SimpleGrid . uuid ) ;
195+ this . onDisposed . trigger ( ) ;
194196 this . onDisposed . reset ( ) ;
197+ this . world = null as any ;
198+ this . components = null as any ;
195199 }
196200
197201 private setupEvents ( active : boolean ) {
198- const camera = this . components . camera as SimpleCamera ;
199- const controls = camera . controls ;
202+ if ( ! ( this . world . camera instanceof SimpleCamera ) ) {
203+ return ;
204+ }
205+ const controls = this . world . camera . controls ;
200206 if ( active ) {
201207 controls . addEventListener ( "update" , this . updateZoom ) ;
202208 } else {
@@ -205,9 +211,9 @@ export class SimpleGrid
205211 }
206212
207213 private updateZoom = ( ) => {
208- const camera = this . components . camera as SimpleCamera ;
209- this . material . uniforms . uZoom . value = camera . get ( ) . zoom ;
214+ if ( ! ( this . world . camera instanceof SimpleCamera ) ) {
215+ return ;
216+ }
217+ this . material . uniforms . uZoom . value = this . world . camera . three . zoom ;
210218 } ;
211219}
212-
213- ToolComponent . libraryUUIDs . add ( SimpleGrid . uuid ) ;
0 commit comments