1.
float remap2(float a,float b,float t)
{
return (t - a) / (b - a);
}
float remap(float a,float b,float c,float d,float t)
{
return (t - a) / (b - a) * (d - c) + c;
}
vec4 Eye(vec2 uv)
{
vec4 col = vec4(0.0);
return col;
}
vec4 Mouth(vec2 uv)
{
vec4 col = vec4(0.0);
return col;
}
vec4 Head(vec2 uv)
{
vec4 col = vec4(0.9,0.65,0.1,1.0);
float d = length(uv);
//uv坐标到中点距离,小于0.49的透明为0,大于0.5的为1,中间差值过度
col.a = smoothstep(0.5,0.49,d);
float edge = remap2(0.35,0.5,d);
col.rgb *= edge;
return col;
}
vec4 Smile(vec2 uv)
{
vec4 col = vec4(0.0);
vec4 head = Head(uv);
col = mix(col,head,head.a);
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
//将uv的x坐标进行放大
uv.x *= iResolution.x / iResolution.y;
vec4 col = Smile(uv);
fragColor = col;
}
2
float remap2(float a,float b,float t)
{
return clamp((t - a) / (b - a),0.0,1.0);
}
float remap(float a,float b,float c,float d,float t)
{
return (t - a) / (b - a) * (d - c) + c;
}
vec2 withIn(vec2 uv,vec4 rect)
{
return (uv - rect.xy) / (rect.zw - rect.xy);
}
vec4 Eye(vec2 uv)
{
uv -= 0.5;
vec4 col = vec4(1.0);
float d = length(uv);
col.a = smoothstep(0.5,0.4,d);
return col;
}
vec4 Mouth(vec2 uv)
{
vec4 col = vec4(0.0);
return col;
}
vec4 Head(vec2 uv)
{
vec4 col = vec4(0.9,0.65,0.1,1.0);
float d = length(uv);
//uv坐标到中点距离,小于0.49的透明为0,大于0.5的为1,中间差值过度
col.a = smoothstep(0.5,0.49,d);
float edge = remap2(0.35,0.5,d);
edge *= edge;
col.rgb *= 1.0 - edge * 0.5;
col.rgb = mix(col.rgb,vec3(0.6,0.3,0.1),smoothstep(0.47,0.48,d));
float highLight = smoothstep(0.41,0.405,d) * 0.75;
highLight *= remap(0.41,-0.1,0.75,0.0,uv.y);
col.rgb = mix(col.rgb,vec3(1.0),highLight);
d = length(uv - vec2(0.25,-0.2));
float cheek = smoothstep(0.2,0.01,d) * 0.4;
cheek *= smoothstep(0.17,0.16,d);
col.rgb = mix(col.rgb,vec3(1.0,0.1,0.1),cheek);
return col;
}
vec4 Smile(vec2 uv)
{
vec4 col = vec4(0.0);
uv.x = abs(uv.x);
vec4 head = Head(uv);
vec4 eye = Eye(withIn(uv,vec4(0.03,-0.1,0.37,0.25)));
col = mix(col,head,head.a);
col = mix(col,eye,eye.a);
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
//将uv的x坐标进行放大
uv.x *= iResolution.x / iResolution.y;
vec4 col = Smile(uv);
fragColor = col;
}
3
float remap2(float a,float b,float t)
{
return clamp((t - a) / (b - a),0.0,1.0);
}
float remap(float a,float b,float c,float d,float t)
{
return (t - a) / (b - a) * (d - c) + c;
}
vec2 withIn(vec2 uv,vec4 rect)
{
return (uv - rect.xy) / (rect.zw - rect.xy);
}
vec4 Eye(vec2 uv)
{
uv -= 0.5;
float d = length(uv);
vec4 irisCol = vec4(0.3,0.5,1.0,1.0);
vec4 col = mix(vec4(1.0),irisCol,smoothstep(0.1,0.7,d));
col.rgb = mix(col.rgb,vec3(0.0),smoothstep(0.3,0.28,d));
col.a = smoothstep(0.5,0.48,d);
return col;
}
vec4 Mouth(vec2 uv)
{
vec4 col = vec4(0.0);
return col;
}
vec4 Head(vec2 uv)
{
vec4 col = vec4(0.9,0.65,0.1,1.0);
float d = length(uv);
//uv坐标到中点距离,小于0.49的透明为0,大于0.5的为1,中间差值过度
col.a = smoothstep(0.5,0.49,d);
float edge = remap2(0.35,0.5,d);
edge *= edge;
col.rgb *= 1.0 - edge * 0.5;
col.rgb = mix(col.rgb,vec3(0.6,0.3,0.1),smoothstep(0.47,0.48,d));
float highLight = smoothstep(0.41,0.405,d) * 0.75;
highLight *= remap(0.41,-0.1,0.75,0.0,uv.y);
col.rgb = mix(col.rgb,vec3(1.0),highLight);
d = length(uv - vec2(0.25,-0.2));
float cheek = smoothstep(0.2,0.01,d) * 0.4;
cheek *= smoothstep(0.17,0.16,d);
col.rgb = mix(col.rgb,vec3(1.0,0.1,0.1),cheek);
return col;
}
vec4 Smile(vec2 uv)
{
vec4 col = vec4(0.0);
uv.x = abs(uv.x);
vec4 head = Head(uv);
vec4 eye = Eye(withIn(uv,vec4(0.03,-0.1,0.37,0.25)));
col = mix(col,head,head.a);
col = mix(col,eye,eye.a);
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
//将uv的x坐标进行放大
uv.x *= iResolution.x / iResolution.y;
vec4 col = Smile(uv);
fragColor = col;
}
4
float remap2(float a,float b,float t)
{
return clamp((t - a) / (b - a),0.0,1.0);
}
float remap(float a,float b,float c,float d,float t)
{
return (t - a) / (b - a) * (d - c) + c;
}
vec2 withIn(vec2 uv,vec4 rect)
{
return (uv - rect.xy) / (rect.zw - rect.xy);
}
vec4 Eye(vec2 uv)
{
uv -= 0.5;
float d = length(uv);
vec4 irisCol = vec4(0.3,0.5,1.0,1.0);
vec4 col = mix(vec4(1.0),irisCol,smoothstep(0.1,0.7,d) * 0.5);
//乘法是且
col.rgb *= 1.0 - smoothstep(0.45,0.5,d) * 0.5 * clamp(-uv.y - uv.x,0.0,1.0);
col.rgb = mix(col.rgb,vec3(0.0),smoothstep(0.3,0.28,d));
irisCol.rgb *= 1.0 + smoothstep(0.3,0.05,d);
col.rgb = mix(col.rgb,irisCol.rgb,smoothstep(0.28,0.25,d));
col.rgb = mix(col.rgb,vec3(0.0),smoothstep(0.16,0.14,d));
float highLight = smoothstep(0.1,0.09,length(uv - vec2(-0.15,0.15)));
//加法是或
highLight += smoothstep(0.07,0.05,length(uv + vec2(-0.08,0.08)));
col.rgb = mix(col.rgb,vec3(1.0),highLight);
col.a = smoothstep(0.5,0.48,d);
return col;
}
vec4 Mouth(vec2 uv)
{
uv -= 0.5;
vec4 col = vec4(0.5,0.18,0.05,1);
uv.y *= 1.5;
uv.y -= uv.x * uv.x * 2.0;
float d = length(uv);
col.a = smoothstep(0.5,0.48,d);
float td = length(uv - vec2(0.0,0.6));
vec3 toothCol = vec3(1.0) * smoothstep(0.6,0.35,d);
col.rgb = mix(col.rgb,toothCol,smoothstep(0.4,0.37,d));
td = length(uv + vec2(0.0,0.5));
col.rgb = mix(col.rgb,vec3(1.0,0.5,0.5),smoothstep(0.5,0.2,td));
return col;
}
vec4 Head(vec2 uv)
{
vec4 col = vec4(0.9,0.65,0.1,1.0);
float d = length(uv);
//uv坐标到中点距离,小于0.49的透明为0,大于0.5的为1,中间差值过度
col.a = smoothstep(0.5,0.49,d);
float edge = remap2(0.35,0.5,d);
edge *= edge;
col.rgb *= 1.0 - edge * 0.5;
col.rgb = mix(col.rgb,vec3(0.6,0.3,0.1),smoothstep(0.47,0.48,d));
float highLight = smoothstep(0.41,0.405,d) * 0.75;
highLight *= remap(0.41,-0.1,0.75,0.0,uv.y);
col.rgb = mix(col.rgb,vec3(1.0),highLight);
d = length(uv - vec2(0.25,-0.2));
float cheek = smoothstep(0.2,0.01,d) * 0.4;
cheek *= smoothstep(0.17,0.16,d);
col.rgb = mix(col.rgb,vec3(1.0,0.1,0.1),cheek);
return col;
}
vec4 Smile(vec2 uv)
{
vec4 col = vec4(0.0);
uv.x = abs(uv.x);
vec4 head = Head(uv);
vec4 eye = Eye(withIn(uv,vec4(0.03,-0.1,0.37,0.25)));
vec4 mouth = Mouth(withIn(uv,vec4(-0.3,-0.4,0.3,-0.1)));
col = mix(col,head,head.a);
col = mix(col,eye,eye.a);
col = mix(col,mouth,mouth.a);
return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv -= 0.5;
//将uv的x坐标进行放大
uv.x *= iResolution.x / iResolution.y;
vec4 col = Smile(uv);
fragColor = col;
}
完整shader
Shader "Custom/Smile"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
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 remap2(float a,float b,float t)
{
return clamp((t - a) / (b - a),0.0,1.0);
}
float remap(float a,float b,float c,float d,float t)
{
return (t - a) / (b - a) * (d - c) + c;
}
float2 withIn(float2 uv,float4 rect)
{
return (uv - rect.xy) / (rect.zw - rect.xy);
}
float4 Eye(float2 uv)
{
uv -= 0.5;
float d = length(uv);
float4 irisCol = float4(0.3,0.5,1.0,1.0);
float4 col = lerp(float4(1.0,1.0,1.0,1.0),irisCol,smoothstep(0.1,0.7,d) * 0.5);
//乘法是且
col.rgb *= 1.0 - smoothstep(0.45,0.5,d) * 0.5 * clamp(-uv.y - uv.x,0.0,1.0);
col.rgb = lerp(col.rgb,float3(0.0,0.0,0.0),smoothstep(0.3,0.28,d));
irisCol.rgb *= 1.0 + smoothstep(0.3,0.05,d);
col.rgb = lerp(col.rgb,irisCol.rgb,smoothstep(0.28,0.25,d));
col.rgb = lerp(col.rgb,float3(0.0,0.0,0.0),smoothstep(0.16,0.14,d));
float highLight = smoothstep(0.1,0.09,length(uv - float2(-0.15,0.15)));
//加法是或
highLight += smoothstep(0.07,0.05,length(uv + float2(-0.08,0.08)));
col.rgb = lerp(col.rgb,float3(1.0,1.0,1.0),highLight);
col.a = smoothstep(0.5,0.48,d);
return col;
}
float4 Mouth(float2 uv)
{
uv -= 0.5;
float4 col = float4(0.5,0.18,0.05,1);
uv.y *= 1.5;
uv.y -= uv.x * uv.x * 2.0;
float d = length(uv);
col.a = smoothstep(0.5,0.48,d);
float td = length(uv - float2(0.0,0.6));
float3 toothCol = float3(1.0,1.0,1.0) * smoothstep(0.6,0.35,d);
col.rgb = lerp(col.rgb,toothCol,smoothstep(0.4,0.37,d));
td = length(uv + float2(0.0,0.5));
col.rgb = lerp(col.rgb,float3(1.0,0.5,0.5),smoothstep(0.5,0.2,td));
return col;
}
float4 Head(float2 uv)
{
float4 col = float4(0.9,0.65,0.1,1.0);
float d = length(uv);
//uv坐标到中点距离,小于0.49的透明为0,大于0.5的为1,中间差值过度
col.a = smoothstep(0.5,0.49,d);
float edge = remap2(0.35,0.5,d);
edge *= edge;
col.rgb *= 1.0 - edge * 0.5;
col.rgb = lerp(col.rgb,float3(0.6,0.3,0.1),smoothstep(0.47,0.48,d));
float highLight = smoothstep(0.41,0.405,d) * 0.75;
highLight *= remap(0.41,-0.1,0.75,0.0,uv.y);
col.rgb = lerp(col.rgb,float3(1.0,1.0,1.0),highLight);
d = length(uv - float2(0.25,-0.2));
float cheek = smoothstep(0.2,0.01,d) * 0.4;
cheek *= smoothstep(0.17,0.16,d);
col.rgb = lerp(col.rgb,float3(1.0,0.1,0.1),cheek);
return col;
}
float4 Smile(float2 uv)
{
float4 col = float4(0.0,0.0,0.0,0.0);
uv.x = abs(uv.x);
float4 head = Head(uv);
float4 eye = Eye(withIn(uv,float4(0.03,-0.1,0.37,0.25)));
float4 mouth = Mouth(withIn(uv,float4(-0.3,-0.4,0.3,-0.1)));
col = lerp(col,head,head.a);
col = lerp(col,eye,eye.a);
col = lerp(col,mouth,mouth.a);
return col;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 texCol = tex2D(_MainTex,i.uv);
i.uv -= 0.5;
float4 c = Smile(i.uv);
//这个是不透明的,alpha 0和1都无所谓,反正不生效
fixed4 col = fixed4(c.x,c.y,c.z,1);
float d = length(fixed3(c.x,c.y,c.z) - fixed3(0.0,0.0,0.0));
if(d < 1e-6){
return texCol;
}
return col;
}
ENDCG
}
}
}
效果: