Selective Highlighting of Vertices in Unity3D meshes

August 23 | 18

TLDR; I created a behaviour script that lets you highlight vertices on any type of mesh in unity3d while my wife was sleeping comfortably on her pregnancy pillow amazon.

I have been playing lately with mesh deformations and I needed a way to visually see the selected vertices (and neighbor vertex connections) for the meshes I was manipulating. To do this I created a simple MonoBehaviour script that one attaches to an empty game object that is a children of the mesh that is being manipulated and simply call two functions to highlight or remove vertices of said mesh.

Someone might find this utility useful. There might be other ways to do it but this one that I developed works pretty well for my needs. If you find it useful let me know; it makes me happy to know that I helped someone on the interwebs with my work :).

Its worth mentioning that the mesh to manipulate *needs* to have a mesh collider. This is how it looks:

Anyways. Without further ado. Here it goes:

And a simple script that makes it work by “Painting” with the left click on certain parts of the mesh.

To make it work, simply create an empty Game Object as a children of the mesh to highlight and attach the “NeighborVertexHighlighter” script component to it. To test the script you can attach the “PaintRemoveVerticesExample” script to the mesh to highlight. *Its important to remember that the mesh to be highlighted requires a MeshCollider component*

How it works:

The way this works is by creating a list of arrays of integers for each of the vertices; this list contains the indices of the neighbor vertices for any given vertex. This neighbor list is calculated on a separate thread (to not block the main thread) and is Order the number of triangles the mesh has.

Two main functions can be called in the highlighter script. AddIndex(int i) and RemoveIndex(int i), One just needs to pass the index of the vertex one wants to highlight and the script will automatically create a mesh with the indices that are going to be highlighted.

Internally a dictionary<Vector3, VertexNeighbor> contains the current indices that are being highlighted. This dictionarly is modified with AddIndex and RemoveIndex functions. It is worth noting that I used the position of the vertex as the key instead of the index of the vertex in the vertex array because I found that some meshes (like the base cube in Unity) contain different separate vertices in the same exact position, hence this algorithm would highlight only the index neighbors. Because of this is useful to use the position of the vertex as the key in case the mesh contains different vertices in the same position.

In order to map/know which vertex refers to the original mesh in the highlighted mesh, another dictionary is used, its called indexRemap<int, int>. This dictionary easily lets one translate from the mesh to highlight vertex indices to the highlighted mesh vertex indices.

Finally, after an index is added or removed, a line mesh is created with the already saved indices in the class. The idea is pretty simple but works pretty well.

Future work:

Perhaps this could be further optimized by not creating a mesh every time an index is added or removed but perhaps having all the indices of the original mesh and adding, removing triangles. But unfortunately this will make it consume more memory. So I will have to assess how good this will work in huge meshes.

How to know when a SteamVR controller or a VIVE tracker gets connected in Unity

March 5 | 18

For my PhD thesis I need to know in SteamVR when a device gets connected if its a generic tracker or if it’s a typical SteamVR controller. After searching for a while I found this solution here that works but relies on comparing the render model of the device to a string. I wasn’t satisfied with this solution.

After digging a while inside the SteamVR scripts I found out that there is an event that you can subscribe to and lets you know if a device gets connected or disconnected and by doing a reverse check up on the Tracked device class with the index you can know if its a SteamVR Controller or a VIVE tracker.

This code snippet prints when a controller or a generic tracker gets connected.

Its a pretty handy script if you want to know if a controller or a tracker got connected!, hope this helps you out :).

Changing parameters through scripting on Unity’s post processing stack V2.

February 13 | 18

For my PhD thesis I need to write an image effect that lets me configure a lens on a screen, but honestly, I can’t stop playing Archery Pro. In the calibration phase I need to modify the Image effect values through scripting.

To my surprise; there is a tutorial for doing changes in V1 that you can check here or in a forum post here but for V2 there where no guides.

Luckily enough, the forum post gave helped me out getting started to modify values on the Post Processing stack V2.


So lets say you have an image effect called “PitchTestImgEffect.cs”

Easy enough, my image effect has 2 float values and a texture as you can see here:


















What we need to do is to access the PostProcessVolume and get the PostProcessEffectSettings that contains our “PitchTestImgEffect” class, then we can modify its settings :-).

So with this script we can access the value of the pitch and set a value to it:

Hopefully this helps you out :). If it helped you, drop me a line and let me know.

Creating a tool for changing the fork seals on my R1150gsa

November 9 | 17

So I’m experimenting here, and this short post, even though it has to do with programming, its more oriented on how to make a tool for doing some work on my bike.

Anyways, My fork seals started to leak on my beloved R1150gs adv and, as I have to do everything here on my own, (as there is no BMW motorcycle dealership here in AR) I ordered the fork seals and the dust seals from Tom Cutter at rubber chicken garage (Great guy and he sells OEM BMW parts). But I was missing somehow a tool for pushing the seals inside the forks. Some people mention to use PVC pipes, others mention that a 34mm socket would work, but I wanted to do it precise (so no PVC piping) and I didnt want to spend ~10usd on a socket just for doing that.

So I started to think on a device that I can attach one socket to and that can push around the hard circular surface area of the seals, but at the end I decided to buy a fur pillow.














I ended up designing a mix between a nut and a flat cylinder that just touches specifically the hard surface of the seals in order to push them inside the forks





























It was pretty straight forward doing this in OpenScad and the final result:




























And of course the code to produce this is pretty simple:


Getting SteamVR tracking data in Unity without a HMD

November 8 | 17

TL;DR: I managed to get SteamVR tracking data in unity (position and rotation) from a VIVE tracker / SteamVR Controller without the need of the Head Mounted Display (HMD) being connected. It runs on my 2012 mbp and I’m pretty happy with the results.

Long version :

For the VR project I’m working on I only need the pose on tracked objects from SteamVR. I’m not using a Head Mounted Display (HMD) because 1. I don’t need it and 2. It wouldn’t run on my 2012 mbp.

I followed the tutorials proposed here and here (which are basically the same) but unfortunately I couldn’t manage to make them work with Unity, even though I managed to get to the state where SteamVR status icon said “NOT READY” (which according to them is fine for their tutorials), it wasn’t enough for unity to be able to initialize trackers and get data.








Turns out you need to tell SteamVR in the config files to not only set:

But you also need to force SteamVR to load a null driver (headless driver) and let SteamVR be able to load more than one driver.

In order to do this and be 100% sure where are your config files being loaded the best way to do it is to check for the log files, specifically “vrserver.txt

Finding the log files for SteamVR.

In order to find where does SteamVR stores the config files, just start steam VR, and then click on Settings -> Developer and then click on “Set log directory“. With this you can now where SteamVR stores vrserver.txt log.















The vrserver.txt log is really important because it tells where and which .vrsettings files loads!. To my surprise; I thought I was only loading one custom file I made, but turns out that’s not the case, there where 5 files in different places being loaded!

Diving into vrserver.txt in SteamVR

Now that we know where is the vrserver.txt log file we can proceed to open it. Fear not!, it contains a lot of stuff but we are looking for a few lines specifically in my case looked like this:

Here basically says where does SteamVR loads the *.vrsettings files to set up SteamVR.

Now basically what we need to do is open each file and checkout where in all those files the “requireHmd“, “activateMultipleDrivers” and “forcedDriver” vars are set.

Wherever you find them, set their values to:

If you cannot find them in any place, just add them.

So what we are doing here is forcing SteamVR to load the null (headless) driver.

The null driver looks like this:

You also need to set the “enable” var in the null driver to true to make it work.

Here you can set a bunch of settings depending on how you want to behave. “driver_null” is a non-physical HMD, here you can add any features you want to it.

Now, close SteamVR and open it again, now, instead of having the “Not Ready” label when it starts, you should see this:










In any case you don’t see this, check again the vrsettings.txt log file and read it, it will tell you if you made a mistake in the JSON files (*.vrsettings) you just modified.

Getting tracking data in Unity!

After you have done this you should be golden to try it out in Unity3D, just create a simple project, drag and drop the SteamVR camera rig prefab and set the target eye to “None”

Notice how in the log the loaded driver is “connected to null: Null Serial Number”, that means we successfully connected to the headless driver.














It worked for me in OSX, but it should also work for you following the same workflow in Windows / Linux, if you have issues, just drop me a line I could take a look at your settings and maybe help out 😉