void PrepareBokehDOF(CommandBuffer cb, DragonPostProcess pp,RenderTexture colorRT,RenderTexture alphaRT) { int width = colorRT.width; int height = colorRT.height; float aspect = (float)width / height; float radiusInPixels = (float)pp.mProperty.kernelSize * 8f + 6f; float maxCoC = Mathf.Min(0.05f, radiusInPixels / colorRT.height); pp.depthOfFieldParams.z = maxCoC; pp.depthOfFieldParams.w = 1.0f / aspect; pp.mMaterialDOF.SetVector(CommonSet.ShaderProperties.depthOfFieldParams, pp.depthOfFieldParams); // CoC calculation pass cb.GetTemporaryRT(CommonSet.ShaderProperties.cocTex, width, height, 0, FilterMode.Bilinear, pp.cocFormat, RenderTextureReadWrite.Linear); var cocTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.cocTex); cb.Blit(null, cocTexID, pp.mMaterialDOF, (int)DragonPostProcess.FastDOFPass.COC); cb.SetGlobalTexture(CommonSet.ShaderProperties.cocTex, cocTexID); int dw = width / 2; int dh = height / 2; // Downsampling and prefiltering pass cb.GetTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); var depthOfFieldTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.depthOfFieldTex); cb.GetTemporaryRT(CommonSet.ShaderProperties.sceneWithAlphaTex, dw / 2, dh / 2, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); var sceneWithAlphaTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.sceneWithAlphaTex); int dofMainPass = (int)DragonPostProcess.MainPass.ForDOF; if (alphaRT && pp.AlphaBufferEnable()) { if (pp.PlayerLayerEnable()) dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithAlphaPlayer; else dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithAlphaBuffer; } else { if (pp.PlayerLayerEnable()) dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithPlayer; else dofMainPass = (int)DragonPostProcess.MainPass.ForDOF; } cb.Blit(colorRT, sceneWithAlphaTexID, pp.uberMaterial, dofMainPass); cb.Blit(sceneWithAlphaTexID, depthOfFieldTexID, pp.mMaterialDOF, (int)DragonPostProcess.BokehDOFPass.DownSampleAndFilter); cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.sceneWithAlphaTex); // Bokeh simulation pass cb.GetTemporaryRT(CommonSet.ShaderProperties.dofTempTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); var dofTempTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.dofTempTex); cb.Blit(depthOfFieldTexID, dofTempTexID, pp.mMaterialDOF, (int)DragonPostProcess.BokehDOFPass.FilterSmall + pp.mProperty.kernelSize); cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex); cb.GetTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); depthOfFieldTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.depthOfFieldTex); cb.Blit(dofTempTexID, depthOfFieldTexID, pp.mMaterialDOF, (int)DragonPostProcess.BokehDOFPass.PostFilter); // Give the results to the uber shader. pp.uberMaterial.SetVector(CommonSet.ShaderProperties.depthOfFieldParams, pp.depthOfFieldParams); cb.SetGlobalTexture(CommonSet.ShaderProperties.depthOfFieldTex, depthOfFieldTexID); }
void PrepareFastDOF(CommandBuffer cb, DragonPostProcess pp, RenderTexture colorRT, RenderTexture alphaRT) { int width = colorRT.width; int height = colorRT.height; float aspect = Mathf.Min(width, height) / 1080f; pp.depthOfFieldParams.z = Mathf.Min(pp.mProperty.dofRadius * aspect, 2.5f); pp.mMaterialDOF.SetVector(CommonSet.ShaderProperties.depthOfFieldParams, pp.depthOfFieldParams); // CoC calculation pass cb.GetTemporaryRT(CommonSet.ShaderProperties.cocTex, width, height, 0, FilterMode.Bilinear, pp.cocFormat, RenderTextureReadWrite.Linear); var cocTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.cocTex); cb.Blit(null, cocTexID, pp.mMaterialDOF, (int)DragonPostProcess.FastDOFPass.COC); cb.SetGlobalTexture(CommonSet.ShaderProperties.cocTex, cocTexID); int dw = width / 2; int dh = height / 2; // Downsampling and prefiltering pass cb.GetTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); var depthOfFieldTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.depthOfFieldTex); cb.GetTemporaryRT(CommonSet.ShaderProperties.sceneWithAlphaTex, dw / 2, dh / 2, 0, FilterMode.Bilinear, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear); var sceneWithAlphaTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.sceneWithAlphaTex); int dofMainPass = (int)DragonPostProcess.MainPass.ForDOF; if (alphaRT && pp.mProperty.alphaBuffer) { if (pp.mProperty.playerLayer) dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithAlphaPlayer; else dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithAlphaBuffer; } else { if (pp.mProperty.playerLayer) dofMainPass = (int)DragonPostProcess.MainPass.ForDOFWithPlayer; else dofMainPass = (int)DragonPostProcess.MainPass.ForDOF; } cb.Blit(colorRT, sceneWithAlphaTexID, pp.uberMaterial, dofMainPass); cb.Blit(sceneWithAlphaTexID, depthOfFieldTexID, pp.mMaterialDOF, (int)DragonPostProcess.FastDOFPass.PreFilter); cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.sceneWithAlphaTex); //blur pass cb.GetTemporaryRT(CommonSet.ShaderProperties.dofTempTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); var dofTempTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.dofTempTex); cb.Blit(depthOfFieldTexID, dofTempTexID, pp.mMaterialDOF, (int)DragonPostProcess.FastDOFPass.FragBlurH); cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex); cb.GetTemporaryRT(CommonSet.ShaderProperties.depthOfFieldTex, dw, dh, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); depthOfFieldTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.depthOfFieldTex); cb.Blit(dofTempTexID, depthOfFieldTexID, pp.mMaterialDOF, (int)DragonPostProcess.FastDOFPass.FragBlurV); // Give the results to the uber shader. pp.uberMaterial.SetVector(CommonSet.ShaderProperties.depthOfFieldParams, pp.depthOfFieldParams); cb.SetGlobalTexture(CommonSet.ShaderProperties.depthOfFieldTex, depthOfFieldTexID); }
//刷新组件 public void UpdateComponent() { mainCam = Camera.main; if (mainCam == null || uberCameraPrefab == null || alphaCameraPrefab == null) { return; } pp = mainCam.GetComponent <DragonPostProcess>(); if (pp == null) { pp = mainCam.gameObject.AddComponent <DragonPostProcess>(); } Transform alphaCamTran = mainCam.transform.Find("AlphaCamera"); if (alphaCamTran == null) { GameObject obj = Instantiate(alphaCameraPrefab) as GameObject; obj.name = "AlphaCamera"; alphaCamTran = obj.transform; alphaCamTran.parent = mainCam.transform; } alphaPass = alphaCamTran.GetComponent <AlphaPass>(); Camera alphaCamera = alphaCamTran.GetComponent <Camera>(); alphaCamera.depth = mainCam.depth + 1; Transform distortionCamTran = mainCam.transform.Find("DistortionCamera"); if (distortionCamTran == null) { GameObject obj = Instantiate(distortionCameraPrefab) as GameObject; obj.name = "DistortionCamera"; distortionCamTran = obj.transform; distortionCamTran.parent = mainCam.transform; } distortionPass = distortionCamTran.GetComponent <DistortionPass>(); Camera distortionCamera = distortionCamTran.GetComponent <Camera>(); distortionCamera.depth = mainCam.depth + 2; Transform uberCamTran = mainCam.transform.Find("UberCamera"); if (uberCamTran == null) { GameObject obj = Instantiate(uberCameraPrefab) as GameObject; obj.name = "UberCamera"; uberCamTran = obj.transform; uberCamTran.parent = mainCam.transform; } uberPass = uberCamTran.GetComponent <UberPass>(); Camera uberCamera = uberCamTran.GetComponent <Camera>(); uberCamera.depth = mainCam.depth + 3; alphaCamera.transform.localPosition = distortionCamera.transform.localPosition = uberCamera.transform.localPosition = Vector3.zero; alphaCamera.transform.localEulerAngles = distortionCamera.transform.localEulerAngles = uberCamera.transform.localEulerAngles = Vector3.zero; alphaCamera.nearClipPlane = distortionCamera.nearClipPlane = uberCamera.nearClipPlane = mainCam.nearClipPlane; alphaCamera.farClipPlane = distortionCamera.farClipPlane = uberCamera.farClipPlane = mainCam.farClipPlane; alphaCamera.fieldOfView = distortionCamera.fieldOfView = uberCamera.fieldOfView = mainCam.fieldOfView; alphaCamera.allowMSAA = distortionCamera.allowMSAA = uberCamera.allowMSAA = false; //alphaCamera.allowHDR = uberCamera.allowHDR = false; }
//将Unity中数值导入到环境 public void UnityToEnvironmentObject(EnvironmentObject env) { DragonPostProcess postProcess = Camera.main.GetComponent <DragonPostProcess>(); if (postProcess) { env.data.property = postProcess.mProperty; } else { env.data.property = new DragonPostProcess.Property(); } #if UNITY_EDITOR env.data.sceneSkybox = RenderSettings.skybox; if (RenderSettings.ambientMode == AmbientMode.Flat) { env.data.sceneAmbientMode = EnvironmentObject.EnvironmentData.CustomAmbientMode.Color; if (RenderSettings.ambientSkyColor == Color.black) { env.data.sceneAmbient = false; } else { env.data.sceneAmbient = true; } } else if (RenderSettings.ambientMode == AmbientMode.Trilight) { env.data.sceneAmbientMode = EnvironmentObject.EnvironmentData.CustomAmbientMode.Gradient; if (RenderSettings.ambientSkyColor == Color.black && RenderSettings.ambientSkyColor == Color.black && RenderSettings.ambientSkyColor == Color.black) { env.data.sceneAmbient = false; } else { env.data.sceneAmbient = true; } } else if (RenderSettings.ambientMode == AmbientMode.Skybox) { env.data.sceneAmbientMode = EnvironmentObject.EnvironmentData.CustomAmbientMode.Skybox; if (RenderSettings.ambientIntensity == 0) { env.data.sceneAmbient = false; } else { env.data.sceneAmbient = true; } } if (env.data.sceneAmbient) { env.data.sceneAmbientSkyColor = RenderSettings.ambientSkyColor; env.data.sceneAmbientGroundColor = RenderSettings.ambientEquatorColor; env.data.sceneAmbientEquatorColor = RenderSettings.ambientGroundColor; env.data.sceneAmbientIntensity = RenderSettings.ambientIntensity; } if (RenderSettings.reflectionIntensity == 0) { env.data.sceneReflection = false; } else { env.data.sceneDefaultReflectionMode = RenderSettings.defaultReflectionMode; env.data.sceneDefaultReflectionResolution = RenderSettings.defaultReflectionResolution; env.data.sceneReflectionIntensity = RenderSettings.reflectionIntensity; env.data.sceneReflectionBounces = RenderSettings.reflectionBounces; env.data.sceneCustomReflection = RenderSettings.customReflection; } if (env.data.sceneSunLight && RenderSettings.sun) { if (RenderSettings.sun.intensity == 0) { env.data.sceneDirectionalLight = false; } else { env.data.sceneDirectionalLightColor = env.data.sceneSunLight.color = RenderSettings.sun.color; env.data.sceneDirectionalLightIntensity = env.data.sceneSunLight.intensity = RenderSettings.sun.intensity; env.data.sceneShadowStrength = env.data.sceneSunLight.shadowStrength = RenderSettings.sun.shadowStrength; env.data.sceneSunLight.transform.position = RenderSettings.sun.transform.position; env.data.sceneSunLight.transform.rotation = RenderSettings.sun.transform.rotation; env.data.sceneSunLight.shadows = RenderSettings.sun.shadows; env.data.sceneSunLight.shadowRadius = RenderSettings.sun.shadowRadius; env.data.sceneSunLight.shadowAngle = RenderSettings.sun.shadowAngle; env.data.sceneSunLight.lightmapBakeType = RenderSettings.sun.lightmapBakeType; env.data.sceneSunLight.shadowBias = RenderSettings.sun.shadowBias; env.data.sceneSunLight.shadowNormalBias = RenderSettings.sun.shadowNormalBias; env.data.sceneSunLight.shadowNearPlane = RenderSettings.sun.shadowNearPlane; } if (RenderSettings.sun.transform.parent.GetComponent <EnvironmentObject>()) { } else { RenderSettings.sun.gameObject.SetActive(false); } } env.data.sceneShadowDistance = QualitySettings.shadowDistance; env.data.sceneShadowResolution = QualitySettings.shadowResolution; env.data.sceneNormalfog = RenderSettings.fog; env.data.sceneNormalfogColor = RenderSettings.fogColor; env.data.sceneNormalfogMode = RenderSettings.fogMode; env.data.sceneNormalfogDensity = RenderSettings.fogDensity; env.data.sceneNormalfogStartDistance = RenderSettings.fogStartDistance; env.data.sceneNormalfogEndDistance = RenderSettings.fogEndDistance; #endif }
void PrepareUberCB(CommandBuffer cb,DragonPostProcess pp) { cb.BeginSample("UberPostProcess"); RenderTexture colorRT = EnvironmentManager.Instance.pp.colorRT; RenderTexture alphaRT = EnvironmentManager.Instance.alphaPass.alphaRT; RenderTexture distortionRT = EnvironmentManager.Instance.distortionPass.distortionRT; if (alphaRT) cb.SetGlobalTexture(CommonSet.ShaderProperties.alphaTex, alphaRT); if (distortionRT) cb.SetGlobalTexture(CommonSet.ShaderProperties.distortionTex, distortionRT); if (initLut) { pp.GetLutTex(cb); pp.forceUpdate = true; initLut = false; } if(pp.SceneBloomEnable()) { pp.PrepareBloom(cb, colorRT, pp.mMaterialBloom, pp.mProperty.bloom.bloomRadius,CommonSet.ShaderProperties.bloomTex,false,pp.mProperty.bloom.bloomAnamorphicRatio); } if(pp.PlayerBloomEnable()) { pp.PrepareBloom(cb, colorRT, pp.mMaterialPlayerBloom, pp.mProperty.playerBloom.bloomRadius,CommonSet.ShaderProperties.playerBloomTex,pp.PlayerColorLookupEnable(), pp.mProperty.playerBloom.bloomAnamorphicRatio); } if (pp.AlphaBloomEnable() && alphaRT) { pp.PrepareBloom(cb, alphaRT, pp.mMaterialAlphaBloom, pp.mProperty.alphaBloom.bloomRadius ,CommonSet.ShaderProperties.alphaBloomTex,false, pp.mProperty.alphaBloom.bloomAnamorphicRatio); } if (pp.DofFastEnable()) PrepareFastDOF(cb,pp, colorRT,alphaRT); bool radial = pp.RadialBlurEnable() && pp.mProperty.radialAmount > 0; int mainPass = (int)DragonPostProcess.MainPass.Main; if(alphaRT && pp.AlphaBufferEnable()) { if (pp.PlayerLayerEnable()) mainPass = (int)DragonPostProcess.MainPass.WithAlphaPlayer; else mainPass = (int)DragonPostProcess.MainPass.WithAlphaBuffer; } else { if (pp.PlayerLayerEnable()) mainPass = (int)DragonPostProcess.MainPass.WithPlayer; else mainPass = (int)DragonPostProcess.MainPass.Main; } bool aaEnable = pp.AntialiasingEnable(); //FXAA radial if (radial || aaEnable) { cb.GetTemporaryRT(CommonSet.ShaderProperties.uberTex, colorRT.width, colorRT.height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); var uberTexId = new RenderTargetIdentifier(CommonSet.ShaderProperties.uberTex); cb.Blit(colorRT, uberTexId, pp.uberMaterial, mainPass); if (radial) { if (aaEnable) { cb.GetTemporaryRT(CommonSet.ShaderProperties.rBlurTex, colorRT.width, colorRT.height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); var rBlurTexID = new RenderTargetIdentifier(CommonSet.ShaderProperties.rBlurTex); cb.Blit(uberTexId, rBlurTexID, pp.mMaterialRadialBlur, 0); uberTexId = rBlurTexID; } else cb.Blit(uberTexId, BuiltinRenderTextureType.CameraTarget, pp.mMaterialRadialBlur, 0); } if (aaEnable) cb.Blit(uberTexId, BuiltinRenderTextureType.CameraTarget, pp.mMaterialFXAA, (int)DragonPostProcess.FinalPass.AA); if(radial && aaEnable) cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.rBlurTex); cb.ReleaseTemporaryRT(CommonSet.ShaderProperties.uberTex); } else { cb.Blit(colorRT, BuiltinRenderTextureType.CameraTarget, pp.uberMaterial, mainPass); } if (pp.SceneBloomEnable()) pp.FinishBloom(cb, CommonSet.ShaderProperties.bloomTex); if (pp.PlayerBloomEnable()) pp.FinishBloom(cb, CommonSet.ShaderProperties.playerBloomTex); if (pp.AlphaBloomEnable() && alphaRT) { pp.FinishBloom(cb, CommonSet.ShaderProperties.alphaBloomTex); } if (pp.DofFastEnable()) FinishFastDOF(cb); cb.EndSample("UberPostProcess"); }