ImageSubscriber.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. © Siemens AG, 2017-2018
  3. Author: Dr. Martin Bischoff (martin.bischoff@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. using UnityEngine;
  15. namespace RosSharp.RosBridgeClient
  16. {
  17. [RequireComponent(typeof(RosConnector))]
  18. public class ImageSubscriber : UnitySubscriber<MessageTypes.Sensor.CompressedImage>
  19. {
  20. public MeshRenderer meshRenderer;
  21. private Texture2D texture2D;
  22. private byte[] imageData;
  23. private bool isMessageReceived;
  24. protected override void Start()
  25. {
  26. base.Start();
  27. texture2D = new Texture2D(1, 1);
  28. meshRenderer.material = new Material(Shader.Find("Standard"));
  29. }
  30. private void Update()
  31. {
  32. if (isMessageReceived)
  33. ProcessMessage();
  34. }
  35. protected override void ReceiveMessage(MessageTypes.Sensor.CompressedImage compressedImage)
  36. {
  37. imageData = compressedImage.data;
  38. isMessageReceived = true;
  39. }
  40. private void ProcessMessage()
  41. {
  42. texture2D.LoadImage(imageData);
  43. texture2D.Apply();
  44. meshRenderer.material.SetTexture("_MainTex", texture2D);
  45. isMessageReceived = false;
  46. }
  47. }
  48. }