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.

9 other projects use a version of this, 8 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use receiving-code-review, executing-plans and 2 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.
  • CloudflareSkill

    receiving-code-review

    Use when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation

    Also in 3 projects that use this

  • OpenAISkill

    executing-plans

    Use when you have a written implementation plan to execute in a separate session with review checkpoints

    Also in 3 projects that use this

  • VercelSkill

    web-design-guidelines

    Review UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "check accessibility", "audit design", "review UX", or "check my site against best practices".

    Also in 3 projects that use this

  • StrayDragonSkill

    ui-ux-pro-max

    UI/UX design intelligence. 50 styles, 21 palettes, 50 font pairings, 20 charts, 8 stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind). Actions: plan, build, create, design, implement, review, fix, improve, optimize, enhance, refactor, check UI/UX code. Projects: website, landing page, dashboard, admin panel, e-commerce, SaaS, portfolio, blog, mobile app, .html, .tsx, .vue, .svelte. Elements: button, modal, navbar, sidebar, card, table, form, chart. Styles: glassmorphism, claymorphism, minimalism, brutalism, neumorphism, bento grid, dark mode, responsive, skeuomorphism, flat design. Topics: color palette, accessibility, animation, layout, typography, font pairing, spacing, hover, shadow, gradient.

    Also in 3 projects that use this

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 8 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 8 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.