OnSceneGUI render projection onto object surface

Main question

I’m building a custom Terrain editor, and I can’t determine how to get a projection of the ‘brush’ to flow over the terrain like the built-in Unity terrain editor.

At the moment of I’m using Handles.DrawLines, function to render a square, but this doesn’t “hug” the terrain shape.

I looked through the scripting API, but didn’t find anything else in Handles, EditorUtility, or EditorGUIUtility that might be useful. I could possibly create a game object in the editor that projects onto the terrain, but not sure if you can hide it, unless I can somehow create non-permanent objects in OnSceneGUI. I could maybe use some kind of Gizmo but that looked like it was just for static renderings around the gameobject when it’s rendered / didn’t look like there was an option for mouse movement.

Random side blurb

Does anyone know of a good resource for creating custom Unity Editor functionality? I’ve been struggling to find any kind of explanation for how to do anything beyond adding a custom property to the inspector window. This is one of the things holding me back from actually developing in Unity, because I haven’t been able to learn methods to create custom tooling in order to remove repetitive tasks.

Example questions

  • Can I create instance variables in an Editor class? Is that safe?
  • The question I’m asking now about creating the projection

Do you know the Normal Vector of the Terrain at the position you want to draw the brush? (probably, because you’re creating the terrain yourself)

If the brush has a transform, you can just set transform.up to that normal.
If not and your brush doesn’t have to be square, you can use

Handles.DrawSolidDisc(Vector3 center, Vector3 normal, float radius)

See - Unity Docs

radius can be your brush size, center is the position where the brush has touched the terrain (so its local height), and normal is the Terrain surface normal vector at that position.