Atlaslog
Atlaslog / Examples / three.js / Interactive Points
three.js · Runnable Example

Interactive Points

Runnable three.js example: Interactive Points. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All three.js examples
/demos/three-interactive-points.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/three-interactive-points.html
<!--
  说明:两万个点组成的三维散点,鼠标靠近时用一个圆环圈住最近的点并读出序号
  核心逻辑:Raycaster.params.Points.threshold 决定多大范围内的点算命中
  依赖:three.js 0.160.0、OrbitControls · jsDelivr CDN(ESM + importmap)
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>点云拾取</title>
<style>
  html,body{height:100%;margin:0;background:#070d12}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
  .bar{flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600;font-variant-numeric:tabular-nums}
  .bar input[type=range]{width:150px;accent-color:#4f9ef8}
  .bar .sp{flex:1 1 auto}
  #stage{flex:1 1 auto;min-height:0;position:relative;cursor:crosshair}
  #stage canvas{display:block}
  #hud{position:absolute;left:12px;bottom:12px;padding:8px 12px;border-radius:6px;
       background:rgba(13,19,26,.84);border:1px solid #223040;font-size:12px;line-height:1.8;
       color:#c8d2e0;pointer-events:none;font-variant-numeric:tabular-nums}
  #hud em{font-style:normal;color:#7fd1a6}
</style>
</head>
<body>
<div class="bar">
  <span>点数 <b>20000</b></span>
  <label>阈值 <input type="range" id="thr" min="0.1" max="1.6" step="0.05" value="0.55"></label>
  <span><b id="thrv">0.55</b></span>
  <span class="sp"></span>
  <span>移动鼠标拾取点 · 拖拽旋转</span>
</div>
<div id="stage">
  <div id="hud">命中序号 <em id="idx">无</em><br>坐标 <em id="pos">—</em></div>
</div>

<script type="importmap">
{
  "imports": {
    "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
    "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
  }
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

const stage = document.getElementById('stage');
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(stage.clientWidth, stage.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
stage.appendChild(renderer.domElement);

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070d12);

const camera = new THREE.PerspectiveCamera(45, stage.clientWidth / stage.clientHeight, 0.1, 200);
camera.position.set(13, 10, 16);

const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.49;
controls.update();

scene.add(new THREE.HemisphereLight(0xa8c8ff, 0x0d141a, 1.3));

function hash(n) {
  const x = Math.sin(n * 61.7 + 23.1) * 43758.5453;
  return x - Math.floor(x);
}

const N = 20000;
const positions = new Float32Array(N * 3);
const colors = new Float32Array(N * 3);
const c = new THREE.Color();

/* 三个高斯团,像三处独立的采样区 */
for (let i = 0; i < N; i++) {
  const g = Math.floor(hash(i) * 3);
  const cx = (g - 1) * 5.5;
  const u = hash(i + 0.3);
  const v = hash(i + 0.7);
  const w = hash(i + 1.1);

  const r = Math.pow(u, 0.55) * 3.4;
  const a = v * Math.PI * 2;
  positions[i * 3] = cx + Math.cos(a) * r;
  positions[i * 3 + 1] = Math.abs(w - 0.5) * 2.4 + (1 - u) * 2.2;
  positions[i * 3 + 2] = Math.sin(a) * r * 0.8;

  const t = (positions[i * 3 + 1]) / 4.6;
  c.setHSL(0.55 - 0.45 * t, 0.65, 0.42 + 0.25 * t);
  colors[i * 3] = c.r;
  colors[i * 3 + 1] = c.g;
  colors[i * 3 + 2] = c.b;
}

const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geo.setAttribute('color', new THREE.BufferAttribute(colors, 3));
geo.computeBoundingSphere();

const mat = new THREE.PointsMaterial({
  size: 0.11,
  vertexColors: true,
  sizeAttenuation: true
});

const points = new THREE.Points(geo, mat);
scene.add(points);

/* 圆环:命中时移到点的位置并正对相机 */
const ring = new THREE.Mesh(
  new THREE.RingGeometry(0.26, 0.34, 28),
  new THREE.MeshBasicMaterial({ color: 0x7fffd4, side: THREE.DoubleSide, transparent: true, opacity: 0.95 })
);
ring.visible = false;
scene.add(ring);

const raycaster = new THREE.Raycaster();
raycaster.params.Points = { threshold: 0.55 };
const pointer = new THREE.Vector2(-10, -10);

const idxEl = document.getElementById('idx');
const posEl = document.getElementById('pos');

const thrEl = document.getElementById('thr');
const thrvEl = document.getElementById('thrv');
thrEl.addEventListener('input', function () {
  const v = Number(thrEl.value);
  raycaster.params.Points.threshold = v;
  thrvEl.textContent = v.toFixed(2);
});

function pick(event) {
  const r = renderer.domElement.getBoundingClientRect();
  pointer.x = ((event.clientX - r.left) / r.width) * 2 - 1;
  pointer.y = -((event.clientY - r.top) / r.height) * 2 + 1;

  raycaster.setFromCamera(pointer, camera);
  const hits = raycaster.intersectObject(points, false);

  if (!hits.length) {
    ring.visible = false;
    idxEl.textContent = '无';
    posEl.textContent = '—';
    return;
  }

  const hit = hits[0];
  const i = hit.index;
  ring.position.set(
    positions[i * 3],
    positions[i * 3 + 1],
    positions[i * 3 + 2]
  );
  ring.quaternion.copy(camera.quaternion);
  ring.visible = true;

  idxEl.textContent = String(i);
  posEl.textContent =
    '(' + positions[i * 3].toFixed(2) + ', ' +
    positions[i * 3 + 1].toFixed(2) + ', ' +
    positions[i * 3 + 2].toFixed(2) + ')';
}

renderer.domElement.addEventListener('pointermove', pick);
renderer.domElement.addEventListener('pointerleave', function () {
  ring.visible = false;
  idxEl.textContent = '无';
});

renderer.setAnimationLoop(function () {
  controls.update();
  renderer.render(scene, camera);
});

window.addEventListener('resize', function () {
  const w = stage.clientWidth;
  const h = stage.clientHeight;
  camera.aspect = w / h;
  camera.updateProjectionMatrix();
  renderer.setSize(w, h);
});
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More three.js examples

three.js · EXAMPLELights three.js · EXAMPLELights Physical three.js · EXAMPLELights Spotlight