/// <summary> /// Refraction RenderTexture /// </summary> private void DrawRefractionRenderTexture(Camera cam) { CreateRenderTexAndCam(cam, ref _refractionRenderTex, ref _refractionCamera); WaterHelper.CloneCameraModes(cam, _refractionCamera); Vector3 pos = transform.position; Vector3 normal = transform.up; Matrix4x4 projection = cam.worldToCameraMatrix; projection *= Matrix4x4.Scale(new Vector3(1, Mathf.Clamp(1 - RefractionAngle, 0.001f, 1), 1)); _refractionCamera.worldToCameraMatrix = projection; Vector4 clipPlane = WaterHelper.CameraSpacePlane(_refractionCamera, pos, normal, -1.0f, 0); projection = cam.projectionMatrix; /*projection[2] = clipPlane.x + projection[3];//x * projection[6] = clipPlane.y + projection[7];//y * projection[10] = clipPlane.z + projection[11];//z * projection[14] = clipPlane.w + projection[15];//w* * _refractionCamera.projectionMatrix = projection;*/ _refractionCamera.projectionMatrix = WaterHelper.CalculateObliqueMatrix(projection, clipPlane); _refractionCamera.cullingMask = ~(1 << 4) & Layers.value; // never render water layer _refractionCamera.targetTexture = _refractionRenderTex; _refractionCamera.transform.position = cam.transform.position; _refractionCamera.transform.eulerAngles = cam.transform.eulerAngles; _refractionCamera.Render(); }
private void DrawReflectionRenderTexture(Camera cam) { Vector3 pos = transform.position; Vector3 normal = transform.up; CreateRenderTexAndCam(cam, ref _reflectionRenderTex, ref _reflectionCamera); WaterHelper.CloneCameraModes(cam, _reflectionCamera); float d = -Vector3.Dot(normal, pos) - reflectClipPlaneOffset; Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d); Matrix4x4 reflection = WaterHelper.CalculateReflectionMatrix(Matrix4x4.zero, reflectionPlane); Vector3 oldpos = cam.transform.position; Vector3 newpos = reflection.MultiplyPoint(oldpos); _reflectionCamera.worldToCameraMatrix = cam.worldToCameraMatrix * reflection; // Setup oblique projection matrix so that near plane is our reflection // plane. This way we clip everything below/above it for free. Vector4 clipPlane = WaterHelper.CameraSpacePlane(_reflectionCamera, pos, normal, 1.0f, reflectClipPlaneOffset); Matrix4x4 projection = cam.projectionMatrix; projection = WaterHelper.CalculateObliqueMatrix(projection, clipPlane); //Matrix4x4 projection = cam.CalculateObliqueMatrix(clipPlane); _reflectionCamera.projectionMatrix = projection; _reflectionCamera.cullingMask = ~(1 << 4) & Layers.value; // never render water layer _reflectionCamera.targetTexture = _reflectionRenderTex; GL.invertCulling = true; _reflectionCamera.transform.position = newpos; Vector3 euler = cam.transform.eulerAngles; _reflectionCamera.transform.eulerAngles = new Vector3(0, euler.y, euler.z); _reflectionCamera.Render(); _reflectionCamera.transform.position = oldpos; GL.invertCulling = false; }