Open-source 3D anatomy explorer: 2,234 selectable BodyParts3D meshes, system layers, search, and exploded views.
TypeScript
Handle gzip model responses across local and hosted previews
Ashe Magalhaes984ecfd6eede
diff --git a/app/model-download.ts b/app/model-download.tsnew file mode 100644index 0000000..d5e4575--- /dev/null+++ b/app/model-download.ts@@ -0,0 +1,11 @@+/** Static hosts may serve .gz as a compressed response or as a gzip file.+ * Fetch already decodes Content-Encoding; inspect the payload to avoid decoding twice.+ */+export async function decodeModelResponse(response:Response,expectedBytes:number,compressed:boolean):Promise<ArrayBuffer>{+ if(!response.ok)throw new Error('An anatomy file could not be loaded.');+ const payload=await response.arrayBuffer(),signature=new Uint8Array(payload,0,Math.min(2,payload.byteLength));+ const gzip=compressed&&signature[0]===0x1f&&signature[1]===0x8b;+ const buffer=gzip?await new Response(new Blob([payload]).stream().pipeThrough(new DecompressionStream('gzip'))).arrayBuffer():payload;+ if(buffer.byteLength!==expectedBytes)throw new Error('An anatomy file was incomplete. Please reload the viewer.');+ return buffer;+}diff --git a/app/page.tsx b/app/page.tsxindex e36112b..df8f72a 100644--- a/app/page.tsx+++ b/app/page.tsx@@ -28,7 +28,7 @@ export default function Home(){const reset=()=>{setState(s=>({...initial,visible:sex==='female'?[...DEFAULT_VISIBLE,'integumentary']:DEFAULT_VISIBLE,reset:s.reset+1}));setChosen(null);setDetails(false);setPanel(null);};const openPanel=(next:'layers'|'search')=>{setDetails(false);setPanel(p=>p===next?null:next);};return <main className="studio">- {atlas&&<AnatomyScene atlas={atlas} state={state} onSelect={choosePart} onProgress={setProgress} onError={setError}/>}+ {atlas&&<AnatomyScene atlas={atlas} state={state} onSelect={choosePart} onProgress={n=>{setProgress(n);if(n===100)setError('');}} onError={setError}/>}<div className="vignette"/><header className="identity"><div className="eyebrow"><span className="status-dot"/> ANATOMICAL EXPLORER</div><h1>Human <em>Atlas</em><span className="edition">01</span></h1><div className="identity-meta">{atlas?atlas.parts.length.toLocaleString():sex==='female'?'888':'2,234'} modeled pieces <span>·</span> {sex==='female'?'HRA':'BodyParts3D'}</div><div className="anatomy-choice"><Select value={sex} onValueChange={value=>{if(value==='male'||value==='female')setSex(value);}} items={[{value:'male',label:'Male anatomy'},{value:'female',label:'Female anatomy'}]}><SelectTrigger aria-label="Choose male or female anatomy"><SelectValue/></SelectTrigger><SelectContent className="anatomy-choice-menu"><SelectItem value="male">Male anatomy</SelectItem><SelectItem value="female">Female anatomy</SelectItem></SelectContent></Select></div></header><nav className="top-actions" aria-label="Explorer panels"><button className={panel==='search'?'active':''} onClick={()=>openPanel('search')} aria-label="Search anatomy"><Search size={18}/><span>Find a structure</span><kbd>/</kbd></button><button className="icon-button" aria-label="About this atlas" onClick={()=>{setDetails(false);setPanel(null);setAbout(true);}}><Info size={18}/></button></nav>diff --git a/app/scene.tsx b/app/scene.tsxindex 54afb08..b0b616a 100644--- a/app/scene.tsx+++ b/app/scene.tsx@@ -3,6 +3,7 @@ import * as T from 'three';import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';import {RoomEnvironment} from 'three/examples/jsm/environments/RoomEnvironment.js';import {mergeGeometries} from 'three/examples/jsm/utils/BufferGeometryUtils.js';+import {decodeModelResponse} from './model-download';import {PointerTap} from './pointer-tap';import {SYSTEMS,type Atlas,type SceneState} from './anatomy';interface Props {atlas:Atlas;state:SceneState;onSelect:(id:string)=>void;onProgress:(n:number)=>void;onError:(s:string)=>void}@@ -55,7 +56,7 @@ export default function AnatomyScene({atlas,state,onSelect,onProgress,onError}:Pconst mats=new Map(SYSTEMS.map(s=>[s.id,materialFor(s.id)]));let loaded=0;const loadChunk=async(ci:number)=>{- const chunk=atlas.chunks[ci],compressed=!!chunk.gzip&&typeof DecompressionStream!=='undefined';const response=await fetch(compressed?chunk.gzip!:chunk.url,{signal:abort.signal});if(!response.ok)throw new Error('An anatomy file could not be loaded.');const buffer=compressed&&response.body?await new Response(response.body.pipeThrough(new DecompressionStream('gzip'))).arrayBuffer():await response.arrayBuffer();if(disposed)return;+ const chunk=atlas.chunks[ci],compressed=!!chunk.gzip&&typeof DecompressionStream!=='undefined';const response=await fetch(compressed?chunk.gzip!:chunk.url,{signal:abort.signal});const buffer=await decodeModelResponse(response,chunk.bytes,compressed);if(disposed)return;const groups=new Map<string,T.BufferGeometry[]>();atlas.parts.forEach((p,i)=>{if(p.chunk!==ci)return;