openusd-explorer / src /spatial.ts
ZIP Space Deployer
Deploy ZIP — 2026-10-26 20:37 UTC
7407d55
Raw History Blame Contribute Delete
1.55 kB
import type {Asset,PlacedAsset,View} from './types.ts';
function hash(text:string){let h=2166136261;for(const c of text)h=Math.imul(h^c.charCodeAt(0),16777619);return (h>>>0)/4294967296;}
export function placeAssets(assets:Asset[]):PlacedAsset[]{
let ring=0,start=0,count=1;
return assets.map((asset,index)=>{
while(index>=start+count){start+=count;ring++;count=ring*6;}
const angle=(index-start)/count*Math.PI*2-Math.PI*0.8+(ring%2)*0.15;
const radius=ring*410+(hash(asset.id)-0.5)*38;
return {asset,x:ring?Math.cos(angle)*radius:0,y:ring?Math.sin(angle)*radius*0.9:0,size:300+hash(asset.id+'size')*52};
});
}
export function visibleAssets(placed:PlacedAsset[],view:View,width:number,height:number,padding=200){return placed.filter(item=>{const x=item.x*view.k+view.x,y=item.y*view.k+view.y,half=item.size*view.k/2;return x+half>-padding&&x-half<width+padding&&y+half>-padding&&y-half<height+padding;});}
export function homeView(width:number,height:number):View{return {x:width/2,y:height/2+15,k:width<700?0.66:1};}
/** The selected viewer has a minimum screen size, unlike catalogue thumbnails.
* Its controls follow its real screen bounds, without thumbnail preloading padding.
*/
export function viewerFrame(x:number,y:number,scale:number,width:number,height:number){
const dimension=Math.min(width<700?width-38:470,Math.max(270,350*scale));
const left=x-dimension/2,top=y-dimension/2;
return {dimension,left,top,inView:width>0&&height>0&&dimension>0&&left<width&&left+dimension>0&&top<height&&top+dimension>0};
}