Instead of having two separate game objects with different layers and different meshes, is there a way to have only one game object but just replace the mesh for different camera? (similar idea to replacement shader but per object basis for mesh)
Nope.
public class Swapmesh : MonoBehaviour
{
public Mesh mesh0;
public Mesh mesh1;
public GameObject cam0;
public GameObject cam1;
private MeshFilter filterP;
// Start is called before the first frame update
void Awake()
{
filterP = GetComponent<MeshFilter>();
}
private void OnWillRenderObject()
{
if (Camera.current.gameObject == cam0)
filterP.sharedMesh = mesh0;
else if (Camera.current.gameObject == cam1)
filterP.sharedMesh = mesh1;
}
}
It’s kinda slow but it’s works.
Wanted to touch on a problem with this particular implementation. OnWillRenderObject() is called after culling, which means if your two meshes have significantly different bounds the object may not display when expected on the edges of the screen or when using occlusion. Basically culling will be done using the bounds of the mesh last set.
There are lots of ways to swap the mesh via script that’ll work, some probably more efficiently. Unless you’re rendering a lot of objects you want to be different between the cameras, using two game objects and layers will probably be faster. The other option would be to not use a mesh renderer component at all and use Graphics.DrawMesh or command buffers to draw your objects manually.