一个卡通风格的 shader

自己整理了下,想用在自己的小项目里的,具体功能有 卡通 ,描边(含精细度),法线,高光(卡通风格有这个的话....有了的话配合贴图效果感觉也蛮不错), 边缘光,只是一些基础的功能,汇总一下暂时够用了。以后要找自己也找得到,不用做无用的重复工作。
  
Shader "AnimalClamp/Cartoons" {
Properties
{
_MainTex("MainTex", 2D) = "white" {}
_BumpMap("Bump Map", 2D) = "bump" {}
_Color("Main Color",color) = (1,1,1,1)
_OutlineCol("Outline Color",color) = (0,0,0,1)
_SpecColor("Spec Color",Color) = (0,0,0,1)
_RimColor("Rim Color", color)= (0,0,0,1)
_RimPower("Rim Power", range(0,2))= 0
_Outline("Thick of Outline",range(0,0.1)) = 0.02
_Smoothness("Smoothness",range(0,1)) = 0.5
_Shine("Shininess", range(0,1)) = 0
_BumpScale("BumpScale",range(0,10)) = 1
}

SubShader{
Pass {
Tags{ "LightMode" = "Always" }
Cull Front
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Outline;
float _Smoothness;
half4 _OutlineCol;
struct v2f {
float4 pos:SV_POSITION;
};


v2f vert(appdata_full v) {
v2f o;
float3 dir = normalize(v.vertex.xyz);
float3 dir2 = v.normal;
float D = dot(dir,dir2);
dir = dir*sign(D);
dir = dir * _Smoothness + dir2 * (1 - _Smoothness);
v.vertex.xyz += dir * _Outline;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
float4 frag(v2f i) :COLOR
{
return _OutlineCol;
}
ENDCG
}

Pass {
Tags{ "LightMode" = "ForwardBase" }
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

sampler2D _MainTex;
sampler2D _BumpMap;
float _Shine;
float4 _LightColor0;
float4 _Color;
float _BumpScale;
float4 _SpecColor;
float4 _RimColor;
float _RimPower;

struct v2f 
{
float4 pos:SV_POSITION;
float3 lightDir:TEXCOORD0;
float3 viewDir:TEXCOORD1;
float3 normal:TEXCOORD2;
float2 uv:TEXCOORD3;
};

v2f vert(appdata_full v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.normal = v.normal;
o.lightDir = ObjSpaceLightDir(v.vertex);
o.viewDir = ObjSpaceViewDir(v.vertex);
o.uv = v.texcoord.xy;
return o;
}
float4 frag(v2f i) :COLOR
{
float4 col = tex2D(_MainTex, i.uv);

float3 normalDir = normalize(i.normal);//法线方向
float3 viewDir = normalize(i.viewDir);//视角方向
float3 lightDir = normalize(i.lightDir);//入射光照方向
float3 reflectDir = reflect(-lightDir, normalDir);//反射光线方向

float cosAngle = max(0, dot(normalDir, i.lightDir));//法线方向和光照方向夹角的余弦
float diff = (cosAngle + 1) / 2;
diff = smoothstep(0,1,diff);
float4 c = _Color * diff * _LightColor0;

//法线贴图
float3 normalMap = UnpackNormal(tex2D(_BumpMap, i.uv));
normalMap.xy *= _BumpScale;
normalMap.z = (1.0 - saturate(dot(normalMap.xy, normalMap.xy)));

float3 normalInfo = c * _LightColor0.rgb * max(0, dot(normalMap, lightDir));

//高光公式进行计算 Specular = LightColor * SpecColor * pow(max(0,dot(R,V)),Shiness),R=reflect(-L,N)  
float3 specular = _SpecColor * _LightColor0.rgb * pow(max(0.0, max(0, dot(normalDir, i.lightDir))), 10/_Shine) * clamp(cosAngle, 0, 1);

//Rim Color 边缘光
float rim = 1 - max(0, dot(viewDir, normalDir));
float3 rimColor = _RimColor * pow(rim, 1 / _RimPower);

return float4(normalInfo, 1) * float4(specular, 1) + c * col + float4(rimColor, 1);
}
ENDCG
}
}
}

这个shader基本没有什么难度,就是法线的运用。我只是之前的工作稍微进行了汇总,每个功能的原理也很简单,百度一搜一大片的,用起来的话,没有特殊需求还是蛮不错的。
都说图形工程师,可能干个一两年才算刚入门。图形嘛,其实说白了就是算法算法算法,我刚开始工作的师父真的是尽心尽力地教了我许多~
其实,能把光影的效果写得很漂亮也很开心了~

争取2017.08.30之前实现一套手机可用PBR通用方案,希望可以对你有一些帮助~
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Requires Unity 5.6.0 or higher. RealToon v5.1.2 An AAA Anime/Toon Shader to make your characters & objects to look as close to real Anime/Cartoon. Toon/Cel shading shader. (For Games, Film/Animations & Illustrations/Arts) - It is Fully Multi-Lighting. Features: - Smooth Object Normal: *Smooth object normal for better or clean shading. - Self Shadow: *(Adjust Size/Threshold and Hardness without using any texture) - Normal Map: *(For more details) - FReflection: *(Use images/textures as reflection.) *(Can also adjust the size and position) *(This is not a matcap style reflection or cubemap.) - Outline: *(Can change the Color and Width) *(With noise/distort outline for sketch style outline and dynamic) *(You can choose to use outline or without outline "for less drawcalls"). - Gloss (Texture) (Custom Gloss): *(Use images/textures as gloss) *(Can choose to follow light position & object rotation.) - ShadowT (Texture): *(Gradient or Flat base shadow/shade.) - RealToon Refraction: *Anime/cartoon style glass, ice, liquid, etc. - Reduce Shadow: *Reduce real-time shadow of an object - material without affecting other parts of an object. - Fade Transparency with outline and transparent affects shadow. - Vertex color support for Outline, Self Shadow & Smooth Object Normal features. - Includes RealToon (Lite Version) shaders for mobile or lite type games. - Includes RealToon (Tessellation Version) shaders. - Includes RealToon (LWRP Version) (Unity 2018 with LWRP V4.0.0 to latest) - Includes effects like sobel outline. - More... Other Features - Use Directional Light and Point & Spot light or multiple lights at the same time without problem and maintaining that anime/cartoon style shadow color & brightness. - Fully color shadow and maintain light falloff. - Fog affect Outline. - Receive GI with Flat/Smooth shade & Skylight/Environment Light. - Do baked/real-time reflection. - Can use patterned textures for manga/comics like shadow. - Use it with or without shadow. - Computer (PC,Mac & Linux), Mobile & Video Game Console (Nintendo Switch) in one shader. - You can also use this in creating Anime/Cartoon film & Illustrations/Arts in unity3d. - You can also use this in making VTuber works with or without real-time shadows. - Can now be used in 2D type games. - Includes a simple tool/script Frame By Frame Rendering for animation & illustration/art use. *Renders frames to Image Sequence or PNG Sequence file. - Includes a simple custom shadow resolution script for higher quality shadow. (For Film/Animation & Illustration/Art use) - This can also be use in VRChat/VRC. - More features to come so be sure to check my social network/media.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值