Skip to content

v2.2.0 #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/logic/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CompressedTexture, Texture, TextureLoader } from "three";
import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader";
import { useLoader, useThree } from "@react-three/fiber";
import { useGLTF } from "@react-three/drei";

let ktx2loader: KTX2Loader | undefined;
const KTX_CDN = "https://cdn.jsdelivr.net/gh/pmndrs/drei-assets@master/basis/";

/**
* A single hook akin to useTexture but with ktx support
*
* KTX_CDN is from drei so that we don't download two separate transcoders when using the useKtx2 hook elsewhere
* https://github.com/pmndrs/drei/blob/a2daf02853f624ef6062c70ba0b218bc03e5b626/src/core/useKTX2.tsx#L7
* @param url
*/
export function useImage(url: string) {
const IS_KTX2 = url.toLowerCase().endsWith("ktx2");
const gl = useThree((st) => st.gl);

const loader = IS_KTX2 ? KTX2Loader : TextureLoader;
return useLoader<CompressedTexture | Texture, string>(
loader,
url,
(loader: any) => {
if (IS_KTX2) {
(loader as KTX2Loader).detectSupport(gl);
(loader as KTX2Loader).setTranscoderPath(KTX_CDN);
}
}
);
}

/**
* A hook to load gltf models with draco, meshopt, and ktx2 support out of the box
*
* For all cases, functionality is to only download decoder files if needed by the file
* @param url
*/
export function useModel(url: string) {
const gl = useThree((st) => st.gl);

return useGLTF(url, true, true, (loader) => {
if (!ktx2loader) {
ktx2loader = new KTX2Loader();
ktx2loader.setTranscoderPath(KTX_CDN);
ktx2loader.detectSupport(gl);
}
loader.setKTX2Loader(ktx2loader);
});
}
1 change: 1 addition & 0 deletions src/logic/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./assets";
export * from "./collision";
export * from "./dom";
export * from "./keyboard";
Expand Down