shader入门精要读书笔记40 素描风格的渲染
生活随笔
收集整理的這篇文章主要介紹了
shader入门精要读书笔记40 素描风格的渲染
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總結(jié)下頭文件:
HLSLSupport.cginc - (自動(dòng)包含) 為跨平臺(tái)的著色器編譯宏和定義提供幫助。
UnityShaderVariables.cginc - (自動(dòng)包含)常用的全局變量。
UnityCG.cginc - 常用的輔助函數(shù)
AutoLight.cginc - 照明&陰影功能,如表面著色器在內(nèi)部使用這個(gè)文件
Light.cginc - 標(biāo)準(zhǔn)表面著色器照明模式,當(dāng)你寫surface shaders時(shí)自動(dòng)包含。
TerrainEngine.cginc - 對(duì)地形和植被著色器的輔助函數(shù)
一、思路:
素描效果,我們需要根據(jù)不同光照的漫發(fā)射,選擇不同的紋理混合
二、實(shí)現(xiàn):
代碼:
Shader "Unity Shaders Book/Chapter 14/Hatching" {Properties {_Color ("Color Tint", Color) = (1, 1, 1, 1)_TileFactor ("Tile Factor", Float) = 1 //紋理的平鋪系數(shù),越大,素描線越密_Outline ("Outline", Range(0, 1)) = 0.1 控制輪廓線寬度_OutlineColor ("Outline Color", Color) = (0, 0, 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"} //不透明的幾何體UsePass "Unity Shaders Book/Chapter 14/Toon Shading/OUTLINE" //使用之前的輪廓線渲染,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; //定義兩個(gè)fixed3,共6個(gè)數(shù),存儲(chǔ)6個(gè)混合權(quán)重(共6個(gè)紋理)fixed3 hatchWeights1 : TEXCOORD2;float3 worldPos : TEXCOORD3;SHADOW_COORDS(4)};v2f vert(a2v v) {v2f o;o.pos = UnityObjectToClipPos(v.vertex);o.uv = v.texcoord.xy * _TileFactor; //紋理采樣坐標(biāo)fixed3 worldLightDir = normalize(WorldSpaceLightDir(v.vertex));fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);fixed diff = max(0, dot(worldLightDir, worldNormal)); //得到漫反射系數(shù)o.hatchWeights0 = fixed3(0, 0, 0); //權(quán)重初始化為0o.hatchWeights1 = fixed3(0, 0, 0);float hatchFactor = diff * 7.0; //把diff范圍弄到[0,7]if (hatchFactor > 6.0) { //根據(jù)大小分別分配權(quán)重// Pure white, do nothing //漫發(fā)射很大,以至于用白色} else if (hatchFactor > 5.0) {o.hatchWeights0.x = hatchFactor - 5.0; //大于5的分配給第一個(gè)圖的權(quán)重} else if (hatchFactor > 4.0) {o.hatchWeights0.x = hatchFactor - 4.0; //大于四將多余4的部分分配給第一個(gè)圖o.hatchWeights0.y = 1.0 - o.hatchWeights0.x; //剩余的部分權(quán)重給相鄰,也就是第二個(gè)圖。} 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; //得到世界空間頂點(diǎn)坐標(biāo)TRANSFER_SHADOW(o); //陰影衰減計(jì)算等等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);//進(jìn)行權(quán)重計(jì)算,最后計(jì)算純白色,把所有的剪掉就是留白部分fixed4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5 + whiteColor;//相加混合UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);//陰影衰減計(jì)算return fixed4(hatchColor.rgb * _Color.rgb * atten, 1.0);}ENDCG}}FallBack "Diffuse" }總結(jié)
以上是生活随笔為你收集整理的shader入门精要读书笔记40 素描风格的渲染的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: steam搬砖项目,怎么做?详细解答
- 下一篇: 模拟后台接收短信验证码