SHOW:
|
|
- or go back to the newest paste.
1 | Shader "Unlit/Oil Painting" | |
2 | { | |
3 | Properties | |
4 | { | |
5 | _MainTex("Texture", 2D) = "white" {} | |
6 | _Radius ("Radius", Range(0, 10)) = 0 | |
7 | } | |
8 | SubShader | |
9 | { | |
10 | Blend SrcAlpha OneMinusSrcAlpha | |
11 | Pass | |
12 | { | |
13 | CGPROGRAM | |
14 | #pragma vertex vert | |
15 | #pragma fragment frag | |
16 | #pragma target 3.0 | |
17 | #include "UnityCG.cginc" | |
18 | ||
19 | struct v2f { | |
20 | float4 pos : SV_POSITION; | |
21 | half2 uv : TEXCOORD0; | |
22 | }; | |
23 | ||
24 | sampler2D _MainTex; | |
25 | float4 _MainTex_ST; | |
26 | ||
27 | v2f vert(appdata_base v) { | |
28 | v2f o; | |
29 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
30 | o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); | |
31 | return o; | |
32 | } | |
33 | ||
34 | int _Radius; | |
35 | float4 _MainTex_TexelSize; | |
36 | ||
37 | float4 frag (v2f i) : SV_Target | |
38 | { | |
39 | half2 uv = i.uv; | |
40 | ||
41 | float3 mean[4] = { | |
42 | {0, 0, 0}, | |
43 | {0, 0, 0}, | |
44 | {0, 0, 0}, | |
45 | {0, 0, 0} | |
46 | }; | |
47 | ||
48 | float3 sigma[4] = { | |
49 | {0, 0, 0}, | |
50 | {0, 0, 0}, | |
51 | {0, 0, 0}, | |
52 | {0, 0, 0} | |
53 | }; | |
54 | ||
55 | float2 start[4] = {{-_Radius, -_Radius}, {-_Radius, 0}, {0, -_Radius}, {0, 0}}; | |
56 | ||
57 | float2 pos; | |
58 | float3 col; | |
59 | for (int k = 0; k < 4; k++) { | |
60 | for(int i = 0; i <= _Radius; i++) { | |
61 | for(int j = 0; j <= _Radius; j++) { | |
62 | pos = float2(i, j) + start[k]; | |
63 | col = tex2Dlod(_MainTex, float4(uv + float2(pos.x * _MainTex_TexelSize.x, pos.y * _MainTex_TexelSize.y), 0., 0.)).rgb; | |
64 | mean[k] += col; | |
65 | sigma[k] += col * col; | |
66 | } | |
67 | } | |
68 | } | |
69 | ||
70 | float sigma2; | |
71 | ||
72 | float n = pow(_Radius + 1, 2); | |
73 | float4 color = tex2D(_MainTex, uv); | |
74 | float min = 1; | |
75 | ||
76 | for (int l = 0; l < 4; l++) { | |
77 | mean[l] /= n; | |
78 | sigma[l] = abs(sigma[l] / n - mean[l] * mean[l]); | |
79 | sigma2 = sigma[l].r + sigma[l].g + sigma[l].b; | |
80 | ||
81 | if (sigma2 < min) { | |
82 | min = sigma2; | |
83 | color.rgb = mean[l].rgb; | |
84 | } | |
85 | } | |
86 | return color; | |
87 | } | |
88 | ENDCG | |
89 | } | |
90 | } | |
91 | } |