Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera - Cant Change Background Type or Color with script

Discussion in 'Scripting' started by dschmuck, Oct 24, 2019.

  1. dschmuck

    dschmuck

    Joined:
    Nov 27, 2017
    Posts:
    8
    I have been trying to get my camera's backround type and color be change using a script.
    I looked at the scirpting API but these lines simple don't have any effect on my camera.
    Does anybody have an idea why?


    Camera cam = Camera.main;
    cam.backgroundColor = Color.black;
    cam.clearFlags = CameraClearFlags.SolidColor;

    Also I haven't found any information on how to change the cameras's "volume layer mask" using a script. Does anybody know how to do that?

    I am using Unity 2019.3.07b and the HDRP.
    Glad for any help I can get!
     
    paladim likes this.
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Weird that the camera does not change color. You do have to set the Visual Environment volume override to None to see the traditional Camera background color. But now that you said it, I tried it and couldn't change the background color via code, even though I can see the value change in debug printout. I don't think cameras need any kind of apply command to make the value change?
     
    dschmuck likes this.
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You can set the "new stuff" with code too. You need to have proper components so that it's working.

    Add Gradient Sky override to your scene settings, and try following:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.Rendering.HDPipeline;
    5. using UnityEngine.Rendering;
    6.  
    7. public class ChangeCameraVolumeSettings : MonoBehaviour
    8. {
    9.     VisualEnvironment visualEnvironment;
    10.     GradientSky gradientSky;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Volume volume = gameObject.GetComponent<Volume>();
    16.         VisualEnvironment tempVis;
    17.         if (volume.profile.TryGet<VisualEnvironment>(out tempVis) )
    18.         {
    19.             visualEnvironment = tempVis;
    20.         }
    21.         GradientSky tempGradSky;
    22.         if (volume.profile.TryGet<GradientSky>(out tempGradSky) )
    23.         {
    24.             gradientSky = tempGradSky;
    25.         }
    26.  
    27.         // Set the sky type
    28.         visualEnvironment.skyType.value = (int)SkyType.Gradient;
    29.  
    30.         // Set the sky parameters
    31.         gradientSky.top.value = Color.red;
    32.         gradientSky.middle.value = Color.green;
    33.         gradientSky.bottom.value = Color.blue;
    34.         gradientSky.gradientDiffusion.value = 2.0f;
    35.     }
    36. }
    This will change your gradient sky settings. You can apply similar procedure to other overrides.

    Be sure that you include UnityEngine.Experimental.Rendering.HDPipeline and UnityEngine.Rendering.
     
    GAAC_Unity and dschmuck like this.
  4. dschmuck

    dschmuck

    Joined:
    Nov 27, 2017
    Posts:
    8
    Thanks so much @Olmi , I hadn't included HDRenderpipeline!
    Using HDRP to change the background color there is a new attribute called backgroundColorHDR, which you have to adjust in the HDAdditionalCameraData component.
    This is super confusing if you believe it is simply using the background color and clearflags...

    If anybody else faces the issue, these lines did the trick:
    cam.GetComponent<HDAdditionalCameraData>().clearColorMode = HDAdditionalCameraData.ClearColorMode.Color;
    cam.GetComponent<HDAdditionalCameraData>().backgroundColorHDR = Color.black;
    cam.GetComponent<HDAdditionalCameraData>().volumeLayerMask = 0;


    Thanks Olmi
     
  5. ismaeel_unity

    ismaeel_unity

    Joined:
    Oct 21, 2019
    Posts:
    12
    Thanks @dschmuck. Your solution works like a charm. Was looking exactly for this.
     
    Fiard and dschmuck like this.
  6. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    This is so stupid and messy, if they were going to make the API essentially broken, they should have had an HDRPCamera instead so you know which API actually works.
     
  7. Fiard

    Fiard

    Joined:
    May 15, 2013
    Posts:
    4
    Actually lost the whole hour on this as well ...
     
  8. OUTERDARKNESS

    OUTERDARKNESS

    Joined:
    Feb 27, 2013
    Posts:
    35
    Yep, 4 years later and this post is still saving lives. Lost a couple of hours before finding this weird undocumented fix.
    Thanks @dschmuck