feat(workshop): scad 3D 뷰어 — import 한 줄짜리 .scad는 STL을 직접 읽어 면 색(vertexColors) 표시, 닫히지 않은 메시도 열림
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d0ee3e16e7
commit
e34ba072a7
1 file changed
+17
-2
@@ -73,14 +73,29 @@ async function load(){
|
||||
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{
|
||||
const r=await fetch(base,{credentials:'include',headers:authH()});
|
||||
// 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:0x6aa5e8,metalness:0.1,roughness:0.55,side:THREE.DoubleSide,flatShading:false}));
|
||||
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);
|
||||
const edges=new THREE.LineSegments(new THREE.EdgesGeometry(geo,35),new THREE.LineBasicMaterial({color:0x1d3552,transparent:true,opacity:0.35}));
|
||||
mesh.add(edges);
|
||||
|
||||
Reference in new issue
Block a user