agentleFS
komogortevkomogortev/threejs-skills/skills/threejs-interaction/SKILL.md

threejs-interaction

Skill1 starsChanged 8 months agoMITView on GitHub

Three.js interaction - raycasting, controls, mouse/touch input, object selection. Use when handling user input, implementing click detection, adding camera controls, or creating interactive 3D experiences.

24 other projects use a version of this, 20 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use pose-library-design, threejs-asset-pipeline and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • LinHao-citySkill

    pose-library-design

    Design reusable 2D character pose libraries, action cycles, and expression states for data-driven animation.

    Also in 4 projects that use this

  • CloudAI-XSkill

    threejs-asset-pipeline

    Shrinking glTF assets for three.js with gltf-transform: inspect, validate and optimize commands, Draco, meshopt and quantization, KTX2, WebP and AVIF textures, the loader setup each output needs, and gltfjsx for React. Use when compressing, optimizing or validating 3D models before loading them.

    Also in 4 projects that use this

  • LinHao-citySkill

    video-toolkit

    Create professional videos autonomously using claude-code-video-toolkit — AI voiceovers, image generation, music, talking heads, and Remotion rendering.

    Also in 4 projects that use this

  • CloudAI-XSkill

    threejs-react-three-fiber

    Building three.js scenes from React with React Three Fiber and drei: renderer setup, the scene graph as JSX, frame-loop, state and loading hooks, pointer events, and adaptive performance, on WebGL or with WebGPU and TSL. Use when writing React components that render three.js.

    Also in 4 projects that use this

  • GopyrSkill

    azure-speech-to-text

    Also in 3 projects that use this

  • GopyrSkill

    dashscope

    Also in 3 projects that use this

  • +5 more from LinHao-city

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 18 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use layers to filter raycast targets — raycaster.layers.set(N)

    line 188
  2. Distinctive:

    Use simplified invisible collision meshes for raycasting complex models

    line 189

“Common” is measured: kept by at least 6 in 10 of the 18 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Three.js Interaction

Quick Start

import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

function onClick(event) {
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  raycaster.setFromCamera(mouse, camera);
  const intersects = raycaster.intersectObjects(scene.children);
  if (intersects.length > 0) console.log("Clicked:", intersects[0].object);
}

window.addEventListener("click", onClick);

Raycaster

const raycaster = new THREE.Raycaster();

// From camera (mouse picking)
raycaster.setFromCamera(mousePosition, camera);

// From arbitrary origin
raycaster.set(origin, direction); // direction must be normalized

// Intersections
const intersects = raycaster.intersectObjects(objects, recursive);
// intersects[0]: { distance, point, face, faceIndex, object, uv, normal }

Correct Mouse Coordinates

// Normalize to [-1, 1] NDC
function updateMouse(event, canvas) {
  const rect = canvas.getBoundingClientRect();
  mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
  mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}

Raycaster Options

raycaster.near = 0;
raycaster.far = 100;
raycaster.params.Line.threshold = 0.1;
raycaster.params.Points.threshold = 0.1;
raycaster.layers.set(1); // Only intersect layer 1

Hover Effects (Throttled)

let lastRaycast = 0;
let hoveredObject = null;

function onMouseMove(event) {
  if (Date.now() - lastRaycast < 50) return; // 20fps max
  lastRaycast = Date.now();

  updateMouse(event, renderer.domElement);
  raycaster.setFromCamera(mouse, camera);
  const intersects = raycaster.intersectObjects(hoverableObjects);

  if (hoveredObject) {
    hoveredObject.material.emissive.set(0x000000);
    document.body.style.cursor = "default";
  }

  if (intersects.length > 0) {
    hoveredObject = intersects[0].object;
    hoveredObject.material.emissive.set(0x444444);
    document.body.style.cursor = "pointer";
  } else {
    hoveredObject = null;
  }
}

Camera Controls

OrbitControls

import { OrbitControls } from "three/addons/controls/OrbitControls.js";

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minPolarAngle = 0;
controls.maxPolarAngle = Math.PI / 2;
controls.minDistance = 2;
controls.maxDistance = 50;
controls.target.set(0, 1, 0);
controls.autoRotate = false;

// Must call in animation loop when damping is enabled
function animate() {
  controls.update();
  renderer.render(scene, camera);
}

PointerLockControls (FPS)

import { PointerLockControls } from "three/addons/controls/PointerLockControls.js";

const controls = new PointerLockControls(camera, document.body);
document.addEventListener("click", () => controls.lock());

controls.addEventListener("lock", () => console.log("Pointer locked"));
controls.addEventListener("unlock", () => console.log("Pointer unlocked"));

TransformControls (Editor Gizmo)

import { TransformControls } from "three/addons/controls/TransformControls.js";

const transformControls = new TransformControls(camera, renderer.domElement);
scene.add(transformControls);
transformControls.attach(selectedMesh);
transformControls.setMode("translate"); // 'translate', 'rotate', 'scale'

transformControls.addEventListener("dragging-changed", (e) => {
  orbitControls.enabled = !e.value; // Disable orbit while dragging
});

Keyboard Input

const keys = new Set();
document.addEventListener("keydown", (e) => keys.add(e.code));
document.addEventListener("keyup", (e) => keys.delete(e.code));

function update() {
  const speed = 0.1;
  if (keys.has("KeyW")) player.position.z -= speed;
  if (keys.has("KeyS")) player.position.z += speed;
  if (keys.has("KeyA")) player.position.x -= speed;
  if (keys.has("KeyD")) player.position.x += speed;
  if (keys.has("Space")) player.position.y += speed;
}

Coordinate Conversion

// World → Screen
function worldToScreen(position, camera) {
  const v = position.clone().project(camera);
  return {
    x: ((v.x + 1) / 2) * window.innerWidth,
    y: (-(v.y - 1) / 2) * window.innerHeight,
  };
}

// Ray-Plane intersection (e.g., place object on ground)
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const intersection = new THREE.Vector3();
raycaster.ray.intersectPlane(groundPlane, intersection);

Performance Tips

  1. Throttle mousemove raycasts (50ms minimum interval)
  2. Use layers to filter raycast targets — raycaster.layers.set(N)
  3. Use simplified invisible collision meshes for raycasting complex models
  4. Disable controls when not needed: controls.enabled = false

See Also

  • threejs-fundamentals — Camera and scene setup
  • threejs-animation — Animating interactions

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.