MOP_tutorial / _pick.html
AK51's picture
Upload 50 files
3de135f verified
Raw History Blame Contribute Delete
4.62 kB
<!doctype html>
<html><head><meta charset="utf-8">
<style>body{margin:0;background:#000;color:#fff;font:13px monospace}#wrap{position:relative;width:760px;height:760px}canvas{position:absolute;left:0;top:0}#out{position:absolute;left:770px;top:0;white-space:pre}</style>
<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>window.addEventListener("error",e=>document.body.insertAdjacentHTML("afterbegin","<pre style=color:red>"+e.message+"</pre>"));</script>
</head><body><div id="wrap"></div><div id="out"></div>
<script type="module">
// Debug helper: render a model with a pixel grid; raycast given pixels (px=x:y;x:y) to normalised model coords,
// and draw given normalised points (pts=x,y,z;x,y,z) as numbered markers.
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
const q = new URLSearchParams(location.search);
const W = 760, H = 760;
const renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
renderer.setSize(W, H); renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.outputColorSpace = THREE.SRGBColorSpace;
document.getElementById('wrap').appendChild(renderer.domElement);
const scene = new THREE.Scene(); scene.background = new THREE.Color(0x101420);
scene.environment = new THREE.PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture; scene.environmentIntensity = 0.8;
const sun = new THREE.DirectionalLight(0xffffff, 3); sun.position.set(5, 3, 4); scene.add(sun);
const gltf = await new GLTFLoader().setMeshoptDecoder(MeshoptDecoder).loadAsync(`models/${q.get('m')}.glb`);
const obj = gltf.scene; obj.updateMatrixWorld(true);
const box = new THREE.Box3().setFromObject(obj, true), size = box.getSize(new THREE.Vector3()), c = box.getCenter(new THREE.Vector3());
const R = Math.max(size.x, size.y, size.z) / 2;
const holder = new THREE.Group(); holder.add(obj); obj.position.sub(c); holder.scale.setScalar(1 / R); scene.add(holder);
const cam = new THREE.PerspectiveCamera(40, 1, 0.01, 100);
const v = (q.get('v') || '1,0.6,1.2').split(',').map(Number);
const zoom = +(q.get('z') || 1);
const tg = new THREE.Vector3(...(q.get('t') || '0,0,0').split(',').map(Number));
cam.position.set(...v).normalize().multiplyScalar(4.6 / zoom).add(tg); cam.lookAt(tg);
holder.updateMatrixWorld(true);
renderer.render(scene, cam);
const ov = document.createElement('canvas'); ov.width = W; ov.height = H; document.getElementById('wrap').appendChild(ov);
const g = ov.getContext('2d');
g.strokeStyle = 'rgba(0,255,255,0.25)'; g.fillStyle = 'rgba(0,255,255,0.8)'; g.font = '10px monospace';
for (let x = 0; x <= W; x += 50) { g.beginPath(); g.moveTo(x, 0); g.lineTo(x, H); g.stroke(); g.fillText(x, x + 2, 10); }
for (let y = 0; y <= H; y += 50) { g.beginPath(); g.moveTo(0, y); g.lineTo(W, y); g.stroke(); g.fillText(y, 2, y - 2); }
const out = [];
const ray = new THREE.Raycaster();
(q.get('px') || '').split(';').filter(Boolean).forEach((s, i) => {
const [x, y] = s.split(':').map(Number);
ray.setFromCamera(new THREE.Vector2(x / W * 2 - 1, -(y / H) * 2 + 1), cam);
const hit = ray.intersectObject(holder, true)[0];
g.fillStyle = hit ? '#ff0' : '#f00'; g.beginPath(); g.arc(x, y, 6, 0, 7); g.fill(); g.fillStyle = '#000'; g.fillText('p' + i, x - 6, y + 3);
if (hit) { const p = hit.point.clone(); out.push(`p${i} ${x}:${y} -> [${p.x.toFixed(3)}, ${p.y.toFixed(3)}, ${p.z.toFixed(3)}] ${hit.object.material.name}`); }
else out.push(`p${i} ${x}:${y} -> MISS`);
});
(q.get('pts') || '').split(';').filter(Boolean).forEach((s, i) => {
const p = new THREE.Vector3(...s.split(',').map(Number)).project(cam);
const x = (p.x + 1) / 2 * W, y = (1 - p.y) / 2 * H;
g.fillStyle = '#f0f'; g.beginPath(); g.arc(x, y, 9, 0, 7); g.fill(); g.fillStyle = '#fff'; g.font = 'bold 12px monospace'; g.fillText(String(i + 1), x - 4, y + 4);
});
const mats = new Set(); obj.traverse(o => { if (o.isMesh) mats.add(o.material.name + ' ' + o.material.color.getHexString() + ' m' + o.material.metalness.toFixed(1) + ' r' + o.material.roughness.toFixed(1) + (o.material.map ? ' map' : '')); });
out.push('size ' + size.toArray().map(n => n.toFixed(2)).join(' x ') + ' R ' + R.toFixed(2), ...mats);
document.getElementById('out').textContent = out.join('\n');
document.title = 'done';
</script></body></html>