CanvasRect.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class CanvasRect : CanvasMesh
  6. {
  7. protected override Vector3 MeshInverseTransform(Vector3 localPosition)
  8. {
  9. return localPosition;
  10. }
  11. protected override void GenerateMesh(out List<Vector3> verts,
  12. out List<int> tris,
  13. out List<Vector2> uvs)
  14. {
  15. verts = new List<Vector3>();
  16. tris = new List<int>();
  17. uvs = new List<Vector2>();
  18. var resolution = _canvasRenderTexture.GetBaseResolutionToUse();
  19. Vector2 worldSize = new Vector2(
  20. _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.x)),
  21. _canvasRenderTexture.PixelsToUnits(Mathf.RoundToInt(resolution.y))
  22. ) / transform.lossyScale;
  23. float xPos = worldSize.x * 0.5f;
  24. float xNeg = -xPos;
  25. float yPos = worldSize.y * 0.5f;
  26. float yNeg = -yPos;
  27. verts.Add(new Vector3(xNeg, yNeg, 0));
  28. verts.Add(new Vector3(xNeg, yPos, 0));
  29. verts.Add(new Vector3(xPos, yPos, 0));
  30. verts.Add(new Vector3(xPos, yNeg, 0));
  31. tris.Add(0);
  32. tris.Add(1);
  33. tris.Add(2);
  34. tris.Add(0);
  35. tris.Add(2);
  36. tris.Add(3);
  37. uvs.Add(new Vector2(0, 0));
  38. uvs.Add(new Vector2(0, 1));
  39. uvs.Add(new Vector2(1, 1));
  40. uvs.Add(new Vector2(1, 0));
  41. }
  42. }
  43. }