Setting the file. One moment.
Cache · Frost Sequence Camera Orbit · heygen-com/hyperframes · Skills Docs
ContentsBack to the top of the page Three Mesh BVH LICENSE
source/src/ cache.ts
TypeScript · 97 lines · 3 KB
export
const
BUILD_VERSION
=
"frost-build-10-planar-cap-normals"
;
11
12 export interface CachedGeometry {
13 position : Float32Array ;
14 normal : Float32Array ;
15 uv : Float32Array | null ;
16 index : Uint32Array | null ;
17 }
18 export interface CachedBuild {
19 geometries : CachedGeometry [];
20 sdfs : {
21 data : Float32Array ;
22 res : number ;
23 bound : number ;
24 thickness : number ;
25 sampleBound : number ;
26 }[];
27 rests : Float32Array [];
28 }
29
30 function open () : Promise < IDBDatabase | null > {
31 return new Promise (( resolve ) => {
32 try {
33 if ( typeof indexedDB === "undefined" ) return resolve ( null );
34 const req = indexedDB. open ( DB , VERSION );
35 req. onupgradeneeded = () => {
36 req.result. createObjectStore ( STORE );
37 };
38 req. onsuccess = () => resolve (req.result);
39 req. onerror = () => resolve ( null );
40 } catch {
41 resolve ( null );
42 }
43 });
44 }
45
46 export async function readBuild ( key : string ) : Promise < CachedBuild | null > {
47 const db = await open ();
48 if ( ! db) return null ;
49 return new Promise (( resolve ) => {
50 try {
51 const tx = db. transaction ( STORE , "readonly" );
52 const req = tx. objectStore ( STORE ). get (key);
53 req. onsuccess = () => resolve ((req.result as CachedBuild ) ?? null );
54 req. onerror = () => resolve ( null );
55 } catch {
56 resolve ( null );
57 }
58 });
59 }
60
61 export async function writeBuild ( key : string , build : CachedBuild ) : Promise < void > {
62 const db = await open ();
63 if ( ! db) return ;
64 await new Promise < void >(( resolve ) => {
65 try {
66 const tx = db. transaction ( STORE , "readwrite" );
67 const store = tx. objectStore ( STORE );
68 store. clear (); // one entry: the current build
69 store. put (build, key);
70 tx. oncomplete = () => resolve ();
71 tx. onerror = () => resolve ();
72 } catch {
73 resolve ();
74 }
75 });
76 }
77
78 export function packGeometry ( g : THREE . BufferGeometry ) : CachedGeometry {
79 const a = ( name : string ) =>
80 g.attributes[name] ? (g.attributes[name].array as Float32Array ) : null ;
81 return {
82 position: a ( "position" ) ! ,
83 normal: a ( "normal" ) ! ,
84 uv: a ( "uv" ),
85 index: g.index ? new Uint32Array (g.index.array as ArrayLike < number >) : null ,
86 };
87 }
88
89 export function unpackGeometry ( c : CachedGeometry ) : THREE . BufferGeometry {
90 const g = new THREE . BufferGeometry ();
91 g. setAttribute ( "position" , new THREE . Float32BufferAttribute (c.position, 3 ));
92 g. setAttribute ( "normal" , new THREE . Float32BufferAttribute (c.normal, 3 ));
93 if (c.uv) g. setAttribute ( "uv" , new THREE . Float32BufferAttribute (c.uv, 2 ));
94 if (c.index) g. setIndex ( new THREE . BufferAttribute (c.index, 1 ));
95 g. computeBoundingBox ();
96 return g;
97 }