Search Unity

DrawRenderer with light

Discussion in 'General Graphics' started by Avol, Jun 26, 2019.

  1. Avol

    Avol

    Joined:
    May 27, 2016
    Posts:
    46
    How would I add directional light to my command buffer rendered meshes? Layers do not work as objects intersect and get mixed into single camera.


    Code (CSharp):
    1.  
    2. public class SingleObjectCamera : MonoBehaviour
    3. {
    4.     [SerializeField]    private        GameObject        _Target;
    5.  
    6.     private            CommandBuffer        _CommandBuffer;
    7.     private            Camera                _Camera;
    8.  
    9.     private            SkinnedMeshRenderer[]            _SkinnedRenderers;
    10.     private            MeshRenderer[]                        _Renderers;
    11.  
    12.     void Start()
    13.     {
    14.         _Camera        = GetComponent<Camera>();
    15.  
    16.         _CommandBuffer = new CommandBuffer();
    17.         _CommandBuffer.SetRenderTarget(_Camera.targetTexture);
    18.  
    19.         _Camera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _CommandBuffer);
    20.  
    21.         _SkinnedRenderers    = _Target.GetComponentsInChildren<SkinnedMeshRenderer>();
    22.         _Renderers            = _Target.GetComponentsInChildren<MeshRenderer>();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         _CommandBuffer.Clear();
    28.  
    29.         _CommandBuffer.SetGlobalColor("_CurrentLight_Color", Color.red);
    30.  
    31.         _CommandBuffer.SetGlobalVector("_CurrentLight_WorldSpacePosition", new Vector4(
    32.             Vector3.down.x,
    33.             Vector3.down.y,
    34.             Vector3.down.z,
    35.             0.0f));
    36.  
    37.  
    38.         for (int i = 0; i < _SkinnedRenderers.Length; i++)
    39.             _CommandBuffer.DrawRenderer(_SkinnedRenderers[i], _SkinnedRenderers[i].material);
    40.  
    41.         for (int i = 0; i < _Renderers.Length; i++)
    42.             _CommandBuffer.DrawRenderer(_Renderers[i], _Renderers[i].material);
    43.  
    44.         _Camera.Render();
    45.     }
    46. }
    47.  
     
unityunity