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

        // rt设置为当前的渲染目标
        Graphics.SetRenderTarget(rt);

        // 将手动创建的rt执行clear操作,使其变成一个白色的贴图
        GL.Clear(true, true, Color.white);

        // start drawcall
        pureColorMaterial.color = new Color(0, 0.5f, 0.8f);
        pureColorMaterial.SetPass(0);

        // 使用同一个材质,绘制多个模型
        foreach (Transform transform in cubeTransforms)
        {
            Graphics.DrawMeshNow(cubeMesh, transform.localToWorldMatrix);
        }

        // end drawcall

        skyboxDraw.DrawSkybox(cam, rt.colorBuffer, depthTexture.depthBuffer);

        // 将rt绘制到cam.targetTexture,cam.targetTexture如果为null,则绘制到屏幕上
        Graphics.Blit(rt, cam.targetTexture);
    }
예제 #2
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();
        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);
    }