pmndrs / react-three-fiber

🇨🇭 A React renderer for Three.js
https://docs.pmnd.rs/react-three-fiber
MIT License
27.62k stars 1.6k forks source link

[useLoader/OBJLoader]: Load *.obj from Input String/Bytes #3342

Closed Ahmadre closed 2 months ago

Ahmadre commented 2 months ago

At first thank you so much for this awesome R3F ❤️

But I don't see any possibility to load my *.obj files via input bytes or string.

Let's assume you generate your OBJ-Files directly in React and it looks like this:

const myGeneratedOBJInputOnRuntime = 
# Version 1.0.0
# My Custom OBJ File
o Frame
v -110.758003 -64.459198 -30.410595
v -110.758003 -64.653900 -30.290594
v -110.758003 -64.217102 -30.427595
...
;

How can I pass this input string into useLoader or in general load my OBJ directly on runtime?

CodyJasonBennett commented 2 months ago

This isn't very specific to R3F, but most loaders implement a parse method which they call once they fetched a resource via their underlying FileLoader. It will either accept text or an arraybuffer -- OBJLoader expecting text. Alternatively, you could make your own loader and implement load to make async requests as well if you want special handling/preprocessing.

function Box() {
  const obj = useMemo(() => new OBJLoader().parse(text), [text])
  return <primitive object={obj} scale={10} />
}
Ahmadre commented 2 months ago

This isn't very specific to R3F, but most loaders implement a parse method which they call once they fetched a resource via their underlying FileLoader. It will either accept text or an arraybuffer -- OBJLoader expecting text. Alternatively, you could make your own loader and implement load to make async requests as well if you want special handling/preprocessing.


function Box() {

  const obj = useMemo(() => new OBJLoader().parse(text), [text])

  return <primitive object={obj} scale={10} />

}

@CodyJasonBennett Thank you so much! You helped me a lot 😊