Commit 9c5f974a authored by Tommé Nöll's avatar Tommé Nöll
Browse files

made corruption camera scaling for zone visibility work with perspective camera

parent f4eb9b16
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class CameraZone : MonoBehaviour public class CameraZone : MonoBehaviour
{ {
public enum FitMode { public enum FitMode {
grow, grow,
shrink, shrink,
average, average,
useWidth, useWidth,
useHeight useHeight
} }
[Tooltip("How to scale the zone if the cameras aspect ration doesn't match")] [Tooltip("How to scale the zone if the cameras aspect ration doesn't match")]
public FitMode fitMode; public FitMode fitMode;
private Bounds bounds; private Bounds bounds;
private new static CameraController camera; private new static CameraController camera;
[SerializeField] [Range(0, 1f)] [SerializeField] [Range(0, 1f)]
private float triggerArea = 0.9f; private float triggerArea = 0.9f;
private bool playerWasInZone = false; private bool playerWasInZone = false;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
CalculateBounds(); CalculateBounds();
FindCamera(); FindCamera();
} }
public static void FindCamera() public static void FindCamera()
{ {
if (!camera) if (!camera)
{ {
camera = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>(); camera = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>();
} }
#if UNITY_EDITOR #if UNITY_EDITOR
camera.Init(); camera.Init();
#endif #endif
} }
public void CalculateBounds() public void CalculateBounds()
{ {
bounds = new Bounds(transform.position, transform.localScale); bounds = new Bounds(transform.position, transform.localScale);
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
Vector3 scaledPos = camera.player.transform.position; Vector3 scaledPos = camera.player.transform.position;
scaledPos = bounds.center + (scaledPos - bounds.center) / triggerArea; scaledPos = bounds.center + (scaledPos - bounds.center) / triggerArea;
ApplyTarget(bounds.Contains(scaledPos)); ApplyTarget(bounds.Contains(scaledPos));
} }
public void ApplyTarget(bool apply) public void ApplyTarget(bool apply)
{ {
if (apply) if (apply)
{ {
//if (!playerWasInZone) if (!playerWasInZone)
CalculateBounds(); {
{ if (camera.GetOrthographic())
if (camera.GetOrthographic()) {
{ camera.targetPos = transform.position;
camera.targetPos = transform.position; camera.targetSize = CalculateCameraSize(bounds, fitMode);
camera.targetSize = CalculateCameraSize(bounds, fitMode); }
} else
else {
{ camera.targetPos = CalculatePerpectiveCameraPosition(transform.position, bounds, fitMode); //calculate camera z to effectively change its size
camera.targetPos = VecZ(transform.position, -(CalculateCameraSize(bounds, fitMode) / 2) / Mathf.Tan(camera.GetFov() / 2 * Mathf.PI / 180)); }
} }
} }
} else
else {
{ if (playerWasInZone)
if (playerWasInZone) {
{ camera.ResetTarget();
camera.ResetTarget(); }
} }
}
playerWasInZone = apply;
playerWasInZone = apply; }
}
public static Vector3 CalculatePerpectiveCameraPosition(Vector3 position, Bounds bounds, FitMode fitMode)
public static float CalculateCameraSize(Bounds bounds, FitMode fitMode) {
{ return VecZ(position, -(CalculateCameraSize(bounds, fitMode) / 2) / Mathf.Tan(camera.GetFov() / 2 * Mathf.PI / 180));
float aspect = camera.GetAspectRatio(); // width / height }
//float cameraWidthScaled = bounds.size.y * aspect;
float cameraHeightScaled = bounds.size.x / aspect; public static float CalculateCameraSize(Bounds bounds, FitMode fitMode)
switch (fitMode) {
{ float aspect = camera.GetAspectRatio(); // width / height
case FitMode.useWidth : //float cameraWidthScaled = bounds.size.y * aspect;
return cameraHeightScaled; float cameraHeightScaled = bounds.size.x / aspect;
case FitMode.useHeight : switch (fitMode)
return bounds.size.y; {
case FitMode.grow : case FitMode.useWidth :
return Mathf.Max(bounds.size.y, cameraHeightScaled); return cameraHeightScaled;
case FitMode.shrink : case FitMode.useHeight :
return Mathf.Min(bounds.size.y, cameraHeightScaled); return bounds.size.y;
case FitMode.average : case FitMode.grow :
default : return Mathf.Max(bounds.size.y, cameraHeightScaled);
return (bounds.size.y + cameraHeightScaled) / 2; case FitMode.shrink :
} return Mathf.Min(bounds.size.y, cameraHeightScaled);
} case FitMode.average :
default :
void OnDrawGizmosSelected() return (bounds.size.y + cameraHeightScaled) / 2;
{ }
CalculateBounds(); }
Vector3 corner1 = new Vector3(bounds.min.x, bounds.max.y, 0); void OnDrawGizmosSelected()
Vector3 corner2 = new Vector3(bounds.max.x, bounds.min.y, 0); {
CalculateBounds();
Gizmos.color = Color.blue; Vector3 corner1 = new Vector3(bounds.min.x, bounds.max.y, 0);
Gizmos.DrawLine(VecZ(bounds.max, 0), corner1); Vector3 corner2 = new Vector3(bounds.max.x, bounds.min.y, 0);
Gizmos.DrawLine(VecZ(bounds.max, 0), corner2);
Gizmos.DrawLine(VecZ(bounds.min, 0), corner1);
Gizmos.DrawLine(VecZ(bounds.min, 0), corner2); Gizmos.color = Color.blue;
} Gizmos.DrawLine(VecZ(bounds.max, 0), corner1);
Gizmos.DrawLine(VecZ(bounds.max, 0), corner2);
private static Vector3 VecZ(Vector3 vec, float z) Gizmos.DrawLine(VecZ(bounds.min, 0), corner1);
{ Gizmos.DrawLine(VecZ(bounds.min, 0), corner2);
return new Vector3(vec.x, vec.y, z); }
}
} private static Vector3 VecZ(Vector3 vec, float z)
{
return new Vector3(vec.x, vec.y, z);
}
}
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
[ExecuteInEditMode]
public class CorruptionCamera : MonoBehaviour
{ [ExecuteInEditMode]
public CorruptionMap corruptionMap; public class CorruptionCamera : MonoBehaviour
public new Camera camera; {
public Camera mainCamera; public CorruptionMap corruptionMap;
[SerializeField] private float editorCorruptionCameraScaleFactor = 20f; public new Camera camera;
public Camera mainCamera;
// Start is called before the first frame update [SerializeField] private float editorCorruptionCameraScaleFactor = 20f;
void Start()
{ // Start is called before the first frame update
corruptionMap.Init(); void Start()
camera = GetComponent<Camera>(); {
camera.targetTexture = corruptionMap.mapTexture; corruptionMap.Init();
camera.backgroundColor = new Color(corruptionMap.globalCorruption, 0, 0, 1); camera = GetComponent<Camera>();
} camera.targetTexture = corruptionMap.mapTexture;
camera.backgroundColor = new Color(corruptionMap.globalCorruption, 0, 0, 1);
// Update is called once per frame }
void Update()
{ // Update is called once per frame
camera.orthographicSize = mainCamera.orthographicSize; void Update()
#if UNITY_EDITOR {
if (!Application.IsPlaying(gameObject)) camera.orthographicSize = mainCamera.orthographicSize;
{ #if UNITY_EDITOR
ScaleZonesVisible(); if (!Application.IsPlaying(gameObject))
camera.orthographicSize *= editorCorruptionCameraScaleFactor; {
corruptionMap.plantSurfaceMaterial.SetFloat("_CorruptionCameraScaleFactor", editorCorruptionCameraScaleFactor); ScaleZonesVisible();
} if (camera.orthographic)
#endif {
} camera.orthographicSize *= editorCorruptionCameraScaleFactor;
}
//make sure all corruption zones are being rendered in scene view, else
//by checking if they are visible to the corruption camera {
private void ScaleZonesVisible() camera.transform.position = new Vector3(camera.transform.position.x, camera.transform.position.y, mainCamera.transform.position.z * editorCorruptionCameraScaleFactor);
{ }
Bounds bounds = new Bounds(transform.position, new Vector3(0, 0, 0)); corruptionMap.plantSurfaceMaterial.SetFloat("_CorruptionCameraScaleFactor", editorCorruptionCameraScaleFactor);
foreach (Transform zoneTransform in corruptionMap.transform) }
{ #endif
Bounds zoneBounds = zoneTransform.gameObject.GetComponent<SpriteRenderer>().bounds; }
bounds.Encapsulate(zoneBounds.min);
bounds.Encapsulate(zoneBounds.max); //make sure all corruption zones are being rendered in scene view,
} //by checking if they are visible to the corruption camera
//Debug.Log(bounds.min + ", " + bounds.max); private void ScaleZonesVisible()
//match bounds center to camera position by increasing bounds size {
Vector3 min = bounds.min; /*if (!camera.orthographic)
Vector3 max = bounds.max; {
if (bounds.center.x < transform.position.x) editorCorruptionCameraScaleFactor = 1;
{ return;
max.x = 2 * transform.position.x - min.x; }*/
}
else Bounds bounds = new Bounds(transform.position, new Vector3(0, 0, 0));
{ foreach (Transform zoneTransform in corruptionMap.transform)
min.x = 2 * transform.position.x - max.x; {
} Bounds zoneBounds = zoneTransform.gameObject.GetComponent<SpriteRenderer>().bounds;
bounds.Encapsulate(zoneBounds.min);
if (bounds.center.y < transform.position.y) bounds.Encapsulate(zoneBounds.max);
{ }
max.y = 2 * transform.position.y - min.y; //Debug.Log(bounds.min + ", " + bounds.max);
} //match bounds center to camera position by increasing bounds size
else Vector3 min = bounds.min;
{ Vector3 max = bounds.max;
min.y = 2 * transform.position.y - max.y; if (bounds.center.x < transform.position.x)
} {
max.x = 2 * transform.position.x - min.x;
bounds.SetMinMax(min, max); }
else
CameraZone.FindCamera(); {
float targetSize = CameraZone.CalculateCameraSize(bounds, CameraZone.FitMode.grow); min.x = 2 * transform.position.x - max.x;
}
editorCorruptionCameraScaleFactor = Mathf.Max(1, targetSize / mainCamera.orthographicSize / 2);
if (bounds.center.y < transform.position.y)
} {
} max.y = 2 * transform.position.y - min.y;
}
else
{
min.y = 2 * transform.position.y - max.y;
}
bounds.SetMinMax(min, max);
CameraZone.FindCamera();
float targetSize = CameraZone.CalculateCameraSize(bounds, CameraZone.FitMode.grow);
editorCorruptionCameraScaleFactor = Mathf.Max(1, targetSize / mainCamera.orthographicSize / 2);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment