黑白变化很有用,可以用来做颜色融合。裁剪,扰动,变化等。
1.面片着色
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
// Time varying pixel color
float d = length(uv);
float c = d;
// Output to screen
fragColor = vec4(vec3(c),1.0);
}
边角的向量模长为 0.5^2 + 0.5^2 = 0.5
越靠近中心,模长越接近0,也就是黑色
效果如下:
shader完整代码:
Shader "Custom/quadCol4"
{
Properties
{
_Color("Main Color",Color) = (1,1,1,1)
}
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;
};
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float d = length(i.uv.xy - float2(0.5,0.5));
fixed4 col = fixed4(d,d,d,1);
return col;
}
ENDCG
}
}
}
越接近中点越黑,越接近边角越白。
效果:
没有渐变的:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
// Time varying pixel color
float d = length(uv - vec2(0.5,0.5));
float c = 0.0;
if(d < .3){
c = 1.0;
}
fragColor = vec4(vec3(c),1.0);
}
效果: