Beispiel #1
0
        public override bool Update(BuiltinSkyParameters builtinParams)
        {
            var cmd = builtinParams.commandBuffer;

            UpdateGlobalConstantBuffer(cmd);

            int currPrecomputationParamHash = m_Settings.GetPrecomputationHashCode();

            if (currPrecomputationParamHash != m_LastPrecomputationParamHash)
            {
                // Hash does not match, have to restart the precomputation from scratch.
                m_LastPrecomputedBounce = 0;
            }

            if (m_LastPrecomputedBounce == 0)
            {
                // Allocate temp tables if needed
                if (m_GroundIrradianceTables[1] == null)
                {
                    m_GroundIrradianceTables[1] = AllocateGroundIrradianceTable(1);
                }

                if (m_InScatteredRadianceTables[3] == null)
                {
                    m_InScatteredRadianceTables[3] = AllocateInScatteredRadianceTable(3);
                }

                if (m_InScatteredRadianceTables[4] == null)
                {
                    m_InScatteredRadianceTables[4] = AllocateInScatteredRadianceTable(4);
                }
            }

            if (m_LastPrecomputedBounce == m_Settings.numberOfBounces.value)
            {
                // Free temp tables.
                // This is a deferred release (one frame late)!
                RTHandles.Release(m_GroundIrradianceTables[1]);
                RTHandles.Release(m_InScatteredRadianceTables[3]);
                RTHandles.Release(m_InScatteredRadianceTables[4]);
                m_GroundIrradianceTables[1]    = null;
                m_InScatteredRadianceTables[3] = null;
                m_InScatteredRadianceTables[4] = null;
            }

            if (m_LastPrecomputedBounce < m_Settings.numberOfBounces.value)
            {
                PrecomputeTables(cmd);
                m_LastPrecomputedBounce++;

                // Update the hash for the current bounce.
                m_LastPrecomputationParamHash = currPrecomputationParamHash;

                // If the sky is realtime, an upcoming update will update the sky lighting. Otherwise we need to force an update.
                return(builtinParams.updateMode != EnvironmentUpdateMode.Realtime);
            }

            return(false);
        }
        public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderForCubemap, bool renderSunDisk)
        {
            CommandBuffer cmd = builtinParams.commandBuffer;

            UpdateGlobalConstantBuffer(cmd);

            int currPrecomputationParamHash = m_Settings.GetPrecomputationHashCode();

            if (currPrecomputationParamHash != m_LastPrecomputationParamHash)
            {
                // Hash does not match, have to restart the precomputation from scratch.
                m_LastPrecomputedBounce = 0;
            }

            if (m_LastPrecomputedBounce == 0)
            {
                // Allocate temp tables if needed
                if (m_GroundIrradianceTables[1] == null)
                {
                    m_GroundIrradianceTables[1] = AllocateGroundIrradianceTable(1);
                }

                if (m_InScatteredRadianceTables[3] == null)
                {
                    m_InScatteredRadianceTables[3] = AllocateInScatteredRadianceTable(3);
                }

                if (m_InScatteredRadianceTables[4] == null)
                {
                    m_InScatteredRadianceTables[4] = AllocateInScatteredRadianceTable(4);
                }
            }

            if (m_LastPrecomputedBounce == m_Settings.numberOfBounces.value && m_LastPrecomputationFrameIndex < builtinParams.frameIndex)
            {
                // Free temp tables.
                // This is a deferred release (one frame late)!
                RTHandles.Release(m_GroundIrradianceTables[1]);
                RTHandles.Release(m_InScatteredRadianceTables[3]);
                RTHandles.Release(m_InScatteredRadianceTables[4]);
                m_GroundIrradianceTables[1]    = null;
                m_InScatteredRadianceTables[3] = null;
                m_InScatteredRadianceTables[4] = null;
            }

            if (m_LastPrecomputedBounce < m_Settings.numberOfBounces.value)
            {
                // When rendering into a cubemap for environment lighting, if the sky is not realtime, then we need to do all the bounces at once
                // It's ok in terms of performance because it's only done once (as long as parameters don't change).
                // However, editing parameters will be very slow.
                if (builtinParams.updateMode != EnvironmentUpdateMode.Realtime)
                {
                    for (int i = m_LastPrecomputedBounce; i < m_Settings.numberOfBounces.value; ++i)
                    {
                        PrecomputeTables(cmd);
                        m_LastPrecomputedBounce++;
                    }

                    // Update the hash for the current bounce.
                    m_LastPrecomputationParamHash  = currPrecomputationParamHash;
                    m_LastPrecomputationFrameIndex = builtinParams.frameIndex;
                }
                // In case of realtime environment lighting, we need to update only one bounce and only once per frame
                // (the same sky can be rendered into a cubemap and in the regular view).
                // Also, we obviously want the precomputation to run at least once.
                else if ((m_LastPrecomputationFrameIndex < builtinParams.frameIndex) || (m_LastPrecomputedBounce == 0))
                {
                    PrecomputeTables(cmd);
                    m_LastPrecomputedBounce++;

                    // Update the hash for the current bounce.
                    m_LastPrecomputationParamHash  = currPrecomputationParamHash;
                    m_LastPrecomputationFrameIndex = builtinParams.frameIndex;
                }
            }

            // Precomputation is done, shading is next.
            Quaternion planetRotation = Quaternion.Euler(m_Settings.planetRotation.value.x,
                                                         m_Settings.planetRotation.value.y,
                                                         m_Settings.planetRotation.value.z);

            Quaternion spaceRotation = Quaternion.Euler(m_Settings.spaceRotation.value.x,
                                                        m_Settings.spaceRotation.value.y,
                                                        m_Settings.spaceRotation.value.z);

            s_PbrSkyMaterialProperties.SetMatrix(HDShaderIDs._PixelCoordToViewDirWS, builtinParams.pixelCoordToViewDirMatrix);
            s_PbrSkyMaterialProperties.SetVector(HDShaderIDs._WorldSpaceCameraPos1, builtinParams.worldSpaceCameraPos);
            s_PbrSkyMaterialProperties.SetMatrix(HDShaderIDs._ViewMatrix1, builtinParams.viewMatrix);
            s_PbrSkyMaterialProperties.SetMatrix(HDShaderIDs._PlanetRotation, Matrix4x4.Rotate(planetRotation));
            s_PbrSkyMaterialProperties.SetMatrix(HDShaderIDs._SpaceRotation, Matrix4x4.Rotate(spaceRotation));

            if (m_LastPrecomputedBounce != 0)
            {
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._GroundIrradianceTexture, m_GroundIrradianceTables[0]);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._AirSingleScatteringTexture, m_InScatteredRadianceTables[0]);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._AerosolSingleScatteringTexture, m_InScatteredRadianceTables[1]);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._MultipleScatteringTexture, m_InScatteredRadianceTables[2]);
            }
            else
            {
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._GroundIrradianceTexture, Texture2D.blackTexture);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._AirSingleScatteringTexture, CoreUtils.blackVolumeTexture);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._AerosolSingleScatteringTexture, CoreUtils.blackVolumeTexture);
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._MultipleScatteringTexture, CoreUtils.blackVolumeTexture);
            }

            int hasGroundAlbedoTexture = 0;

            if (m_Settings.groundAlbedoTexture.value != null)
            {
                hasGroundAlbedoTexture = 1;
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._GroundAlbedoTexture, m_Settings.groundAlbedoTexture.value);
            }
            s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasGroundAlbedoTexture, hasGroundAlbedoTexture);

            int hasGroundEmissionTexture = 0;

            if (m_Settings.groundEmissionTexture.value != null)
            {
                hasGroundEmissionTexture = 1;
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._GroundEmissionTexture, m_Settings.groundEmissionTexture.value);
            }
            s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasGroundEmissionTexture, hasGroundEmissionTexture);

            int hasSpaceEmissionTexture = 0;

            if (m_Settings.spaceEmissionTexture.value != null)
            {
                hasSpaceEmissionTexture = 1;
                s_PbrSkyMaterialProperties.SetTexture(HDShaderIDs._SpaceEmissionTexture, m_Settings.spaceEmissionTexture.value);
            }
            s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._HasSpaceEmissionTexture, hasSpaceEmissionTexture);

            s_PbrSkyMaterialProperties.SetInt(HDShaderIDs._RenderSunDisk, renderSunDisk ? 1 : 0);

            CoreUtils.DrawFullScreen(builtinParams.commandBuffer, s_PbrSkyMaterial, s_PbrSkyMaterialProperties, renderForCubemap ? 0 : 1);
        }