Exemple #1
0
    private void OnPostRender()
    {
        Camera cam = Camera.current;

        //调整屏幕大小以满足超采样
        if (screenHeight != cam.pixelHeight * superSample || screenWidth != cam.pixelWidth * superSample)
        {
            screenHeight = (int)(cam.pixelHeight * superSample);
            screenWidth  = (int)(cam.pixelWidth * superSample);
            ReSize(cameraTarget, screenWidth, screenHeight);
            ReSize(depthTexture, screenWidth, screenHeight);
            foreach (var i in GBufferTextures)
            {
                ReSize(i, screenWidth, screenHeight);
            }

            for (int i = 0; i < GBuffers.Length; i++)
            {
                GBuffers[i] = GBufferTextures[i].colorBuffer;
            }
        }
        Shader.SetGlobalTexture(_DepthTexture, depthTexture);

        //计算每帧的投影矩阵,依靠投影矩阵计算视锥裁面
        Matrix4x4 proj  = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
        Matrix4x4 vp    = proj * cam.worldToCameraMatrix;
        Matrix4x4 invvp = vp.inverse;

        Shader.SetGlobalMatrix(_InvVP, invvp);

        CullMesh.UpdateFrame(cam, ref invvp, transform.position);
        SortMesh.UpdateFrame();

        JobHandle cullHandle = CullMesh.Schedule();
        JobHandle sortHandle = SortMesh.Schedule(cullHandle);

        JobHandle.ScheduleBatchedJobs();//使用Job System的调用在分线程中完成剔除和排序

        for (int i = 0; i < gbufferIDs.Length; i++)
        {
            Shader.SetGlobalTexture(gbufferIDs[i], GBufferTextures[i]);
        }

        Graphics.SetRenderTarget(GBuffers, depthTexture.depthBuffer);
        GL.Clear(true, true, Color.black);
        //start draw call
        deferredMaterial.SetPass(0);
        sortHandle.Complete();
        for (int i = 0; i < SortMesh.sortObj.Length; i++)//遍历每个obj的Mesh进行输出
        {
            DrawElements(ref SortMesh.sortObj[i]);
        }
        lighting.DrawLight(GBufferTextures, gbufferIDs, cameraTarget, cam);
        skyDraw.SkyBoxDraw(cam, cameraTarget.colorBuffer, depthTexture.depthBuffer);
        //end draw call
        Graphics.Blit(cameraTarget, cam.targetTexture);
    }
    private void OnPostRender()
    {
        Camera cam = Camera.current;

        if (screenHeight != cam.pixelHeight * superSample || screenWidth != cam.pixelWidth * superSample)
        {
            screenHeight = (int)(cam.pixelHeight * superSample);
            screenWidth  = (int)(cam.pixelWidth * superSample);
            ReSize(cameraTarget, screenWidth, screenHeight);
            ReSize(depthTexture, screenWidth, screenHeight);
            foreach (var i in GBufferTextures)
            {
                ReSize(i, screenWidth, screenHeight);
            }
            for (int i = 0; i < GBuffers.Length; ++i)
            {
                GBuffers[i] = GBufferTextures[i].colorBuffer;
            }
        }
        Shader.SetGlobalTexture(_DepthTexture, depthTexture);
        Matrix4x4 proj  = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
        Matrix4x4 vp    = proj * cam.worldToCameraMatrix;
        Matrix4x4 invvp = vp.inverse;

        Shader.SetGlobalMatrix(_InvVP, invvp);
        CullMesh.UpdateFrame(cam, ref invvp, transform.position);
        SortMesh.UpdateFrame();
        JobHandle cullhandle = CullMesh.Schedule();
        JobHandle sortHandle = SortMesh.Schedule(cullhandle);

        JobHandle.ScheduleBatchedJobs();
        for (int i = 0; i < gbufferIDs.Length; ++i)
        {
            Shader.SetGlobalTexture(gbufferIDs[i], GBufferTextures[i]);
        }
        Graphics.SetRenderTarget(GBuffers, depthTexture.depthBuffer);
        GL.Clear(true, true, Color.black);
        //Start Draw Mesh
        deferredMaterial.SetPass(0);
        sortHandle.Complete();
        for (int i = 0; i < SortMesh.sorts.Length; ++i)
        {
            DrawElements(ref SortMesh.sorts[i]);
        }
        lighting.DrawLight(GBufferTextures, gbufferIDs, cameraTarget, cam);
        //End Deferred Lighting
        skyDraw.DrawSkybox(cam, cameraTarget.colorBuffer, depthTexture.depthBuffer);
        Graphics.Blit(cameraTarget, cam.targetTexture);
    }