@@ -31,13 +31,15 @@ const s = new Vector3(1, 1, 1)
3131const q = new Quaternion ( )
3232const m = new Matrix4 ( )
3333
34- function apply ( index : number , positions : Float32Array , quaternions : Float32Array , object ?: Object3D ) {
34+ function apply (
35+ index : number ,
36+ positions : Float32Array ,
37+ quaternions : Float32Array ,
38+ scale = s ,
39+ object ?: Object3D ,
40+ ) {
3541 if ( index !== undefined ) {
36- m . compose (
37- v . fromArray ( positions , index * 3 ) ,
38- q . fromArray ( quaternions , index * 4 ) ,
39- object ? object . scale : s ,
40- )
42+ m . compose ( v . fromArray ( positions , index * 3 ) , q . fromArray ( quaternions , index * 4 ) , scale )
4143 if ( object ) {
4244 object . matrixAutoUpdate = false
4345 object . matrix . copy ( m )
@@ -67,10 +69,11 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
6769} ) => {
6870 const { invalidate } = useThree ( )
6971
70- const [ { bodies, events, refs, subscriptions, worker } ] = useState < PhysicsContext > ( ( ) => ( {
72+ const [ { bodies, events, refs, scaleOverrides , subscriptions, worker } ] = useState < PhysicsContext > ( ( ) => ( {
7173 bodies : { } ,
7274 events : { } ,
7375 refs : { } ,
76+ scaleOverrides : { } ,
7477 subscriptions : { } ,
7578 worker : new CannonWorkerAPI ( {
7679 allowSleep,
@@ -178,14 +181,16 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
178181 for ( const ref of Object . values ( refs ) ) {
179182 if ( ref instanceof InstancedMesh ) {
180183 for ( let i = 0 ; i < ref . count ; i ++ ) {
181- const index = bodies [ `${ ref . uuid } /${ i } ` ]
184+ const uuid = `${ ref . uuid } /${ i } `
185+ const index = bodies [ uuid ]
182186 if ( index !== undefined ) {
183- ref . setMatrixAt ( i , apply ( index , positions , quaternions ) )
187+ ref . setMatrixAt ( i , apply ( index , positions , quaternions , scaleOverrides [ uuid ] ) )
188+ ref . instanceMatrix . needsUpdate = true
184189 }
185- ref . instanceMatrix . needsUpdate = true
186190 }
187191 } else {
188- apply ( bodies [ ref . uuid ] , positions , quaternions , ref )
192+ const scale = scaleOverrides [ ref . uuid ] || ref . scale
193+ apply ( bodies [ ref . uuid ] , positions , quaternions , scale , ref )
189194 }
190195 }
191196 if ( shouldInvalidate ) {
@@ -241,7 +246,7 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
241246 } , [ tolerance ] )
242247
243248 const value = useMemo < PhysicsContext > (
244- ( ) => ( { bodies, events, refs, subscriptions, worker } ) ,
249+ ( ) => ( { bodies, events, refs, scaleOverrides , subscriptions, worker } ) ,
245250 [ bodies , events , refs , subscriptions , worker ] ,
246251 )
247252
0 commit comments