Building a Clickable, Auto-Rotating PCB Viewer with Vue and Three.js
Interactive 3D product viewers are increasingly common in manufacturing, repair documentation, and hardware configurators. This walkthrough addresses two real pain points that break those experiences: material-sharing bugs that corrupt highlight states, and jarring auto-rotation that feels bolted-on rather than designed.
A complete Vue 3 component wraps a Three.js scene to load and interact with a GLB-format PCB model. Clicking any chip or component triggers a Raycaster-based pick that highlights the part in semi-transparent blue while automatically restoring the previous selection's original material. A material-cloning cache prevents the common beginner mistake of mutating shared materials across multiple meshes.
Two UI buttons toggle the main PCB substrate on and off, letting a user inspect internal components in isolation. A separate auto-rotate control uses Tween.js to smoothly animate the camera into an orbiting viewpoint before enabling OrbitControls.autoRotate, and reverses the transition when stopping, with all animation frames, tweens, and timers cleaned up on each state change to prevent jitter.
The component also handles full resource disposal on destroy: canceling requestAnimationFrame loops, disposing geometries and materials, and calling forceContextLoss on the WebGL renderer to avoid memory leaks.
Attaching scene, camera, renderer, and controls to the window object is a pragmatic shortcut for a demo but would cause collisions in a multi-instance or micro-frontend setup.
The 2-second delay between camera arrival and autoRotate activation is a UX detail that avoids the disorienting effect of rotation starting mid-transition.
Force-cleaning all tweens, timers, and animation frames before every state change is defensive but necessary; OrbitControls and Tween.js have no built-in conflict resolution when both try to control the camera.