Skip to content

Commit

Permalink
Disable all picking related code by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-breysse committed Jan 13, 2022
1 parent cfd0466 commit 969e322
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if UNITY_EDITOR
//#define ENABLE_PICKING
#endif

using System;
using System.Collections.Generic;
using Unity.Burst;
Expand Down Expand Up @@ -32,16 +36,21 @@ public struct DrawKey : IEquatable<DrawKey>
public uint submeshIndex;
public BatchMaterialID material;
public ShadowCastingMode shadows;

#if ENABLE_PICKING
public int pickableObjectInstanceID;
#endif

public bool Equals(DrawKey other)
{
return
#if ENABLE_PICKING
pickableObjectInstanceID == other.pickableObjectInstanceID &&
#endif
meshID == other.meshID &&
submeshIndex == other.submeshIndex &&
material == other.material &&
shadows == other.shadows &&
pickableObjectInstanceID == other.pickableObjectInstanceID;
shadows == other.shadows;
}
}

Expand Down Expand Up @@ -270,12 +279,12 @@ public void Execute()
flags = BatchDrawCommandFlags.None,
sortingPosition = 0
};

#if ENABLE_PICKING
if (draws.drawCommandPickingInstanceIDs != null)
{
draws.drawCommandPickingInstanceIDs[outBatch] = drawBatches[remappedIndex].key.pickableObjectInstanceID;
}

#endif
outBatch++;
}

Expand Down Expand Up @@ -330,6 +339,10 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
return new JobHandle();
}

#if ENABLE_PICKING
bool isPickingCulling = cullingContext.viewType == BatchCullingViewType.Picking;
#endif

var splitCounts = new NativeArray<int>(cullingContext.cullingSplits.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < splitCounts.Length; ++i)
{
Expand All @@ -344,8 +357,10 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
drawCommands.drawCommands = Malloc<BatchDrawCommand>(m_drawBatches.Length *
splitCounts.Length * 10); // TODO: Multiplying the DrawCommand count by splitCount*10 is NOT an conservative upper bound. But in practice is enough. Sorting would give us a real conservative bound...

drawCommands.drawCommandPickingInstanceIDs = Malloc<int>(m_drawBatches.Length);
drawCommands.visibleInstances = Malloc<int>(m_instanceIndices.Length);
#if ENABLE_PICKING
drawCommands.drawCommandPickingInstanceIDs = isPickingCulling ? Malloc<int>(m_drawBatches.Length) : null;
#endif

// Zero init: Culling job sets the values!
drawCommands.drawRangeCount = 0;
Expand Down Expand Up @@ -385,7 +400,7 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
return jobHandleOutput;
}

#if UNITY_EDITOR
#if ENABLE_PICKING
public static Material LoadPickingMaterial()
{
Shader shader = Shader.Find("Hidden/HDRP/BRGPicking");
Expand All @@ -409,7 +424,7 @@ void Start()
{
m_BatchRendererGroup = new BatchRendererGroup(this.OnPerformCulling, IntPtr.Zero);

#if UNITY_EDITOR
#if ENABLE_PICKING
m_pickingMaterial = LoadPickingMaterial();
m_BatchRendererGroup.SetPickingMaterial(m_pickingMaterial);
#endif
Expand Down Expand Up @@ -502,13 +517,17 @@ void Start()
renderer.GetSharedMaterials(sharedMaterials);

var shadows = renderer.shadowCastingMode;
int instanceID = renderer.gameObject.GetInstanceID();

for (int matIndex = 0; matIndex < sharedMaterials.Count; matIndex++)
{
var material = m_BatchRendererGroup.RegisterMaterial(sharedMaterials[matIndex]);

var key = new DrawKey { material = material, meshID = mesh, submeshIndex = (uint)matIndex, shadows = shadows, pickableObjectInstanceID = instanceID };
var key = new DrawKey { material = material, meshID = mesh, submeshIndex = (uint)matIndex, shadows = shadows };

#if ENABLE_PICKING
key.pickableObjectInstanceID = renderer.gameObject.GetInstanceID();
#endif

var drawBatch = new DrawBatch
{
key = key,
Expand Down Expand Up @@ -681,7 +700,7 @@ private void OnDestroy()
m_instanceIndices.Dispose();
m_drawIndices.Dispose();

#if UNITY_EDITOR
#if ENABLE_PICKING
DestroyImmediate(m_pickingMaterial);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if UNITY_EDITOR
//#define ENABLE_PICKING
#endif

using System;
using System.Collections.Generic;
using Unity.Burst;
Expand Down Expand Up @@ -33,16 +37,21 @@ public struct DrawKey : IEquatable<DrawKey>
public uint submeshIndex;
public BatchMaterialID material;
public ShadowCastingMode shadows;

#if ENABLE_PICKING
public int pickableObjectInstanceID;
#endif

public bool Equals(DrawKey other)
{
return
#if ENABLE_PICKING
pickableObjectInstanceID == other.pickableObjectInstanceID &&
#endif
meshID == other.meshID &&
submeshIndex == other.submeshIndex &&
material == other.material &&
shadows == other.shadows &&
pickableObjectInstanceID == other.pickableObjectInstanceID;
shadows == other.shadows;
}
}

Expand Down Expand Up @@ -271,12 +280,12 @@ public void Execute()
flags = BatchDrawCommandFlags.None,
sortingPosition = 0
};

#if ENABLE_PICKING
if (draws.drawCommandPickingInstanceIDs != null)
{
draws.drawCommandPickingInstanceIDs[outBatch] = drawBatches[remappedIndex].key.pickableObjectInstanceID;
}

#endif
outBatch++;
}

Expand Down Expand Up @@ -331,6 +340,10 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
return new JobHandle();
}

#if ENABLE_PICKING
bool isPickingCulling = cullingContext.viewType == BatchCullingViewType.Picking;
#endif

var splitCounts = new NativeArray<int>(cullingContext.cullingSplits.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < splitCounts.Length; ++i)
{
Expand All @@ -345,8 +358,10 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
drawCommands.drawCommands = Malloc<BatchDrawCommand>(m_drawBatches.Length *
splitCounts.Length * 10); // TODO: Multiplying the DrawCommand count by splitCount*10 is NOT an conservative upper bound. But in practice is enough. Sorting would give us a real conservative bound...

drawCommands.drawCommandPickingInstanceIDs = Malloc<int>(m_drawBatches.Length);
drawCommands.visibleInstances = Malloc<int>(m_instanceIndices.Length);
#if ENABLE_PICKING
drawCommands.drawCommandPickingInstanceIDs = isPickingCulling ? Malloc<int>(m_drawBatches.Length) : null;
#endif

// Zero init: Culling job sets the values!
drawCommands.drawRangeCount = 0;
Expand Down Expand Up @@ -386,7 +401,7 @@ public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCulling
return jobHandleOutput;
}

#if UNITY_EDITOR
#if ENABLE_PICKING
public static Material LoadPickingMaterial()
{
Shader shader = Shader.Find("Hidden/Universal Render Pipeline/BRGPicking");
Expand All @@ -402,7 +417,6 @@ public static Material LoadPickingMaterial()
}

Material m_pickingMaterial;

#endif


Expand All @@ -411,7 +425,7 @@ void Start()
{
m_BatchRendererGroup = new BatchRendererGroup(this.OnPerformCulling, IntPtr.Zero);

#if UNITY_EDITOR
#if ENABLE_PICKING
m_pickingMaterial = LoadPickingMaterial();
m_BatchRendererGroup.SetPickingMaterial(m_pickingMaterial);
#endif
Expand Down Expand Up @@ -505,13 +519,17 @@ void Start()
renderer.GetSharedMaterials(sharedMaterials);

var shadows = renderer.shadowCastingMode;
int instanceID = renderer.gameObject.GetInstanceID();

for (int matIndex = 0; matIndex < sharedMaterials.Count; matIndex++)
{
var material = m_BatchRendererGroup.RegisterMaterial(sharedMaterials[matIndex]);

var key = new DrawKey { material = material, meshID = mesh, submeshIndex = (uint)matIndex, shadows = shadows, pickableObjectInstanceID = instanceID };
var key = new DrawKey { material = material, meshID = mesh, submeshIndex = (uint)matIndex, shadows = shadows };

#if ENABLE_PICKING
key.pickableObjectInstanceID = renderer.gameObject.GetInstanceID();
#endif

var drawBatch = new DrawBatch
{
key = key,
Expand Down Expand Up @@ -684,7 +702,7 @@ private void OnDestroy()
m_instanceIndices.Dispose();
m_drawIndices.Dispose();

#if UNITY_EDITOR
#if ENABLE_PICKING
DestroyImmediate(m_pickingMaterial);
#endif
}
Expand Down

0 comments on commit 969e322

Please sign in to comment.