pmndrs/react-three-fiber

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

Ahmadre opened this issue · 2 comments

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?

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} />
}

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 😊