-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
TresInstancedMesh breaks with a dynamic instance count #746
Comments
Also to be clear, I don't believe this is an issue with threejs as there is an official example showing dynamically changing instance counts on the same InstancedMesh working no problem: https://threejs.org/examples/?q=inst#webgl_instancing_dynamic |
Hey @alvarosabu, is someone working on this? I can Investigate and work on this |
Hey buddy @Neosoulink, not yet, but it would be great if you could help us with it. |
@darkvertex it's important you understand that the As mugen87 mentionned it here: https://discourse.threejs.org/t/modified-three-instancedmesh-dynamically-instancecount/18124/10
From the Doc InstancedMesh.count
With that said, why are you getting the error:
I'm unsure, but the error is triggered after the Now let's see alternatives, Here I've created an alternative solution to increase const material = new MeshNormalMaterial();
const count = shallowRef(1);
const instancedMesh = shallowRef();
const createPrimitiveInstance = () =>
h('primitive', {
ref: instancedMesh,
object: new InstancedMesh(susane, material, count.value ?? 1),
});
const InstancedComponent = shallowRef(() => createPrimitiveInstance());
const resetInstancedComp = () => {
instancedMesh.value?.dispose();
InstancedComponent.value = () => createPrimitiveInstance();
};
onLoop(({ elapsed }) => {
// codes ...
});
onMounted(() => {
setInterval(() => {
if (count.value < 100) {
count.value++;
resetInstancedComp();
console.log('instancedMesh updated', count.value);
}
}, 3000);
});
//////////
<template>
<TresCanvas v-bind="stateCanvas">
<InstancedComponent />
</TresCanvas>
</template> The above code will add each 3 sec a new instance to the scene. @alvarosabu, I don't think it's related to Error coming from |
Thanks for taking a look, @Neosoulink! You are indeed right that "count" cannot go higher than the value fed to the constructor. I missed that in the docs, my bad. -- It turns out that the "count" property is editable however, so as long it is a lower or equal value than what was given to the constructor. With this corrected knowledge, I forked my bug repro stackblitz and got it working given the "count" prop never exceeds the value of the constructor. I give it a higher number (10) at consturction and track the desired count separate from the max (initial) count, and ensure my desired count does not exceed the initial. This works reasonably well now: Now, what if the stuff fed to ":args=..." (constructor) changes? Would it be a crazy expectation that if constructor (":args=...") change because the given props changed upstream, the old threejs InstancedMesh object ought to be cleanly disposed of and a new one recreated in its place? If you hold the Shift key while clicking on the monkey in the middle in my example above, it will attempt to set the "maxCount" to a higher number (1000 instead of 10), which was passed to the ":args", so I would have thought TresJS would handle re-syncing, but instead you will see this error below: I can kinda work around my issues by setting a very high initial count, but it would be nice if TresJS could autorecreate the InstancedMesh should the constructor's reactive values change. Thoughts? |
@darkvertex, nice to know you partially solved the issue! But this issue is more about ThreeJs itself, If you want to increase the max-count, you have to recreate a new See my |
@Neosoulink Ah, I get what you did now. Okay. With your approach of having a swapable "component", do you know how could I get |
Hi @darkvertex the event issue with dynamic components should be fixed with #763, hasn't being released yet. Probably today |
@alvarosabu Oh, que bueno! ❤️ Will I need to do any code gymnastics to make the raycast click stuff detect the instanceid under the click, given that this is a dynamic component? Or does TresJS figure it out by the threejs object type? |
I don't have a bug repro stackblitz for you at this time, but I wanted to share some oddities: I tried to update my real app code to 4.2.0 and it goes into an infinite loop of recreating the InstancedMesh, whereas 4.0.2 works fine but had no working click events in my component. The trick of using a function to return a primitive seems to be the culprit somehow, as when I change directly to having a When I test with static data it seems I have click events with it, but when I have the whole thing hooked up, my instances are updating nicely, and on-demand rendering is working nicely too, but I cannot get any click events to fire at all... I'm stumped. 🤔 Is there a special debug verbosity flag or something I can enable to get more visibility on what's happening inside Tres? |
Hi @darkvertex have you tried checking the scene graph on the vue devtools? |
Ok, I think I found my issue with click events not firing. Calling Curiously, doing |
When I tested just manually setting two instances at the root of the component without anything dynamic, click events worked without having to recompute any bounding volumes. It's as if the bounding spheres are computed the very very first render and then that's it. |
Describe the bug
I took a stackblitz from @alvarosabu demonstrating TresJS with an InstancedMesh with a bunch of ondulating Suzanne monkey heads, and then I edited the code so it starts with a ref() count of 3, and on each
@click
of the InstancedMesh, it is SUPPOSED to increment the count by 1 but then breaks.I am developing something with a realtime floor map showing people markers, and the number of people changes dynamically as they enter the room, potentially every few seconds. Everything worked great with static count values but breaks when it changes dynamically. :/
It errors like this:
(and no, I'm not setting .position on the InstancedMesh, so not sure what that's about.)
Reproduction
https://stackblitz.com/edit/tresjs-instanced-mesh-fklgmc?file=src%2Fcomponents%2FTheExperience.vue
Steps to reproduce
System Info
The text was updated successfully, but these errors were encountered: