当前位置:首页 >> 中药养生 >> Shader基础知识普及之:素描效果的实现

Shader基础知识普及之:素描效果的实现

发布时间:2025-05-23

EXCOORD1;

float3 hatchWeights1:TEXCOORD2;

SHADOW_COORDS(4)

float3 worldPos:TEXCOORD3;

};

v2f vert (appdata_full v)

{

v2f o;

o.vertex = UnityObjectToClipPos(v.vertex);

o.uv = v.texcoord* _TileFactor;

float3 worldLightDir = normalize(WorldSpaceLightDir(v.vertex));

float3 worldNormal = UnityObjectToWorldNormal(v.normal);

float diffuse = max(0, dot(worldLightDir, worldNormal));

o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz ;

o.hatchWeights0 = float3(0, 0, 0);

o.hatchWeights1 = float3(0, 0, 0);

float hatchFactor = diffuse * 7.0;

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;

}

TRANSFER_SHADOW(o);

return o;

}

fixed4 frag (v2f i) : SV_Target

{

float4 hatchTex0 = tex2D(_Hatch0, i.uv) * i.hatchWeights0.x;

float4 hatchTex1 = tex2D(_Hatch1, i.uv) * i.hatchWeights0.y;

float4 hatchTex2 = tex2D(_Hatch2, i.uv) * i.hatchWeights0.z;

float4 hatchTex3 = tex2D(_Hatch3, i.uv) * i.hatchWeights1.x;

float4 hatchTex4 = tex2D(_Hatch4, i.uv) * i.hatchWeights1.y;

float4 hatchTex5 = tex2D(_Hatch5, i.uv) * i.hatchWeights1.z;

float4 whiteColor = float4(1, 1, 1, 1)*(1 - i.hatchWeights0.x - i.hatchWeights0.y - i.hatchWeights0.z - i.hatchWeights1.x - i.hatchWeights1.y - i.hatchWeights1.z);

float4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5+ whiteColor;

UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

return float4(hatchColor.rgb*_Color.rgb*atten, 1.0);

}

ENDCG

}

}

}

首先对静态透过描边操作者,通过测算折射与物体凹凸不平折射值确定值分量,从而确定三维采集状况。

上面我们对逻辑透过拆分和解析:

描边逻辑:运用于两个通道,其之前一个通道沿单位向量同方向挤出一点,输出逐步形成描边的红色

透视转成操作者:将单位向量同方向转成到最终透视阶段输出并透过偏移操作者:

float3 vnormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);

float2 offset = TransformViewToProjection(vnormal.xy);

o.pos.xy += offset * _OutlineFactor;

测算静态凹凸不平折射:

float diffuse = max(0, dot(worldLightDir, worldNormal));

o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz ;

对折射结果与相异三维构造透过复合操作者:折射越暗,线条越密集

float hatchFactor = diffuse * 7.0;

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;

}

复合蓝色基础性红色信息:折射深色部分值越好,蓝色越少

float4 whiteColor = float4(1, 1, 1, 1)*(1 - i.hatchWeights0.x - i.hatchWeights0.y - i.hatchWeights0.z - i.hatchWeights1.x - i.hatchWeights1.y - i.hatchWeights1.z);

将所以红色信息透过复合操作者

float4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5+ whiteColor;

叠加阴影特性

UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

返回最终特性

return float4(hatchColor.rgb*_Color.rgb*atten, 1.0);

特性调整:

我们增设一个范围0-10的三维长条形分量,修正分量处理方式三维较深素质

相关示例:o.uv = v.texcoord* _TileFactor;

调整长条形分量大小,实现线条稀较深度的特性调整,如下图

增设高密度线条

增设低密度线条

作准备好六张素描色块三维,在六边形着色阶段测算逐六边形的折射太阳光,通过太阳光结果决定六张色块复合值,然后将数据传输到片元图形,片元图形先根据值结果复合六张色块的调制结果。

本期的素描风格shader到此就结束了,我们下期想念!

死精症怎样预防
咽喉肿痛吃什么药好得快
吃氨糖对关节炎的效果
比较好的老年痴呆医院
脉血康肠溶片副作用
哪种眼药水可以长期使用缓解视疲劳
产妇塑身
干眼症用什么眼药水
眼睛发痒干涩怎么办
营养眼睛的药物有哪些
友情链接: