@@ -28,7 +28,9 @@ import {
2828import { VoxelMeshBuilder } from "./mesh/VoxelMeshBuilder.ts" ;
2929import {
3030 VoxelSerializer ,
31- type VoxelWorldJSON
31+ type VoxelWorldJSON ,
32+ type VoxelObjectLayerJSON ,
33+ type VoxelObjectJSON
3234} from "./serialization/VoxelSerializer.ts" ;
3335import {
3436 TilesetManager ,
@@ -477,6 +479,112 @@ export class VoxelRenderer extends ActorComponent {
477479 return layer . centerToWorld ( ) ;
478480 }
479481
482+ // --- Object Layer API --- //
483+
484+ addObjectLayer (
485+ name : string ,
486+ options ?: Partial < Pick < VoxelObjectLayerJSON , "visible" | "order" > >
487+ ) : VoxelObjectLayerJSON {
488+ const layer = this . world . addObjectLayer ( name , options ) ;
489+ this . #onLayerUpdated?.( {
490+ action : "object-layer-added" ,
491+ layerName : name ,
492+ metadata : { }
493+ } ) ;
494+
495+ return layer ;
496+ }
497+
498+ removeObjectLayer (
499+ name : string
500+ ) : boolean {
501+ const result = this . world . removeObjectLayer ( name ) ;
502+ if ( result ) {
503+ this . #onLayerUpdated?.( {
504+ action : "object-layer-removed" ,
505+ layerName : name ,
506+ metadata : { }
507+ } ) ;
508+ }
509+
510+ return result ;
511+ }
512+
513+ getObjectLayer (
514+ name : string
515+ ) : VoxelObjectLayerJSON | undefined {
516+ return this . world . getObjectLayer ( name ) ;
517+ }
518+
519+ getObjectLayers ( ) : readonly VoxelObjectLayerJSON [ ] {
520+ return this . world . getObjectLayers ( ) ;
521+ }
522+
523+ updateObjectLayer (
524+ name : string ,
525+ patch : Partial < Pick < VoxelObjectLayerJSON , "visible" > >
526+ ) : boolean {
527+ const result = this . world . updateObjectLayer ( name , patch ) ;
528+ if ( result ) {
529+ this . #onLayerUpdated?.( {
530+ action : "object-layer-updated" ,
531+ layerName : name ,
532+ metadata : { patch }
533+ } ) ;
534+ }
535+
536+ return result ;
537+ }
538+
539+ addObject (
540+ layerName : string ,
541+ object : VoxelObjectJSON
542+ ) : boolean {
543+ const result = this . world . addObjectToLayer ( layerName , object ) ;
544+ if ( result ) {
545+ this . #onLayerUpdated?.( {
546+ action : "object-added" ,
547+ layerName,
548+ metadata : { objectId : object . id }
549+ } ) ;
550+ }
551+
552+ return result ;
553+ }
554+
555+ removeObject (
556+ layerName : string ,
557+ objectId : string
558+ ) : boolean {
559+ const result = this . world . removeObjectFromLayer ( layerName , objectId ) ;
560+ if ( result ) {
561+ this . #onLayerUpdated?.( {
562+ action : "object-removed" ,
563+ layerName,
564+ metadata : { objectId }
565+ } ) ;
566+ }
567+
568+ return result ;
569+ }
570+
571+ updateObject (
572+ layerName : string ,
573+ objectId : string ,
574+ patch : Partial < VoxelObjectJSON >
575+ ) : boolean {
576+ const result = this . world . updateObjectInLayer ( layerName , objectId , patch ) ;
577+ if ( result ) {
578+ this . #onLayerUpdated?.( {
579+ action : "object-updated" ,
580+ layerName,
581+ metadata : { objectId, patch }
582+ } ) ;
583+ }
584+
585+ return result ;
586+ }
587+
480588 async loadTileset (
481589 def : TilesetDefinition
482590 ) : Promise < void > {
0 commit comments