unity 写一个水墨效果shader
时间: 2023-10-24 07:42:37 浏览: 251
下面是一个简单的水墨效果Shader的示例:
```shader
Shader "Custom/Watercolor" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_EdgeDarkness ("Edge Darkness", Range(0, 1)) = 0.1
_InkAmount ("Ink Amount", Range(0, 1)) = 0.5
_InkDensity ("Ink Density", Range(0, 1)) = 0.5
_InkSpread ("Ink Spread", Range(0, 1)) = 0.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float _EdgeDarkness;
float _InkAmount;
float _InkDensity;
float _InkSpread;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target {
float4 color = tex2D(_MainTex, i.uv);
// Edge Detection
float3 sobel_x = float3(-1, 0, 1);
float3 sobel_y = float3(-1, 0, 1);
float4 edge = 1 - length(tex2D(_MainTex, i.uv + sobel_x.xy * _ScreenParams.zw)) +
length(tex2D(_MainTex, i.uv + sobel_x.zy * _ScreenParams.zw)) +
length(tex2D(_MainTex, i.uv + sobel_y.xy * _ScreenParams.zw)) -
length(tex2D(_MainTex, i.uv + sobel_y.zy * _ScreenParams.zw));
// Ink effect
float ink = saturate((_InkDensity + _InkSpread) - length(edge.rgb) * _InkSpread);
color = lerp(color, float4(0, 0, 0, 1), ink * _InkAmount);
// Edge darkness
float edgeDarkness = saturate((_EdgeDarkness * 2.0 - length(edge.rgb)) * 10.0);
color.rgb = lerp(color.rgb, float3(0, 0, 0), edgeDarkness);
return color;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
这个Shader包括了边缘检测、水墨效果和边缘加深三个特效。你可以在Shader中调整参数来达到不同的效果。
阅读全文
相关推荐

















