实现了一个简单的卡通渲染效果

在这里插入图片描述

介绍

简单参考下实现了基本卡通着色渲染效果:
主要包含了描边和内部色块

开始构建了一个场景用于展示光线的变化,并放置了一个角色。

npr_1

接下去加入描边的效果,可以感觉到人物轮廓变明显了。

npr_2

然后再加入了内部的色块变化并调小了点描边的大小。可以看到人物有了一定的风格颜色。

npr_3

但我们这个光照的高光看着过于油腻,收到 这里启发,加上了菲涅尔系数使其更加真实,最终结果如下。

npr_4

由于描边的方式是过程式几何轮廓线渲染,其核心是两个Pass,第一个用于渲染背面的面片也就是将其顶点延发现方向移动一定距离,第二个Pass渲染正面的内容。其不太适合平整模型,可以看到正方形的影子有种脱节的感觉,为了更明显展示缺陷也没加内部色块变化。

最终的shader如下:

Shader  "Custom/NPRShader"{
	Properties {
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_MainTex ("Main Tex", 2D) = "white" {}
		_Ramp ("Ramp Texture", 2D) = "white" {}
		_Outline ("Outline", Range(0, 1)) = 0.1
		_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
		_Specular ("Specular", Color) = (1, 1, 1, 1)
		_SpecularScale ("Specular Scale", Range(0, 0.1)) = 0.01
	}
    SubShader {
		Tags { "RenderType"="Opaque" "Queue"="Geometry"}
		
		Pass {
			NAME "OUTLINE"
			
			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;
				
				float4 pos = mul(UNITY_MATRIX_MV, v.vertex); 
				float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);  
				normal.z = -0.5;
				pos = pos + float4(normalize(normal), 0) * _Outline;
				o.pos = mul(UNITY_MATRIX_P, pos);
				
				return o;
			}
			
			float4 frag(v2f i) : SV_Target { 
				return float4(_OutlineColor.rgb, 1);               
			}
			
			ENDCG
		}
		
		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;
				float4 texcoord : TEXCOORD0;
				float4 tangent : TANGENT;
			}; 
		
			struct v2f {
				float4 pos : POSITION;
				float2 uv : TEXCOORD0;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos : TEXCOORD2;
				SHADOW_COORDS(3)
			};
			
			v2f vert (a2v v) {
				v2f o;
				
				o.pos = UnityObjectToClipPos( v.vertex);
				o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
				o.worldNormal  = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				
				TRANSFER_SHADOW(o);
				
				return o;
			}
			
			float4 frag(v2f i) : SV_Target { 
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);
				float _SpecularFresnel = 0.28;
				fixed4 c = tex2D (_MainTex, i.uv);
				fixed3 albedo = c.rgb * _Color.rgb;
				
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				
				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
				
				fixed diff =  dot(worldNormal, worldLightDir);
				diff = (diff * 0.5 + 0.5) * atten;
				
				fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, diff)).rgb;
				
				fixed spec = dot(worldNormal, worldHalfDir);
				fixed w = fwidth(spec) * 2.0;
				fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);
				
				float fresnel = 1.0 - dot(worldViewDir, worldHalfDir);
				fresnel = pow(fresnel, 5.0);
				fresnel += _SpecularFresnel  * (1.0 - fresnel);

				//float3 finalSpec = _SpecularColor * spec* fresnel;;
				float3 finalSpec = specular * fresnel * _LightColor0.rgb;
				return fixed4(ambient + diffuse + finalSpec, 1.0);
			}
		
			ENDCG
		}
	}
	FallBack "Diffuse"
}

参考内容
《Unity Shader 入门精要》

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 04:52:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 04:52:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 04:52:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 04:52:03       18 阅读

热门阅读

  1. 【无标题】

    2023-12-07 04:52:03       47 阅读
  2. a href自定义下载文件名

    2023-12-07 04:52:03       42 阅读
  3. 设计模式&委派模式(Delegate Pattern)

    2023-12-07 04:52:03       33 阅读
  4. 【LeetCode】258. 各位相加

    2023-12-07 04:52:03       36 阅读
  5. Vue中的组件通信:从子到父的数据传递

    2023-12-07 04:52:03       40 阅读
  6. C++设计模式——建造者模式(Builder)

    2023-12-07 04:52:03       44 阅读
  7. ES6拓展API

    2023-12-07 04:52:03       32 阅读
  8. Socket.D 网络应用协议,首版发布!

    2023-12-07 04:52:03       38 阅读
  9. 字符指针变量

    2023-12-07 04:52:03       38 阅读
  10. 数据结构-基数排序

    2023-12-07 04:52:03       42 阅读
  11. 利用 Python 进行数据分析实验(二)

    2023-12-07 04:52:03       40 阅读