Skip to content

Commit

Permalink
Modfiy/segmentation (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunseok-yang authored Aug 7, 2024
2 parents 17550cf + e2c3405 commit 6ae8588
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 79 deletions.
12 changes: 6 additions & 6 deletions Assets/Resources/RenderPipelines/SegmentationRenderer.asset
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6b3d386ba5cd94485973aee1479b272e, type: 3}
m_Name: RenderObjects
m_Name: SegmentationRenderObjects
m_EditorClassIdentifier:
m_Active: 1
settings:
passTag: RenderObjects
Event: 600
passTag: SegmentationRenderObjects
Event: 300
filterSettings:
RenderQueueType: 0
LayerMask:
Expand All @@ -28,7 +28,7 @@ MonoBehaviour:
overrideShaderPassIndex: 0
overrideMode: 2
overrideDepthState: 0
depthCompareFunction: 2
depthCompareFunction: 0
enableWrite: 0
stencilSettings:
overrideStencilState: 0
Expand Down Expand Up @@ -97,8 +97,8 @@ MonoBehaviour:
failOperation: 0
zFailOperation: 0
m_ShadowTransparentReceive: 0
m_RenderingMode: 0
m_RenderingMode: 1
m_DepthPrimingMode: 0
m_CopyDepthMode: 2
m_CopyDepthMode: 0
m_AccurateGbufferNormals: 0
m_IntermediateTextureMode: 0
25 changes: 9 additions & 16 deletions Assets/Resources/Shader/GeometryGrass.shader
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ Shader "Custom/GeometryGrass"
{
Tags
{
"RenderPipeline" = "UniversalPipeline"
"RenderType" = "Opaque"
"Queue" = "Geometry"
"RenderPipeline" = "UniversalPipeline"
"IgnoreProjector" = "True"
}

LOD 100
Expand All @@ -56,6 +57,9 @@ Shader "Custom/GeometryGrass"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

#pragma require geometry
#pragma require tessellation tessHW

#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _SHADOWS_SOFT
Expand All @@ -65,6 +69,8 @@ Shader "Custom/GeometryGrass"
#pragma multi_compile_local WIND_OFF _

#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"

#define BLADE_SEGMENTS 4

Expand Down Expand Up @@ -311,7 +317,7 @@ Shader "Custom/GeometryGrass"
float4 tangent = (input[0].tangentWS + input[1].tangentWS + input[2].tangentWS) / 3.0f;
float3 bitangent = cross(normal, tangent.xyz) * tangent.w;

pos -= _GrassOffset;
pos -= _GrassOffset.xyz;

float3x3 tangentToLocal = float3x3
(
Expand Down Expand Up @@ -390,25 +396,17 @@ Shader "Custom/GeometryGrass"
"LightMode" = "UniversalForward"
}

ZWrite On
ZTest LEqual

HLSLPROGRAM
#pragma require geometry
#pragma require tessellation tessHW

#pragma vertex vert
#pragma hull hull
#pragma domain domain
#pragma geometry geom
#pragma fragment frag

#pragma multi_compile_instancing

// The lighting sections of the frag shader taken from this helpful post by Ben Golus:
// https://forum.unity.com/threads/water-shader-graph-transparency-and-shadows-universal-render-pipeline-order.748142/#post-5518747

float4 frag (g2f i) : SV_Target
float4 frag(g2f i) : SV_Target
{
float4 color = tex2D(_BaseTex, i.uv);

Expand Down Expand Up @@ -437,18 +435,13 @@ Shader "Custom/GeometryGrass"
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }

ZWrite On
ZTest LEqual

HLSLPROGRAM
#pragma vertex shadowVert
#pragma hull hull
#pragma domain domain
#pragma geometry geom
#pragma fragment shadowFrag

// #pragma multi_compile_instancing

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
Expand Down
60 changes: 30 additions & 30 deletions Assets/Resources/Shader/Segmentation.shader
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,43 @@ Shader "Sensor/Segmentation"
{
_SegmentationColor ("Segmentation Color", Color) = (1, 1, 1, 1)
_SegmentationClassId ("Segmentation Class ID Value in 16bits", Color) = (0, 0, 0, 1)
_DisableColor ("Disable Color output", int) = 0
_Hide ("Hide this label", int) = 0
[Toggle] _DisableColor ("Disable Color method", int) = 0
[Toggle] _Hide ("Hide this label", int) = 0
}

SubShader
{
Tags {
Tags
{
"RenderPipeline" = "UniversalPipeline"
"RenderType" = "Opaque"
"Queue" = "Geometry"
}

Cull Off // Disable backface culling
Lighting Off

HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

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

ENDHLSL

Pass
{
Name "Segmentation"

Cull Off // Disable backface culling
Name "PixelSegmentation"

HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

#pragma vertex vert
#pragma fragment frag

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

struct Attributes
{
float4 positionOS : POSITION;
Expand All @@ -41,32 +49,24 @@ Shader "Sensor/Segmentation"
struct Varyings
{
float4 positionCS : SV_POSITION;
half4 color : COLOR;
};

Varyings vert(Attributes input)
Varyings vert(Attributes i)
{
Varyings output = (Varyings)0;
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.positionCS = vertexInput.positionCS;

half4 output_color = half4(0, 0, 0, 1);
if (_Hide == 0)
{
output_color = (_DisableColor == 0)? _SegmentationColor : _SegmentationClassId;
}
output.color = output_color;
return output;
Varyings o;
VertexPositionInputs vertexInput = GetVertexPositionInputs(i.positionOS.xyz);
o.positionCS = vertexInput.positionCS;
return o;
}

half4 frag(Varyings i) : SV_Target
{
half4 output_color = half4(0, 0, 0, 1);
half4 segColor = half4(0, 0, 0, 0);
if (_Hide == 0)
{
output_color = (_DisableColor == 0)? _SegmentationColor : _SegmentationClassId;
segColor = (_DisableColor == 0)? _SegmentationColor : _SegmentationClassId;
}
return output_color;
return segColor;
}
ENDHLSL
}
Expand Down
7 changes: 3 additions & 4 deletions Assets/Scripts/CLOiSimPlugins/MowingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void SetBladingRatio(in float bladeMin, in float bladeMax, in float bladi
{
this.heightThreshold = bladingHeight;
this.ratio = Mathf.Lerp(bladeMin, bladeMax, bladingHeight);
Debug.Log("_bladingRatio: " + this.ratio);
// Debug.Log("_bladingRatio: " + this.ratio);
}
}

Expand Down Expand Up @@ -298,10 +298,9 @@ private bool FindTargetBlade(Target targetBlade)
var bladeBounds = new Bounds();
foreach (var meshFilter in meshFilters)
{
Debug.Log(meshFilter.name);
Debug.Log(meshFilter.sharedMesh.bounds);
// Debug.Log(meshFilter.name);
// Debug.Log(meshFilter.sharedMesh.bounds);
var bounds = meshFilter.sharedMesh.bounds;
// bounds.center = Vector3.zero;
bladeBounds.Encapsulate(bounds);
}

Expand Down
24 changes: 17 additions & 7 deletions Assets/Scripts/Core/Modules/SegmentationTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public bool Hide
get => _hide;
set {
_hide = value;
HideLabelForMaterialPropertyBlock(_hide);
HideLabelForMaterialPropertyBlock(value);
}
}

Expand Down Expand Up @@ -89,6 +89,7 @@ public void Refresh()

mpb.SetColor("_SegmentationColor", color);
mpb.SetColor("_SegmentationClassId", classValue);
mpb.SetInt("_Hide", 0);

// Debug.Log(TagName + ": mode=" + Main.SegmentationManager.Mode +
// " color=" + color +
Expand Down Expand Up @@ -119,7 +120,7 @@ private void UpdateClass()
default:
return;
}

// Debug.Log("UpdateClass(): " + _className);
Main.SegmentationManager.AddClass(_className, this);
}

Expand All @@ -130,8 +131,13 @@ private void AllocateMaterialPropertyBlock(in MaterialPropertyBlock mpb)
foreach (var renderer in renderers)
{
// Debug.Log($"{this.name} material length {renderer.materials.Length}");
for (var i =0 ; i < renderer.materials.Length; i++)
for (var i = 0; i < renderer.materials.Length; i++)
{
var existMpb = new MaterialPropertyBlock();
renderer.GetPropertyBlock(existMpb, i);
var hide = existMpb.GetInt("_Hide");
// Debug.Log($"{this.name} {i} hide={hide}");
mpb.SetInt("_Hide", hide);
renderer.SetPropertyBlock(mpb, i);
}
}
Expand All @@ -147,15 +153,19 @@ private void AllocateMaterialPropertyBlock(in MaterialPropertyBlock mpb)
/// Hides the label in material property block.
/// </summary>
/// <param name="value">if set to <c>true</c>, hide this segmentation.</param>
private void HideLabelForMaterialPropertyBlock(in bool value)
public void HideLabelForMaterialPropertyBlock(in bool value)
{
// Debug.Log("HideLabelForMaterialPropertyBlock: " + name);
var mpb = new MaterialPropertyBlock();
var renderers = GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
renderer.GetPropertyBlock(mpb);
mpb.SetInt("_Hide", value? 1 : 0);
renderer.SetPropertyBlock(mpb);
for (var i = 0; i < renderer.materials.Length; i++)
{
renderer.GetPropertyBlock(mpb, i);
mpb.SetInt("_Hide", value ? 1 : 0);
renderer.SetPropertyBlock(mpb, i);
}
}
}
}
4 changes: 1 addition & 3 deletions Assets/Scripts/Core/ObjectSpawning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ void OnDestroy()
// Update is called once per frame
void LateUpdate()
{
var leftControlPressed = Input.GetKey(KeyCode.LeftControl);

if (leftControlPressed)
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetMouseButtonUp(0))
{
Expand Down
4 changes: 1 addition & 3 deletions Assets/Scripts/Core/SegmentationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public void AttachTag(string className, Transform target)

private static IEnumerator AttachTagCoroutine(string className, Transform target)
{
yield return new WaitForEndOfFrame();

var segmentationTag = target?.gameObject.GetComponentInChildren<SegmentationTag>();
var segmentationTag = target?.GetComponentInChildren<SegmentationTag>();
if (segmentationTag == null)
{
segmentationTag = target?.gameObject.AddComponent<SegmentationTag>();
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Devices/DepthCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ protected override void SetupCamera()

camSensor.clearFlags = CameraClearFlags.Depth;
camSensor.allowHDR = false;
camSensor.allowMSAA = false;
camSensor.depthTextureMode = DepthTextureMode.Depth;

_universalCamData.requiresColorOption = CameraOverrideOption.Off;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Devices/MicomSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void SetIMU(in string sensorName)
var imuList = gameObject.GetComponentsInChildren<SensorDevices.IMU>();
foreach (var imu in imuList)
{
Debug.Log(imu.name + " , " + imu.DeviceName);
// Debug.Log(imu.name + " , " + imu.DeviceName);
if (imu.DeviceName.Contains("::" + sensorName + "::") ||
imu.name.CompareTo(sensorName) == 0)
{
Expand Down
10 changes: 6 additions & 4 deletions Assets/Scripts/Devices/SegmentationCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ protected override void SetupCamera()
camSensor.backgroundColor = Color.black;
camSensor.clearFlags = CameraClearFlags.SolidColor;
camSensor.allowHDR = false;
camSensor.allowMSAA = false;
camSensor.allowMSAA = true;

// Refer to SegmentationRenderer (Universal Renderer Data)
_universalCamData.SetRenderer(1);
_universalCamData.renderPostProcessing = true;
_universalCamData.requiresColorOption = CameraOverrideOption.On;
_universalCamData.requiresColorOption = CameraOverrideOption.Off;
_universalCamData.requiresDepthOption = CameraOverrideOption.Off;
_universalCamData.requiresColorTexture = true;
_universalCamData.requiresColorTexture = false;
_universalCamData.requiresDepthTexture = false;
_universalCamData.renderShadows = false;
_universalCamData.dithering = false;
_universalCamData.dithering = true;
_universalCamData.antialiasing = AntialiasingMode.FastApproximateAntialiasing;

}

protected override void InitializeMessages()
Expand Down
8 changes: 7 additions & 1 deletion Assets/Scripts/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,21 @@ private void SaveWorld()

void LateUpdate()
{
#if UNITY_EDITOR
if (Input.GetKey(KeyCode.LeftAlt))
#else
if (Input.GetKey(KeyCode.LeftControl))
#endif
{
// Debug.Log("LeftControl Triggered");
if (Input.GetKeyUp(KeyCode.R))
{
_resetTriggered = true;
// Debug.Log("Reset Triggered");
_resetTriggered = true;
}
else if (Input.GetKeyUp(KeyCode.S))
{
// Debug.Log("Save World");
OnSaveButtonClicked();
}
}
Expand Down
Loading

0 comments on commit 6ae8588

Please sign in to comment.