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); }
private void OnPostRender() { Camera cam = Camera.current; if (screenHeight != cam.pixelHeight || screenWidth != cam.pixelWidth) { screenHeight = (int)(cam.pixelHeight); screenWidth = (int)(cam.pixelWidth); ReSize(renderTexture, 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); // Graphics.SetRenderTarget(renderTexture); for (int i = 0; i < GBufferIDs.Length; ++i) { Shader.SetGlobalTexture(GBufferIDs[i], GBufferTextures[i]); } Graphics.SetRenderTarget(GBuffers, depthTexture.depthBuffer); GL.Clear(true, true, Color.green); // Start Drawcall // pureColorMaterial.color = new Color(0, 0.8f, 0); // pureColorMaterial.SetPass(0); deferredMat.SetPass(0); foreach (var oneCube in cubeTransform) { Graphics.DrawMeshNow(cubeMesh, oneCube.localToWorldMatrix); } lighting.DrawLight(GBufferTextures, GBufferIDs, renderTexture, cam); skydraw.DrawSkybox(cam, renderTexture.colorBuffer, depthTexture.depthBuffer); Graphics.Blit(renderTexture, cam.targetTexture); }
private void OnPostRender() { Camera cam = Camera.current; Shader.SetGlobalTexture(_DepthTexture, depthTexture); //Matrix4x4 proj = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false); //Matrix4x4 vp = proj * cam.worldToCameraMatrix; //Matrix4x4 inverseVp = vp.inverse; //Shader.SetGlobalMatrix(_InvVP, inverseVp); //CullMesh.UpdateFrame(cam, ref inverseVp, transform.position); //SortMesh.UpdateFrame(); //JobHandle cullHandle = CullMesh.Schedule(); //JobHandle sortHandle = SortMesh.Schedule(cullHandle); //JobHandle.ScheduleBatchedJobs(); //渲染目标改为gbuffer Graphics.SetRenderTarget(GBuffers, depthTexture.depthBuffer); //Graphics.SetRenderTarget(cameraRT); GL.Clear(true, true, Color.grey); //start dc DeferredMaterial.color = new Color(0, 0.2f, 0.8f); DeferredMaterial.SetPass(0); //sortHandle.Complete(); //for (int i = 0; i < SortMesh.sorts.Length; i++) //{ // DrawElements(ref SortMesh.sorts[i]); //} for (int i = 0; i < cubeTrans.Length; i++) { Graphics.DrawMeshNow(cubeMesh, cubeTrans[i].localToWorldMatrix); } lighting.DrawLight(GBufferTextures, GBufferIDs, cameraRT, cam); //Graphics.Blit(GBufferTextures[1], cameraRT); skybox.DrawSkybox(cam, cameraRT.colorBuffer, depthTexture.depthBuffer); //end dc Graphics.Blit(cameraRT, cam.targetTexture); }