变成立方体贴图
Shader "Sbin/CubeMap" {
Properties
{
_Cube ("CubeMap",cube) = "white" {}
}
SubShader
{
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
samplerCUBE _Cube;
struct v2f
{
float4 pos:POSITION;
float3 R:TEXCOORD0;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos=mul(UNITY_MATRIX_MVP,v.Vertex);
//视向量在世界空间中
float3 V=-WorldSpaceViewDir(v.Vertex);
float3 N=mul(v.normal,(float3X3)_World2Object);
N=normalize(N);
o.R=reflect(V,N);//反射光线
return o;
}
fixed4 frag(v2f IN):COLOR
{
fixed4 color=texCUBE(_Cube,IN.R);
return color;
}
ENDCG
}
}
FallBack "Diffuse"
}
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
_Cube("Cubemap",cube)=""{}
}
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
samplerCUBE _Cube;
float4 _MainColor;
float4 _SecondColor;
float _R;
half _Glossiness;
half _Metallic;
struct Input
{
//float2 uv_MainTex;
float x;
float3 worldRefl;
};
void vert(inout appdata_full v,out Input o)
{
//o.uv_MainTex=v.TEXCOORD.xy;
//视向量
float3 V=-WorldSpaceViewDir(v.Vertex);
float3 N=mul(v.normal,(float3X3)_World2Object);
N=normalize(N);
o.worldRefl=reflect(V,N);//反射光线
o.x=v.Vertex.x;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = texCUBE (_Cube, IN.worldRefl);
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"
}