Tresjs / tres

Declarative ThreeJS using Vue Components

Home Page:https://tresjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use of functions in the template instances?

JaimeTorrealba opened this issue · comments

Description

What is the right way to use some of the in-built funcions for the template instance?

For example, to add layers or other methods like getObjectId, add, etc?

Now, if I try this:
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="simpleLog"> Doesn't work, or even execute my function

Same with layers
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :layers-enable="5">

Suggested solution

Accept the inline methods

Alternative

No response

Additional context

I know I can do this by template ref, but for many cases will be useful and less code, if we could access that methods inline.

What do you think?

Validations

Hey @JaimeTorrealba !

What do you think?

I think what you're proposing could be done behind the scenes. E.g., use a watcher to watch this prop and if it changes and if the value is a function, then call the function.

But for me, there's a conceptual mismatch with that pattern.

Vue templates, along with HTML/XML/etc. are declarative by design. They just describe. They aren't imperative and they don't have side effects in and of themselves.

If simpleLog were immediately called here ...

<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="simpleLog">

... that would at least look imperative and like it's producing side effects.

For me, that's not the expected behavior. A DOM equivalent to your example would be:

<div onclick="simpleLog">

Like your example currently, just putting that in an HTML document doesn't execute simpleLog. It just describes: "the onclick handler should be equal to simpleLog". It's up to the executing environment to execute the function when the user clicks or when the programmer triggers the click with JS.

unmm, yes I know we can do that using template ref, But I'm not entire agree with your answer :). Or maybe I don't understand you

maybe "simpleLog" was not a good example lets call it "callback"

The way I see it we don't have side effects, ThreeJs comes with a lot of methods like clone, getObjectById, getWorldPosition, etc
Same way, the DOM has some methods in two. (I don't see how the onClick is equivalent to getObjectById)

I find, for example, a little annoying, to have to create a template ref for something like adding my mesh to a different layer.

https://play.tresjs.org/#eNqVVW1P2zoU/itW0FW5uiFJx+BuGUwd3TRt0l4EfFv2wU1OE4NjW7bTUqH+9x3bNE3QXhiiUn3Oc57zfnofXWswb5RKVh1EeXRmSs2UJQZspwinoj4vImuK6HUhWKuktuSemIZyLteXsIzJmtqyIVuy1LIlEySZDIAX1LDyqqGVXH+iKiZXl+8v5pJLfaVoCTH5LK+lAFQpJuqexDYaYPJqwONinFOxoqYHzSzKbkxaSj3GftELZudSWC35T+AMhJXGWRSilMJYUnNyTu4LQUjJgWofX04mBy+evb2Yn0xip8GMMQeTE6s78BLKVUNzsqTcBEGAYC7XGwX549QdQnZWdRjaLv/8cT0cyO4Lko/rg+rtIOx28wlMg6Hvu3H4r1P7jhwGdUxWlHdAzl+HDP0r4XQD2iQg6ILD4TO0wtyRVHLUyfrQo1C6xc9ZGiYC+48PC63i1AK+CDkbtGV1tGCiwlmpuZ8Vgn9e/xU9KSgtW8GctqApyZU0zDIpEP3tZUzc//ciIunObNzBXuzYfMoalmgaEuyd7cLBer0H2YLVG5JTXRvnZRqTaXISk+ORn3THGLLxz5AOCs7SQa74NHbD3dfGthw7sZDVJpS0RSdM5CTD3hCiaFX51oVnA6xubE6mWfaPF6xZZZv+vS3EATY3EP0BS8iClre1lp2ojsowpAdZ5vwgDbbJBxjFUdiEo5aq5MZIgUvt6YsHBe5yHhw62XCNnKKIGmuVydO0rATaV8DZSicCbCpUmw7hs+PkNJmmFTM2dWJEF5Gf4RFxWLi/4Q4WSP9/ku3pH8RjL3YN9FZR8ZTYe+zseZL1ge+EScuczZDbnaGn8DrcDBlPM4z3PzDtsAx4ETsDT67vAD6bZskJMuJewV3Sjqv7gMPd11D9BXEw+B01Ao8qaNkTSHdQl/zz5DTlbDGkdIw4mlucSWvwvixZ/WgiS9kqxkF/Ue4ejCfTH7WPXtYfXW/TQHn7E/mNuQshf8VhAb3Ccu9biTsKNqjfXX2GO/zeK1tZdfyhOb9QXgLexs7FGGAXuIMY9gDno/3gFwzX/9q8u7MgzC6pUaAWfx+89NvucvWzFgTffeE83rdj/psq7TM7To77gm9/AFxrnho=

in fact that doesn't work, maybe for #230

Why don't open the possibilty to have something like

<script setup>
const myCallback = e => { // e here will be the actual function getObjectId, then I can do as I like for example
const myInnerMesh = e(''myInnerObjectId")
}
</script>
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="myCallback">

Same with others methods like clone or add?

Or maybe I don't understand you

No problem. Maybe my answer wasn't clear. ;)

Do I understand this right?

<script setup>
const myCallback = e => { 
  // e here will be the actual function getObjectId, then I can do as I like for example
  const myInnerMesh = e("myInnerObjectId")
}
</script>
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="myCallback">
  • :field="value" in a template normally means "bind value to field" (declarative)
  • here, you're using it to mean, "call value and send it the actual value of field" (imperative)

So any other alternative to this issue?
more directives?

So any other alternative to this issue?

Afaik, the "blessed" way to do this in Vue is what you're already doing:

  • <script> for interactivity and <template> for declaring bindings.
  • ref="..." for moving from <template> to <script>.

Fwiw, I also get tired of typing out shallowRefs, etc. But I find that reading random people's Vue SFC code is a lot easier than reading random people's straight JS code.

So I think there's good stuff in the "blessed" way of doing things. Even if it takes longer to write.

Hey, @JaimeTorrealba thank you for opening this discussion, I find it really valuable and interesting.

Vue templates, along with HTML/XML/etc. are declarative by design. They just describe. They aren't imperative and they don't have side effects in and of themselves.

That would at least look imperative and like it's producing side effects.

I agree with @andretchen0 on this, passing a function through a prop is an antipattern in Vue, and since the goal of the library is to be a declarative solution using vue renderer this approach would kinda break that statement.

In Vue you pass data down you emit events up. React does allow pass functions down as prop by design.

One alternative would be to dynamically enable emits for the methods but this is done on the runtime and not in the custom renderer, or as we initially thought, to detect if the instance[prop] is a function and then pass the value as args of that function, but I guess @JaimeTorrealba for some cases you need the returned value right?

If the last question answer is yes, as @andretchen0 suggested, using refs is the correct way forward. We could try emits or directives, but then you will have to create an individual directive for each function in the ecosystem of three.

Hey, thanks for taking the time, to read and response @alvarosabu, @andretchen0.

I'll definitely create some directives to address some of these methods (for example, v-layer, of course I'll open an issue first) but now I understand better the situation :)