Unity Shader实现素描风格的渲染

 更新时间:2020年04月29日 10:28:09   作者:vvc223c  
这篇文章主要为大家详细介绍了Unity Shader实现素描风格的渲染,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity Shader实现素描风格的具体代码,供大家参考,具体内容如下

原理

使用6张素描纹理进行渲染,在渲染阶段,在顶点着色阶段计算逐顶点的光照,根据光照结果决定6张纹理的混合权重,并传递给片元着色器。在片元着色器中根据这些权重来混合6张纹理的采样结果

Shader实现

Shader "Hatching" 
{
 Properties {
 _Color ("Color Tint", Color) = (1, 1, 1, 1)//颜色
 _TileFactor ("Tile Factor", Float) = 1//纹理的平铺系数,数值越大素描线条越密
 _Outline ("Outline", Range(0, 1)) = 0.1
 _Hatch0 ("Hatch 0", 2D) = "white" {}
 _Hatch1 ("Hatch 1", 2D) = "white" {}
 _Hatch2 ("Hatch 2", 2D) = "white" {}
 _Hatch3 ("Hatch 3", 2D) = "white" {}
 _Hatch4 ("Hatch 4", 2D) = "white" {}
 _Hatch5 ("Hatch 5", 2D) = "white" {}//对应的6张素描纹理
 }
 
 SubShader {
 Tags { "RenderType"="Opaque" "Queue"="Geometry"}
 
 
 Pass {
 Tags { "LightMode"="ForwardBase" }
 
 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;
 float _TileFactor;
 sampler2D _Hatch0;
 sampler2D _Hatch1;
 sampler2D _Hatch2;
 sampler2D _Hatch3;
 sampler2D _Hatch4;
 sampler2D _Hatch5;
 
 struct a2v {
 float4 vertex : POSITION;
 float4 tangent : TANGENT; 
 float3 normal : NORMAL; 
 float2 texcoord : TEXCOORD0; 
 };
 
 struct v2f {
 float4 pos : SV_POSITION;
 float2 uv : TEXCOORD0;
 fixed3 hatchWeights0 : TEXCOORD1;//
 fixed3 hatchWeights1 : TEXCOORD2;// 6个混合权重,存在两个fixed3变量中
 float3 worldPos : TEXCOORD3;
 SHADOW_COORDS(4)
 };
 
 v2f vert(a2v v) {
 v2f o;
 
 o.pos = UnityObjectToClipPos(v.vertex);
 
 o.uv = v.texcoord.xy * _TileFactor;
 
 fixed3 worldLightDir = normalize(WorldSpaceLightDir(v.vertex));
 fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
 fixed diff = max(0, dot(worldLightDir, worldNormal));//漫反射系数
 
 o.hatchWeights0 = fixed3(0, 0, 0);
 o.hatchWeights1 = fixed3(0, 0, 0);
 
 float hatchFactor = diff * 7.0;//把diff缩放到[0,7]范围
 
 //纯白
 if (hatchFactor > 6.0) 
 {
 
 } else if (hatchFactor > 5.0) {
  o.hatchWeights0.x = hatchFactor - 5.0;
 } else if (hatchFactor > 4.0) {
  o.hatchWeights0.x = hatchFactor - 4.0;
  o.hatchWeights0.y = 1.0 - o.hatchWeights0.x;
 } else if (hatchFactor > 3.0) {
  o.hatchWeights0.y = hatchFactor - 3.0;
  o.hatchWeights0.z = 1.0 - o.hatchWeights0.y;
 } else if (hatchFactor > 2.0) {
  o.hatchWeights0.z = hatchFactor - 2.0;
  o.hatchWeights1.x = 1.0 - o.hatchWeights0.z;
 } else if (hatchFactor > 1.0) {
  o.hatchWeights1.x = hatchFactor - 1.0;
  o.hatchWeights1.y = 1.0 - o.hatchWeights1.x;
 } else {
  o.hatchWeights1.y = hatchFactor;
  o.hatchWeights1.z = 1.0 - o.hatchWeights1.y;
 }
 
 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
 
 TRANSFER_SHADOW(o);
 
 return o; 
 }
 
 fixed4 frag(v2f i) : SV_Target 
 { //根据相应的权重进行采样
 fixed4 hatchTex0 = tex2D(_Hatch0, i.uv) * i.hatchWeights0.x;
 fixed4 hatchTex1 = tex2D(_Hatch1, i.uv) * i.hatchWeights0.y;
 fixed4 hatchTex2 = tex2D(_Hatch2, i.uv) * i.hatchWeights0.z;
 fixed4 hatchTex3 = tex2D(_Hatch3, i.uv) * i.hatchWeights1.x;
 fixed4 hatchTex4 = tex2D(_Hatch4, i.uv) * i.hatchWeights1.y;
 fixed4 hatchTex5 = tex2D(_Hatch5, i.uv) * i.hatchWeights1.z;
 fixed4 whiteColor = fixed4(1, 1, 1, 1) * (1 - i.hatchWeights0.x - i.hatchWeights0.y - i.hatchWeights0.z - 
  i.hatchWeights1.x - i.hatchWeights1.y - i.hatchWeights1.z);
 
 fixed4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5 + whiteColor;
 
 UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
  
 return fixed4(hatchColor.rgb * _Color.rgb * atten, 1.0);
 }
 
 ENDCG
 }
 }
 FallBack "Diffuse"
}

效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • Unity实现卡牌翻动效果

    Unity实现卡牌翻动效果

    这篇文章主要为大家详细介绍了Unity实现卡牌翻动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#实现顺序队列和链队列的代码实例

    C#实现顺序队列和链队列的代码实例

    今天小编就为大家分享一篇关于C#实现顺序队列和链队列的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • C# winform 请求http的实现(get,post)

    C# winform 请求http的实现(get,post)

    本文主要介绍了C# winform 请求http的实现(get,post),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • c#添加Newtonsoft.Json包的操作

    c#添加Newtonsoft.Json包的操作

    这篇文章主要介绍了c#添加Newtonsoft.Json包的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C#实现去除Strings中空格的方法

    C#实现去除Strings中空格的方法

    这篇文章主要介绍了C#实现去除Strings中空格的方法,较为详细的介绍了C#实现去除字符串首尾及中间空格的方法,是非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • C# Onnx CenterNet实现目标检测的示例详解

    C# Onnx CenterNet实现目标检测的示例详解

    这篇文章主要为大家详细介绍了C# Onnx CenterNet实现目标检测的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • C#线程间不能调用剪切板的解决方法

    C#线程间不能调用剪切板的解决方法

    这篇文章主要介绍了C#线程间不能调用剪切板的解决方法,需要的朋友可以参考下
    2014-07-07
  • WPF中的导航框架概述

    WPF中的导航框架概述

    这篇文章介绍了WPF中的导航框架,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#实现FTP文件下载及超时控制详解

    C#实现FTP文件下载及超时控制详解

    这篇文章主要为大家详细介绍了C#实现FTP文件下载及超时控制的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • Winform学生信息管理系统主页面设计(2)

    Winform学生信息管理系统主页面设计(2)

    这篇文章主要为大家详细介绍了Winform学生信息管理系统主页面设计思路,感兴趣的小伙伴们可以参考一下
    2016-05-05

最新评论