Skip to content

Commit

Permalink
Fix wrong value in Segmentation Camera
Browse files Browse the repository at this point in the history
- Disable PostProcessig in SegmentationRenderer -> cause wrong pixel info
- Remove Encode16BitsToRG() and Encode16BitsToGR() in ColorEncoding
- Store classId value only in MPB for SegmentationShader
- Modify SegmentationShader to handle class id value and
  encode class id value to 16bits and store R/G value in little-endian order
- Modify SegmentationCamera Device
  - targetColorFormat, GraphicsFormat.R8G8_UNorm -> R8G8B8A8_UNorm
  • Loading branch information
hyunseok-yang committed Nov 23, 2024
1 parent 226beb1 commit 7029d5b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MonoBehaviour:
- {fileID: -5373475574186842855}
m_RendererFeatureMap: 19e55a676f946db5
m_UseNativeRenderPass: 0
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
postProcessData: {fileID: 0}
shaders:
blitPS: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3}
copyDepthPS: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
Expand Down
22 changes: 12 additions & 10 deletions Assets/Resources/Shader/Segmentation.shader
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ Shader "Sensor/Segmentation"
{
Properties
{
_SegmentationColor ("Segmentation Color", Color) = (1, 1, 1, 1)
_SegmentationClassId ("Segmentation Class ID Value in 16bits", Color) = (0, 0, 0, 1)
[Toggle] _DisableColor ("Disable Color method", int) = 0
_SegmentationValue ("Segmentation Value", int) = 0
[Toggle] _Hide ("Hide this label", int) = 0
}

Expand All @@ -24,9 +22,7 @@ Shader "Sensor/Segmentation"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

CBUFFER_START(UnityPerMaterial)
half4 _SegmentationColor;
half4 _SegmentationClassId;
int _DisableColor;
int _SegmentationValue;
int _Hide;
CBUFFER_END

Expand All @@ -38,18 +34,18 @@ Shader "Sensor/Segmentation"

HLSLPROGRAM

#pragma target 4.5
#pragma target 4.6
#pragma vertex vert
#pragma fragment frag

struct Attributes
{
float4 positionOS : POSITION;
half4 positionOS : POSITION;
};

struct Varyings
{
float4 positionCS : SV_POSITION;
half4 positionCS : SV_POSITION;
};

Varyings vert(Attributes i)
Expand All @@ -65,7 +61,13 @@ Shader "Sensor/Segmentation"
half4 segColor = half4(0, 0, 0, 0);
if (_Hide == 0)
{
segColor = (_DisableColor == 0)? _SegmentationColor : _SegmentationClassId;
// Encode 16Bits To RG
float R = ((_SegmentationValue >> 8) & 0xFF) / 255.0;
float G = (_SegmentationValue & 0xFF) / 255.0;

// Due to little-endian data, SWAP
segColor.r = G;
segColor.g = R;
}
return segColor;
}
Expand Down
18 changes: 2 additions & 16 deletions Assets/Scripts/Core/Modules/ColorEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static Color EncodeTagAsColor(in string tag)
var r = (byte)(hash >> 16);
var g = (byte)(hash >> 8);
var b = (byte)(hash);
// Debug.Log($"EncodeTagAsColor({tag}): {r} {g} {b}");
return new Color32(r, g, b, 255);
// Debug.Log($"EncodeTagAsColor({tag}): {r} {g} {b} {a}");
return new Color32(r, g, b, a);
}

public static Color EncodeLayerAsColor(in int layer)
Expand All @@ -84,18 +84,4 @@ public static Color EncodeLayerAsColor(in int layer)

return color;
}

public static Color Encode16BitsToRG(in UInt16 value)
{
var classIdR = (byte)((value >> 8) & 0xFF);
var classIdG = (byte)(value & 0xFF);
return new Color32(classIdR, classIdG, 0, 0);
}

// for little endian order
public static Color Encode16BitsToGR(in UInt16 value)
{
var rgColor = Encode16BitsToRG(value);
return new Color(rgColor.g, rgColor.r, 0, 0);
}
}
15 changes: 4 additions & 11 deletions Assets/Scripts/Core/Modules/SegmentationTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,12 @@ public void Refresh()
break;
}

var grayscale = color.grayscale;

_classId = (UInt16)(grayscale * UInt16.MaxValue);

var classValue = ColorEncoding.Encode16BitsToGR(_classId);

mpb.SetColor("_SegmentationColor", color);
mpb.SetColor("_SegmentationClassId", classValue);
_classId = (UInt16)(color.grayscale * UInt16.MaxValue);

mpb.SetInt("_SegmentationValue", (int)_classId);
mpb.SetInt("_Hide", 0);

// Debug.Log(TagName + ": mode=" + Main.SegmentationManager.Mode +
// " color=" + color +
// " calssId=" + classValue.r + " " + classValue.g);
// Debug.Log($"{TagName}: mode={Main.SegmentationManager.Mode} color={color} {_classId}");
// Debug.Log($"{TagName} : {grayscale} > {_classId}");

AllocateMaterialPropertyBlock(mpb);
Expand Down
15 changes: 4 additions & 11 deletions Assets/Scripts/Devices/SegmentationCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,15 @@ protected override void SetupTexture()
}

// for Unsigned 16-bit
_targetColorFormat = GraphicsFormat.R8G8_UNorm;
_targetColorFormat = GraphicsFormat.R8G8B8A8_UNorm;
_readbackDstFormat = GraphicsFormat.R8G8_UNorm;

_camImageData = new CameraData.Image(_camParam.image.width, _camParam.image.height, pixelFormat);
}

protected override void SetupCamera()
{
// Debug.Log("Segmenataion Setup Camera");
if (!_camParam.segmentation_type.Equals("semantic"))
{
Debug.Log("Only support semantic segmentation");
}
_camSensor.backgroundColor = Color.black;
_camSensor.clearFlags = CameraClearFlags.SolidColor;
_camSensor.allowHDR = false;
_camSensor.allowMSAA = true;
// Debug.Log("Segmenataion Setup");

// Refer to SegmentationRenderer (Universal Renderer Data)
_universalCamData.SetRenderer(1);
Expand Down Expand Up @@ -99,6 +91,7 @@ protected override void ImageProcessing(ref NativeArray<byte> readbackData, in f
if (imageData != null)
{
image.Data = imageData;
// Debug.LogFormat($"{image.Data[0]}|{image.Data[1]}|{image.Data[2]}|{image.Data[3]}");
if (_camParam.save_enabled && _startCameraWork)
{
var saveName = name + "_" + Time.time;
Expand Down

0 comments on commit 7029d5b

Please sign in to comment.