- Home /
Have cameras render using different light sources
I have two cameras in my game: MainCamera and Minimap. I want MainCamera to render lighting using Light1 and Minimap to render lighting using Light2. How do I go about doing this?
Answer by Cherno · Aug 14, 2015 at 06:20 PM
Mirrored here: MiniMap Camera goes dark when sun goes "down"
The hacky way to do this is to use the OnPreRender and OnPostRender methods in a script that is attached to the cameras. In these methods, you have to activate and deactivate all the lights that the camera should render/not render.
Note that this is not recommended as it will probably have a severe impact on your FPS.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ExcludeLight : MonoBehaviour {
public List<Light> Lights;
public bool cullLights = true;
void OnPreCull(){
if(cullLights == true) {
foreach (Light light in Lights){
light.enabled = false;
}
}
}
void OnPostRender(){
if(cullLights == true) {
foreach (Light light in Lights){
light.enabled = true;
}
}
}
}
If the issue is a minimap being rendered as it is affected by scene light (you probably want it to be unlit, with no shadows), then it's a lot more complicated. You have to use RenderToTexture to render the minimap camera's scene, apply a Replacement Shader which makes the camera use a shader that renders everything unlit, and display the rendertexture somehow (via OnGUI or UI.Image).
Here are my scripts
This is the one that goes onto the map camera. Drag the Unlit shader into the unlitShader slot in the inspector, or un-comment the commented line.
using UnityEngine;
using System.Collections;
public class MapCamera : MonoBehaviour {
public Shader unlitShader;
void Start() {
//unlitShader = Shader.Find("Unlit/Texture");
GetComponent<Camera>().SetReplacementShader(unlitShader,"");
}
}
This script can go to any GameObject I guess, I had it on my player object. the material variable can hold a masked material, where the mask makes the minimap texture round, for example. Leave the renderTexture variable alone, it gets assigned automatically. the variable mapTextureSzite obviously controls the size of your minimap in pixels.
using UnityEngine;
using System.Collections;
public class MiniMap : MonoBehaviour {
public RenderTexture renderTexture;
public Material material;
public FilterMode filterMode = FilterMode.Point;
public Camera miniMapCamera;
public Vector2 mapTextureSize = new Vector2(256,256);
void Start() {
renderTexture = new RenderTexture(mapTextureSize.x,mapTextureSize.y,16);
miniMapCamera.targetTexture = renderTexture;
renderTexture.filterMode = filterMode;
}
void OnGUI() {
GUI.depth = 4;
if(miniMapCamera.enabled == true) {
if(Event.current.type == EventType.Repaint) {
Graphics.DrawTexture(new Rect(Screen.width - mapTextureSize.x, Screen.height - mapTextureSize.y, mapTextureSize.x, mapTextureSize.y), renderTexture, material);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611235909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Improving the look of Unique/User Planned Scenes 0 Answers
3D model not affected by darkness? 2 Answers
HUD animation with game in background 0 Answers
is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers
How can i make the camera look up and down through keyboard keys? 2 Answers