Skip to content
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

Property 'setMaterials' does not exist on type 'Loader' when using useLoader extensions #2579

Closed
charlesinwald opened this issue Oct 18, 2022 · 3 comments · Fixed by #2598
Closed
Labels
bug Something isn't working core target agnostic (normally to do with reconciler) Typescript issues to do with TS

Comments

@charlesinwald
Copy link

I am trying to set the materials of an object, using react-three-fiber's useLoader. However on the callback function of OBJLoader, setMaterials is not defined

  const materials = useLoader(
    MTLLoader,
    process.env.PUBLIC_URL + "/texturedMesh.mtl"
  );
  const object = useLoader(
    OBJLoader,
    process.env.PUBLIC_URL + "/texturedMesh.obj",
    (loader) => {
      materials.preload();
      loader.setMaterials(materials);
    }
  );
TS2339: Property 'setMaterials' does not exist on type 'Loader'.
    23 |     (loader) => {
    24 |       materials.preload();
  > 25 |       loader.setMaterials(materials);
       |              ^^^^^^^^^^^^
    26 |     }
    27 |   );
    28 |   // );

The contents of loader are:

OBJLoader {manager: LoadingManager, crossOrigin: 'anonymous', withCredentials: false, path: '', resourcePath: '', …}
crossOrigin: "anonymous"
manager: LoadingManager
    addHandler: ƒ (regex, loader)
    getHandler: ƒ (file)
    itemEnd: ƒ (url)
    itemError: ƒ (url)
    itemStart: ƒ (url)
    onError: item => {…}
    onLoad: () => { set({ active: false }); }
    onProgress: (item, loaded, total) => {…}
    onStart: (item, loaded, total) => {…}
    removeHandler: ƒ (regex)
    resolveURL: ƒ (url)
    setURLModifier: ƒ (transform)
    [[Prototype]]: Object
materials: null
path: ""
requestHeader: {}
resourcePath: ""
withCredentials: false
[[Prototype]]: Loader
@CodyJasonBennett
Copy link
Member

Is this merely a type error or does this also produce a runtime (JS) error?

@charlesinwald
Copy link
Author

If I disable the type error as below, I don't seem to get any error at runtime, however I'm not seeing the materials applied.

const object = useLoader(
    OBJLoader,
    process.env.PUBLIC_URL + "/texturedMesh.obj",
    (loader) => {
      materials.preload();
      console.log("loader", loader);
      // ignore property does not exist on type 'OBJLoader'
      // @ts-ignore // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      loader.setMaterials(materials);
    }
  ); 

@charlesinwald
Copy link
Author

By updating the code to the following:

  const object = useLoader(
    OBJLoader,
    process.env.PUBLIC_URL + "/texturedMesh.obj",
    (loader) => {
      materials.preload();
      console.log("loader", loader);
      if (loader instanceof OBJLoader) {
        console.log("instanceof OBJLoader");
        loader.setMaterials(materials);
        console.log("after setMaterials");
      }
    }
  );

I am able to setMaterials without error, however I don't actually see the materials applied. The materials property is no longer null, it is now:

materials: MaterialCreator
baseUrl: "/"
crossOrigin: "anonymous"
manager: LoadingManager {onStart: ƒ, onLoad: ƒ, onProgress: ƒ, onError: ƒ, itemStart: ƒ, …}
materials: {TextureAtlas_1001: MeshPhongMaterial, TextureAtlas_1002: MeshPhongMaterial, TextureAtlas_1003: MeshPhongMaterial, TextureAtlas_1004: MeshPhongMaterial, TextureAtlas_1005: MeshPhongMaterial}
materialsArray: []
materialsInfo: {TextureAtlas_1001: {…}, TextureAtlas_1002: {…}, TextureAtlas_1003: {…}, TextureAtlas_1004: {…}, TextureAtlas_1005: {…}}
nameLookup: {}
options: {}
side: 0
wrap: 1000
[[Prototype]]: Object

Should materialsArray be empty? This should be referencing the MTL file and the five texture pngs in the public folder of my project.

@CodyJasonBennett CodyJasonBennett added bug Something isn't working Typescript issues to do with TS core target agnostic (normally to do with reconciler) labels Oct 28, 2022
@CodyJasonBennett CodyJasonBennett changed the title setMaterials is not defined Property 'setMaterials' does not exist on type 'Loader' when using useLoader extensions Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core target agnostic (normally to do with reconciler) Typescript issues to do with TS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants