- Home /
Split Screen where Waterplane intersects with Camera
Hi!
As mentioned here http://forum.unity3d.com/threads/39292-Request-Special-Underwater-Blurring-Script, I have two cameras in my scene, one for the underwater-effect and one for the over-water game. I have a 3rd person controller, and if I swim or wade in the water the camera is able to rotate around the player and dive into it. Therefore I want the screen to split on the line, where the water intersects with the camera. Is there a easy way to find the point of the intersection of the water and the camera and set that as the split value?
Thank you!
Answer by Cherno · May 18, 2014 at 03:57 PM
Sounds like you just need the compare the camera's y position and the water plane's y position.
Hi! Thank you for your answer!
Yes, I surely did that. Works perfectly if the $$anonymous$$ouseLook-Asis is only on X. But thus I have the $$anonymous$$ouseLook on X and Y Axes, the camera can 'dive' into the water even though it's not under the water surface (because of the angle). Is there a possibility to solve this problem?
Here's my code, attached to a child-objcet camera of the $$anonymous$$ainCam:
using UnityEngine;
using System.Collections;
public class CamSplit : $$anonymous$$onoBehaviour
{
public GameObject water;
public float split;
public float maxViewHeight;
Camera topCam;
void Start ()
{
Transform topTrans = transform.parent;
topCam = (Camera) topTrans.gameObject.GetComponent("Camera");
}
void Update ()
{
split = -(0.5f + ((1/(maxViewHeight*2)) * (transform.position.y - water.transform.position.y)));
if (split <= 0)
{
split = 0.0001f;
}
split = $$anonymous$$athf.Clamp01(split);
float halfHeight = $$anonymous$$athf.Tan(camera.fieldOfView * $$anonymous$$athf.Deg2Rad * .5f) * camera.nearClipPlane;
float upperTop = halfHeight;
float upperBottom = (split - .5f) * halfHeight * 2f;
float lowerTop = upperBottom;
float lowerBottom = -halfHeight;
$$anonymous$$atrix4x4 upper$$anonymous$$at = topCam.projection$$anonymous$$atrix;
upper$$anonymous$$at[1, 1] = (2f * camera.nearClipPlane) / (upperTop - upperBottom);
upper$$anonymous$$at[1, 2] = (upperTop + upperBottom) / (upperTop - upperBottom);
topCam.projection$$anonymous$$atrix = upper$$anonymous$$at;
$$anonymous$$atrix4x4 lower$$anonymous$$at = camera.projection$$anonymous$$atrix;
lower$$anonymous$$at[1, 1] = (2f * camera.nearClipPlane) / (lowerTop - lowerBottom);
lower$$anonymous$$at[1, 2] = (lowerTop + lowerBottom) / (lowerTop - lowerBottom);
camera.projection$$anonymous$$atrix = lower$$anonymous$$at;
Rect botCamRect = camera.rect;
botCamRect.height = split;
camera.rect = botCamRect;
Rect topCamRect = topCam.rect;
topCamRect.height = 1f - split;
topCamRect.y = split;
topCam.rect = topCamRect;
}
}
I am not sure what you mean, are you talking about how you can see the submerged area under teh water surface if you are a little bit higher than the surface? If so, from looking at how many AAA titles still have this problem, I guess there's no easy solution.
Your answer
Follow this Question
Related Questions
Camera.ScreenToWorldPoint Not Giving Right Point 3 Answers
Clicking Trigger 1 Answer
Lock the screen 1 Answer
Camera Shake 4 Answers