0

In my scene I have 2 camera(split screen). it's possible change the trasparency of a layer only for one camera? for examples the "layer1" have alphatrasparency = 0.5 for right camera while left camera show "layer1" without trasparency.

SGiux
  • 619
  • 3
  • 10
  • 34

1 Answers1

0

Ostensibly no

There's a way to do it, though. It's a bit of a hack though, as it doesn't depend on physics layers, but rather the presence of a custom monobehavior script. Here's what I remember about this off the top of my head (I can dig up an implementation later, if needed).

Step 1: you will need a MonoBehaviour script attached to every game object you want to have rendered differently. This script is absolutely essential.

Step 2: this script will contain one function (you can use an existing behaviour script if all the objects already have one in common and you can just add this function to that script). Call it whatever you want, but something like AboutToBeRendered(Camera cam)

Step 3: create another script and attach it to both cameras. This script will also have one function in it: OnPreRender()

Step 4: In this OnPreRender method you will need to do:

  • find all game objects from Step 1
  • get their component with the AboutToBeRendered method
  • invoke that method, passing the camera as the paramter

Step 5: Writing the AboutToBeRendered method.

  • determinie which of the two cameras was passed to the method
  • set the material's color to be transparent or not, as needed

OnPreRender() is only called on scripts with a camera component on the same GameObject, indicating that this camera is about to render the scene. But what we actually want is for the object about to be rendered to know that it's about to be rendered and by which camera. Which is why we need the script in step 1.

I suppose you could skip step 1 and only look at every object in the scene and look at its physics layer, but that's going to be more expensive to figure out than "get me all instances of this component." You could do it based on Tag instead, as FindObjectsWithTag is generally considered to be pretty fast, but if you're already using the tag for something else, you're out of luck, and there's no comparable method for getting objects in a given physics layer.

However, you'd have to tweak the material's alpha value in the camera script rather than letting the object itself decide what value it should be.

In either case, the object's material would need to support transparency. When I did this I was preventing the object from being rendered entirely, so I just disabled its MeshRenderer component.

Community
  • 1
  • 1
  • Ok but how I can do this? I create a render texture and I put in a camera and then? I'm new of Unity. For layer I mean only the objects that belong to that layer . – SGiux Jun 20 '17 at 17:54
  • Oh, you meant something else entirely. egads. My answer is going to still start with "ostensibly no" but end with a paragraph on how to get around it. Buuuut....doing it is a *huge pain* and requires at least one hack. I did it once about 2 years ago but I'm on the wrong computer to pull up the code. – Draco18s no longer trusts SE Jun 20 '17 at 17:59
  • @Chuck94 updated the answer, its what I can remember of having done something similar. – Draco18s no longer trusts SE Jun 20 '17 at 18:19
  • Ok. For step 1, actually the objects that I want transparency only from one camera they are created dynamically. So I can not attached a script – SGiux Jun 21 '17 at 09:55
  • @Chuck94 sure you can, `GameObject ob = Instantiate(...); ob.AddComponent()` and even if you couldn't, I still talk about how you might be able to avoid having that script. – Draco18s no longer trusts SE Jun 21 '17 at 13:31
  • Thank you for your patience. This is what I need for my thesis. I greatly appreciated your help. – SGiux Jun 22 '17 at 12:09