加载不在公共文件夹中的GLTF文件

发布于 2025-01-18 05:43:45 字数 535 浏览 4 评论 0原文

我发现一个问题,如果我不能将它们放入React应用程序中的公共文件夹中,则无法将GLTF文件加载到场景中。例如,我有mypvpanel.gltf,我想将其加载到React App或NPM库中,或者我总是与React组件一起使用。

我的问题是:

  • 如果您开发NPM软件包,这是否可以从特定文件夹中导入资产。软件包是否具有/public/Assets之类的东西?
  • 我可以配置自己的软件包以使用具有静态资产的文件夹吗?
  • 如何在没有公共文件夹的情况下加载GLTF文件。例如,我们可以在React应用程序中导入.png文件。

I found a problem that I cannot load GLTF files into a scene if I cannot place them in the Public folder in my React application. For example, I have MyPVPanel.gltf, and I want to load it in react app or in my npm library, or I always to be with my react components.
enter image description here

My questions are:

  • Is this possible to import assets from a specific folder if you develop an npm package. Do packages have something like /public or /assets?
  • Can I configure my own package to have such a folder with static assets?
  • How to load GLTF files without a public folder. For example, we can import .png files without problems in react application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2025-01-25 05:43:45

因此,有一个名为 data Uri uri uri 。您可以以这种格式转换小文件(基本上是一个基本64编码的字符串,并在其开头带有一些元数据)。

数据:[< mediatype>] [; base64],< data>

在我的情况下流。

因此,如果要在应用程序中的特定文件夹中加载小文件,首先,将您的.gltf文件重命名为.json。并且将其导入并将其转换为base64代码后,您可以使用 /code> 来自three.js库。

这是一个代码示例:

import myGLTF from './MyPVPanel.json';
import {GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader";
import myGLTF from "./MyPVPanel.json";

const loader = new GLTFLoader();

const stringGLTF = JSON.stringify(myGLTF) // convert Object to a String
const base64EncodedGLTF = btoa(stringGLTF) // Base64 encode the String

//First part is: 'data:application/octet-stream;base64,'
const resultingDataURI = `data:application/octet-stream;base64,${base64EncodedGLTF}`;

loader.load(
  resultingDataURI,
  (gltf: any) => {
    console.log('Loaded!!!');
    this.scene.add(gltf.scene)
  },
  () => {},
  (e: any) => console.error(e)
);
}}>Load</button>

So there is a thing named data URI. You can convert small files in that format(which is basically a base64 encoded string with some metadata at the beginning of it).

data:[<mediatype>][;base64],<data>

In my case [<mediatype>] is data:application/octet-stream.

So if you want to load small file from specific folder in you application, first of all rename your .gltf file into .json. And after importing and converting it into base64 code you can load it using GLTFLoader from three.js library.

Here is a code example:

import myGLTF from './MyPVPanel.json';
import {GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader";
import myGLTF from "./MyPVPanel.json";

const loader = new GLTFLoader();

const stringGLTF = JSON.stringify(myGLTF) // convert Object to a String
const base64EncodedGLTF = btoa(stringGLTF) // Base64 encode the String

//First part is: 'data:application/octet-stream;base64,'
const resultingDataURI = `data:application/octet-stream;base64,${base64EncodedGLTF}`;

loader.load(
  resultingDataURI,
  (gltf: any) => {
    console.log('Loaded!!!');
    this.scene.add(gltf.scene)
  },
  () => {},
  (e: any) => console.error(e)
);
}}>Load</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文