Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. //transformedVertices = new Vector2[newGeometryEstimate];
  2. transformedVertices.EnsureCapacity(newGeometryEstimate);
  3.  
  4. // Based on the algorithm described above, this will create a matrix permitting us
  5. //to multiply a given vertex yielding a vertex transformed to an XY plane (where Z is
  6. //undefined.)
  7. // This algorithm cannot work if we're already in that plane. We know if we're already
  8. //in that plane if X and Y are both zero and Z is nonzero.
  9. bool canUseSimplifiedTransform = Mathf.Approximately(plane.x, 0f) && Mathf.Approximately(plane.y, 0f);
  10. if (!canUseSimplifiedTransform) {
  11. Matrix4x4 flattenTransform;
  12.  
  13. Vector3 v1 = Vector3.forward;
  14. Vector3 v2 = new Vector3(plane.x, plane.y, plane.z).normalized;
  15. Vector3 v3 = Vector3.Cross(v1, v2).normalized;
  16. Vector3 v4 = Vector3.Cross(v3, v1);
  17.  
  18. float cos = Vector3.Dot(v2, v1);
  19. float sin = Vector3.Dot(v2, v4);
  20.  
  21. Matrix4x4 m1 = Matrix4x4.identity;
  22. m1.SetRow(0, (Vector4)v1);
  23. m1.SetRow(1, (Vector4)v4);
  24. m1.SetRow(2, (Vector4)v3);
  25.  
  26. Matrix4x4 m1i = m1.inverse;
  27.  
  28. Matrix4x4 m2 = Matrix4x4.identity;
  29. m2.SetRow(0, new Vector4(cos, sin, 0, 0));
  30. m2.SetRow(1, new Vector4(-sin, cos, 0, 0));
  31.  
  32. flattenTransform = m1i * m2 * m1;
  33. for (int i = 0; i < newVertices.Count; i++) {
  34. transformedVertices.AddUnsafe((Vector2)flattenTransform.MultiplyPoint3x4(newVertices[i]));
  35. }
  36. }
  37. else {
  38. for (int i = 0; i < newVertices.Count; i++) {
  39. transformedVertices.AddUnsafe(new Vector2(newVertices[i].x, -newVertices[i].y));
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement