void Start() { screenWidth = Screen.width; screenHeight = Screen.height; cameraTarget = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); GBufferTextures = new RenderTexture[] { new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear) }; depthTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear); GBuffers = new RenderBuffer[GBufferTextures.Length]; for (int i = 0; i < GBuffers.Length; ++i) { GBuffers[i] = GBufferTextures[i].colorBuffer; } gbufferIDs = new int[] { Shader.PropertyToID("_GBuffer0"), Shader.PropertyToID("_GBuffer1"), Shader.PropertyToID("_GBuffer2"), Shader.PropertyToID("_GBuffer3"), }; SortMesh.InitSortMesh(allRenderObjects.Length); CullMesh.allObjects = allRenderObjects; foreach (var i in allRenderObjects) { i.Init(); } }
// Start is called before the first frame update void Start() { cameraRT = new RenderTexture(Screen.width, Screen.height, 24); GBufferTextures = new RenderTexture[] { new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), }; //深度贴图? depthTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear); GBuffers = new RenderBuffer[GBufferTextures.Length]; for (int i = 0; i < GBuffers.Length; i++) { GBuffers[i] = GBufferTextures[i].colorBuffer; } //这就是纹理id把 GBufferIDs = new int[] { Shader.PropertyToID("_GBuffer0"), Shader.PropertyToID("_GBuffer1"), Shader.PropertyToID("_GBuffer2"), Shader.PropertyToID("_GBuffer3") }; SortMesh.InitSortMesh(renderObjects.Length); CullMesh.renderObjects = renderObjects; foreach (var i in renderObjects) { i.Init(); } }
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); }
void Start() { screenWidth = Screen.width; screenHeight = Screen.height; //定制了3块RT,第一块是CameraTarget,也就是光照计算后的结果 //第二块是四个贴图,GBuffer,在Shader中输出这四个值 //第三块是depthTexture用来处理深度问题 cameraTarget = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); GBufferTextures = new[] { new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear), }; depthTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear); GBuffers = new RenderBuffer[GBufferTextures.Length]; for (int i = 0; i < GBuffers.Length; i++) { GBuffers[i] = GBufferTextures[i].colorBuffer; } gbufferIDs = new[] { Shader.PropertyToID("_GBuffer0"), Shader.PropertyToID("_GBuffer1"), Shader.PropertyToID("_GBuffer2"), Shader.PropertyToID("_GBuffer3"), }; //把所有的Obj进行排序和剔除 SortMesh.InitSortMesh(allRenderObjs.Length); CullMesh.allObjects = allRenderObjs; //单例初始化 foreach (var i in allRenderObjs) { i.Init(); } }
public static JobHandle Schedule(JobHandle cull) { SortMesh instance = new SortMesh(); return(instance.Schedule(LayerCount, 1, cull)); }
public static JobHandle Schedule(JobHandle cull) { SortMesh instance = new SortMesh(); return(instance.Schedule <SortMesh>(LAYERCOUNT, 1, cull)); }