This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
Moving instancedMesh individual instances #103
Answered
by
IRobot1
Polyterative
asked this question in
Q&A
-
Following this question I was able to build a grid of objects. I'm unable to understand how to transform the individual objects like moving only 1 specific instance. I'm using
My code gap = 1
numberOfItems: number = 1024;
numberOfRows: number = Math.sqrt(this.numberOfItems);
numberOfColumns: number = Math.sqrt(this.numberOfItems);
data = new Array(this.numberOfItems).fill(0).map((d, index) => ({
position: new Vector3(Math.floor(index / this.numberOfRows) * this.gap, 0, (index % this.numberOfColumns) * this.gap - 15),
// scale: new Vector3(Math.random(), Math.random(), Math.random()),
scale: new Vector3(.1, .1, .1),
color: new Color().setHex(Math.random() * 0xffffff) //.setColorName('white')
}));
ready(inst: InstancedMesh) {
this.data.forEach((item, index) => {
const matrix = new Matrix4();
matrix.setPosition(item.position);
matrix.scale(item.scale);
inst.setMatrixAt(index, matrix);
inst.instanceMatrix.needsUpdate = true;
// inst.setColorAt(index, item.color);
})
}
onAnimationReady($event: { state: NgtRender; object: InstancedMesh }): void {
let { state, object } = $event;
console.log('animation ready', state, object);
// doesn't work
object.matrix.makeTranslation(1, 0, 0);
// trying to move the individual instances closer to the center 0,0,0, no effects
object.children.forEach((child, index) => {
let distanceFromCenter = child.position.distanceTo(new Vector3(0, 0, 0));
// move the individual instance closer to the center 0,0,0
child.position.add(new Vector3(-distanceFromCenter / 100, -distanceFromCenter / 100, -distanceFromCenter / 100));
});
this.changeDetector.markForCheck();
this.changeDetector.detectChanges();
} <ngt-instanced-mesh #inst="ngtInstancedMesh"
[args]="[data.length]"
(ready)="ready(inst.mesh)"
(animateReady)="onAnimationReady($any($event))"
>
<ngt-sphere-geometry></ngt-sphere-geometry>
<ngt-mesh-standard-material></ngt-mesh-standard-material>
</ngt-instanced-mesh> visual out: |
Beta Was this translation helpful? Give feedback.
Answered by
IRobot1
Apr 25, 2022
Replies: 1 comment 1 reply
-
You were close. I'll let you fix the movement logic. onAnimationReady($event: { state: NgtRender; object: Object3D }): void {
let object = <InstancedMesh>$event.object;
this.data.forEach((child, index) => {
let distanceFromCenter = child.position.distanceTo(new Vector3(0, 0, 0));
// move the individual instance closer to the center 0,0,0
child.position.add(new Vector3(-distanceFromCenter / 100, -distanceFromCenter / 100, -distanceFromCenter / 100));
const matrix = new Matrix4();
matrix.setPosition(child.position);
matrix.scale(child.scale);
object.setMatrixAt(index, matrix);
});
object.instanceMatrix.needsUpdate = true;
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Polyterative
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You were close. I'll let you fix the movement logic.