ImageSubscriberMod.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // Modified by Lydia Ebbinghaus & Jana-Sophie schönfeld.
  15. using UnityEngine;
  16. namespace RosSharp.RosBridgeClient
  17. {
  18. [RequireComponent(typeof(RosConnector))]
  19. public class ImageSubscriberMod : UnitySubscriber<MessageTypes.Sensor.CompressedImage>
  20. {
  21. public MeshRenderer meshRenderer;
  22. public bool stopProcessing;
  23. private Texture2D texture2D;
  24. // Modified.
  25. // Changed to public to check if imageMessage is received and to get image data.
  26. public byte[] imageData;
  27. public bool isMessageReceived;
  28. // Added to allow subscriber to process messages.
  29. public bool oneMessageReceived;
  30. protected override void Start()
  31. {
  32. base.Start();
  33. texture2D = new Texture2D(1, 1);
  34. if(!stopProcessing)meshRenderer.material = new Material(Shader.Find("Standard"));
  35. }
  36. private void Update()
  37. {
  38. if (!stopProcessing && isMessageReceived)
  39. ProcessMessage();
  40. }
  41. protected override void ReceiveMessage(MessageTypes.Sensor.CompressedImage compressedImage)
  42. {
  43. imageData = compressedImage.data;
  44. isMessageReceived = true;
  45. // Modified.
  46. oneMessageReceived=true;
  47. }
  48. private void ProcessMessage()
  49. {
  50. texture2D.LoadImage(imageData);
  51. texture2D.Apply();
  52. meshRenderer.material.SetTexture("_MainTex", texture2D);
  53. isMessageReceived = false;
  54. }
  55. }
  56. }