0.3.1 ā€¢ Published 1 year ago

draft-n-draw v0.3.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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.

MethodObjectOptions
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().

MethodObjectSpecial options
drawBox3THREE.Box3None.
drawRay{ origin: THREE.Vector3, direction: THREE.Vector3, distance: number }None.
drawPointTHREE.Vector3radius: number
drawWireTriangleTHREE.TriangleNone.
drawTriangleTHREE.TrianglewinZFight: boolean
drawWireSphere{ center: THREE.Vector3, radius: number } or THREE.SphereNone.
drawSphere{ center: THREE.Vector3, radius: number } or THREE.SphereNone.

And finally some draws for lights. Unless stated otherwise all options are shared with draw().

MethodObjectSpecial options
drawSpotlightTHREE.SpotLightNone.
drawPointlightTHREE.PointLightNone.
drawDirectionalLightTHREE.DirectionalLightNone.
drawHemisphereLightTHREE.HemisphereLightNone.