当前位置:网站首页>Unity shader cartoon style rendering
Unity shader cartoon style rendering
2022-07-28 17:06:00 【Morita Rinko】
Cartoon style rendering
The key to achieving
- Render a more obvious outline
Use two Pass
first Pass: Render the whole back panel with contour , And in the perspective space , Expand the model vertices outward along the normal direction .
the second Pass: Normal rendering element
In order to avoid some concave models blocking the front panel after expansion , We take the normal z Component processing , Make it a negative value , In this way, it will not exceed the original part after expansion .
- Add more obvious highlights
Also need to calculate , Normals and h Dot product result of vector .
We judge the result of dot multiplication , If it is greater than the threshold , Indicates that it is within the highlight range . If it is less than the threshold, it means it is not in the highlight range .
In order to solve the problem of uneven transition at the edge of the highlight , We make use of smoothstep function , If the difference between the two is greater than a certain value , Then for 1, If it is less than a certain value, it is 0, If within a certain range , Then for [0,1] Number between , So when it is within a certain range of the boundary , Let's take the difference between the two colors , Make the transition smooth .
Realization effect
Code
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/Chapter14-ToonShading"
{
Properties
{
_Color ("Color Tint", Color) = (1,1,1,1)
_MainTex ("Main Tex", 2D) = "white" {
}
// Diffuse gradient texture
_Ramp("Ramp Texture",2D)="white"{
}
// Marginal width
_Outline("Outline",Range(0,1))=0.1
// Marginal color
_OutlineColor("Outline Color",Color)=(0,0,0,1)
// The color of the highlight
_Specular("Specular",Color)=(1,1,1,1)
// The threshold of highlight
_SpecularScale("Specular Scale",Range(0,0.1))=0.01
}
SubShader
{
// Render back
Pass{
NAME "OUTLINE"
// Eliminate the front
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unityCG.cginc"
float _Outline;
fixed4 _OutlineColor;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
};
struct v2f{
float4 pos:SV_POSITION;
};
v2f vert(a2v v){
v2f o;
// Coordinate conversion to perspective space
float4 pos =mul(UNITY_MATRIX_MV,v.vertex);
// Normal is converted to perspective coordinates
float3 normal =mul((float3x3)UNITY_MATRIX_MV,v.normal);
// Vertex expansion
normal.z=-0.5;
pos =pos+float4(normalize(normal),0)*_Outline;
o.pos =mul(UNITY_MATRIX_P,pos);
return o;
}
fixed4 frag(v2f i):SV_Target{
return float4(_OutlineColor.rgb,1);
}
ENDCG
}
// Render front
Pass{
Tags{
"LightMode" = "ForwardBase"}
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
#include "UnityShaderVariables.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Ramp;
fixed4 _Specular;
fixed _SpecularScale;
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
float2 texcoord:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
float2 uv:TEXCOORD2;
SHADOW_COORDS(3)
};
v2f vert(a2v v){
v2f o;
o.pos =UnityObjectToClipPos(v.vertex);
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i):SV_Target{
// Calculate the various variables needed
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);
// Sample the color
fixed4 c = tex2D (_MainTex, i.uv);
// Anti chromaticity
fixed3 albedo = c.rgb * _Color.rgb;
// Ambient light
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
// shadow
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
// Half Lambert diffuse reflection coefficient
fixed diff = dot(worldNormal, worldLightDir);
// Diffuse reflectance coefficient
diff = (diff * 0.5 + 0.5) * atten;
// Diffuse light
fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, diff)).rgb;
// highlights
fixed spec = dot(worldNormal, worldHalfDir);
// Very small w
fixed w = fwidth(spec) * 2.0;
// stay 0-1 Difference between ,step(0.0001, _SpecularScale) Guarantee _SpecularScale by 0 The highlight color is 0
fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);
// Global illumination plus diffuse reflection plus highlights
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
边栏推荐
- Leetcode647. Palindrome substring
- Rsync service deployment and parameter details
- Brother Ali teaches you how to correctly understand the problem of standard IO buffer
- 做题笔记4(第一个错误的版本,搜索插入位置)
- MySQL安装教程
- Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
- How should I understand craft
- 负整数及浮点数的二进制表示
- Call DLL file without source code
- Technology sharing | how to recover the erroneously deleted table and the data in the table?
猜你喜欢

HTAP comes at a price

Comprehensively design an oppe homepage -- after sales service of the page

在AD中添加差分对及连线

Interesting kotlin 0x08:what am I

Comprehensively design an oppe homepage -- page service part

Call DLL file without source code

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导

PostgreSQL每周新闻—2022年7月20日

Applet: scroll view slides to the bottom by default
随机推荐
【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导
Cluster construction and use of redis5
Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
阿里云 MSE 支持 Go 语言流量防护
华为Mate 40系列曝光:大曲率双曲面屏,5nm麒麟1020处理器!还将有天玑1000+的版本
Understanding of asmlinkage
Games101-assignment05 ray tracing - rays intersect triangles
Is smart park the trend of future development?
Oracle system composition
Implementation of transfer business
2019年全球移动通信基站市场:爱立信、华为、诺基亚分列前三
Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low
做题笔记4(第一个错误的版本,搜索插入位置)
epoll水平出发何边沿触发
Given positive integers n and m, both between 1 and 10 ^ 9, n < = m, find out how many numbers have even digits between them (including N and m)
Ruoyi's solution to error reporting after integrating flyway
Add differential pairs and connections in Ad
[JS] eight practical new functions of 1394-es2022
Epoll horizontal departure, which edge triggers
浏览器解码过程分析
