Rethink how the Tres core gets/creates/shares its state #578
Replies: 5 comments 6 replies
-
Suggestion: NodeOps functions should close over
|
Beta Was this translation helpful? Give feedback.
-
Investigation: allow a context to be supplied to
|
Beta Was this translation helpful? Give feedback.
-
Hey @andretchen0 I just addressed some of your concerns regarding the Multiple loops on the scene graph
@andretchen0 The thing is, ThreeJS renders all the scene graph at once when using This is certainly something I'm not eager to do because it adds a level of complexity and possible side effects that would make it difficult to maintain, but to consider it I would like to understand how the end user would benefit from having individual loops and if there are not end-user alternatives, for example Imagine a scene with 100 objects, all of them with event listeners to handle.
Wouldn't be easier to just not render the object with a v-if instead of using the individual onLoop to stop the rendering?
Let's continue the dicussion |
Beta Was this translation helpful? Give feedback.
-
Hi @andretchen0 thanks for the clarification, I think I understand better now what would be the use-case for updating instances. Additionally, it might be beneficial to touch upon how Three.js itself manages update and render loops, to provide further context. As TresJS serves as a custom renderer for Three.js within the Vue ecosystem. Three.js typically utilizes a single render loop, managed via To be fair But we don't force it's use, is just convenient as is a similar DX as ThreeJS and others custom rendered offer. But let lastTime = 0;
let accumulatedTime = 0;
const fixedUpdateTime = 1 / 60; // Fixed update time step (e.g., 60 times per second)
let speedModifier = 1.0; // Normal speed
function animate(now) {
requestAnimationFrame(animate);
const deltaTime = (now - lastTime) / 1000; // Convert to seconds
lastTime = now;
// Accumulate time and update at fixed intervals
accumulatedTime += deltaTime * speedModifier;
while (accumulatedTime >= fixedUpdateTime) {
update(fixedUpdateTime); // Update with fixed time step
accumulatedTime -= fixedUpdateTime;
}
updateObject(); // Update object
} I even think we do that on the devtools to render the fps and memory graphs. What I want to clarify is that is not possible to define a rendering loop per object, an updating loop would be another story.
Just to clarify tho, our primary scope is not to be a game framework/engine, we are a ThreeJS custom renderer for Vue. I'm pretty sure there would be more optimized options than TresJS to build games although I love seing games created with it. But since you mention the usecase of GUI and HUD, considering you are using Vue, wouldn't be more appealing to create the graphic UI interfaces with Vue and leave the gameplay to Tres? I mean, why creating a heart bar at the top with ThreeJS if you can easily do it with Vue?
Look at @Dekier great example: https://marcin-dekier.com/, the items HUD is just a vue component sharing a state with Tres. |
Beta Was this translation helpful? Give feedback.
-
I don't know if this help, but time ago a read https://gameprogrammingpatterns.com/ (recommended book). There's a special chapter about game loop and update() functions. As we don't own the loop (which its own by the browser the RAF API) getting mess with it could lead to bad practice and side effects that are hard to debug or understand. (they in the book talk in detail about this) I'm more in the @alvarosabu side we should not do a renderloop per object... if the users need to pause (or slow) some specific component for now I would say, it has to be done manually. Also, I don't believe we're a game engine, or we should act like one. More importantly for me is that, we don't have enough hands... I believe there are Other priorities to Tres (like other package) maybe in the future we could come back to think this issue better, or create a game engine utils pkg. |
Beta Was this translation helpful? Give feedback.
-
The way that the core gets/creates/shares its state has a few bugs that are difficult to address currently. In addition, there are some feature requests that could be productively impacted with a few good decisions related to state.
I'd like to get some ideas and feedback from the community.
(Below take "instance" to mean "an instance of
<TresCanvas />
".)Open issues
Shared state
The current version of the core has open issues related to state shared across instances:
pause
immediately afteronLoop
doesn't do anything #251 – perhaps relateduseRenderLoop
per page share state #252extend
example throws error #565 –extend
shares the catalogue across instances.useRenderLoop().onLoop
ticks before the renderer can meaningfully render #595Incomplete
context
Sometimes
context
doesn't have all the expected parts.<OrbitControls />
#418Feature requests
There are also nice-to-have feature requests that could potentially be solved by inverting dependencies and passing a
context
to<TresCanvas />
.And from the wishlist
DX
There's also a DX issue that could be impacted:
<script setup>
is executed before the<template>
. But in a file with<TresCanvas />
,<template>
is currently where thecontext
is created. So, users who want to usecontext
in<script setup>
have to wait untilonMounted
or use aref
depending on their needs.Pokes
@alvarosabu @Tinoooo @JaimeTorrealba @userquin
Beta Was this translation helpful? Give feedback.
All reactions