- Home /
Turn off shadows for a specific camera?
Hello,
I have two cameras in my scene. One is a top-down camera that is used to render a "radar" (2D) on the HUD. The other camera is the 3rd person gameplay camera that follows the player.
I also have a single directional light in my scene that generates shadows. For my top-down camera, I do not want shadows to be rendered. For the player camera I do want shadows to be rendered.
Is there a way to turn off shadows for a specific camera?
Answer by Robert 1 · Mar 17, 2010 at 01:29 PM
I ended up turning off shadows on the scene's lights. This worked perfectly. The solution provided by 'burnumd' does not really work for some reason. It causes strange rendering artifacts as well as makes the scene very dark as if no lights exist at all.
@Bob consider adding comments ins$$anonymous$$d of posting something as an answer.. this is not the usual forum like site. Read more on how this thing works: http://answers.unity3d.com/faq
I know how this works, because I use Stackoverflow as well. I'm literally answering my own question. There's nothing to comment on. I appreciate your kindness, however.
Your answer may be adequate for yourself, but I don't understand it at all. Do you mean you turned off shadows using OnPreRender, or what?
Quite literally I turned off shadows on the light in my scene. There's an option for this in the inspector when you select the light in the scene.
Answer by burnumd · Mar 15, 2010 at 07:06 PM
You can do what you describe using the Camera.OnPreRender () and Camera.OnPostRender () methods. So, you would attach a script to your minimap camera where, in OnPreRender, you store the current shadow distance in a class variable using QualitySettings.shadowDistance and set the shadow distance to 0. In OnPostRender, you simply restore the stored shadow distance.
var storedShadowDistance : float;
function OnPreRender () { storedShadowDistance = QualitySettings.shadowDistance; QualitySettings.shadowDistance = 0; }
function OnPostRender () { QualitySettings.shadowDistance = storedShadowDistance; }
I tried this, however shadows are still rendering for some reason. Any idea why?
It works fine for me. Are you, perhaps, referring to lighting effects rather than shadows?
Sorry I had it on the wrong camera, it works now. However, the scene is extremely dark on my radar now, as if no lighting is being calculated at all for the landscape. Does the directional light have no affect when shadowDistance is 0?
Answer by Noctys · Feb 26, 2015 at 09:38 PM
I know this is old, but wanted to throw in an example that works based on Burnumd answer.
public class ShadowToggler : MonoBehaviour
{
public Light[] SoftLights;
public Light[] HardLights;
void Start()
{
if (gameObject.GetComponent<Camera>() == null) { Debug.Log("No Camera Found"); }
}
void OnPreRender()
{
foreach (Light l in SoftLights) { l.shadows = LightShadows.None; }
foreach (Light l in HardLights) { l.shadows = LightShadows.None; }
}
void OnPostRender()
{
foreach (Light l in SoftLights) { l.shadows = LightShadows.Soft; }
foreach (Light l in HardLights) { l.shadows = LightShadows.Hard; }
}
}
There is probably a better way than having to add your lights to the arrays, but this is the simple method that allows for multiple lights and shadow types.
Answer by Cherno · Mar 19, 2015 at 07:35 AM
From my experiemnce, using PreCull and PostRender methods are extremely resource intensive. There is a far easier way to do it: Just pass the Camera a replacement shader, one which doesn't render shadows (Unlit/Texture, for example).
using UnityEngine;
using System.Collections;
public class MapCamera : MonoBehaviour {
public Shader unlitShader;
void Start() {
//unlitShader = Shader.Find("Unlit/Texture");
GetComponent<Camera>().SetReplacementShader(unlitShader,"");
}
}
Your answer
Follow this Question
Related Questions
Water light seems affected by camera angle 0 Answers
Hide a light for a camera 2 Answers
Problem with lights 0 Answers
I can see objects through other ones. 0 Answers
Special lighting for close-up camera 1 Answer