I am writing a Unity Package that is intended for generating raymarching shaders in Unity. As a reference for what it generally looks like, here is another (not mine) similar project.
My package is used mainly in the editor (not runtime) and primitives are handled by regular game objects in the hierarchy. I would like to add support for selecting the objects with mouse in the scene view. I already have a shader that can return some ID for a given fragment, but I have no idea how to integrate this with the unity editor. The intended implementation would look similar to this.
I have been looking everywhere, including generated ShaderGraph shaders, and the only thing relevant in any way were passes defined with “LightMode” = “Picking” and “LightMode” = “SceneSelectionPass”
Shader graph has something related to that topic, but there is no documentation, no examples and no info how to support that. I found a bunch of relevant snippets and links:
// -- Property used by ScenePickingPass
#ifdef SCENEPICKINGPASS
float4 _SelectionID;
#endif
// -- Properties used by SceneSelectionPass
#ifdef SCENESELECTIONPASS
int _ObjectId;
int _PassValue;
#endif
#define SHADERPASS ScenePickingPass
#define BUILTIN_TARGET_API 1
#define SCENEPICKINGPASS 1
Pass {
Name "ScenePickingPass"
Tags {
"LightMode" = "Picking"
}
// ...
}
Does anyone how I can do that? In BIRP first, in URP second (but optional for now).
I was searching the same topic, seems like you only person who tried anything.))
My current knowledge is that you just need to implement 2 passes
Pass
{
Name "SceneSelectionPass"
Tags { "LightMode" = "SceneSelectionPass" }
// Render State
Cull Off
HLSLPROGRAM
#define SCENESELECTIONPASS 1
#pragma vertex YOUR_VERTEX_MODIFIED_PROGRAM_HERE
#pragma fragment frag
// -- Property used by ScenePickingPass
#ifdef SCENEPICKINGPASS
float4 _SelectionID;
#endif
// -- Properties used by SceneSelectionPass
#ifdef SCENESELECTIONPASS
int _ObjectId;
int _PassValue;
#endif
half4 frag() : SV_TARGET
{
half4 outColor = 0;
#ifdef SCENESELECTIONPASS
outColor = half4(_ObjectId, _PassValue, 1.0, 1.0);
#elif defined(SCENEPICKINGPASS)
outColor = _SelectionID;
#endif
return outColor;
}
ENDHLSL
}
Pass
{
Name "ScenePickingPass"
Tags { "LightMode" = "Picking" }
// Render State
Cull Back
HLSLPROGRAM
#define SCENEPICKINGPASS 1
#pragma vertex YOUR_VERTEX_MODIFIED_PROGRAM_HERE
#pragma fragment frag
// -- Property used by ScenePickingPass
#ifdef SCENEPICKINGPASS
float4 _SelectionID;
#endif
// -- Properties used by SceneSelectionPass
#ifdef SCENESELECTIONPASS
int _ObjectId;
int _PassValue;
#endif
half4 frag() : SV_TARGET
{
half4 outColor = 0;
#ifdef SCENESELECTIONPASS
outColor = half4(_ObjectId, _PassValue, 1.0, 1.0);
#elif defined(SCENEPICKINGPASS)
outColor = _SelectionID;
#endif
return outColor;
}
ENDHLSL
}
Full example :
SelectionVertexMod.shader (4.3 KB)
For me the outline effect worked perfectlly, but picking pass do some crazy stuff!
I am sure it uses my custom ScenePickingPass (tried to comment out the return value)
Currently the issue is this:
- no vertex modifications == perfect
- make some offset == picking are still works but it is “srhinking”
- make big offset == no picking at all!
My current version is that Unity mods the vertex data or some projection matrix, however why when no vertex offset it works perfect?
The most craze thing is that shadergraph behaves exactly the same! Can you confirm that?
I was able to confirm that my SceneSelectionPass used via RenderDoc (yes you can capture editor seneview window there!), but no ScenePickingPass maybe due to one time render when you click somewhere I am not sure.
As always Unity didn’t document anything((((
Ony more clue related to generated meshes on CPU side
Box picking seems to be completely different beast. I think it works via bbox’s maybe view alligned.
Oh wow, thank you! I have not expected a reply to that thread after so much time, but I am super glad you wrote! If I manage to get back to to that issue I will certainly try to try your solution and report my findings. For the Renderdoc, I unfortunately had no luck running it on Linux 
Regarding the vertex modification bug, I have no idea what may be causing this, but if I were to guess I would say maybe some weirdness related to outline generation? I don’t know how they do it, but maybe they use a technique for growing a vertices by some delta and it’s messing some calculations???