@@ -6,8 +6,9 @@ import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
66import { MeshoptDecoder } from "three/addons/libs/meshopt_decoder.module.js" ;
77import { AgentWeaponAnimation } from "./agent-weapon-animation" ;
88import { createNativeWorldGun , loadNativeWeapons , nativeWorldRig } from "./weapon-motion" ;
9+ import { disposeWorldGun } from "./weapons" ;
910
10- vi . mock ( "./skins" , ( ) => ( { registerSkinModel ( ) { } } ) ) ;
11+ vi . mock ( "./skins" , ( ) => ( { registerSkinModel ( ) { } , unregisterSkinModel ( ) { } } ) ) ;
1112vi . mock ( "./knife-motion" , ( ) => ( { viewHandsTemplate : ( ) => new T . Group ( ) } ) ) ;
1213vi . mock ( "./gltf" , ( ) => ( { loadModel : ( path : string ) => parse ( path ) } ) ) ;
1314
@@ -122,6 +123,64 @@ test("native secondary reload moves the actual magazine and scrubs back without
122123 secondary . dispose ( ) ;
123124} ) ;
124125
126+ test ( "carried weapons share vertices without sharing poses or disposing another actor's resources" , async ( ) => {
127+ const pack = await parse ( "/models/agents/inspects/animations/world/rifle/rifle_ak.glb" ) ;
128+ const clip = pack . animations . find ( ( clip ) => clip . name === "reload_ak@weapon" ) ! ;
129+ const make = ( ) => {
130+ const weapon = createNativeWorldGun ( 0 , true , undefined , false , true ) ;
131+ const clock = new T . AnimationMixer ( new T . Group ( ) )
132+ . clipAction ( new T . AnimationClip ( "body" , clip . duration , [ ] ) )
133+ . play ( ) ;
134+ const secondary = new AgentWeaponAnimation ( weapon , pack . scene ) ;
135+ secondary . bind ( clock , clip ) ;
136+ const meshes : T . SkinnedMesh [ ] = [ ] ;
137+ weapon . traverse ( ( node ) => {
138+ if ( node instanceof T . SkinnedMesh ) meshes . push ( node ) ;
139+ } ) ;
140+ return { weapon, clock, secondary, meshes } ;
141+ } ;
142+ const a = make ( ) ,
143+ b = make ( ) ;
144+ expect ( a . meshes . length ) . toBeGreaterThan ( 0 ) ;
145+ let disposedGeometry = 0 ,
146+ disposedMaterial = 0 ;
147+ a . meshes . forEach ( ( mesh , i ) => {
148+ expect ( mesh . geometry ) . toBe ( b . meshes [ i ] . geometry ) ;
149+ expect ( mesh . skeleton ) . not . toBe ( b . meshes [ i ] . skeleton ) ;
150+ expect ( mesh . material ) . not . toBe ( b . meshes [ i ] . material ) ;
151+ mesh . geometry . addEventListener ( "dispose" , ( ) => disposedGeometry ++ ) ;
152+ for ( const material of Array . isArray ( mesh . material ) ? mesh . material : [ mesh . material ] )
153+ material . addEventListener ( "dispose" , ( ) => disposedMaterial ++ ) ;
154+ } ) ;
155+ b . clock . time = 1.2 ;
156+ b . secondary . update ( ) ;
157+ const vertices = ( ) => {
158+ const values : number [ ] = [ ] ,
159+ point = new T . Vector3 ( ) ;
160+ for ( const mesh of b . meshes )
161+ for ( let i = 0 ; i < mesh . geometry . getAttribute ( "position" ) . count ; i ++ )
162+ values . push ( ...mesh . getVertexPosition ( i , point ) . toArray ( ) ) ;
163+ return values ;
164+ } ;
165+ const before = vertices ( ) ;
166+ a . clock . time = 0.7666561604 ;
167+ a . secondary . update ( ) ;
168+ expect ( vertices ( ) ) . toEqual ( before ) ;
169+ a . secondary . dispose ( ) ;
170+ disposeWorldGun ( a . weapon ) ;
171+ expect ( disposedGeometry ) . toBe ( 0 ) ;
172+ expect ( disposedMaterial ) . toBeGreaterThan ( 0 ) ;
173+ b . secondary . update ( ) ;
174+ expect ( vertices ( ) ) . toEqual ( before ) ;
175+ const next = make ( ) ;
176+ next . meshes . forEach ( ( mesh , i ) => expect ( mesh . geometry ) . toBe ( b . meshes [ i ] . geometry ) ) ;
177+ for ( const rig of [ b , next ] ) {
178+ rig . secondary . dispose ( ) ;
179+ disposeWorldGun ( rig . weapon ) ;
180+ }
181+ expect ( disposedGeometry ) . toBe ( 0 ) ;
182+ } ) ;
183+
125184test ( "late animation and skin arrivals use bind scale rather than the currently hidden magazine" , async ( ) => {
126185 const pack = await parse ( "/models/agents/inspects/animations/world/rifle/rifle_ak.glb" ) ;
127186 const clip = pack . animations . find ( ( clip ) => clip . name === "reload_ak@weapon" ) ! ;
0 commit comments