Exemplo n.º 1
0
    void CreateRenderBuffer(ScriptableRenderContext context)
    {
        int bufferWidth  = Screen.width;
        int bufferHeight = Screen.height;

#if !UNITY_EDITOR
        bufferWidth  >>= 1;
        bufferHeight >>= 1;
#endif

        CmdBuff.GetTemporaryRT(RenderBuffer1ID, bufferWidth, bufferHeight, 0, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR);
        CmdBuff.GetTemporaryRT(RenderBuffer2ID, bufferWidth, bufferHeight, 0, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR);
        CmdBuff.GetTemporaryRT(DepthBufferID, bufferWidth, bufferHeight, 16, FilterMode.Point, RenderTextureFormat.Depth);
        CmdBuff.GetTemporaryRT(ShadowBufferID, (int)shadowMapSize, (int)shadowMapSize, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);

        if (renderBuffers == null)
        {
            renderBuffers = new RenderTargetIdentifier[4];
        }
        renderBuffers[0] = new RenderTargetIdentifier(RenderBuffer1ID);
        renderBuffers[1] = new RenderTargetIdentifier(RenderBuffer2ID);
        renderBuffers[2] = new RenderTargetIdentifier(DepthBufferID);
        renderBuffers[3] = new RenderTargetIdentifier(ShadowBufferID);

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 2
0
    void ApplyBloom(CommandBuffer _cmd, RenderTargetIdentifier _src)
    {
        int width  = Screen.width;
        int height = Screen.height;

        for (int i = 0; i < MAX_BLOOM_BUFFER_SIZE; ++i)
        {
            width  >>= 1;
            height >>= 1;

            _cmd.GetTemporaryRT(BloomBufferIDs[i], width, height, 0, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR);
        }

        _cmd.Blit(_src, BloomBufferIDs[0], matBloom, 0);

        for (int i = 1; i < MAX_BLOOM_BUFFER_SIZE; ++i)
        {
            CmdBuff.Blit(BloomBufferIDs[0], BloomBufferIDs[i], matBloom, 1);
        }

        for (int i = MAX_BLOOM_BUFFER_SIZE - 1; 0 < i; --i)
        {
            CmdBuff.Blit(BloomBufferIDs[i], BloomBufferIDs[i - 1], matBloom, 2);
        }

        CmdBuff.Blit(BloomBufferIDs[0], _src, matBloom, 3);

        for (int i = 0; i < MAX_BLOOM_BUFFER_SIZE; ++i)
        {
            _cmd.ReleaseTemporaryRT(BloomBufferIDs[i]);
        }
    }
Exemplo n.º 3
0
    void SetCamera(ScriptableRenderContext context, Camera camera)
    {
        CmdBuff.SetGlobalVector(WorldSpaceCameraPosID, camera.transform.position);
        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();

        context.SetupCameraProperties(camera);
    }
Exemplo n.º 4
0
 void Flip(ScriptableRenderContext context, Camera camera)
 {
     //if (camera.cameraType == CameraType.Game)
     {
         ApplyBloom(CmdBuff, renderBuffers[0]);
         ApplyToneMapping(CmdBuff, renderBuffers[0], renderBuffers[1]);
         ApplyFXAA(CmdBuff, renderBuffers[1], renderBuffers[0]);
         CmdBuff.Blit(renderBuffers[0], BuiltinRenderTextureType.CameraTarget);
     }
 }
Exemplo n.º 5
0
    void ReleaseRenderBuffer(ScriptableRenderContext context)
    {
        CmdBuff.ReleaseTemporaryRT(RenderBuffer1ID);
        CmdBuff.ReleaseTemporaryRT(RenderBuffer2ID);
        CmdBuff.ReleaseTemporaryRT(DepthBufferID);
        CmdBuff.ReleaseTemporaryRT(ShadowBufferID);

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 6
0
    void ClearRenderBuffer(ScriptableRenderContext context, Camera camera)
    {
        bool isClearColor, isClearDepth;

        GetClearFlags(camera, out isClearColor, out isClearDepth);

        CmdBuff.ClearRenderTarget(isClearDepth, isClearColor, camera.backgroundColor);

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 7
0
    void DefaultRenderFlow(ScriptableRenderContext context, Camera camera)
    {
        ScriptableCullingParameters cullingParameters;

        if (!camera.TryGetCullingParameters(out cullingParameters))
        {
            return;
        }

#if UNITY_EDITOR
        if (camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
        }
#endif

        cullingParameters.shadowDistance = Mathf.Min(setting.shadow.maxDistance, camera.farClipPlane);

        CmdBuff.BeginSample(camera.name);

        cullingResults = context.Cull(ref cullingParameters);

        SetShadow(context, camera);

        SetCamera(context, camera);

        ClearRenderBuffer(context, camera);

        SetLighting(context, camera);

        DrawDepthOnly(context, camera);

        DrawOpaque(context, camera);

        DrawSky(context, camera);

#if UNITY_EDITOR
        if (UnityEditor.Handles.ShouldRenderGizmos())
        {
            context.DrawGizmos(camera, GizmoSubset.PreImageEffects);
        }
#endif

#if UNITY_EDITOR
        if (UnityEditor.Handles.ShouldRenderGizmos())
        {
            context.DrawGizmos(camera, GizmoSubset.PostImageEffects);
        }
#endif

        CmdBuff.EndSample(camera.name);
        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 8
0
    void SetRenderBuffer(ScriptableRenderContext context, Camera camera)
    {
        bool isClearColor, isClearDepth;

        GetClearFlags(camera, out isClearColor, out isClearDepth);

        //if (camera.cameraType == CameraType.Game)
        {
            CmdBuff.SetRenderTarget(renderBuffers[0], renderBuffers[2]);
            CmdBuff.ClearRenderTarget(isClearDepth, isClearColor, camera.backgroundColor);
        }

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 9
0
    void SetLighting(ScriptableRenderContext context, Camera camera)
    {
        int lightIndex = -1;

        for (int i = 0; i < cullingResults.visibleLights.Length; ++i)
        {
            VisibleLight curLight = cullingResults.visibleLights[i];
            if (curLight.lightType != LightType.Directional)
            {
                continue;
            }

            lightIndex = i;
            break;
        }

        if (lightIndex < 0)
        {
            CmdBuff.DisableShaderKeyword("ENABLE_DIRECTIONAL_LIGHT");
            context.ExecuteCommandBuffer(CmdBuff);
            CmdBuff.Clear();
            return;
        }

        CmdBuff.EnableShaderKeyword("ENABLE_DIRECTIONAL_LIGHT");
        CmdBuff.SetGlobalColor(LightColorID, cullingResults.visibleLights[lightIndex].finalColor);

        Vector4 lightDirection = cullingResults.visibleLights[lightIndex].localToWorldMatrix.GetColumn(2); // cullingResults.visibleLights[lightIndex].light.transform.forward

        lightDirection.x *= -1f;
        lightDirection.y *= -1f;
        lightDirection.z *= -1f;
        CmdBuff.SetGlobalVector(LightDirectionID, lightDirection);

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 10
0
    void CustomRenderFlow(ScriptableRenderContext context, Camera camera)
    {
        ScriptableCullingParameters cullingParameters;

        if (!camera.TryGetCullingParameters(out cullingParameters))
        {
            return;
        }

        cullingParameters.shadowDistance = Mathf.Min(setting.shadow.maxDistance, camera.farClipPlane);

        CmdBuff.BeginSample(camera.name);

        cullingResults = context.Cull(ref cullingParameters);

        SetShadow(context, camera);

        SetCamera(context, camera);

        ClearRenderBuffer(context, camera);

        SetRenderBuffer(context, camera);

        SetLighting(context, camera);

        DrawDepthOnly(context, camera);

        DrawOpaque(context, camera);

        DrawSky(context, camera);

        Flip(context, camera);

        CmdBuff.EndSample(camera.name);
        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }
Exemplo n.º 11
0
    void SetShadow(ScriptableRenderContext context, Camera camera)
    {
        if (camera.cameraType != CameraType.Game)
        {
            return;
        }

        int lightIndex = -1;

        for (int i = 0; i < cullingResults.visibleLights.Length; ++i)
        {
            VisibleLight curLight = cullingResults.visibleLights[i];
            if (curLight.lightType != LightType.Directional)
            {
                continue;
            }

            if (curLight.light.shadows == LightShadows.None)
            {
                continue;
            }
            if (curLight.light.shadowStrength <= 0f)
            {
                continue;
            }

            Bounds bounds;
            if (cullingResults.GetShadowCasterBounds(i, out bounds) == false)
            {
                continue;
            }

            lightIndex = i;
            break;
        }

        if (lightIndex < 0)
        {
            CmdBuff.SetGlobalTexture(ShadowMapID, Texture2D.whiteTexture);
            context.ExecuteCommandBuffer(CmdBuff);
            CmdBuff.Clear();
            return;
        }

        CmdBuff.SetGlobalFloat(ShadowBiasID, cullingResults.visibleLights[lightIndex].light.shadowBias);
        CmdBuff.SetGlobalFloat(ShadowStrengthID, cullingResults.visibleLights[lightIndex].light.shadowStrength);
        CmdBuff.SetGlobalVector(ShadowMapSizeID, new Vector4(invShadowMapSize, invShadowMapSize, shadowMapSize, shadowMapSize));

        if (cullingResults.visibleLights[lightIndex].light.shadows == LightShadows.Soft)
        {
            CmdBuff.EnableShaderKeyword("_SHADOWS_SOFT");
        }
        else
        {
            CmdBuff.DisableShaderKeyword("_SHADOWS_SOFT");
        }

        Matrix4x4       viewMatrix;
        Matrix4x4       projMatrix;
        ShadowSplitData shadowSplitData;

        bool res = cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
            lightIndex,                                     // int activeLightIndex,
            0,                                              // int splitIndex,
            1,                                              // int splitCount,
            new Vector3(1.0f, 0.0f, 0.0f),                  // Vector3 splitRatio,
            (int)setting.shadow.atlasSize,                  // int shadowResolution,
            QualitySettings.shadowNearPlaneOffset,          // float shadowNearPlaneOffset,
            out viewMatrix,                                 // out Matrix4x4 viewMatrix,
            out projMatrix,                                 // out Matrix4x4 projMatrix,
            out shadowSplitData                             // out Experimental.Rendering.ShadowSplitData shadowSplitData
            );

        if (res == false)
        {
            CmdBuff.SetGlobalTexture(ShadowMapID, Texture2D.whiteTexture);
            context.ExecuteCommandBuffer(CmdBuff);
            CmdBuff.Clear();
            return;
        }

        CoreUtils.SetRenderTarget(CmdBuff, renderBuffers[3], RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.Depth);

        CmdBuff.SetViewProjectionMatrices(viewMatrix, projMatrix);

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();

        ShadowDrawingSettings shadowDrawingSettings = new ShadowDrawingSettings(cullingResults, lightIndex);

        shadowDrawingSettings.splitData = shadowSplitData;
        context.DrawShadows(ref shadowDrawingSettings);


        // https://docs.unity3d.com/2019.2/Documentation/Manual/SL-PlatformDifferences.html
        if (SystemInfo.usesReversedZBuffer)
        {
            projMatrix.m20 = -projMatrix.m20;
            projMatrix.m21 = -projMatrix.m21;
            projMatrix.m22 = -projMatrix.m22;
            projMatrix.m23 = -projMatrix.m23;
        }

        // [-1,1] remap to [0, 1]
        var scaleOffset = Matrix4x4.identity;

        scaleOffset.m00 = scaleOffset.m11 = scaleOffset.m22 = 0.5f;
        scaleOffset.m03 = scaleOffset.m13 = scaleOffset.m23 = 0.5f;

        CmdBuff.SetGlobalTexture(ShadowMapID, renderBuffers[3]);
        CmdBuff.SetGlobalMatrix(ShadowMatrixID, scaleOffset * (projMatrix * viewMatrix));

        context.ExecuteCommandBuffer(CmdBuff);
        CmdBuff.Clear();
    }