Add Mesh instances

How do I add multiple mesh instances to a scene?
Got Error: GraphNode is already parented

Hi @iso74,

What are you trying to do?

To create a new mesh instance you need the following things:

  • A mesh
  • A graph node
  • A material

Check how the terrain generation example does it:

var meshInstance = new pc.MeshInstance(node, mesh, material);

Then that mesh instance you can either add it to a model or add it directly to a layer for rendering.

I guess I dont know how to add them (25) to a layer…

const node = new pc.GraphNode();
      for (let p = 0; p < 25; p++) {
        let cloud = new pc.MeshInstance(node, cloudGeo, material);
Must each instance have a node or can I have it like this?

No each mesh instance requires a graph node, I think you can reuse the same graph node (not 100% sure) but it’s best practice to create a new one.

To add them to a layer you can use the following method directly on a reference of a pc.Layer. All layers are available on the layer composition under scene:

var worldLayer ='World');

1 Like

The examples show to have a graph node for each mesh instance and add all the meshinstances to a model. The model then can be added to model component on an entity.


1 Like

@Leonidas one question, how do I make the meshes visible, how can I point the camera to the world layer?

Hmm, not sure I follow. If your active camera contains in its layer list the World layer (which by default in editor project is the case) then all mesh instances / model components that are rendering to that layer will be visible by default.

I mean if you rotate your camera to the direction of that object and it’s close enough :slight_smile:

How do I actually rotate a mesh instance?
I thought it would be:
myMeshInstance.node.rotation.x = 1.20 like this…

All pc.GraphNode instances share the same translations methods with the pc.Entity class, actually the pc.GraphNode class is the base class from which the pc.Entity class is inhertied.

So you can use the common methods you are used to:

myMeshInstance.node.setEulerAngles(0, 90, 0);

1 Like