Every instances in InstancedMesh go to same position?

Hi all!
HeEeEEeEeeeEelp!!!
I’ve been struggling to move each of my instanced in my instancedMesh in different positions,
as the docs claims I can do?
(Use InstancedMesh if you have to render a large number of objects with the same geometry and material but with different world transformations.)

How the hell do I read and write those world transformations?

(and I do no mean InstancedBufferGeometry, but instancedMesh)

All I was able to do is rotate all of them at one to match a lookat, on creation, all instances in the instancedMesh move to the same position, and it does the same when I try to animate them…
I’m trying to avoid using
excerpts from my code follow.

     //create instances group
    instancedStars = new THREE.InstancedMesh( starRefGeo,starRefMat, 100, true);
    for ( var i ; i < 100 ; i ++ ) {
        x = Math.floor(Math.random()*10+1);
        y = Math.floor(Math.random()*10+1);
        z = Math.floor(Math.random()*10+1);
        instancedStars.setPositionAt(i , position3vector.set(x ,y, z));
    }
    scene.add( instancedStars );

// where they ALLmove to  the same randomized position (see at the bottom)????
function animateThings() {
    instanceLookAt.lookAt(camera.position);
    // rotate all instancedStars as lookAt
    var rotationAsQuaternion = new THREE.Vector4();
    var x = instanceLookAt.quaternion.x;
    var y = instanceLookAt.quaternion.y;
    var z = instanceLookAt.quaternion.z;
    var w = instanceLookAt.quaternion.w;
    rotationAsQuaternion.set( x, y, z, w ).normalize();
    instancedStars.quaternion.copy( rotationAsQuaternion );   
        for ( var i = 0; i < instancedStars.count; i++ ) {
        x = Math.floor(Math.random()*10+1);
        y = Math.floor(Math.random()*10+1);
        z = Math.floor(Math.random()*10+1);
        _position.set( x, y, z );
        instancedStars.position.copy( _position );
    } 
}

I just spent a whole night retrying TONS of examples - this is driving me crazy!!!

oh, and I’m using R113

Thanks for any help you can offer!
:wink:

This is not how it works since you just change the position of the entire instanced mesh over and over again. I suggest you do it like in this official example:

https://threejs.org/examples/webgl_instancing_dynamic

So the idea is to represent the transformation of an instance with a matrix and then set it via InstancedMesh.setMatrixAt().

2 Likes

Hi, thanks for trying to help!

yeah indeed, i can actually see that I’m just changing the position of the entire instanced mesh over and over again.

I cant GET any position x y z or vector3 from one specific instances of the instance group with this “instancedMesh.position.copy( _position );”

regarding InstancedMesh.setMatrixAt()

yes it is the way the doc says to do it.
it says use this to do this.
do it.
bye.

I havent actually found one single demo that GETS only ONE of the instances position and say, moves it aside or even better ONE example online that SETS only ONE of the instances position and say, moves it aside.

ill be the first to say, hey i cant make this work, so i must be wrong, but isnt SET to write and GET to read?

makes no sense to try to get with set…

after a full night at this failing again and again, i went back to the docs so many times i dont even recall if there is a
InstancedMesh.getMatrixAt() but i havent seen one example that reads and write ONE of the positions.

point me to an demo that demonstrate creation using instancedMesh, getting/reading/extracting ONE specific asset position after creation, and moving that SINGLE asset aside ONLY within the instance group and ill be able to understand.

one.

well, im not saying it doesnt exist, im askin for help…

about this specific demo, i’ve seen, read re-read tinkered and tried many things using this example as ref…
without success.

this example, as most the other example ive found, only rotates all the assets…or changes all the assets positions at once using the same math formula. it doesnt move ONE assets aside, and it doesnt go back to get then set ONE asset position any differently than the others…its always all at once

…i have read and went back many times the doc says i should be able to move them distinctively…

its infuriating, the only user forum post that actually seemed to adress specifically instancedMesh goes with the affirmation that “its easy to move one instance around, but what about the materials?” yet, big talks, but he doesnt actually show any example of that easy single item position change i supossedly easely did, and starts rambling about material shaders…

Try it with this updated example: Edit fiddle - JSFiddle - Code Playground

It only translate the first instance in the render loop.

6 Likes

OMG! THANK YOU SO MUCH lol

bright days ahead, back to it !

thanks

2 Likes

Here is another example https://codepen.io/Dx13/pen/vYGOeMb. I ran into the same issues. Know that its still not this easy. After you apply animation to say 5000 or so objects, animations will start to bog down the processor. You will see dips in performance with a large number of instances. The previous example, provided to you, only animates the instances that are moused over. You will need to overcome this with custom Tween Managers using “onBeforeRender()” if you plan to animate lots of objects at the same time. Another issue that Ive run into, that has made me rewrite the instancedMesh, is the contingency of instance.count to copying the same geometry inside the mesh wrapper. Unique geometries are also doable.