"반투명" 버튼과 투명도 슬라이더(20~100%). 선택은 localStorage에 기억(접근 불가여도 동작). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
120 lines
8.7 KiB
HTML
120 lines
8.7 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="ko">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>SCAD 3D 보기</title>
|
||
<link rel="icon" type="image/png" sizes="64x64" href="/homeclaw_logo.png">
|
||
<script>try{var _t=localStorage.getItem('homeclaw_theme')||'dark';document.documentElement.setAttribute('data-theme',_t);}catch(e){}</script>
|
||
<style>
|
||
:root{--bg:#f2f4f7;--fg:#1b2430;--bar:#ffffff;--line:#d3dbe4;--muted:#5d6b7c;--btn:#e8edf3;--on:#2b7fd9}
|
||
:root[data-theme="dark"]{--bg:#161b22;--fg:#e4eaf1;--bar:#1e252e;--line:#2c3744;--muted:#93a1b2;--btn:#2a3340;--on:#5aa2f0}
|
||
html,body{height:100%;margin:0;background:var(--bg);color:var(--fg);font-family:'Noto Sans KR',system-ui,sans-serif;overflow:hidden}
|
||
#bar{position:fixed;top:0;left:0;right:0;display:flex;flex-wrap:wrap;align-items:center;gap:6px 10px;padding:8px 12px;background:var(--bar);border-bottom:1px solid var(--line);z-index:2;font-size:13px}
|
||
#bar b{font-size:14px}#bar .sp{flex:1}
|
||
button,a.b{background:var(--btn);color:var(--fg);border:1px solid var(--line);border-radius:6px;padding:4px 10px;font-size:12px;cursor:pointer;text-decoration:none}
|
||
button.on{background:var(--on);color:#fff;border-color:var(--on)}
|
||
#c{position:fixed;inset:0;top:44px}
|
||
#msg{position:fixed;inset:44px 0 0 0;display:flex;align-items:center;justify-content:center;text-align:center;padding:24px;color:var(--muted);line-height:1.7;z-index:1;white-space:pre-wrap}
|
||
#info{position:fixed;left:12px;bottom:10px;font-size:12px;color:var(--muted);z-index:2;font-variant-numeric:tabular-nums}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div id="bar">
|
||
<b id="ttl">SCAD</b><span id="sz" style="color:var(--muted)"></span><span class="sp"></span>
|
||
<button id="bw" onclick="toggleWire()">와이어</button>
|
||
<button id="bg" onclick="toggleGlass()" title="안쪽 부품·배선이 비치게 모델을 반투명으로">반투명</button><input id="go" type="range" min="20" max="100" step="5" value="65" oninput="setGlass(this.value)" title="불투명도(%)" style="width:70px;vertical-align:middle;display:none">
|
||
<button onclick="resetView()">시점 초기화</button>
|
||
<button id="bs" onclick="toggleSpin()">자동 회전</button>
|
||
<a class="b" id="src" target="_blank" rel="noopener"></> 소스</a>
|
||
<a class="b" id="dl" download>⬇ STL</a>
|
||
</div>
|
||
<div id="msg">OpenSCAD로 모델을 만드는 중… (복잡하면 수십 초 걸립니다)</div>
|
||
<div id="c"></div>
|
||
<div id="info">드래그: 회전 · 휠: 확대 · 우클릭 드래그: 이동</div>
|
||
<script src="/vendor/three/three.min.js"></script>
|
||
<script src="/vendor/three/STLLoader.js"></script>
|
||
<script src="/vendor/three/OrbitControls.js"></script>
|
||
<script>
|
||
const q=new URLSearchParams(location.search), pid=q.get('projectId')||'', rel=q.get('relPath')||'';
|
||
document.getElementById('ttl').textContent=rel.split('/').pop()||'SCAD';
|
||
document.title=(rel.split('/').pop()||'SCAD')+' · 3D';
|
||
function authH(){let t='';try{t=sessionStorage.getItem('smallclaw_token')||localStorage.getItem('smallclaw_token')||'';}catch(e){}return t?{Authorization:'Bearer '+t}:{};}
|
||
let scene,camera,renderer,controls,mesh,wire=false,fit=null;
|
||
function init(){
|
||
const c=document.getElementById('c');
|
||
scene=new THREE.Scene();
|
||
scene.background=new THREE.Color(getComputedStyle(document.body).backgroundColor);
|
||
camera=new THREE.PerspectiveCamera(40,c.clientWidth/c.clientHeight,0.1,100000);
|
||
camera.up.set(0,0,1); // OpenSCAD는 Z가 위
|
||
renderer=new THREE.WebGLRenderer({antialias:true});
|
||
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
|
||
renderer.setSize(c.clientWidth,c.clientHeight);
|
||
c.appendChild(renderer.domElement);
|
||
controls=new THREE.OrbitControls(camera,renderer.domElement);
|
||
controls.enableDamping=true;
|
||
scene.add(new THREE.HemisphereLight(0xffffff,0x445566,0.75));
|
||
const d=new THREE.DirectionalLight(0xffffff,0.8); d.position.set(1,-1.5,2); scene.add(d);
|
||
const d2=new THREE.DirectionalLight(0xffffff,0.35); d2.position.set(-1,1,-0.5); scene.add(d2);
|
||
addEventListener('resize',()=>{camera.aspect=c.clientWidth/c.clientHeight;camera.updateProjectionMatrix();renderer.setSize(c.clientWidth,c.clientHeight);});
|
||
(function loop(){requestAnimationFrame(loop);controls.update();renderer.render(scene,camera);})();
|
||
}
|
||
function resetView(){
|
||
if(!fit)return;
|
||
const {c,r}=fit; const dist=r/Math.sin(camera.fov*Math.PI/360)*1.1;
|
||
camera.position.set(c.x+dist*0.6,c.y-dist*0.8,c.z+dist*0.55);
|
||
camera.near=Math.max(0.01,dist/1000);camera.far=dist*20;camera.updateProjectionMatrix();
|
||
controls.target.copy(c);controls.update();
|
||
}
|
||
let glass=false, glassOp=0.65;
|
||
function applyGlass(){ if(!mesh)return; const m=mesh.material; const tr=glass&&glassOp<0.995; m.transparent=tr; m.opacity=tr?glassOp:1; m.depthWrite=!tr; m.needsUpdate=true; mesh.children.forEach(ch=>{ if(ch.isLineSegments){ ch.material.opacity=glass?0.08:0.35; ch.material.needsUpdate=true; } }); document.getElementById('bg').classList.toggle('on',glass); const g=document.getElementById('go'); g.style.display=glass?'':'none'; g.value=Math.round(glassOp*100); }
|
||
function toggleGlass(){ glass=!glass; try{localStorage.setItem('scadViewerGlass',glass?'1':'0');}catch(e){} applyGlass(); }
|
||
function setGlass(v){ glassOp=Math.min(1,Math.max(0.2,Number(v)/100)); try{localStorage.setItem('scadViewerGlassOp',String(glassOp));}catch(e){} applyGlass(); }
|
||
function toggleWire(){wire=!wire;if(mesh)mesh.material.wireframe=wire;document.getElementById('bw').classList.toggle('on',wire);}
|
||
function toggleSpin(){controls.autoRotate=!controls.autoRotate;controls.autoRotateSpeed=2.5;document.getElementById('bs').classList.toggle('on',controls.autoRotate);}
|
||
async function load(){
|
||
const msg=document.getElementById('msg');
|
||
if(!pid||!rel){msg.textContent='프로젝트/파일이 지정되지 않았습니다.';return;}
|
||
const base='/api/workshop/scad-stl?projectId='+encodeURIComponent(pid)+'&relPath='+encodeURIComponent(rel);
|
||
document.getElementById('src').href='/api/files/workshop/'+encodeURIComponent(pid)+'/'+rel.split('/').map(encodeURIComponent).join('/');
|
||
try{
|
||
// import("x.stl"); 한 줄짜리 .scad면 OpenSCAD를 거치지 않고 그 STL을 그대로 읽는다(면 색 보존 + 닫히지 않은 메시도 OK).
|
||
let r=null;
|
||
try{
|
||
const sr=await fetch(document.getElementById('src').href,{credentials:'include',headers:authH()});
|
||
if(sr.ok){
|
||
const code=(await sr.text()).replace(/\/\*[\s\S]*?\*\//g,'').replace(/\/\/.*$/gm,'').trim();
|
||
const m=code.match(/^import\s*\(\s*"([^"]+\.stl)"\s*\)\s*;$/i);
|
||
if(m){
|
||
const parts=rel.split('/').slice(0,-1);
|
||
for(const seg of m[1].split('/')){ if(seg==='..')parts.pop(); else if(seg&&seg!=='.')parts.push(seg); }
|
||
const fr=await fetch('/api/files/workshop/'+encodeURIComponent(pid)+'/'+parts.map(encodeURIComponent).join('/'),{credentials:'include',headers:authH()});
|
||
if(fr.ok)r=fr;
|
||
}
|
||
}
|
||
}catch(e){ r=null; }
|
||
if(!r)r=await fetch(base,{credentials:'include',headers:authH()});
|
||
if(!r.ok){let d={};try{d=await r.json();}catch(e){} throw new Error((d.detail||d.error||('서버 오류 '+r.status)));}
|
||
const buf=await r.arrayBuffer();
|
||
const dl=document.getElementById('dl'); dl.href=URL.createObjectURL(new Blob([buf],{type:'application/octet-stream'})); dl.download=(rel.split('/').pop()||'model').replace(/\.scad$/i,'')+'.stl';
|
||
const geo=new THREE.STLLoader().parse(buf);
|
||
geo.computeVertexNormals(); geo.computeBoundingBox();
|
||
const bb=geo.boundingBox, size=new THREE.Vector3(); bb.getSize(size);
|
||
mesh=new THREE.Mesh(geo,new THREE.MeshStandardMaterial({color:geo.hasColors?0xffffff:0x6aa5e8,vertexColors:!!geo.hasColors,metalness:0.1,roughness:0.55,side:THREE.DoubleSide,flatShading:false}));
|
||
scene.add(mesh);
|
||
try{glass=localStorage.getItem('scadViewerGlass')==='1'; const o=Number(localStorage.getItem('scadViewerGlassOp')); if(o>=0.2&&o<=1)glassOp=o;}catch(e){} applyGlass(); // 반투명 선택은 기억
|
||
const edges=new THREE.LineSegments(new THREE.EdgesGeometry(geo,35),new THREE.LineBasicMaterial({color:0x1d3552,transparent:true,opacity:0.35}));
|
||
mesh.add(edges);
|
||
const ctr=new THREE.Vector3(); bb.getCenter(ctr);
|
||
const grid=new THREE.GridHelper(Math.max(size.x,size.y)*2.2||100,20,0x888888,0x444444); grid.rotation.x=Math.PI/2; grid.position.set(ctr.x,ctr.y,bb.min.z); grid.material.opacity=0.4; grid.material.transparent=true; scene.add(grid);
|
||
fit={c:ctr,r:Math.max(size.length()/2,1)};
|
||
resetView();
|
||
document.getElementById('sz').textContent=' · '+size.x.toFixed(1)+' × '+size.y.toFixed(1)+' × '+size.z.toFixed(1)+' mm · 삼각형 '+(geo.attributes.position.count/3).toLocaleString();
|
||
msg.remove();
|
||
}catch(e){ msg.textContent='3D 모델을 만들지 못했습니다.\n'+e.message+'\n\n소스(</> 버튼)를 확인하세요.'; }
|
||
}
|
||
init(); load();
|
||
</script>
|
||
</body>
|
||
</html>
|