Unity Polybrush with vertex color shader

 

  Earlier this year, we announced that ProBuilder and Polybrush would officially become part of Unity. We've covered ProBuilder for fast level modeling in detail, and Polybrush can help us with rough sculpting, texture blending, object scattering, and vertex color painting.

   

  Usually in order to deal with vertex colors, 3D programming has to be done and estimated for the end result. But with Polybrush, we can draw directly in the editor, and then see the effect after processing.

  This article will show some examples of shaders using vertex colors and how to use Polybrush with them.

  Get Polybrush

  You can visit the Asset Store to download Polybrush:

  https://www.assetstore.unity3d.com/cn/?stay#!/content/111427

  After the download is complete, import the resource pack, click the menu bar, and select Tools > Polybrush > Polybrush Window.

  

  Open the Polybrush window, as shown in the figure below, we can see four function options:

  Sculpting

  Vertex Color Painting

  Object Scattering

  Texture Blending

  

  Features

  1

  Sculpture

  This function has two tab pages, the first tab page is used to raise and lower the vertex position, and the second tab page is used to smooth the vertex position.

  

  2

  object scatter

  Before using this part of the function, you first need to add some prefabs to the Palette. The prefabs are located in the directory Procore > PolybrushPrefab Palettes > Default.

  

  Drag the prefab you want to draw into here. If you want to delete a prefab, select it and press backspace.

  

  Objects can now be clicked to place, the brush's Strength property controls object density.

  

  3

  texture blending

  The texture blending feature requires a specific shader setup, which uses three UV sets. The resource pack contains some examples to see how to set it up.

  The TriPlanar Blend function of Polybrush is shown below.

  

  Add the desired texture to the material, and then select the texture in Texture Paint Settings. The Flood option will paint the entire mesh as the selected color, the Fill option will only paint the vertices in the preview image, and the Brush option will draw a circular area of ​​a specific radius with the cursor as the center.

  Vertex color painting

  Now let's cover vertex color painting in detail.

  1

  Read vertex color in shader

  In order to access the vertex color in the shader, you just need to add a line of code to the Input Struct (the structure used by the surface shader) or Appdata Struct (the structure used by the vertex/fragment shader).

  Vertex/Fragment Shader

  The following code will read the color in the Appdata struct, declare the color in the v2F struct, and then pass the color between the two structures. You can read the color using "i.color" in the fragment shader.

  

  surface shader

   You can read the vertex color with "IN.vertexColor".

  

  If you don't want to write code, visit the following link, here is the most basic vertex shader:

  https://pastebin.com/6wwsnJgY

  

  Now that we can paint vertex colors, let's look at some interesting examples.

  example

  1

  highlight

  Visit the link below, this is a Toon Specular shader (Toon Specular):

  https://www.patreon.com/posts/quick-game-art-13059579

  

  This shader works even better without a static mask, and we can use this shader to paint anywhere to make objects look wet or look like glass.

  To achieve this part, we multiply the pseudo-highlight value by IN.vertexColor.r, and the rim light of the o.Emission channel is also multiplied by IN.vertexColor.r, so the painted area also has a glow effect.

   float3 spec = (step(_SpecSize, rampS.r) * IN.vertexColor.r;

   o.Emission = pow(rim, _RimPower) * IN.vertexColor.r;

  This means that wherever the red is drawn, it will glow, and if you don't want the glow to be too obvious, you can use a darker red.

  

  Specular vertex color shader code: https://pastebin.com/xBMNuySz

  The cartoon color scale is shown in the figure below:

  

  The highlight color scale is shown in the figure below:

  

  2

  swing animation

  The animation is similar to dynamic grass, if we don't want to use Cloth/Physics simulation effects on some static objects such as flags, leaves and chains, we can use a shader that handles vertex animation.

  

  Most global solutions introduce clipping issues. The benefit of using a vertex color palette solution is that you can set the amount of movement allowed. In the example below, we want to keep the top of the flag from moving, but only move the middle of the pole a little bit.

  

  We can multiply the red vertex color channel to control the position of the effect.

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

  

  Swing vertex color shader code: https://pastebin.com/SsDaBEgy

  3

  Three-plane multi-texture

  The texture blending tools shown earlier in this article provide very smooth or blurred effects. For some art styles this is fine, but we could try to make the edges sharper with noise.

  

  This shader uses the same base color as the triplane, but instead of using "Grass" on the world normal, it uses the red and blue vertex channels to add new textures.

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

  Multiply this with the texture shown on the red channel, then for the edges around the texture, multiply an area slightly larger than the original part by the inverted original part, leaving only a small part.

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

  The vertex colors are very blurry, but thanks to the Step function and the noise texture, the result is very clean. We could also rework the texture blending shader to achieve a similar effect, since multiple UV sets result in more textures.

  

  Triplane vertex color shader code: https://pastebin.com/r9EcWvgH

  summary

  I will share about Polybrush here, I hope you can use this tool and try the examples in the article, but be aware that if you modify the import settings of the mesh being processed, it may destroy the geometry and cause you to have to remake it.

  Polybrush is still in the beta stage, but it is planned to be directly integrated into Unity this year. For related information, please pay attention to the official Chinese forum of Unity (UnityChina.cn)!

  recommended reading

  Unity Shader shader optimization

  Shader Graph shader view sample project introduction

  Unity Shader Training Camp (1): Getting Started

  Unity Shader Bootcamp (2) - MVP Transformation and Normal Mapping

  ProBuilder Quick Level Modeling Practice

  Create Realistic VR Visual Experiences with Unity

  Unity VR/AR project optimization techniques and tools

  Unity UI performance optimization tips

  Made with Unity Animated Short Film "Dabai's Dream of Counting Sheep"

  official activity

  Unity official teacher training registration is in full swing

  Unity will hold a 5-day professional Unity official teacher training course from October 22 to 26. We sincerely invite teachers to learn and share the latest technology with Unity! [Learn more...]

 

Guess you like

Origin blog.csdn.net/jiergio/article/details/103622453