Shader "Sbin/FragColor"
{
Properties
{
_MainColor("MainColor",color)=(1,1,1,1)
_SecondColor("SecondColor",color)=(1,1,1,1)
_Center("Center",Range(-0.51,0.51))=0
_R("R",Range(0,0.5))=0.2
}
SubShader
{
//透明处理
tags{"queue"="transparent"}
pass
{
CGPROGRAM
#pragma vertex Vert
#pragma fragment frag
#include "unitycg.cginc"
float4 _MainColor;
float4 _SecondColor;
float _R;
struct v2f
{
float4 pos:POSITION;
float4 y:TEXCOORD0;
}
v2f vert(appdata_base v)
{
v2f o;
o.pos=mul(UNITY_MATRIX_MVP,v.Vertex);
o.y=v.Vertex.y;
return o;
}
fixed frag(v2f IN):COLOR
{
float d=IN.y-_Center;
d=d/abs(d);
float f=saturate(d/_R);
d*=f;
d=d/2+0.5;//范围在0到1之间
return lerp(_MainColor,_SecondColor,d);//差值运算
}
ENDCG
}
}
}
挂在汽车上转起来
using UnityEngine;
using System.Collections;
using System.Linq;
public class CheckVertex : MonoBehaviour
{
public MeshFilter mf1;
public MeshFilter mf2;
public MeshFilter mf3;
// Use this for initialization
void Start ()
{
Vector3[] verts = mf1.mesh.vertices;
float max = verts.Max(v => v.x);
float min = verts.Min(v => v.x);
Debug.Log(max + " " + min);
}
// Update is called once per frame
void Update ()
{
transform.Rotate(Vector3.up, Time.deltaTime * 10);
}
}
Shader "Sbin/CarPaintSurface"
{
Properties
{
_MainColor("MainColor",color)=(1,1,1,1)
_SecondColor("SecondColor",color)=(1,1,1,1)
_Center("Center",Range(-0.51,0.51))=0
_R("R",Range(0,0.5))=0.2
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
//制定使用的表面着色器,表面着色器的函数surf,基于物理模型的光照模型standard,
#pragma surface surf Standard vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
float4 _MainColor;
float4 _SecondColor;
float _R;
half _Glossiness;
half _Metallic;
struct Input
{
float2 uv_MainTex;
float x;
};
void vert(inout appdata_full v,out Input o)
{
o.uv_MainTex=v.TEXCOORD.xy;
o.x=v.Vertex.x;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;//漫反射颜色赋值
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;//金属化的级别
o.Smoothness = _Glossiness;//平滑度
o.Alpha = c.a;
float d=IN.x-_Center;
d=d/abs(d);
float f=saturate(d/_R);
d*=f;
d=d/2+0.5;//范围在0到1之间
o.Albedo*= lerp(_MainColor,_SecondColor,d)*2;//差值运算
}
ENDCG
}
FallBack "Diffuse"
}