- Home /
Question by
efruchter-within · Dec 14, 2016 at 10:04 AM ·
renderingshadows
OnWillRenderObject + ShadowcastingMode not working as expected.
I have a setup where I have a 1st person camera and a third person camera. There is geometry that I want to be invisible in the first person camera, but visible in the third person camera. The geometry must still cast shadows in the first person view.
I have tried using OnWillRenderObject to see if the camera is the first-person one, and then switching shadowcast modes. This does not work as expected, because then the third person camera renders, the object becomes visible in the first person camera too. Here's the code below (I had to add the OnRenderObject portion to make it work, but it's not reliable, and shouldn't be needed):
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Renderer))]
public class ShadowcastOnly : MonoBehaviour
{
Renderer _renderer;
void Awake()
{
_renderer = GetComponent<Renderer>();
}
void OnWillRenderObject()
{
if (Camera.current == Camera.main)
{
_renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
}
else
{
_renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
}
}
void OnRenderObject()
{
_renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612102642im_/https://answers.unity.com/themes/thub/images/avi.jpg)