Unity-Chan Toon Shader cartoon rendering learning

(This article records the re-engraving of UTS2 from 0-1 and will be continuously updated)

1. Introduction to UTS2

Unity-Chan Toon Shader 2.0 (UTS2) is a cartoon shader for images and videos, designed to meet the needs of cartoonists Shading 3DCG animation on demand by creators.

UTS2 open source project address:GitHub - unity3d-jp/UnityChanToonShaderVer2_Project: UnityChanToonShaderVer2 Project / v.2.0.9 Release

2. Replicate UTS2 from 0-1

(1)3 Basic Colors

The diffuse reflection part of UTS2 is mainly composed of three layers, namely the basic color, the first shadow color and the second shadow color, which respectively correspond to white, gray and black in art theory.

The basic color is mainly used to color the light-receiving surface, that is, the bright part of the object:

 

The first shadow color colors the transition between the light-receiving surface and the backlit surface:

 

The second shadow color tints the backlight:

Then there are several parameters that can adjust the range of these three colors and the degree of feathering:

BaseColor Step is used to adjust the range of the first shadow color:

Base/Shade Feather is used to adjust the feathering degree of the first shadow color:

 

ShadeColor Step is used to adjust the range of the second shadow color:

1st/2nd_Shades Feather is used to adjust the feathering degree of the second shadow color:

 

Simple implementation code:

float4 baseMap_var = tex2D(_BaseMap, i.uv);
float3 baseMap_RGB = baseMap_var * _BaseMapCol; //基本色
float4 _1st_ShadowMap_var = lerp(tex2D(_1st_ShadowMap, i.uv), baseMap_var, _Use_BaseAs1st);
float3 _1st_ShadowMap_RGB = _1st_ShadowMap_var * _1st_ShadowMapCol; //第一阴影色
float4 _2nd_ShadowMap_var = lerp(tex2D(_2nd_ShadowMap, i.uv), _1st_ShadowMap_var, _Use_1stAs2nd);
float3 _2nd_ShadowMap_RGB = _2nd_ShadowMap_var * _2nd_ShadowMapCol; //第二阴影色
float halfLambert = 0.5 * dot(setNormalDir, lightDir) + 0.5; //半兰伯特模型
float base_1stShadow_mask = saturate(1.0 - (halfLambert - (_BaseColStep - _BaseShadeFeather)) / _BaseShadeFeather); //基本色到第一阴影色的插值参数
float _1st_2ndShadow_mask = saturate(1.0 - (halfLambert - (_ShadeColStep - _1st2nd_ShadesFeather)) / _1st2nd_ShadesFeather); //第一阴影色到第二阴影色的插值参数
float3 diffuse = lerp(baseMap_RGB, lerp(_1st_ShadowMap_RGB, _2nd_ShadowMap_RGB, _1st_2ndShadow_mask), base_1stShadow_mask); //插值实现颜色过渡

Two lerps are mainly used to achieve color transition.

(2)High Color

1.HighColor Tex is used to specify the highlight map;

2.HighColor Power controls the highlight intensity;

3.Specular Mode determines whether to use the normal highlight effect;

Enable:

closure:

This highlight has sharp edges and lacks a gradient in color.

4.Color Blend Mode determines the blending method of highlights;

The Additive method makes the color brighter. Both of the above highlight modes can be used:

The Multiply method makes the color darker and can only be used in the second highlight form:

5.HighColor Mask is used to specify the highlight mask and determine the highlight range and size; HighColor Mask Level adjusts the mask intensity.

Simple implementation code:

float4 highColorTex_var = tex2D(_HighColorTex, i.uv); //高光贴图
float4 highColorMask_var = tex2D(_HighColorMask, i.uv); //高光遮罩贴图
float3 halfDir = normalize(lightDir + viewDir); //半程向量
float specular = 0.5 * dot(halfDir, lerp(i.nDirWS, setNormalDir, _Is_NormalMapToHighColor)) + 0.5;
float tweakHighColorMask_var = saturate(highColorMask_var.g + _HighColorMaskLevel) * lerp(1.0 - step(specular, 1.0 - pow(_HighColorPower, 5)), pow(specular, exp2(lerp(11, 1, _HighColorPower))), _Is_SpecularToHighColor); //两种高光形式
float3 specualrCol = (highColorTex_var * _HighColor * tweakHighColorMask_var).rgb;
diffuseCol = lerp(saturate(diffuseCol - tweakHighColorMask_var), diffuseCol, lerp(_Is_BlendAddToHighColor, 1, _Is_SpecularToHighColor)); //和漫反射的混合方式

(3)Normal Map

I won’t go into too much about the use of Normal Map, but I’ll focus on using different buttons to control whether normals affect different light effects:

3 Basic Color button

High Color button

The Lerp function is mainly used to decide whether to use normal maps.

Reference:The Day the Game Was Born 09 - Art Chapter Cartoon Rendering Shader UTS2 - Zhihu (zhihu.com)

Guess you like

Origin blog.csdn.net/qq_63133691/article/details/131376033