Ejemplo n.º 1
0
        public void UpdateEnvironment(HDCamera camera, Light sunLight, CommandBuffer cmd)
        {
            // We need one frame delay for this update to work since DynamicGI.UpdateEnvironment is executed directly but the renderloop is not (so we need to wait for the sky texture to be rendered first)
            if (m_NeedLowLevelUpdateEnvironment)
            {
                using (new Utilities.ProfilingSample("DynamicGI.UpdateEnvironment", cmd))
                {
                    // TODO: Properly send the cubemap to Enlighten. Currently workaround is to set the cubemap in a Skybox/cubemap material
                    m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
                    RenderSettings.skybox              = m_StandardSkyboxMaterial;                 // Setup this material as the default to be use in RenderSettings
                    RenderSettings.ambientIntensity    = 1.0f;                                     // fix this to 1, this parameter should not exist!
                    RenderSettings.ambientMode         = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
                    RenderSettings.reflectionIntensity = 1.0f;
                    RenderSettings.customReflection    = null;
                    DynamicGI.UpdateEnvironment();

                    m_NeedLowLevelUpdateEnvironment = false;
                }
            }

            if (IsSkyValid())
            {
                m_CurrentUpdateTime += Time.deltaTime;

                m_BuiltinParameters.commandBuffer = cmd;
                m_BuiltinParameters.sunLight      = sunLight;

                if (
                    m_UpdatedFramesRequired > 0 ||
                    (skySettings.updateMode == EnvironementUpdateMode.OnChanged && skySettings.GetHash() != m_SkyParametersHash) ||
                    (skySettings.updateMode == EnvironementUpdateMode.Realtime && m_CurrentUpdateTime > skySettings.updatePeriod)
                    )
                {
                    using (new Utilities.ProfilingSample("Sky Environment Pass", cmd))
                    {
                        using (new Utilities.ProfilingSample("Update Env: Generate Lighting Cubemap", cmd))
                        {
                            // Render sky into a cubemap - doesn't happen every frame, can be controlled
                            // Note that m_SkyboxCubemapRT is created with auto-generate mipmap, it mean that here we have also our mipmap correctly box filtered for importance sampling.
                            if (m_SkySettings.lightingOverride == null)
                            {
                                RenderSkyToCubemap(m_BuiltinParameters, skySettings, m_SkyboxCubemapRT);
                            }
                            // In case the user overrides the lighting, we already have a cubemap ready but we need to blit it anyway for potential resize and so that we can generate proper mipmaps for enlighten.
                            else
                            {
                                BlitCubemap(cmd, m_SkySettings.lightingOverride, m_SkyboxCubemapRT);
                            }
                        }

                        // Convolve downsampled cubemap
                        RenderCubemapGGXConvolution(cmd, m_BuiltinParameters, skySettings, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);

                        m_NeedLowLevelUpdateEnvironment = true;
                        m_UpdatedFramesRequired--;
                        m_SkyParametersHash = skySettings.GetHash();
                        m_CurrentUpdateTime = 0.0f;
                        #if UNITY_EDITOR
                        // In the editor when we change the sky we want to make the GI dirty so when baking again the new sky is taken into account.
                        // Changing the hash of the rendertarget allow to say that GI is dirty
                        m_SkyboxCubemapRT.imageContentsHash = new Hash128((uint)skySettings.GetHash(), 0, 0, 0);
                        #endif
                    }
                }
            }
            else
            {
                if (m_SkyParametersHash != 0)
                {
                    using (new Utilities.ProfilingSample("Reset Sky Environment", cmd))
                    {
                        // Clear temp cubemap and redo GGX from black and then feed it to enlighten for default light probe.
                        Utilities.ClearCubemap(cmd, m_SkyboxCubemapRT, Color.black);
                        RenderCubemapGGXConvolution(cmd, m_BuiltinParameters, skySettings, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);

                        m_SkyParametersHash             = 0;
                        m_NeedLowLevelUpdateEnvironment = true;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdateEnvironment(HDCamera camera, Light sunLight, ScriptableRenderContext renderContext)
        {
            using (new Utilities.ProfilingSample("Sky Environment Pass", renderContext))
            {
                // We need one frame delay for this update to work since DynamicGI.UpdateEnvironment is executed direclty but the renderloop is not (so we need to wait for the sky texture to be rendered first)
                if (m_NeedLowLevelUpdateEnvironment)
                {
                    // TODO: Properly send the cubemap to Enlighten. Currently workaround is to set the cubemap in a Skybox/cubemap material
                    m_StandardSkyboxMaterial.SetTexture("_Tex", m_SkyboxCubemapRT);
                    RenderSettings.skybox              = m_StandardSkyboxMaterial;                 // Setup this material as the default to be use in RenderSettings
                    RenderSettings.ambientIntensity    = 1.0f;                                     // fix this to 1, this parameter should not exist!
                    RenderSettings.ambientMode         = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
                    RenderSettings.reflectionIntensity = 1.0f;
                    RenderSettings.customReflection    = null;
                    DynamicGI.UpdateEnvironment();

                    m_NeedLowLevelUpdateEnvironment = false;
                }

                if (IsSkyValid())
                {
                    m_CurrentUpdateTime += Time.deltaTime;

                    m_BuiltinParameters.renderContext = renderContext;
                    m_BuiltinParameters.sunLight      = sunLight;

                    if (
                        (skySettings.updateMode == EnvironementUpdateMode.OnDemand && m_UpdateRequired) ||
                        (skySettings.updateMode == EnvironementUpdateMode.OnChanged && skySettings.GetHash() != m_SkyParametersHash) ||
                        (skySettings.updateMode == EnvironementUpdateMode.Realtime && m_CurrentUpdateTime > skySettings.updatePeriod)
                        )
                    {
                        // Render sky into a cubemap - doesn't happen every frame, can be controlled
                        // Note that m_SkyboxCubemapRT is created with auto-generate mipmap, it mean that here we have also our mipmap correctly box filtered for importance sampling.
                        if (m_SkySettings.lightingOverride == null)
                        {
                            RenderSkyToCubemap(m_BuiltinParameters, skySettings, m_SkyboxCubemapRT);
                        }
                        // In case the user overrides the lighting, we already have a cubemap ready but we need to blit it anyway for potential resize and so that we can generate proper mipmaps for enlighten.
                        else
                        {
                            BlitCubemap(renderContext, m_SkySettings.lightingOverride, m_SkyboxCubemapRT);
                        }

                        // Convolve downsampled cubemap
                        RenderCubemapGGXConvolution(renderContext, m_BuiltinParameters, skySettings, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);

                        m_NeedLowLevelUpdateEnvironment = true;
                        m_UpdateRequired    = false;
                        m_SkyParametersHash = skySettings.GetHash();
                        m_CurrentUpdateTime = 0.0f;
                    }
                }
                else
                {
                    if (m_SkyParametersHash != 0)
                    {
                        // Clear temp cubemap and redo GGX from black and then feed it to enlighten for default light probe.
                        Utilities.ClearCubemap(renderContext, m_SkyboxCubemapRT, Color.black);
                        RenderCubemapGGXConvolution(renderContext, m_BuiltinParameters, skySettings, m_SkyboxCubemapRT, m_SkyboxGGXCubemapRT);

                        m_SkyParametersHash             = 0;
                        m_NeedLowLevelUpdateEnvironment = true;
                    }
                }
            }
        }