Performance Question About Rendering

Hello. Which of these yields the best performance? And what is the difference among them? Or is it better to just set the camera as the parent to my gunMesh?

this.scene.afterCameraRender = () => {

      this.self.primaryWeapon.gunMesh.position = this.camera.position;

      this.self.primaryWeapon.gunMesh.rotation = this.camera.rotation;

    };

    this.scene.onAfterRenderObservable.add(() => {

      this.self.primaryWeapon.gunMesh.position = this.camera.position;

      this.self.primaryWeapon.gunMesh.rotation = this.camera.rotation;

    });

    this.scene.afterRender = () => {

      this.self.primaryWeapon.gunMesh.position = this.camera.position;

      this.self.primaryWeapon.gunMesh.rotation = this.camera.rotation;

    };

I donā€™t think you need to update the position in the rendering loop or any of these hooks of the rendering loop.

You only need to set the position and rotation once. Then both camera and gun refer to the same instance of the position and rotation. When you update the camera pos and rot, the gun pos and rot would also be updated.

1 Like

How would I do that? The camera and the gun and the player are three separate meshes. Can I merge them together and then unmerge them? Aka player drops the gun mesh to pick up a new gun etc.

Is it FPS kind scenario? So your camera, player model, and gun are moving together.

When the human player gives inputs like move or rotate, you update all positions of camera, gun and player model together. They should have initial relative position to each other. Then you can do something like this for example:

moveLeft() => {
camera.position.x += delta;
gun.position.x += delta;
player.position.x += delta;
}

Yeah itā€™s an FPS. And yeah that makes sense. What about mouse movement and rotation? Iā€™m assuming I would have to have an event listener for ā€œon mouse moveā€ type of event and then update the rotation values or something like that

You may have a look at this example - https://playground.babylonjs.com/#NT4QZ8#7

may be just not ā€˜allā€™ and then,ā€˜all at onceā€™ if possible, in an FPS. Rather just the ones needed at t, and in order, please;) A simple beg from an FPS addict;) :grin:

1 Like