public void RenderSky(SkyUpdateContext skyContext, HDCamera hdCamera, Light sunLight, RTHandle colorBuffer, RTHandle depthBuffer, DebugDisplaySettings debugSettings, int frameIndex, CommandBuffer cmd)
        {
            if (skyContext.IsValid() && hdCamera.clearColorMode == HDAdditionalCameraData.ClearColorMode.Sky)
            {
                using (new ProfilingSample(cmd, "Sky Pass"))
                {
                    m_BuiltinParameters.commandBuffer             = cmd;
                    m_BuiltinParameters.sunLight                  = sunLight;
                    m_BuiltinParameters.pixelCoordToViewDirMatrix = hdCamera.mainViewConstants.pixelCoordToViewDirWS;
                    m_BuiltinParameters.worldSpaceCameraPos       = hdCamera.mainViewConstants.worldSpaceCameraPos;
                    m_BuiltinParameters.viewMatrix                = hdCamera.mainViewConstants.viewMatrix;
                    m_BuiltinParameters.screenSize                = hdCamera.screenSize;
                    m_BuiltinParameters.colorBuffer               = colorBuffer;
                    m_BuiltinParameters.depthBuffer               = depthBuffer;
                    m_BuiltinParameters.debugSettings             = debugSettings;
                    m_BuiltinParameters.frameIndex                = frameIndex;
                    m_BuiltinParameters.updateMode                = skyContext.skySettings.updateMode.value;

                    skyContext.renderer.SetRenderTargets(m_BuiltinParameters);

                    // If the luxmeter is enabled, we don't render the sky
                    if (debugSettings.data.lightingDebugSettings.debugLightingMode != DebugLightingMode.LuxMeter)
                    {
                        // When rendering the visual sky for reflection probes, we need to remove the sun disk if skySettings.includeSunInBaking is false.
                        skyContext.renderer.RenderSky(m_BuiltinParameters, false, hdCamera.camera.cameraType != CameraType.Reflection || skyContext.skySettings.includeSunInBaking.value);
                    }
                }
            }
        }
Пример #2
0
 SphericalHarmonicsL2 GetAmbientProbe(SkyUpdateContext skyContext)
 {
     if (skyContext.IsValid() && IsCachedContextValid(skyContext))
     {
         ref var context = ref m_CachedSkyContexts[skyContext.cachedSkyRenderingContextId];
         return(context.renderingContext.ambientProbe);
     }
 void RenderCubemapGGXConvolution(SkyUpdateContext skyContext)
 {
     using (new ProfilingSample(m_BuiltinParameters.commandBuffer, "Update Env: GGX Convolution"))
     {
         for (int bsdfIdx = 0; bsdfIdx < m_IBLFilterArray.Length; ++bsdfIdx)
         {
             // First of all filter this cubemap using the target filter
             m_IBLFilterArray[bsdfIdx].FilterCubemap(m_BuiltinParameters.commandBuffer, m_SkyboxCubemapRT, m_SkyboxBSDFCubemapIntermediate);
             // Then copy it to the cubemap array slice
             for (int i = 0; i < 6; ++i)
             {
                 m_BuiltinParameters.commandBuffer.CopyTexture(m_SkyboxBSDFCubemapIntermediate, i, m_SkyboxBSDFCubemapArray, 6 * bsdfIdx + i);
             }
         }
     }
 }
        void RenderSkyToCubemap(SkyUpdateContext skyContext)
        {
            for (int i = 0; i < 6; ++i)
            {
                m_BuiltinParameters.pixelCoordToViewDirMatrix = m_facePixelCoordToViewDirMatrices[i];
                m_BuiltinParameters.viewMatrix  = m_CameraRelativeViewMatrices[i];
                m_BuiltinParameters.colorBuffer = m_SkyboxCubemapRT;
                m_BuiltinParameters.depthBuffer = null;

                CoreUtils.SetRenderTarget(m_BuiltinParameters.commandBuffer, m_SkyboxCubemapRT, ClearFlag.None, 0, (CubemapFace)i);
                skyContext.renderer.RenderSky(m_BuiltinParameters, true, skyContext.skySettings.includeSunInBaking.value);
            }

            // Generate mipmap for our cubemap
            Debug.Assert(m_SkyboxCubemapRT.rt.autoGenerateMips == false);
            m_BuiltinParameters.commandBuffer.GenerateMips(m_SkyboxCubemapRT);
        }
Пример #5
0
        public void UpdateCurrentSkySettings(HDCamera hdCamera)
        {
#if UNITY_EDITOR
            if (HDUtils.IsRegularPreviewCamera(hdCamera.camera))
            {
                m_PreviewSky.skySettings     = GetDefaultPreviewSkyInstance();
                m_CurrentSky                 = m_PreviewSky;
                m_CurrentSkyRenderingContext = m_PreviewSkyRenderingContext;
            }
            else
#endif
            {
                m_VisualSky.skySettings      = GetSkySetting(VolumeManager.instance.stack);
                m_CurrentSky                 = m_VisualSky;
                m_CurrentSkyRenderingContext = m_SkyRenderingContext;
            }

            // Update needs to happen before testing if the component is active other internal data structure are not properly updated yet.
            VolumeManager.instance.Update(m_LightingOverrideVolumeStack, hdCamera.volumeAnchor, m_LightingOverrideLayerMask);
            if (VolumeManager.instance.IsComponentActiveInMask <VisualEnvironment>(m_LightingOverrideLayerMask))
            {
                SkySettings newSkyOverride = GetSkySetting(m_LightingOverrideVolumeStack);
                if (m_LightingOverrideSky.skySettings != null && newSkyOverride == null)
                {
                    // When we switch from override to no override, we need to make sure that the visual sky will actually be properly re-rendered.
                    // Resetting the visual sky hash will ensure that.
                    m_VisualSky.skyParametersHash = -1;
                }
                m_LightingOverrideSky.skySettings = newSkyOverride;
                m_CurrentSky = m_LightingOverrideSky;
            }
            else
            {
                m_LightingOverrideSky.skySettings = null;
            }
        }
        public bool UpdateEnvironment(SkyUpdateContext skyContext, Light sunLight, Vector3 worldSpaceCameraPos, bool updateRequired, bool updateAmbientProbe, int frameIndex, CommandBuffer cmd)
        {
            bool result = false;

            if (skyContext.IsValid())
            {
                skyContext.currentUpdateTime += Time.deltaTime;

                m_BuiltinParameters.commandBuffer       = cmd;
                m_BuiltinParameters.sunLight            = sunLight;
                m_BuiltinParameters.worldSpaceCameraPos = worldSpaceCameraPos;
                m_BuiltinParameters.screenSize          = m_CubemapScreenSize;
                m_BuiltinParameters.debugSettings       = null; // We don't want any debug when updating the environment.
                m_BuiltinParameters.frameIndex          = frameIndex;
                m_BuiltinParameters.updateMode          = skyContext.skySettings.updateMode.value;

                int sunHash = 0;
                if (sunLight != null)
                {
                    sunHash = GetSunLightHashCode(sunLight);
                }
                int skyHash = sunHash * 23 + skyContext.skySettings.GetHashCode();

                bool forceUpdate = (updateRequired || skyContext.updatedFramesRequired > 0 || m_NeedUpdate);
                if (forceUpdate ||
                    (skyContext.skySettings.updateMode.value == EnvironmentUpdateMode.OnChanged && skyHash != skyContext.skyParametersHash) ||
                    (skyContext.skySettings.updateMode.value == EnvironmentUpdateMode.Realtime && skyContext.currentUpdateTime > skyContext.skySettings.updatePeriod.value))
                {
                    using (new ProfilingSample(cmd, "Sky Environment Pass"))
                    {
                        using (new ProfilingSample(cmd, "Update Env: Generate Lighting Cubemap"))
                        {
                            RenderSkyToCubemap(skyContext);

                            if (updateAmbientProbe)
                            {
                                using (new ProfilingSample(cmd, "Update Ambient Probe"))
                                {
                                    cmd.SetComputeBufferParam(m_ComputeAmbientProbeCS, m_ComputeAmbientProbeKernel, m_AmbientProbeOutputBufferParam, m_AmbientProbeResult);
                                    cmd.SetComputeTextureParam(m_ComputeAmbientProbeCS, m_ComputeAmbientProbeKernel, m_AmbientProbeInputCubemap, m_SkyboxCubemapRT);
                                    cmd.DispatchCompute(m_ComputeAmbientProbeCS, m_ComputeAmbientProbeKernel, 1, 1, 1);
                                    cmd.RequestAsyncReadback(m_AmbientProbeResult, OnComputeAmbientProbeDone);
                                }
                            }
                        }

                        if (m_SupportsConvolution)
                        {
                            using (new ProfilingSample(cmd, "Update Env: Convolve Lighting Cubemap"))
                            {
                                RenderCubemapGGXConvolution(skyContext);
                            }
                        }

                        result = true;
                        skyContext.skyParametersHash = skyHash;
                        skyContext.currentUpdateTime = 0.0f;
                        skyContext.updatedFramesRequired--;
                        m_NeedUpdate = false;

#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.rt.imageContentsHash = new Hash128((uint)skyContext.skySettings.GetHashCode(), 0, 0, 0);
#endif
                    }
                }
            }
            else
            {
                if (skyContext.skyParametersHash != 0)
                {
                    using (new ProfilingSample(cmd, "Clear Sky Environment Pass"))
                    {
                        CoreUtils.ClearCubemap(cmd, m_SkyboxCubemapRT, Color.black, true);
                        if (m_SupportsConvolution)
                        {
                            CoreUtils.ClearCubemap(cmd, m_SkyboxBSDFCubemapIntermediate, Color.black, true);
                            for (int bsdfIdx = 0; bsdfIdx < m_IBLFilterArray.Length; ++bsdfIdx)
                            {
                                for (int face = 0; face < 6; ++face)
                                {
                                    cmd.CopyTexture(m_SkyboxBSDFCubemapIntermediate, face, m_SkyboxBSDFCubemapArray, 6 * bsdfIdx + face);
                                }
                            }
                        }
                    }

                    skyContext.skyParametersHash = 0;
                    result = true;
                }
            }

            return(result);
        }