
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
use-ammojs
Advanced tools
Fast physics hooks for use in react-three-fiber. Powered by web-workers and wasm.
Fast Physics hooks for use with react-three-fiber.
Achieved by running the ammo.js physics library in a web-worker. Ammo itself is a WebAssembly wrapper around the powerful Bullet Physics engine. Data is synced with SharedArrayBuffers having minimal impact on the main thread.
yarn add use-ammojs
npm i use-ammojs
Built on top of three-ammo and its related work.
⚠️ Note that the codesandbox examples do not support SharedArrayBuffers due to missing cross-origin isolation and use regular ArrayBuffers as a fallback. Currently the debug-drawer has no ArrayBuffer fallback implemented and will not render anything.
use-cannon is great and a inspiration for this package, but it is missing features like soft-bodies and lacks performance in scenes with large triangle meshes. ammo.js is a direct wrapper around the powerful Bullet Physics engine, which solves these problems.
At the time of writing however use-cannon is more mature and great for most projects.
application/wasm Content-Type in their own deployment. There should be a bundle available without the inlined wasm for that use-case.import { Physics } from "use-ammojs";
<Physics drawDebug>[...]</Physics>;
Automatically parse Shape parameters from the three Mesh (courtesy of three-to-ammo):
import { Box } from "@react-three/drei";
import { useRigidBody, ShapeType } from "use-ammojs";
function MyBox() {
const [ref] = useRigidBody(() => ({
mass: 1,
position: [0, 2, 4],
shapeType: ShapeType.BOX,
}));
return (
<Box ref={ref}>
<meshBasicMaterial attach="material" color="red" />
</Box>
);
}
or define Collision Shapes manually:
const [playerCapsuleRef] = useRigidBody(() => ({
bodyType: BodyType.DYNAMIC,
shapeType: ShapeType.CAPSULE,
angularFactor: new Vector3(0, 0, 0),
shapeConfig: {
fit: ShapeFit.MANUAL,
halfExtents: new Vector3(0.3, 0.6, 0.3),
},
}));
or add collisions to an imported gltf scene:
useRigidBody(
() => ({
shapeType: ShapeType.MESH,
bodyType: BodyType.STATIC,
}),
gltf.scene
);
const [ref] = useSoftBody(() => ({
type: SoftBodyType.TRIMESH,
}));
return (
<Sphere position={[0, 2, 7]} args={[1, 16, 16]} ref={ref}>
<meshPhysicalMaterial attach="material" color="blue" />
</Sphere>
);
TODO;
const { rayTest } = useAmmo();
[...]
const hits = await rayTest({
from: new Vector3(0, 5, 7),
to: new Vector3(0, -1, 7),
multiple: true
})
if (hits.length) {
console.log(hits[0].object.name, hits[0].hitPosition)
}
const [playerRef, api] = useRigidBody(() => ({
bodyType: BodyType.DYNAMIC,
shapeType: ShapeType.CAPSULE,
angularFactor: new Vector3(0, 0, 0),
shapeConfig: {
fit: ShapeFit.MANUAL,
halfExtents: new Vector3(0.3, 0.6, 0.3),
},
}));
function handleRespawn() {
api.setPosition(new Vector3(0, 0, 0));
api.setLinearVelocity(new Vector3(0, 0, 0));
}
<Physics />
Phyiscs Context. Use to wrap all physical objects within the same physics world.
<PhysicsStats />
Shows a stats.js panel with physics timing info. Use within a <Physics /> Context
const { rayTest } = useAmmo();
Utility funcionts available anywhere in the <Physics /> context.
const [ref, api] = useRigidBody();
const [ref, api] = useSoftBody();
To use SharedArrayBuffers for better communication between the main-thread and the web-worker-thread, a cross-origin isolated environment is necessary in modern browsers.
This requires sending the following HTTP headers in the response of the main html document (Learn more):
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
use-ammojs will fallback to using ArrayBuffers and postMessage() transfers if SharedArrayBuffers are not available. This is not as bad as a full copy on each transfer, but it does not allow the data to be availble on both threads at the same time.
yarn add @craco/craco --devreact-scripts with craco in your package.json (see @craco/craco documentation)craco.config.js to project root:const path = require("path");
module.exports = {
webpack: {
configure: (webpackConfig) => {
// Fix that prevents a duplicate react library being imported when using a linked yarn package
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
react: path.resolve("./node_modules/react"),
"@react-three/fiber": path.resolve("./node_modules/@react-three/fiber"),
three: path.resolve("./node_modules/three"),
};
return webpackConfig;
},
},
// Make sure SharedArrayBuffers are available locally
devServer: {
headers: {
"Cross-Origin-Embedder-Policy": "require-corp",
"Cross-Origin-Opener-Policy": "same-origin",
},
},
};
yarn link in use-ammojs root directoryyarn link use-ammojs in your project's directoryyarn start in use-ammojs to start the development bundlerFAQs
Fast physics hooks for use in react-three-fiber. Powered by web-workers and wasm.
We found that use-ammojs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.