/*
* @author Valentin Simonov / http://va.lent.in/
*/
using TouchScript.Utils.Geom;
using UnityEngine;
namespace TouchScript.Layers
{
///
/// specific projection parameters. Used by layers to project pointers in the world and world coordinates onto layers.
///
public class ProjectionParams
{
///
/// Projects a screen point on a 3D plane.
///
/// Screen point.
/// Projection plane.
/// Projected point in 3D.
public virtual Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
{
return ProjectionUtils.ScreenToPlaneProjection(screenPosition, projectionPlane);
}
///
/// Projects a world point onto layer.
///
/// World position.
/// 2D point on layer.
public virtual Vector2 ProjectFrom(Vector3 worldPosition)
{
return new Vector2(worldPosition.x, worldPosition.y);
}
}
///
/// Projection parameters for a camera based .
///
public class CameraProjectionParams : ProjectionParams
{
///
/// Camera used for projection.
///
protected Camera camera;
///
/// Initializes a new instance of the class.
///
/// The camera.
public CameraProjectionParams(Camera camera)
{
this.camera = camera;
}
///
public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
{
return ProjectionUtils.CameraToPlaneProjection(screenPosition, camera, projectionPlane);
}
///
public override Vector2 ProjectFrom(Vector3 worldPosition)
{
return camera.WorldToScreenPoint(worldPosition);
}
}
///
/// Projection parameters for a UI based .
///
public class WorldSpaceCanvasProjectionParams : ProjectionParams
{
///
/// The canvas.
///
protected Canvas canvas;
///
/// Canvas mode.
///
protected RenderMode mode;
///
/// Canvas camera.
///
protected Camera camera;
///
/// Initializes a new instance of the class.
///
/// The canvas.
public WorldSpaceCanvasProjectionParams(Canvas canvas)
{
this.canvas = canvas;
mode = canvas.renderMode;
camera = canvas.worldCamera ?? Camera.main;
}
///
public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
{
return ProjectionUtils.CameraToPlaneProjection(screenPosition, camera, projectionPlane);
}
///
public override Vector2 ProjectFrom(Vector3 worldPosition)
{
return camera.WorldToScreenPoint(worldPosition);
}
}
}