CameraJpegPubExistingTexture.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using Unity.Collections;
  6. using ROS2;
  7. /*
  8. * This class is the same as CameraJpegPub only the rendertexture is already defined and assigned to the Camera
  9. * rendertexture MUST be R8G8B8_UNORM
  10. */
  11. [RequireComponent(typeof(Camera))]
  12. public class CameraJpegPubExistingTexture : MonoBehaviour
  13. {
  14. private Queue<AsyncGPUReadbackRequest> requests = new Queue<AsyncGPUReadbackRequest>();
  15. public float timeBetweenRequests = 0.2f;
  16. public string TopicName = "rt/jaja/image_raw/compressed";
  17. public string frame_id = null;
  18. [Range(0, 100)]
  19. public int JpgQuality = 50;
  20. private Camera MyCamera;
  21. private Texture2D tex;
  22. private RenderTexture m_RenderTexture;
  23. private float timer = 0.0f;
  24. private float[] depth_result;
  25. private ROS2UnityComponent ros2Unity;
  26. private ROS2Node ros2Node;
  27. private IPublisher<sensor_msgs.msg.CompressedImage> chatter_pub;
  28. void Start()
  29. {
  30. ros2Unity = GetComponent<ROS2UnityComponent>();
  31. if (ros2Unity.Ok())
  32. {
  33. if (ros2Node == null)
  34. {
  35. ros2Node = ros2Unity.CreateNode("ROS2LFRcamera");
  36. chatter_pub = ros2Node.CreatePublisher<sensor_msgs.msg.CompressedImage>(TopicName);
  37. }
  38. }
  39. MyCamera = GetComponent<Camera>();
  40. MyCamera.rect = new Rect(0f, 0f, 1f, 1f);
  41. // create RenderTexture and assign it to the main camera
  42. m_RenderTexture = MyCamera.targetTexture; // get it instead of create it
  43. MyCamera.pixelRect = new Rect(0, 0, m_RenderTexture.width, m_RenderTexture.height);
  44. MyCamera.Render();
  45. tex = new Texture2D(MyCamera.pixelWidth, MyCamera.pixelHeight, TextureFormat.BGRA32, false);
  46. Debug.Log("In start, camsize width:" + MyCamera.pixelWidth.ToString() + " height:" + MyCamera.pixelHeight.ToString());
  47. }
  48. void Update()
  49. {
  50. // Handle Request Queue
  51. while (requests.Count > 0)
  52. {
  53. // Get the first Request in the Queue
  54. AsyncGPUReadbackRequest request = requests.Peek();
  55. if (request.hasError)
  56. {
  57. // Error!
  58. Debug.LogWarning("AsyncGPUReadbackRequest Error! :(");
  59. requests.Dequeue(); // Remove from Queue
  60. }
  61. else if (request.done)
  62. {
  63. ReadbackData(request);
  64. requests.Dequeue(); // Remove from Queue
  65. }
  66. else
  67. {
  68. // Request is still processing.
  69. break;
  70. }
  71. // Note : We have to Dequeue items or break,
  72. // or we'll be caught in an infinite loop!
  73. }
  74. // Handle Request Timer
  75. timer += Time.deltaTime;
  76. if (timer > timeBetweenRequests)
  77. {
  78. timer = timer - timeBetweenRequests;
  79. AsyncGPUReadbackRequest rq = AsyncGPUReadback.Request(m_RenderTexture);
  80. requests.Enqueue(rq);
  81. }
  82. }
  83. void ReadbackData(AsyncGPUReadbackRequest request)
  84. {
  85. int len = request.GetData<Color32>().ToArray().Length;
  86. tex.SetPixels32(request.GetData<Color32>().ToArray());
  87. tex.Apply();
  88. sensor_msgs.msg.CompressedImage img = new sensor_msgs.msg.CompressedImage();
  89. img.Header = GetHeader();
  90. img.Format = "jpeg";
  91. img.Data = tex.EncodeToJPG(JpgQuality);
  92. if (ros2Unity.Ok())
  93. {
  94. chatter_pub.Publish(img);
  95. }
  96. }
  97. std_msgs.msg.Header GetHeader()
  98. {
  99. System.DateTime now = System.DateTime.UtcNow;
  100. std_msgs.msg.Header header = new std_msgs.msg.Header();
  101. builtin_interfaces.msg.Time time = new builtin_interfaces.msg.Time();
  102. time.Sec = now.Second;
  103. header.Stamp = time;
  104. header.Frame_id = frame_id;
  105. return header;
  106. }
  107. }