Skip to content

Commit

Permalink
Add scaleOverride property (Fixes #367)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Apr 7, 2022
1 parent 7f5c5ef commit 9e74ef6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
15 changes: 10 additions & 5 deletions packages/react-three-cannon/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type WorkerApi = {
applyTorque: (torque: Triplet) => void
quaternion: QuaternionApi
rotation: VectorApi
scaleOverride: (scale: Triplet) => void
sleep: () => void
wakeUp: () => void
}
Expand Down Expand Up @@ -130,10 +131,10 @@ function subscribe<T extends SubscriptionName>(
}
}

function prepare(object: Object3D, props: BodyProps) {
object.userData = props.userData || {}
object.position.set(...(props.position || [0, 0, 0]))
object.rotation.set(...(props.rotation || [0, 0, 0]))
function prepare(object: Object3D, { position = [0, 0, 0], rotation = [0, 0, 0], userData = {} }: BodyProps) {
object.userData = userData
object.position.set(...position)
object.rotation.set(...rotation)
object.updateMatrix()
}

Expand Down Expand Up @@ -161,7 +162,7 @@ function useBody<B extends BodyProps<unknown[]>>(
): Api {
const ref = useForwardedRef(fwdRef)

const { events, refs, subscriptions, worker } = usePhysicsContext()
const { events, refs, scaleOverrides, subscriptions, worker } = usePhysicsContext()
const debugApi = useDebugContext()

useLayoutEffect(() => {
Expand Down Expand Up @@ -332,6 +333,10 @@ function useBody<B extends BodyProps<unknown[]>>(
position: makeVec('position', index),
quaternion: makeQuaternion(index),
rotation: makeRotation(index),
scaleOverride(scale) {
const uuid = getUUID(ref, index)
if (uuid) scaleOverrides[uuid] = new Vector3(...scale)
},
sleep() {
const uuid = getUUID(ref, index)
uuid && worker.sleep({ uuid })
Expand Down
4 changes: 4 additions & 0 deletions packages/react-three-cannon/src/physics-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Subscriptions,
} from '@pmndrs/cannon-worker-api'
import { createContext, useContext } from 'react'
import type { Vector3 } from 'three'

type CannonEvent = CollideBeginEvent | CollideEndEvent | CollideEvent | RayhitEvent
type CallbackByType<T extends { type: string }> = {
Expand All @@ -16,10 +17,13 @@ type CallbackByType<T extends { type: string }> = {

export type CannonEvents = { [uuid: string]: Partial<CallbackByType<CannonEvent>> }

export type ScaleOverrides = { [uuid: string]: Vector3 }

export type PhysicsContext = {
bodies: { [uuid: string]: number }
events: CannonEvents
refs: Refs
scaleOverrides: ScaleOverrides
subscriptions: Subscriptions
worker: CannonWorkerAPI
}
Expand Down
29 changes: 17 additions & 12 deletions packages/react-three-cannon/src/physics-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ const s = new Vector3(1, 1, 1)
const q = new Quaternion()
const m = new Matrix4()

function apply(index: number, positions: Float32Array, quaternions: Float32Array, object?: Object3D) {
function apply(
index: number,
positions: Float32Array,
quaternions: Float32Array,
scale = s,
object?: Object3D,
) {
if (index !== undefined) {
m.compose(
v.fromArray(positions, index * 3),
q.fromArray(quaternions, index * 4),
object ? object.scale : s,
)
m.compose(v.fromArray(positions, index * 3), q.fromArray(quaternions, index * 4), scale)
if (object) {
object.matrixAutoUpdate = false
object.matrix.copy(m)
Expand Down Expand Up @@ -67,10 +69,11 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
}) => {
const { invalidate } = useThree()

const [{ bodies, events, refs, subscriptions, worker }] = useState<PhysicsContext>(() => ({
const [{ bodies, events, refs, scaleOverrides, subscriptions, worker }] = useState<PhysicsContext>(() => ({
bodies: {},
events: {},
refs: {},
scaleOverrides: {},
subscriptions: {},
worker: new CannonWorkerAPI({
allowSleep,
Expand Down Expand Up @@ -178,14 +181,16 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
for (const ref of Object.values(refs)) {
if (ref instanceof InstancedMesh) {
for (let i = 0; i < ref.count; i++) {
const index = bodies[`${ref.uuid}/${i}`]
const uuid = `${ref.uuid}/${i}`
const index = bodies[uuid]
if (index !== undefined) {
ref.setMatrixAt(i, apply(index, positions, quaternions))
ref.setMatrixAt(i, apply(index, positions, quaternions, scaleOverrides[uuid]))
ref.instanceMatrix.needsUpdate = true
}
ref.instanceMatrix.needsUpdate = true
}
} else {
apply(bodies[ref.uuid], positions, quaternions, ref)
const scale = scaleOverrides[ref.uuid] || ref.scale
apply(bodies[ref.uuid], positions, quaternions, scale, ref)
}
}
if (shouldInvalidate) {
Expand Down Expand Up @@ -241,7 +246,7 @@ export const PhysicsProvider: FC<PhysicsProviderProps> = ({
}, [tolerance])

const value = useMemo<PhysicsContext>(
() => ({ bodies, events, refs, subscriptions, worker }),
() => ({ bodies, events, refs, scaleOverrides, subscriptions, worker }),
[bodies, events, refs, subscriptions, worker],
)

Expand Down

1 comment on commit 9e74ef6

@vercel
Copy link

@vercel vercel bot commented on 9e74ef6 Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

use-cannon – ./

cannon.pmnd.rs
use-cannon.vercel.app
use-cannon-git-master-pmndrs.vercel.app
use-cannon-pmndrs.vercel.app

Please sign in to comment.