Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

HDRP scripting: HDRI Sky only shows applied cubemap after entering play mode

Discussion in 'Graphics Experimental Previews' started by WillianPGermano, Aug 14, 2019.

  1. WillianPGermano

    WillianPGermano

    Joined:
    Apr 12, 2018
    Posts:
    5
    Hello,

    I'm writing an editor script that bakes a cubemap from a reflection probe and assigns it to an HDRI Sky. It works well, but I have to manually enter play mode to see the cubemap. Then I can go back to editor mode and will still see the cubemap applied.

    I've not been able to find out how to have the cubemap visible without manually entering play mode, tried all I could find that might be relevant. This is what I have:

    Code (CSharp):
    1.     void SetSkyVolume()
    2.     {
    3.         GameObject parent = this.transform.gameObject;
    4.  
    5.         GameObject sceneSettings = CoreEditorUtils.CreateGameObject(parent, "HDRISky Scene Settings");
    6.         GameObjectUtility.SetParentAndAlign(sceneSettings, parent);
    7.  
    8.         VolumeProfile profile = VolumeProfileFactory.CreateVolumeProfile(sceneSettings.scene, "HDRISky Scene Settings");
    9.         VisualEnvironment visEnvComp = VolumeProfileFactory.CreateVolumeComponent<VisualEnvironment>(profile, true, false);
    10.         visEnvComp.skyType.value = SkySettings.GetUniqueID<HDRISky>();
    11.  
    12.         HDRISky hdriSkyComp = VolumeProfileFactory.CreateVolumeComponent<HDRISky>(profile, true, false);
    13.         hdriSkyComp.exposure.value = 0f;
    14.  
    15.         if (this.cubemap)
    16.         {
    17.             hdriSkyComp.hdriSky = new CubemapParameter(this.cubemap, true);
    18.         }
    19.  
    20.         Volume volume = sceneSettings.AddComponent<Volume>();
    21.         volume.isGlobal = true;
    22.         volume.sharedProfile = profile;
    23.     }
    I've tried with multiple Unity and HDRP versions, including the latest Unity beta and HDRP ones.

    Regards,

    Willian
     
  2. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789
    In your scene, there should be a volume settings or Render Settings game object
    What you are looking for is the Sky type and Visual Environment.

    Under Visual Environment change Sky Type to HDRI sky
    If there is a Procedural Sky or another sky type other than HDRI Sky. Disable it and add an HDRI Sky Component. Set it up and it will show in the scene and game view.

    upload_2019-8-13_21-40-14.png
     
    WillianPGermano likes this.
  3. remi_unity163

    remi_unity163

    Unity Technologies

    Joined:
    Jun 19, 2018
    Posts:
    16
    Hello,

    When modifying a volume parameter, instead of modifying its value, prefers the Override as it will also check the left override checkbox.
    So:
    visEnvComp.skyType.Override(SkySettings.GetUniqueID<HDRISky>()):
    hdriSkyComp.exposure.Override(0f);
    hdriSkyComp.hdriSky.Override(this.cubemap); //do not attempt to reconstruct the volume parameter

    Also, be sure you truly want to modify all profiles and not only the profile of the volume you are editing (profile instead of sharedProfile).

    Lastly, there is a priority on your volume that help solving overlapping volumes. In case of conflict, your modification can be not used.

    Hope this will help you in scripting on volumes :)
     
    WillianPGermano likes this.
  4. WillianPGermano

    WillianPGermano

    Joined:
    Apr 12, 2018
    Posts:
    5
    Thanks @Grimreaper358, that would indeed work from the interface, but I needed to do it all via scripting. @remi_unity163's answer solved it for me.
     
  5. WillianPGermano

    WillianPGermano

    Joined:
    Apr 12, 2018
    Posts:
    5
    Thanks a lot @remi_unity163,

    It's working now, following your suggestions. Tested with the latest Unity beta: version 2019.3.0a12.

    I only have one issue: using volume.profile (thanks for the tip, I missed the remark about it in the docs) the volume loses the profile link when entering play mode. For good, it's still not there after leaving play mode. When I reassigned it manually I saw the profile was missing the whole HDRISky override.

    Using volume.sharedProfile this problem doesn't happen.

    Regards,

     
  6. remi_unity163

    remi_unity163

    Unity Technologies

    Joined:
    Jun 19, 2018
    Posts:
    16
    Profiles are assets. You need to save it on disc if you want it being persistent when entering play mode.
     
  7. WillianPGermano

    WillianPGermano

    Joined:
    Apr 12, 2018
    Posts:
    5
    Newbie mistake... Thanks!