Hi everyone.

I’m trying to duplicate lots of objects on a model (one object on each vertex of the model) with hardware instancing. is there a way to get the vertex number and each vertex position ?

…

// store matrices for individual instances into array

var matrices = new Float32Array(instanceCount * 16);

var matrix = new pc.Mat4();

var matrixIndex = 0;

for (var i = 0; i < instanceCount; i++) {

matrix.setTRS(pos, pc.Vec3.ZERO, pc.Vec3.ONE);

// copy matrix elements into array of floats

for (var m = 0; m < 16; m++)

matrices[matrixIndex++] = matrix.data[m];

}

…

Hi @Majid_Ahmady,

Where do you want to get the vertex number and position? In a shader or in your JS script?

1 Like

Mmm, since you are the one passing that information to the vertex buffer, you have it available in the matrices array, right?

Unless I don’t understand and you are trying to do something different like mouse picking an instance?

the problem is I don’t know coding. you know in the example for hardware instancing (here: PlayCanvas Examples) there is this part of the code :

…

const instanceCount = 1000;

```
// store matrices for individual instances into array
const matrices = new Float32Array(instanceCount * 16);
let matrixIndex = 0;
const radius = 5;
const pos = new pc.Vec3();
const rot = new pc.Quat();
const scl = new pc.Vec3();
const matrix = new pc.Mat4();
for (let i = 0; i < instanceCount; i++) {
// generate random positions / scales and rotations
pos.set(
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5
);
scl.set(
0.1 + Math.random() * 0.1,
0.1 + Math.random() * 0.3,
0.1 + Math.random() * 0.1
);
rot.setFromEulerAngles(i * 30, i * 50, i * 70);
matrix.setTRS(pos, rot, scl);
// copy matrix elements into array of floats
for (let m = 0; m < 16; m++)
matrices[matrixIndex++] = matrix.data[m];
}
```

…

that you choose the isntance count and the the position and… for each instance gonna be.

now what I need to know is how to get the amount of vertices and position of a mesh. so I can generate on instance on each one of those vertexes.

Ah right, I think I understand what you mean. The size of the array will always be your instances count multiplied by 16:

```
// let's say you have a list of entities
const instanceCount = entities.length;
// store matrices for individual instances into array
const matrices = new Float32Array(instanceCount * 16);
let matrixIndex = 0;
for (let i = 0; i < entities.length; i++) {
const entity = entities[i];
// we can easily extract the entity world transform
const matrix = entity.getWorldTransform();
for (let m = 0; m < 16; m++) matrices[matrixIndex++] = matrix.data[m];
}
// create static vertex buffer containing the matrices
const vertexBuffer = new pc.VertexBuffer(
app.graphicsDevice,
pc.VertexFormat.defaultInstancingFormat,
instanceCount,
pc.BUFFER_STATIC,
matrices
);
// initialize instancing using the vertex buffer on meshInstance of the created box
const meshInstance = box.render.meshInstances[0];
meshInstance.setInstancing(vertexBuffer);
```

1 Like

Thank you Leonidas. Well this kinda worked. but I think I need to to explain more. What I want is I have a Model that I want some Cones to scatter on it (a cone on each vertex). so now We have the vertex count of the model. but what we need is the world transform of each of the vertexes so we can put in the matrix. and again I don’t know how to do that

what I did with your code is this

…

```
var modelx = this.app.root.findByName("Model");
const instanceCount = modelx.model.meshInstances[0].mesh.vertexBuffer.getNumVertices();
// store matrices for individual instances into array
const matrices = new Float32Array(instanceCount * 16);
let matrixIndex = 0;
for (let i = 0; i < instanceCount; i++) {
const entity = entities[i];
// we can easily extract the entity world transform
const matrix = entity.getWorldTransform();
for (let m = 0; m < 16; m++) matrices[matrixIndex++] = matrix.data[m];
}
```

…

again thanks

Ah I see, good, you can use the Mesh API getPosition() method to get a list of the positions:

```
const positions = [];
modelx.model.meshInstances[0].mesh.getPositions(positions);
console.log(positions);
```

https://developer.playcanvas.com/api/pc.Mesh.html#getPositions

1 Like

Thank you very much Leonidas

1 Like