Skip to content

Commit

Permalink
Merge pull request #149 from Autumn60/feature/commonPublisher
Browse files Browse the repository at this point in the history
Feature/common publisher
  • Loading branch information
Autumn60 authored Feb 19, 2024
2 parents 07bee60 + 0b91263 commit 46fde31
Show file tree
Hide file tree
Showing 33 changed files with 296 additions and 159 deletions.
62 changes: 62 additions & 0 deletions Assets/UnitySensors/Editor/Attributes/InterfaceDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using UnityEngine;
using UnityEditor;

namespace UnitySensors.Attribute
{
[CustomPropertyDrawer(typeof(InterfaceAttribute))]
public class InterfaceTypeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
InterfaceAttribute ia = attribute as InterfaceAttribute;

if (property.propertyType != SerializedPropertyType.ObjectReference) return;

MonoBehaviour old = property.objectReferenceValue as MonoBehaviour;

GameObject temp = null;
string oldName = "";

if (Event.current.type == EventType.Repaint)
{
if (old == null)
{
temp = new GameObject("None [" + ia.type.Name + "]");
old = temp.AddComponent<Interface>();
}
else
{
oldName = old.name;
old.name = oldName + " [" + ia.type.Name + "]";
}
}

MonoBehaviour present = EditorGUI.ObjectField(position, label, old, typeof(MonoBehaviour), true) as MonoBehaviour;

if (Event.current.type == EventType.Repaint)
{
if (temp != null)
GameObject.DestroyImmediate(temp);
else
old.name = oldName;
}

if (old == present) return;

if (present != null)
{
if (present.GetType() != ia.type)
present = present.gameObject.GetComponent(ia.type) as MonoBehaviour;

if (present == null) return;
}

property.objectReferenceValue = present;
property.serializedObject.ApplyModifiedProperties();
}
}

public class Interface : MonoBehaviour
{
}
}
11 changes: 11 additions & 0 deletions Assets/UnitySensors/Editor/Attributes/InterfaceDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 6023160553514482212}
_image: {fileID: 0}
_sourceTexture: 0
_image: {fileID: 0}
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 4070371534656761714}
_image: {fileID: 0}
_sourceTexture: 0
_image: {fileID: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEngine;
using System;

namespace UnitySensors.Attribute
{
public class InterfaceAttribute : PropertyAttribute
{
public Type type;

public InterfaceAttribute(Type type)
{
this.type = type;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
using UnityEngine;
using UnitySensors.Attribute;
using UnitySensors.DataType.Sensor.PointCloud;
using UnitySensors.Interface.Sensor;

namespace UnitySensors.Visualization.Sensor
{
public class DepthCameraPointCloudVisualizer : PointCloudVisualizer<PointXYZ>
{
[SerializeField, Interface(typeof(IPointCloudInterface<PointXYZ>))]
private Object _source;

protected override void Start()
{
base.SetSource(_source as IPointCloudInterface<PointXYZ>);
base.Start();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
using UnityEngine;
using UnitySensors.Attribute;
using UnitySensors.DataType.Sensor.PointCloud;
using UnitySensors.Interface.Sensor;

namespace UnitySensors.Visualization.Sensor
{
public class LiDARPointCloudVisualizer : PointCloudVisualizer<PointXYZI>
{
[SerializeField, Interface(typeof(IPointCloudInterface<PointXYZI>))]
private Object _source;

protected override void Start()
{
base.SetSource(_source as IPointCloudInterface<PointXYZI>);
base.Start();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
using UnityEngine;
using UnitySensors.Attribute;
using UnitySensors.DataType.Sensor.PointCloud;
using UnitySensors.Interface.Sensor;

namespace UnitySensors.Visualization.Sensor
{
public class RGBDCameraPointCloudVisualizer : PointCloudVisualizer<PointXYZRGB>
{
[SerializeField, Interface(typeof(IPointCloudInterface<PointXYZRGB>))]
private Object _source;

protected override void Start()
{
base.SetSource(_source as IPointCloudInterface<PointXYZRGB>);
base.Start();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using UnityEngine;
using UnitySensors.Attribute;
using UnitySensors.Interface.Sensor;
using UnitySensors.Interface.Sensor.PointCloud;
using UnitySensors.Sensor;
using UnitySensors.Utils.PointCloud;

namespace UnitySensors.Visualization.Sensor
{
public class PointCloudVisualizer<T> : Visualizer where T : struct, IPointInterface
{
private IPointCloudInterface<T> _sourceInterface;
private Transform _transform;
private IPointCloudInterface<T> _source;

private Material _mat;
private Mesh _mesh;
Expand All @@ -19,11 +21,14 @@ public class PointCloudVisualizer<T> : Visualizer where T : struct, IPointInterf
private int _cachedPointsCount = -1;
private int _bufferSize;

protected override void Init(MonoBehaviour source)
public void SetSource(IPointCloudInterface<T> sourceInterface)
{
Debug.Assert(source is IPointCloudInterface<T>, "No compatibility between source and visualizer.", this);
_transform = source.transform;
_source = (IPointCloudInterface<T>)source;
_sourceInterface = sourceInterface;
}

protected virtual void Start()
{
_transform = this.transform;
_bufferSize = PointUtilities.pointDataSizes[typeof(T)];
_mat = new Material(Shader.Find(PointUtilities.shaderNames[typeof(T)]));
_mat.renderQueue = 3000;
Expand All @@ -36,9 +41,9 @@ protected override void Init(MonoBehaviour source)

protected override void Visualize()
{
if (_source.pointsNum != _cachedPointsCount) UpdateBuffers();
if (_sourceInterface.pointsNum != _cachedPointsCount) UpdateBuffers();
_mat.SetMatrix("LocalToWorldMatrix", _transform.localToWorldMatrix);
_pointsBuffer.SetData(_source.pointCloud.points);
_pointsBuffer.SetData(_sourceInterface.pointCloud.points);
}

private void Update()
Expand All @@ -49,16 +54,16 @@ private void Update()
private void UpdateBuffers()
{
if (_pointsBuffer != null) _pointsBuffer.Release();
_pointsBuffer = new ComputeBuffer(_source.pointsNum, _bufferSize);
_pointsBuffer.SetData(_source.pointCloud.points);
_pointsBuffer = new ComputeBuffer(_sourceInterface.pointsNum, _bufferSize);
_pointsBuffer.SetData(_sourceInterface.pointCloud.points);
_mat.SetBuffer("PointsBuffer", _pointsBuffer);

uint numIndices = (_mesh != null) ? (uint)_mesh.GetIndexCount(0) : 0;
_args[0] = numIndices;
_args[1] = (uint)_source.pointsNum;
_args[1] = (uint)_sourceInterface.pointsNum;
_argsBuffer.SetData(_args);

_cachedPointsCount = _source.pointsNum;
_cachedPointsCount = _sourceInterface.pointsNum;
}

private void OnDisable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
using UnitySensors.Attribute;
using UnitySensors.Sensor;
using UnitySensors.Interface.Sensor;

Expand All @@ -13,23 +14,28 @@ private enum SourceTexture
Texture1
}

[SerializeField]
private RawImage _image;
[SerializeField, Interface(typeof(ITextureInterface))]
private Object _source;
[SerializeField]
private SourceTexture _sourceTexture;
[SerializeField]
private RawImage _image;

private ITextureInterface _source;
private ITextureInterface _sourceInterface;

protected override void Init(MonoBehaviour source)
private void Start()
{
Debug.Assert(source is ITextureInterface, "No compatibility between source and visualizer.", this);
_source = (ITextureInterface)source;
_sourceInterface = _source as ITextureInterface;
if(_source is UnitySensor)
{
(_source as UnitySensor).onSensorUpdated += Visualize;
}
}

protected override void Visualize()
{
if (!_image) return;
_image.texture = _sourceTexture == SourceTexture.Texture0 ? _source.texture0 : _source.texture1;
_image.texture = _sourceTexture == SourceTexture.Texture0 ? _sourceInterface.texture0 : _sourceInterface.texture1;
}
}
}
16 changes: 1 addition & 15 deletions Assets/UnitySensors/Runtime/Scripts/Visualizers/Visualizer.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
using UnityEngine;
using UnitySensors.Attribute;
using UnitySensors.Sensor;

namespace UnitySensors.Visualization
{
public abstract class Visualizer : MonoBehaviour
{
[SerializeField]
private MonoBehaviour _source;

private void Start()
{
if(_source is UnitySensor)
{
UnitySensor sensor = (UnitySensor)_source;
sensor.onSensorUpdated += Visualize;
}

Init(_source);
}

protected abstract void Init(MonoBehaviour source);
protected abstract void Visualize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ca85518969b9bf94aa9948188d795075, type: 3}
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 4853532333615571030}
_frequency: 10
_topicName: /camera/depth/info
_serializer:
_source: {fileID: 4853532333615571030}
_header:
_source: {fileID: 4853532333615571030}
_frame_id: /camera_frame
--- !u!114 &9201004833233192004
MonoBehaviour:
Expand All @@ -127,12 +128,13 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4840ee4519c33a142a3fb06d6d6b3b50, type: 3}
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 4853532333615571030}
_frequency: 10
_topicName: /camera/depth/points
_serializer:
_header:
_source: {fileID: 4853532333615571030}
_frame_id: /camera_frame
_source: {fileID: 4853532333615571030}
--- !u!114 &3086342091309557783
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -145,11 +147,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b474a6242e811f04488e3cb67b65dd35, type: 3}
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 4853532333615571030}
_frequency: 10
_topicName: /camera/depth/image/compressed
_serializer:
_source: {fileID: 4853532333615571030}
_sourceTexture: 0
_header:
_source: {fileID: 4853532333615571030}
_frame_id: /camera_frame
quality: 75
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ca85518969b9bf94aa9948188d795075, type: 3}
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 2016422477619200576}
_frequency: 10
_topicName: /camera/color/info
_serializer:
_source: {fileID: 2016422477619200576}
_header:
_source: {fileID: 2016422477619200576}
_frame_id: /camera_frame
--- !u!114 &7476313626036288691
MonoBehaviour:
Expand All @@ -125,11 +126,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b474a6242e811f04488e3cb67b65dd35, type: 3}
m_Name:
m_EditorClassIdentifier:
_source: {fileID: 2016422477619200576}
_frequency: 10
_topicName: /camera/color/image/compressed
_serializer:
_source: {fileID: 2016422477619200576}
_sourceTexture: 0
_header:
_source: {fileID: 2016422477619200576}
_frame_id: /camera_frame
quality: 75
Loading

0 comments on commit 46fde31

Please sign in to comment.