Skip to content

Commit

Permalink
update only when any handle is moved.
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed Sep 15, 2017
1 parent 9d8092e commit 20734e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Assets/uHomography/Scripts/DraggableVertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public class DraggableVertex
, IBeginDragHandler
, IDragHandler
{
public Camera camera;
public new Camera camera;

Vector3 startPos_;
Vector3 startMousePos_;

public bool hasChanged { get; set; }

public Vector2 viewPosition
{
get
Expand All @@ -39,6 +41,7 @@ public void OnDrag(PointerEventData data)
var startPos = camera.ScreenToWorldPoint(new Vector3(startMousePos_.x, startMousePos_.y, z));
var dPos = currentPos - startPos;
transform.localPosition = startPos_ + dPos;
hasChanged = true;
}
}

Expand Down
34 changes: 26 additions & 8 deletions Assets/uHomography/Scripts/Homography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public class Homography : MonoBehaviour
{
const string VertexPrefabPath = "uHomography/Prefabs/Vertex";

public bool showHandles = true;

[SerializeField]
KeyCode toggleKey = KeyCode.None;
KeyCode handleToggleKey = KeyCode.None;

bool isHandlesVisible_ = true;

Expand Down Expand Up @@ -41,6 +39,9 @@ public class Homography : MonoBehaviour
[SerializeField, HideInInspector]
DraggableVertex v11;

float[] homography_ = null;
float[] invHomography_ = null;

void CreateCameraIfNeeded()
{
if (uiCamera) return;
Expand Down Expand Up @@ -159,9 +160,21 @@ float[] CalcInverseMatrix(float[] mat)
return new float[] { o11, o12, o13, o21, o22, o23, o31, o32, o33 };
}

void UpdateHomographyMatrix()
{
homography_ = CalcHomographyMatrix();
invHomography_ = CalcInverseMatrix(homography_);
v00.hasChanged = v01.hasChanged = v10.hasChanged = v11.hasChanged = false;
}

void Start()
{
UpdateHomographyMatrix();
}

void Update()
{
if (Input.GetKeyDown(toggleKey))
if (Input.GetKeyDown(handleToggleKey))
{
isHandlesVisible_ = !isHandlesVisible_;
v00.gameObject.SetActive(isHandlesVisible_);
Expand All @@ -187,10 +200,15 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)

CreateMaterialIfNeeded();

var homography = CalcHomographyMatrix();
var invHomography = CalcInverseMatrix(homography);
material.SetFloatArray("_Homography", homography);
material.SetFloatArray("_InvHomography", invHomography);
bool isHandleMoved = (v00.hasChanged || v01.hasChanged || v10.hasChanged || v11.hasChanged);
bool isNotInitialized = (homography_.Length == 0) || (invHomography_.Length == 0);
if (isHandleMoved || isNotInitialized)
{
UpdateHomographyMatrix();
}

material.SetFloatArray("_Homography", homography_);
material.SetFloatArray("_InvHomography", invHomography_);
Graphics.Blit(source, destination, material);
}
}
Expand Down

0 comments on commit 20734e3

Please sign in to comment.