TechBuzzPro

In-World Layout Tool for Three.js: Build Guide

· 6 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks through creating a simple in-world layout tool for a Three.js library, letting users click to place, drag to move, and use handles to resize objects directly in the 3D scene. You'll build a basic but functional tool that can be extended for your own projects. It assumes you have a basic Three.js scene set up and are comfortable with JavaScript and raycasting.

What You'll Need

  • A working Three.js project (version 0.150 or newer recommended).
  • Basic understanding of Three.js: scenes, cameras, renderers, and meshes.
  • Familiarity with raycasting (Raycaster) and mouse events.
  • A text editor and a browser for testing.

Step-by-Step

  1. Set up the scene and camera. Create your renderer, scene, and a perspective camera. Add a ground plane (e.g., a large PlaneGeometry with a basic material) to serve as the placement surface. Enable shadows if desired.
  2. Create a Raycaster and mouse vector. You'll need a Raycaster and a Vector2 to store the mouse position in normalized device coordinates (NDC). Update the mouse vector on every pointer move.
  3. Add event listeners. Listen for pointerdown, pointermove, and pointerup on the renderer's canvas. These will drive the placement, dragging, and resizing interactions.
  4. Implement object placement. On pointerdown, if the tool is in "place" mode and the raycast hits the ground plane, create a new mesh (e.g., a box) at the intersection point. Add it to the scene and to a list of layout objects.
  5. Add selection. On pointerdown, first check if the raycast hits any existing layout object. If it does, select it and prepare for dragging or resizing. If not, and you're in place mode, create a new object.
  6. Implement dragging. When an object is selected and you start dragging, on pointermove raycast to the ground plane again and move the object to the new intersection point. Keep the object's Y position constant if you want it to stay on the ground.
  7. Add resize handles. Create small invisible or semi-transparent boxes (or spheres) as handles. Position them at the corners or edges of the selected object. When a handle is dragged, adjust the object's scale or dimensions based on the drag delta.
  8. Update handle positions. After moving or resizing an object, update the handle positions to match the object's new bounds. This usually means recalculating the bounding box and repositioning the handles.
  9. Add keyboard shortcuts (optional). Support keys like Delete to remove the selected object, R to rotate it, or Esc to deselect.
  10. Add a simple UI. Use HTML buttons or a small menu to switch between "place", "move", and "resize" modes. This makes the tool usable.
  11. Test and refine. Run the app, try placing, moving, and resizing objects. Check for edge cases like clicking on the ground when an object is in the way, or dragging an object off the ground plane.

Common Mistakes

  • Forgetting to update the raycaster's camera. The raycaster needs the correct camera to cast rays accurately. Always set raycaster.setFromCamera(mouse, camera) before each raycast.
  • Not checking for intersections with layout objects before placing. If you click on an existing object, you might accidentally create a new one instead of selecting it. Always check object intersections first.
  • Hardcoding the ground plane's Y position. If your ground is not at Y=0, your objects will float or sink. Use the actual intersection point's Y coordinate.
  • Moving the object's position but not its handles. Handles are children of the object or separate meshes; if they're not updated, they'll stay in the old spot.
  • Ignoring the camera's near/far planes. If your objects are too close or too far, the raycaster won't hit them. Adjust the camera's near and far values.

Tips

  • Use a group for handles. Put all resize handles inside a Group so you can show/hide them easily when selecting/deselecting an object.
  • Use a grid or snap. For a more professional feel, implement snapping to a grid. Round the intersection point to the nearest grid cell.
  • Make handles scale-independent. If you resize an object, the handles should maintain their size. Use a constant world-space size for handles, not a fraction of the object's size.
  • Use a separate layer for the ground. If you have many objects, you can use Three.js layers to filter raycast hits, making the ground easier to select.
  • Keep the code modular. Separate the layout tool logic into its own class or module. This makes it easier to reuse and maintain.

Troubleshooting

  • Objects are not being placed. Check that the raycaster is hitting the ground plane. Verify the ground's material has side: THREE.DoubleSide if you're looking from above. Also, ensure the ground is not too small or too far from the camera.
  • Dragging is jittery or objects jump. This usually happens when you're not updating the mouse vector correctly on every move. Make sure the pointermove listener updates the NDC coordinates.
  • Resize handles are not visible. They might be inside the object or too small. Try making them larger and setting depthTest: false on their material so they always render on top.
  • Objects disappear after resizing. If you're changing the geometry's width/height/depth, remember to call geometry.computeBoundingBox() if you rely on it for handles. Also check that you're not setting the scale to zero or negative values.
  • Clicking on an object selects it but you can't drag it. Ensure you're tracking the correct mouse button. Some browsers treat right-click as a context menu. Use event.button === 0 for left-click.

Frequently Asked Questions

Can I use this with any Three.js version?

Yes, the core concepts work with most versions. However, the exact API for raycasting and events has changed slightly over time. The examples here use the modern (r150+) style.

How do I add rotation handles?

You can add a separate set of handles that, when dragged, rotate the object around its Y-axis. Use a similar approach to resize handles but modify object.rotation.y based on the drag delta.

Can I save the layout?

Yes, you can serialize the positions, rotations, and scales of all layout objects to JSON and save it. Then load it back by recreating the meshes and applying the saved transforms.

How do I make the ground plane invisible?

Set the material's transparent to true and opacity to 0, or set visible to false. But if you set visible to false, the raycaster won't hit it. Instead, use a transparent material or set material.colorWrite = false and material.depthWrite = false.

Why are my objects not casting shadows?

Make sure both the object and the ground have castShadow and receiveShadow set to true, and that your renderer has shadowMap.enabled = true. Also check that your light is set to cast shadows.

Can I use this for 3D models (GLTF) instead of boxes?

Absolutely. Instead of creating a box, load a GLTF model and add it to the scene. The rest of the logic (selection, dragging, resizing) works the same, though resizing a complex model might not look great—you might want to scale it uniformly instead.