283 lines
9.8 KiB
HTML
283 lines
9.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>33 - Stanford Armadillo</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
overflow: hidden;
|
|
background: #0a0a0a;
|
|
font-family: 'Segoe UI', monospace;
|
|
}
|
|
#canvas-container {
|
|
position: fixed;
|
|
top: 0; left: 0;
|
|
width: 100%; height: 100%;
|
|
}
|
|
#ui {
|
|
position: fixed;
|
|
top: 0; left: 0;
|
|
width: 100%; height: 100%;
|
|
pointer-events: none;
|
|
z-index: 10;
|
|
}
|
|
#title {
|
|
position: absolute;
|
|
top: 25px; left: 30px;
|
|
color: #fff;
|
|
font-size: 20px;
|
|
letter-spacing: 2px;
|
|
}
|
|
#title span { color: #ff6b6b; font-size: 14px; }
|
|
#stats {
|
|
position: absolute;
|
|
top: 25px; right: 30px;
|
|
color: #888;
|
|
text-align: right;
|
|
font-size: 12px;
|
|
}
|
|
#stats .big {
|
|
font-size: 22px;
|
|
color: #fff;
|
|
font-weight: bold;
|
|
}
|
|
#controls {
|
|
position: absolute;
|
|
bottom: 30px; left: 30px;
|
|
pointer-events: auto;
|
|
background: rgba(0,0,0,0.85);
|
|
padding: 15px;
|
|
border-radius: 10px;
|
|
border: 1px solid rgba(255,107,107,0.3);
|
|
}
|
|
#controls button {
|
|
background: rgba(255,255,255,0.08);
|
|
border: 1px solid rgba(255,255,255,0.15);
|
|
color: #fff;
|
|
padding: 8px 14px;
|
|
margin: 3px;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 11px;
|
|
transition: all 0.3s;
|
|
}
|
|
#controls button:hover {
|
|
background: rgba(255,107,107,0.3);
|
|
border-color: #ff6b6b;
|
|
}
|
|
#controls button.active {
|
|
background: rgba(255,107,107,0.5);
|
|
border-color: #ff6b6b;
|
|
}
|
|
#loading {
|
|
position: fixed;
|
|
top: 50%; left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
color: #ff6b6b;
|
|
font-size: 24px;
|
|
text-align: center;
|
|
z-index: 100;
|
|
}
|
|
#loading .progress {
|
|
font-size: 14px;
|
|
color: #666;
|
|
margin-top: 10px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="loading">
|
|
<div>🦔 加载 Armadillo...</div>
|
|
<div class="progress" id="loading-text">6.6MB | 带颜色扫描数据</div>
|
|
</div>
|
|
|
|
<div id="canvas-container"></div>
|
|
|
|
<div id="ui">
|
|
<div id="title">33 - Stanford <span>ARMADILLO</span> 🦔</div>
|
|
|
|
<div id="stats">
|
|
<div class="big">172,974 顶点</div>
|
|
<div>345,944 三角面</div>
|
|
<div>带 RGB 颜色</div>
|
|
<div>6.6MB 文件</div>
|
|
</div>
|
|
|
|
<div id="controls">
|
|
<div style="color:#ff6b6b;margin-bottom:8px;font-size:12px;">🎨 渲染模式</div>
|
|
<button onclick="setMode('color')" class="active" id="btn-color">彩色顶点</button>
|
|
<button onclick="setMode('normal')" id="btn-normal">法线着色</button>
|
|
<button onclick="setMode('wireframe')" id="btn-wireframe">线框</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
|
|
|
|
<script>
|
|
// ========== 场景 ==========
|
|
const scene = new THREE.Scene();
|
|
scene.background = new THREE.Color(0x0a0a0a);
|
|
scene.fog = new THREE.Fog(0x0a0a0a, 1, 10);
|
|
|
|
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.001, 100);
|
|
camera.position.set(0.15, 0.1, 0.3);
|
|
|
|
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
renderer.setSize(window.innerWidth, window.innerHeight);
|
|
renderer.setPixelRatio(window.devicePixelRatio);
|
|
document.getElementById('canvas-container').appendChild(renderer.domElement);
|
|
|
|
const controls = new THREE.OrbitControls(camera, renderer.domElement);
|
|
controls.enableDamping = true;
|
|
controls.autoRotate = true;
|
|
controls.autoRotateSpeed = 1;
|
|
|
|
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
|
|
scene.add(ambientLight);
|
|
|
|
const mainLight = new THREE.DirectionalLight(0xffffff, 0.8);
|
|
mainLight.position.set(1, 2, 1);
|
|
scene.add(mainLight);
|
|
|
|
// ========== PLY 解析器 ==========
|
|
async function loadArmadillo() {
|
|
const response = await fetch('../../stanfordRecon/Armadillo.ply');
|
|
const buffer = await response.arrayBuffer();
|
|
const data = new Uint8Array(buffer);
|
|
|
|
// 解析头部
|
|
let headerEnd = 0;
|
|
const decoder = new TextDecoder();
|
|
for (let i = 0; i < Math.min(data.length, 5000); i++) {
|
|
const slice = decoder.decode(data.slice(i, i+20));
|
|
if (slice.includes('end_header')) {
|
|
headerEnd = i + 13;
|
|
break;
|
|
}
|
|
}
|
|
|
|
const header = decoder.decode(data.slice(0, headerEnd));
|
|
const vertexMatch = header.match(/element vertex (\d+)/);
|
|
const numVertices = parseInt(vertexMatch[1]);
|
|
|
|
// 检查是否有颜色
|
|
const hasColor = header.includes('property uchar red');
|
|
|
|
// 解析二进制数据
|
|
const vertices = new Float32Array(numVertices * 3);
|
|
const colors = new Float32Array(numVertices * 3);
|
|
|
|
const view = new DataView(buffer, headerEnd);
|
|
let offset = 0;
|
|
|
|
for (let i = 0; i < numVertices; i++) {
|
|
vertices[i*3] = view.getFloat32(offset, true);
|
|
vertices[i*3+1] = view.getFloat32(offset+4, true);
|
|
vertices[i*3+2] = view.getFloat32(offset+8, true);
|
|
offset += 12;
|
|
|
|
if (hasColor) {
|
|
colors[i*3] = view.getUint8(offset) / 255;
|
|
colors[i*3+1] = view.getUint8(offset+1) / 255;
|
|
colors[i*3+2] = view.getUint8(offset+2) / 255;
|
|
offset += 3;
|
|
} else {
|
|
colors[i*3] = 0.8;
|
|
colors[i*3+1] = 0.6;
|
|
colors[i*3+2] = 0.4;
|
|
}
|
|
|
|
// 跳过法线(如果有)
|
|
if (header.includes('property float nx')) {
|
|
offset += 12;
|
|
}
|
|
}
|
|
|
|
return { vertices, colors, numVertices };
|
|
}
|
|
|
|
// ========== 创建网格 ==========
|
|
function createMesh(data, mode) {
|
|
if (currentMesh) {
|
|
scene.remove(currentMesh);
|
|
currentMesh.geometry.dispose();
|
|
currentMesh.material.dispose();
|
|
}
|
|
|
|
const geometry = new THREE.BufferGeometry();
|
|
geometry.setAttribute('position', new THREE.BufferAttribute(data.vertices, 3));
|
|
|
|
if (mode === 'color') {
|
|
geometry.setAttribute('color', new THREE.BufferAttribute(data.colors, 3));
|
|
const material = new THREE.PointsMaterial({
|
|
size: 0.003,
|
|
vertexColors: true,
|
|
sizeAttenuation: true
|
|
});
|
|
currentMesh = new THREE.Points(geometry, material);
|
|
|
|
} else if (mode === 'normal') {
|
|
geometry.computeVertexNormals();
|
|
const material = new THREE.MeshNormalMaterial();
|
|
currentMesh = new THREE.Mesh(geometry, material);
|
|
|
|
} else if (mode === 'wireframe') {
|
|
const material = new THREE.PointsMaterial({
|
|
color: 0xff6b6b,
|
|
size: 0.002,
|
|
transparent: true,
|
|
opacity: 0.6
|
|
});
|
|
currentMesh = new THREE.Points(geometry, material);
|
|
}
|
|
|
|
currentMesh.scale.set(0.01, 0.01, 0.01);
|
|
|
|
// 居中
|
|
geometry.computeBoundingBox();
|
|
const center = new THREE.Vector3();
|
|
geometry.boundingBox.getCenter(center);
|
|
geometry.translate(-center.x, -center.y, -center.z);
|
|
|
|
scene.add(currentMesh);
|
|
}
|
|
|
|
let currentMesh = null;
|
|
let modelData = null;
|
|
let currentMode = 'color';
|
|
|
|
function setMode(mode) {
|
|
currentMode = mode;
|
|
document.querySelectorAll('#controls button').forEach(btn => btn.classList.remove('active'));
|
|
document.getElementById('btn-' + mode).classList.add('active');
|
|
if (modelData) createMesh(modelData, mode);
|
|
}
|
|
|
|
// ========== 加载 ==========
|
|
loadArmadillo().then(data => {
|
|
modelData = data;
|
|
createMesh(data, 'color');
|
|
document.getElementById('loading').style.display = 'none';
|
|
});
|
|
|
|
// ========== 动画 ==========
|
|
function animate() {
|
|
requestAnimationFrame(animate);
|
|
controls.update();
|
|
renderer.render(scene, camera);
|
|
}
|
|
|
|
window.addEventListener('resize', () => {
|
|
camera.aspect = window.innerWidth / window.innerHeight;
|
|
camera.updateProjectionMatrix();
|
|
renderer.setSize(window.innerWidth, window.innerHeight);
|
|
});
|
|
|
|
animate();
|
|
</script>
|
|
</body>
|
|
</html>
|