private void UpdateRenderTexture(int width, int height) { if (reflectionCamera == null) { return; } if (reflectionRT == null || width != reflectionRT.width || height != reflectionRT.height) { if (reflectionRT != null) { reflectionCamera.targetTexture = null; DestroyImmediate(reflectionRT); reflectionRT = null; } reflectionRT = new RenderTexture(width, height, 16, RenderTextureHelper.GetRGBTextureFormat(), RenderTextureReadWrite.sRGB); reflectionRT.filterMode = FilterMode.Bilinear; if (TextureDownSample >= 4) { reflectionRT.antiAliasing = 2; } reflectionCamera.targetTexture = reflectionRT; material.SetTexture(IShaderIds.PlaneReflectMap, reflectionRT); material.EnableKeyword("USING_REFLECTION"); material.SetFloat(IShaderIds.ReflectFresnel, ReflectFresnel); material.SetFloat(IShaderIds.ReflectThreshold, RefThreshold); } #if UNITY_EDITOR if (material != null) { material.SetFloat(IShaderIds.ReflectFresnel, ReflectFresnel); material.SetFloat(IShaderIds.ReflectThreshold, RefThreshold); } #endif }
private void UpdateCameraInfo() { var currentCamera = this.currentCamera == null ? Camera.main : this.currentCamera; if (currentCamera == null) { SetReflectEnable(false); } if (reflectEnable) { reflectionCamera.enabled = true; int width = Screen.width; int height = Screen.height; RenderTextureHelper.GetRenderTextureMaxSize(ref width, ref height); UpdateRenderTexture(width / TextureDownSample, height / TextureDownSample); //需要实时同步相机的参数,比如编辑器下滚动滚轮,Editor相机的远近裁剪面就会变化 UpdateCamearaParams(currentCamera, reflectionCamera); var reflectM = CaculateReflectMatrix(); reflectionCamera.worldToCameraMatrix = currentCamera.worldToCameraMatrix * reflectM; var normal = transform.up; var d = -Vector3.Dot(normal, transform.position); var plane = new Vector4(normal.x, normal.y, normal.z, d); //用逆转置矩阵将平面从世界空间变换到反射相机空间 reflectionCamera.projectionMatrix = CaculateObliqueViewFrustumMatrix(plane); } UpdateCameraEnable(); }
void IPool.OnGet(params object[] args) { try { var width = (int)args[0]; var high = (int)args[1]; RenderTexture = RenderTextureHelper.GetRT(width, high); } catch (Exception) { RenderTexture = null; } }
private void ReleaseRT() { RenderTextureHelper.ReleaseRT(RenderTexture); RenderTexture = null; }