draft-n-draw
v0.3.1
Published
### 🖍️ A utility for drawing debug visualizations in threejs.
Downloads
30
Readme
Draft-n-Draw
🖍️ A utility for drawing debug visualizations in threejs.
Easily visualize bounding boxes, raycasts, points, lights and more in threejs. Fully typed, it can be used with vanilla JS or TypeScript and also has convenient React components for use in react-three-fiber.
npm install draft-n-draw
Getting started
React
You begin by adding the DrafterProvider
component to the top level of your app.
import { Canvas } from '@react-three/fiber';
import { DrafterProvider } from '@draft-n-draw/react';
function App() {
return (
<Canvas>
<DrafterProvider>
<Game />
</DrafterProvider>
</Canvas>
);
}
And then we can access the drafter either inside a component with a hook or outside a component with a getter function.
import { getDrafter, useDrafter } from '@draft-n-draw/react';
// Outside a component.
const drafter = getDrafter();
// Inside a component.
function Component() {
const drafter2 = useDrafter();
//...
}
👉 Note: If you just want the React package you can install @draft-n-draw/react
.
npm install @draft-n-draw/react
Vanilla JS
You begin by creating a drafter for your scene.
import { Drafter } from '@draft-n-draw/vanilla';
const drafter = new Drafter(scene);
👉 Note: If you just want the vanilla JS package you can install @draft-n-draw/vanilla
.
npm install @draft-n-draw/vanilla
Draw!
Then anywhere you are debugging request a draw.
drafter.drawBox3(myBox3);
drafter.drawRay({ origin, direction, distance });
By default, draws only last a single frame and are kept active by being called in a loop. This way you can safely inline them in functions you are debugging without worrying about spawning too many scene objects or cleaning them up when done — Draft-n-Draw will handle that for you.
If instead you want the draw to persist, you can specify it in the draw options.
drafter.drawSpotLight(mySpotLight, { persist: true });
👉 Note: This will cause a new draw to be persisted on screen each call, so be careful!
A persisted draw can be cleaned up using the dispose()
method with the object being visualized as the key.
drafter.dispose(mySpotLight);
Finally, there are additional draw options for controlling the visuals such as drawing on top and adjusting color or opacity.
drafter.drawWireSphere({ center, radius }, { color: 'yellow', alwaysOnTop: true, opacity: 0.5 });
API
The draw coverage is a work in progress. Our drafter is learning on the job!
First we have the generic draw()
method which can draw any custom visualization or helper. It has all the generic options discussed above.
| Method | Object | Options |
| ------ | -------------- | ----------------------------------------------------------------------------------------------- |
| draw() | THREE.Object3D | { color: THREE.ColorRepresentation, alwaysOnTop: boolean, opacity: number, persist: boolean }
|
And then we have draws for math objects. Unless stated otherwise all options are shared with draw()
.
| Method | Object | Special options |
| ---------------- | ----------------------------------------------------------------------- | ------------------ |
| drawBox3 | THREE.Box3 | None. |
| drawRay | { origin: THREE.Vector3, direction: THREE.Vector3, distance: number }
| None. |
| drawPoint | THREE.Vector3 | radius: number |
| drawWireTriangle | THREE.Triangle | None. |
| drawTriangle | THREE.Triangle | winZFight: boolean |
| drawWireSphere | { center: THREE.Vector3, radius: number }
or THREE.Sphere | None. |
| drawSphere | { center: THREE.Vector3, radius: number }
or THREE.Sphere | None. |
And finally some draws for lights. Unless stated otherwise all options are shared with draw()
.
| Method | Object | Special options | | -------------------- | ---------------------- | --------------- | | drawSpotlight | THREE.SpotLight | None. | | drawPointlight | THREE.PointLight | None. | | drawDirectionalLight | THREE.DirectionalLight | None. | | drawHemisphereLight | THREE.HemisphereLight | None. |