Skip to content

Commit

Permalink
Touchup volume debug and gizmo color modification (#7119)
Browse files Browse the repository at this point in the history
* Tunable color

* Add debug mode

* Updating test data.

* Remove leftover that fails build

* Apply formatting changes

Co-authored-by: noreply@unity3d.com <noreply@unity3d.com>
  • Loading branch information
FrancescoC-unity and noreply@unity3d.com authored Feb 16, 2022
1 parent 4f78740 commit 0f8ecd8
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 11 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ struct BakingCell
public Vector3[] probePositions;
public SphericalHarmonicsL2[] sh;
public uint[] validity;

public Vector3[] offsetVectors;
public float[] touchupVolumeInteraction;

public int minSubdiv;
public int indexChunkCount;
Expand Down Expand Up @@ -602,6 +604,7 @@ static void OnAdditionalProbesBakeCompleted()
cell.sh = new SphericalHarmonicsL2[numProbes];
cell.validity = new uint[numProbes];
cell.offsetVectors = new Vector3[virtualOffsets != null ? numProbes : 0];
cell.touchupVolumeInteraction = new float[numProbes];
cell.minSubdiv = probeRefVolume.GetMaxSubdivision();

// Find the subset of touchup volumes that will be considered for this cell.
Expand Down Expand Up @@ -636,13 +639,19 @@ static void OnAdditionalProbesBakeCompleted()
if (touchupVolume.invalidateProbes)
{
invalidatedProbe = true;
// We check as below 1 but bigger than 0 in the debug shader, so any value <1 will do to signify touched up.
cell.touchupVolumeInteraction[i] = 0.5f;

if (validity[j] < 0.05f) // We just want to add probes that were not already invalid or close to.
{
s_ForceInvalidatedProbesAndTouchupVols[cell.probePositions[i]] = touchupBound;
}
}
else if (touchupVolume.overrideDilationThreshold)
{
// The 1 + is used to determine the action (debug shader tests above 1), then we add the threshold to be able to retrieve it in debug phase.
cell.touchupVolumeInteraction[i] = 1.0f + touchupVolume.overriddenDilationThreshold;

s_CustomDilationThresh.Add(i, touchupVolume.overriddenDilationThreshold);
}
break;
Expand Down Expand Up @@ -880,6 +889,7 @@ static BakingCell ConvertCellToBakingCell(ProbeReferenceVolume.Cell cell)
probePositions = cell.probePositions.ToArray(),
validity = cell.validity.ToArray(),
offsetVectors = cell.offsetVectors.ToArray(),
touchupVolumeInteraction = cell.touchupVolumeInteraction.ToArray(),
minSubdiv = cell.minSubdiv,
indexChunkCount = cell.indexChunkCount,
shChunkCount = cell.shChunkCount,
Expand Down Expand Up @@ -1064,6 +1074,7 @@ static void WriteBakingCells(ProbeVolumePerSceneData data, List<BakingCell> baki

// CellSupportData
using var positions = new NativeArray<Vector3>(asset.totalCellCounts.probesCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
using var touchupVolumeInteraction = new NativeArray<float>(asset.totalCellCounts.probesCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
using var offsets = new NativeArray<Vector3>(asset.totalCellCounts.offsetsCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

var sceneStateHash = asset.GetBakingHashCode();
Expand Down Expand Up @@ -1096,6 +1107,7 @@ static void WriteBakingCells(ProbeVolumePerSceneData data, List<BakingCell> baki
validity.GetSubArray(startCounts.probesCount, cellCounts.probesCount).CopyFrom(bakingCell.validity);

positions.GetSubArray(startCounts.probesCount, cellCounts.probesCount).CopyFrom(bakingCell.probePositions);
touchupVolumeInteraction.GetSubArray(startCounts.probesCount, cellCounts.probesCount).CopyFrom(bakingCell.touchupVolumeInteraction);
offsets.GetSubArray(startCounts.offsetsCount, cellCounts.offsetsCount).CopyFrom(bakingCell.offsetVectors);

startCounts.Add(cellCounts);
Expand Down Expand Up @@ -1126,6 +1138,8 @@ static void WriteBakingCells(ProbeVolumePerSceneData data, List<BakingCell> baki
{
fs.Write(new ReadOnlySpan<byte>(positions.GetUnsafeReadOnlyPtr(), positions.Length * UnsafeUtility.SizeOf<Vector3>()));
fs.Write(new byte[AlignRemainder16(fs.Position)]);
fs.Write(new ReadOnlySpan<byte>(touchupVolumeInteraction.GetUnsafeReadOnlyPtr(), touchupVolumeInteraction.Length * UnsafeUtility.SizeOf<float>()));
fs.Write(new byte[AlignRemainder16(fs.Position)]);
fs.Write(new ReadOnlySpan<byte>(offsets.GetUnsafeReadOnlyPtr(), offsets.Length * UnsafeUtility.SizeOf<Vector3>()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
using UnityEditor.Rendering;
using UnityEngine.Rendering;
using UnityEditorInternal;
using System;
using RuntimeSRPPreferences = UnityEngine.Rendering.CoreRenderPipelinePreferences;

namespace UnityEditor.Rendering
{
internal class ProbeTouchupColorPreferences
{
internal static Func<Color> GetColorPrefProbeVolumeGizmoColor;
internal static Color s_ProbeTouchupVolumeGizmoColorDefault = new Color32(222, 132, 144, 45);

static ProbeTouchupColorPreferences()
{
GetColorPrefProbeVolumeGizmoColor = RuntimeSRPPreferences.RegisterPreferenceColor("Scene/Probe Touchup Volume Gizmo", s_ProbeTouchupVolumeGizmoColorDefault);
}

}

[CanEditMultipleObjects]
[CustomEditor(typeof(ProbeTouchupVolume))]
internal class ProbeTouchupVolumeEditor : Editor
Expand All @@ -19,17 +33,19 @@ internal static class Styles
internal static readonly GUIContent s_OverrideDilationThreshold = new GUIContent("Override Dilation Threshold", "Whether to override the dilation threshold used for probes falling within this probe touch-up volume.");
internal static readonly GUIContent s_OverriddenDilationThreshold = new GUIContent("Dilation Threshold", "The dilation threshold to use for this probe volume.");

internal static readonly Color k_GizmoColorBase = new Color32(222, 132, 144, 255);
internal static readonly Color k_GizmoColorBase = ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault;

internal static readonly Color[] k_BaseHandlesColor = new Color[]
{
k_GizmoColorBase,
k_GizmoColorBase,
k_GizmoColorBase,
k_GizmoColorBase,
k_GizmoColorBase,
k_GizmoColorBase
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault,
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault,
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault,
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault,
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault,
ProbeTouchupColorPreferences.s_ProbeTouchupVolumeGizmoColorDefault
};


}


Expand Down Expand Up @@ -106,7 +122,7 @@ static void DrawGizmosSelected(ProbeTouchupVolume touchupVolume, GizmoType gizmo
// Bounding box.
s_ShapeBox.center = Vector3.zero;
s_ShapeBox.size = touchupVolume.size;
s_ShapeBox.baseColor = new Color(0.75f, 0.2f, 0.18f);
s_ShapeBox.SetBaseColor(ProbeTouchupColorPreferences.GetColorPrefProbeVolumeGizmoColor());
s_ShapeBox.DrawHull(true);
}
}
Expand All @@ -128,7 +144,7 @@ protected void OnSceneGUI()
s_ShapeBox.DrawHandle();
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObjects(new Object[] { touchupVolume, touchupVolume.transform }, "Change Probe Touchup Volume Bounding Box");
Undo.RecordObjects(new UnityEngine.Object[] { touchupVolume, touchupVolume.transform }, "Change Probe Touchup Volume Bounding Box");

touchupVolume.size = s_ShapeBox.size;
Vector3 delta = touchupVolume.transform.rotation * s_ShapeBox.center - touchupVolume.transform.position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public enum DebugProbeShadingMode
/// </summary>
ValidityOverDilationThreshold,
/// <summary>
/// Show in red probes that have been made invalid by touchup volumes. Important to note that this debug view will only show result for volumes still present in the scene.
/// </summary>
InvalidatedByTouchupVolumes,
/// <summary>
/// Based on size
/// </summary>
Size
Expand Down Expand Up @@ -357,6 +361,7 @@ CellInstancedDebugProbes CreateInstancedProbes(CellInfo cellInfo)
Vector4[] texels = new Vector4[kProbesPerBatch];
float[] validity = new float[kProbesPerBatch];
float[] relativeSize = new float[kProbesPerBatch];
float[] touchupUpVolumeAction = cell.touchupVolumeInteraction.Length > 0 ? new float[kProbesPerBatch] : null;
Vector4[] offsets = cell.offsetVectors.Length > 0 ? new Vector4[kProbesPerBatch] : null;

List<Matrix4x4> probeBuffer = new List<Matrix4x4>();
Expand Down Expand Up @@ -386,6 +391,13 @@ CellInstancedDebugProbes CreateInstancedProbes(CellInfo cellInfo)
validity[idxInBatch] = cell.GetValidity(i);
texels[idxInBatch] = new Vector4(texelLoc.x, texelLoc.y, texelLoc.z, brickSize);
relativeSize[idxInBatch] = (float)brickSize / (float)maxSubdiv;

if (touchupUpVolumeAction != null)
{
touchupUpVolumeAction[idxInBatch] = cell.touchupVolumeInteraction[i];
}


if (offsets != null)
{
const float kOffsetThresholdSqr = 1e-6f;
Expand Down Expand Up @@ -413,6 +425,7 @@ CellInstancedDebugProbes CreateInstancedProbes(CellInfo cellInfo)
MaterialPropertyBlock prop = new MaterialPropertyBlock();

prop.SetFloatArray("_Validity", validity);
prop.SetFloatArray("_TouchupedByVolume", touchupUpVolumeAction);
prop.SetFloatArray("_RelativeSize", relativeSize);
prop.SetVectorArray("_IndexInAtlas", texels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#define DEBUGPROBESHADINGMODE_SHL0L1 (2)
#define DEBUGPROBESHADINGMODE_VALIDITY (3)
#define DEBUGPROBESHADINGMODE_VALIDITY_OVER_DILATION_THRESHOLD (4)
#define DEBUGPROBESHADINGMODE_SIZE (5)
#define DEBUGPROBESHADINGMODE_INVALIDATED_BY_TOUCHUP_VOLUMES (5)
#define DEBUGPROBESHADINGMODE_SIZE (6)


#endif
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ internal class Cell
public NativeArray<Brick> bricks { get; internal set; }
public NativeArray<uint> validity { get; internal set; }
public NativeArray<Vector3> probePositions { get; internal set; }
public NativeArray<float> touchupVolumeInteraction { get; internal set; } // Only used by a specific debug view.
public NativeArray<Vector3> offsetVectors { get; internal set; }

// Per state data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ internal bool ResolveSharedCellData(TextAsset cellSharedDataAsset, TextAsset cel
var cellSupportData = cellSupportDataAsset ? cellSupportDataAsset.GetData<byte>() : default;
var hasSupportData = cellSupportData.IsCreated;
var positionsByteCount = totalCellCounts.probesCount * UnsafeUtility.SizeOf<Vector3>();
var offsetByteStart = AlignUp16(positionsByteCount);
var touchupInteractionStart = AlignUp16(positionsByteCount);
var touchupInteractionByteCount = totalCellCounts.probesCount * UnsafeUtility.SizeOf<float>();
var offsetByteStart = AlignUp16(positionsByteCount) + AlignUp16(touchupInteractionByteCount);
var offsetByteCount = totalCellCounts.offsetsCount * UnsafeUtility.SizeOf<Vector3>();
if (hasSupportData && offsetByteStart + offsetByteCount != cellSupportData.Length)
return false;
var positionsData = hasSupportData ? cellSupportData.GetSubArray(0, positionsByteCount).Reinterpret<Vector3>(1) : default;
var touchupInteractionData = hasSupportData ? cellSupportData.GetSubArray(touchupInteractionStart, touchupInteractionByteCount).Reinterpret<float>(1) : default;
var offsetsData = hasSupportData ? cellSupportData.GetSubArray(offsetByteStart, offsetByteCount).Reinterpret<Vector3>(1) : default;

var startCounts = new CellCounts();
Expand All @@ -118,6 +121,7 @@ internal bool ResolveSharedCellData(TextAsset cellSharedDataAsset, TextAsset cel
{
cell.probePositions = positionsData.GetSubArray(startCounts.probesCount, counts.probesCount);
cell.offsetVectors = offsetsData.GetSubArray(startCounts.offsetsCount, counts.offsetsCount);
cell.touchupVolumeInteraction = touchupInteractionData.GetSubArray(startCounts.probesCount, counts.probesCount);
}

startCounts.Add(counts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ uniform float _OffsetSize;

UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _Validity)
UNITY_DEFINE_INSTANCED_PROP(float, _TouchupedByVolume)
UNITY_DEFINE_INSTANCED_PROP(float4, _IndexInAtlas)
UNITY_DEFINE_INSTANCED_PROP(float4, _Offset)
UNITY_DEFINE_INSTANCED_PROP(float, _RelativeSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ Shader "Hidden/HDRP/ProbeVolumeDebug"
{
return float4(CalculateDiffuseLighting(i) * exp2(_ExposureCompensation) * GetCurrentExposureMultiplier(), 1);
}
else if (_ShadingMode == DEBUGPROBESHADINGMODE_INVALIDATED_BY_TOUCHUP_VOLUMES)
{
float4 defaultCol = float4(CalculateDiffuseLighting(i) * exp2(_ExposureCompensation) * GetCurrentExposureMultiplier(), 1);
float touchupAction = UNITY_ACCESS_INSTANCED_PROP(Props, _TouchupedByVolume);
if (touchupAction > 0 && touchupAction < 1)
{
return float4(1, 0, 0, 1);
}
return defaultCol;
}
else if (_ShadingMode == DEBUGPROBESHADINGMODE_VALIDITY)
{
float validity = UNITY_ACCESS_INSTANCED_PROP(Props, _Validity);
Expand Down

0 comments on commit 0f8ecd8

Please sign in to comment.