New source found from dndbeyond.com
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
import _extends from '@babel/runtime/helpers/esm/extends';
|
||||
import * as React from 'react';
|
||||
import { GLTFLoader, DRACOLoader, MeshoptDecoder } from 'three-stdlib';
|
||||
import { useLoader } from '@react-three/fiber';
|
||||
import { Clone } from './Clone.js';
|
||||
|
||||
let dracoLoader = null;
|
||||
let decoderPath = 'https://www.gstatic.com/draco/versioned/decoders/1.5.5/';
|
||||
function extensions(useDraco = true, useMeshopt = true, extendLoader) {
|
||||
return loader => {
|
||||
if (extendLoader) {
|
||||
extendLoader(loader);
|
||||
}
|
||||
if (useDraco) {
|
||||
if (!dracoLoader) {
|
||||
dracoLoader = new DRACOLoader();
|
||||
}
|
||||
dracoLoader.setDecoderPath(typeof useDraco === 'string' ? useDraco : decoderPath);
|
||||
loader.setDRACOLoader(dracoLoader);
|
||||
}
|
||||
if (useMeshopt) {
|
||||
loader.setMeshoptDecoder(typeof MeshoptDecoder === 'function' ? MeshoptDecoder() : MeshoptDecoder);
|
||||
}
|
||||
};
|
||||
}
|
||||
const useGLTF = (path, useDraco, useMeshopt, extendLoader) => useLoader(GLTFLoader, path, extensions(useDraco, useMeshopt, extendLoader));
|
||||
useGLTF.preload = (path, useDraco, useMeshopt, extendLoader) => useLoader.preload(GLTFLoader, path, extensions(useDraco, useMeshopt, extendLoader));
|
||||
useGLTF.clear = path => useLoader.clear(GLTFLoader, path);
|
||||
useGLTF.setDecoderPath = path => {
|
||||
decoderPath = path;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
const Gltf = /* @__PURE__ */React.forwardRef(({
|
||||
src,
|
||||
useDraco,
|
||||
useMeshOpt,
|
||||
extendLoader,
|
||||
...props
|
||||
}, ref) => {
|
||||
const {
|
||||
scene
|
||||
} = useGLTF(src, useDraco, useMeshOpt, extendLoader);
|
||||
return /*#__PURE__*/React.createElement(Clone, _extends({
|
||||
ref: ref
|
||||
}, props, {
|
||||
object: scene
|
||||
}));
|
||||
});
|
||||
|
||||
export { Gltf, useGLTF };
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
import * as React from 'react';
|
||||
import { useLayoutEffect, useEffect, useMemo } from 'react';
|
||||
import { TextureLoader, Texture as Texture$1 } from 'three';
|
||||
import { useThree, useLoader } from '@react-three/fiber';
|
||||
|
||||
const IsObject = url => url === Object(url) && !Array.isArray(url) && typeof url !== 'function';
|
||||
function useTexture(input, onLoad) {
|
||||
const gl = useThree(state => state.gl);
|
||||
const textures = useLoader(TextureLoader, IsObject(input) ? Object.values(input) : input);
|
||||
useLayoutEffect(() => {
|
||||
onLoad == null || onLoad(textures);
|
||||
}, [onLoad]);
|
||||
|
||||
// https://github.com/mrdoob/three.js/issues/22696
|
||||
// Upload the texture to the GPU immediately instead of waiting for the first render
|
||||
// NOTE: only available for WebGLRenderer
|
||||
useEffect(() => {
|
||||
if ('initTexture' in gl) {
|
||||
let textureArray = [];
|
||||
if (Array.isArray(textures)) {
|
||||
textureArray = textures;
|
||||
} else if (textures instanceof Texture$1) {
|
||||
textureArray = [textures];
|
||||
} else if (IsObject(textures)) {
|
||||
textureArray = Object.values(textures);
|
||||
}
|
||||
textureArray.forEach(texture => {
|
||||
if (texture instanceof Texture$1) {
|
||||
gl.initTexture(texture);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [gl, textures]);
|
||||
const mappedTextures = useMemo(() => {
|
||||
if (IsObject(input)) {
|
||||
const keyed = {};
|
||||
let i = 0;
|
||||
for (const key in input) keyed[key] = textures[i++];
|
||||
return keyed;
|
||||
} else {
|
||||
return textures;
|
||||
}
|
||||
}, [input, textures]);
|
||||
return mappedTextures;
|
||||
}
|
||||
useTexture.preload = url => useLoader.preload(TextureLoader, url);
|
||||
useTexture.clear = input => useLoader.clear(TextureLoader, input);
|
||||
|
||||
//
|
||||
|
||||
const Texture = ({
|
||||
children,
|
||||
input,
|
||||
onLoad
|
||||
}) => {
|
||||
const ret = useTexture(input, onLoad);
|
||||
return /*#__PURE__*/React.createElement(React.Fragment, null, children == null ? void 0 : children(ret));
|
||||
};
|
||||
|
||||
export { IsObject, Texture, useTexture };
|
||||
+16187
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user