Unity Polybrush and Shaders (Vertex Color)
Some time ago Unity gave us the free beta for Polybrush, a tool that lets you do some rough sculpting, texture blending, object scattering, and vertex color painting. I'll quickly go over all these options but the most interesting one to me is the vertex color painting.

To work with vertex colors you have to go into a 3d program and guess what the end result will be (or spend a bunch of time writing your own solution) But with Polybrush you can just paint right in the editor and see exactly what you're doing.

In this post I'll show a few examples of cool shaders that use vertex colors, and how to use Polybrush with them.

Setting up Polybrush

You can download the Polybrush Beta here: Asset Store Link .

Import the package and go to Tools > Polybrush > Polybrush Window.

In the window we've got Sculpting(1) Vertex Color Painting (2) Object Scattering (3) and Texture Blending(4).

Sculpting

Very straight-forward, the first tab is for Raise/Lower, the second for Smoothing.

Vertex Painting

The main reason for this post, we'll get to that later.

Object Scattering 

Before you can use this part you first have to add some prefabs to the Palette, found in Procore > PolybrushPrefab Palettes > Default.

Drag the prefabs you want to paint in here (Lock the inspector to make this easier). Select and press Backspace if you want to delete one.

Now you can place objects where you click, with the brush strength controlling the density.

Texture Blending

This one requires you to have specific shader setup, it uses 3 uv sets. But there are a few examples included in the package. Open them up to check how it's set up.

This gif shows "Polybrush/TriPlanar Blend"
Just add the texture you want to the material and select it in the Texture Paint Settings.
Flood will make the whole mesh the selected color.
Fill will only paint the vertices in the preview.
Brush will paint in a radius around the cursor.
(Same controls for the Vertex Painter)

Let's look at the Vertex Color painting part now.

Reading Vertex Colors in shaders

To access the vertex colors in a shader you just need to add a line in either the Input Struct (surface shaders), or the Appdata Struct (vertex/fragment shader) 

Vertex/Fragment Shader

Read the colors in the Appdata struct, declare them in the v2F struct, then pass them between the two. You can read them in the Fragment shader with "i.color"

Surface Shader 

Now you can read the vertex colors with "IN.vertexColor"

Here's a link to the most basic vertex color shader if you don't want to do code: PasteBin Link 

And there we go, painting vertex colors. Time to look into the more interesting examples.

Example: Specular

Remember the Toon Specular Shader I made a while ago? It would be pretty cool if instead of setting a static mask, you can just paint where you want things to look wet/ like glass.

For this to work I just multiplied the fake specular value with IN.vertexColor.r
There's also a rimlight in the o.Emission channel that is multiplied with IN.vertexColor.r, so the painted area also has a glowing effect.

 float3 spec = (step(_SpecSize, rampS.r) * IN.vertexColor.r;
 o.Emission =  pow(rim, _RimPower) * IN.vertexColor.r

This means that where ever I paint red, it will be shiny, if something should just be a little bit shiny, just use a darker red.

Specular Vertex Color
Shader Code: PasteBin Link 

Toon Gradient:

Specular Gradient:

Example: Swaying Animation

Similar to the Moving Grass tutorial I made before. 

If you don't want to use Cloth/Physics simulation on some static props like flags, foliage, chains you use a shader that does a bit of vertex animation. 

Most global solutions will leave with a lot of clipping issues. The nice part of the vertex color version is that you can set the amount of allowed movement. In the above example I want the banner to not move at all on the top , but move only a little in the middle, where the pole is.

Again multiplying with the red vertex color channel to control where the effect is.

 v.vertex.yz += sin(_Time.y * movementcalculation ) * v.vertexColor.r; 

Swaying Vertex Color
Shader Code: PasteBin Link 

Example: Triplanar Multi-Texture

The texture blending tool shown in the beginning of the article gives a very smooth/ blurry result. This is fine for some art styles, but I like my edges crisp and with some noise.

This shader uses the same base as the Triplanar Tutorial but instead of basing the "grass" on the world normal, it uses the Red and Blue (could add more!) vertex channels to add a new texture.

float primary = step(0.6* noisetexture,IN.vertexColor.r ); 

Multiply that with the texture you want to display on the red channel

then for the edge around the texture, multiply a slightly bigger area than the primary, with the inverted primary, so only a small portion remains

float primaryEdge = (step(0.5* noisetexture,IN.vertexColor.r )) * (1-primary); 

The vertex colors are very blurry, but because of the step function and the noise texture the result is very clean

(Could probably rework the texture blending shader to do a similar effect, giving more textures because of the multiple UV sets)

Triplanar Vertex Color
Shader Code: PasteBin Link 


That's it for now! Hope you can use this tool and play with these examples.

Thanks for reading and have fun!

NOTE: if you change the import settings of the mesh you're working on, it'll break the geometry and you have to re-do your work

Also, it's a Beta, so things can go wrong!!

Become a patron to

138
Unlock 138 exclusive posts
Be part of the community
Connect via private message