tres icon indicating copy to clipboard operation
tres copied to clipboard

Impossible to `inject` variable in canvas child component

Open emavitta opened this issue 1 year ago • 5 comments

Describe the bug

I'm trying to inject a variable inside a component in a TresCanvas, but it's always undefined.

In the example i log the variable provided by the first component but in the SecondTresjs component, the variable is undefined

Am i doing something wrong? I cannot find anything in the docs about the context.

Reproduction

https://stackblitz.com/edit/vitejs-vite-gtrfxh?file=src%2Fcomponents%2FFirstTresjs.vue

Steps to reproduce

No response

System Info

No response

Used Package Manager

npm

Code of Conduct

emavitta avatar Jun 10 '24 10:06 emavitta

I think the problem lies with the custom Tresjs renderer. I think it loses the previous context from the default renderer.

The first time you inject it's actually outside the TresJS renderer thus still having access to the same context and there for the provided value. The second time you inject the context has changed and the dependency injection does not have any value registered for the key test.

Notice if you inject a value in SecondTresjs.vue and add a child like ThirdTresjs.vue the provided value is correctly injected like in this Example.

Take the explanation with a grain of salt as i'm not 100% sure about the specfics.

schplitt avatar Jun 10 '24 14:06 schplitt

Fwiw, if anyone would like to look into this, here's a slightly more minimal reproduction with some notes and a minimal on-screen test:

https://stackblitz.com/edit/vitejs-vite-5q8vs7?file=src%2FApp.vue

andretchen0 avatar Jun 10 '24 14:06 andretchen0

I think the problem lies with the custom Tresjs renderer. I think it loses the previous context from the default renderer.

The first time you inject it's actually outside the TresJS renderer thus still having access to the same context and there for the provided value. The second time you inject the context has changed and the dependency injection does not have any value registered for the key test.

Notice if you inject a value in SecondTresjs.vue and add a child like ThirdTresjs.vue the provided value is correctly injected like in this Example.

Take the explanation with a grain of salt as i'm not 100% sure about the specfics.

thanks for the solution and the explanation, even though i cannot understand why you cannot have more than one context

emavitta avatar Jun 11 '24 06:06 emavitta

think the problem is in this function

const createInternalComponent = (context: TresContext) =>
  defineComponent({
    setup() {
      const ctx = getCurrentInstance()?.appContext;
      if (ctx) {
        ctx.app = instance as App;
      }
      provide("useTres", context);
      provide("extend", extend);
      if (typeof window !== "undefined") {
        registerTresDevtools(ctx?.app, context);
      }
      return () => h(Fragment, null, slots?.default ? slots.default() : []);
    },
  });

It could be possible to add an additional params (context: TresContext,additionalContext) to the function called inside mountCustomRenderer that pass custom provided variables. It can be an array given to a new props on the canvas (additional-context=['key-one','key-two']) ; this way inside mountCustomRender we can inject these variables, pass them to createInternalComponent() and from here provided again white the same name.

emavitta avatar Jun 13 '24 09:06 emavitta

Hi,

Does anyone have a final answer on how to deal with injected values in a Tree Scene ?

Of course I can pass my variable through props but it can be really annoying.

Thanks :)

EvanSchleret avatar Jul 24 '24 12:07 EvanSchleret

Does anyone have a final answer on how to deal with injected values in a Tree Scene ?

Hi @EvanSchleret we are currently discussing it internally, I personally see value in it. Let's see if it is feasible.

alvarosabu avatar Aug 21 '24 10:08 alvarosabu