冯乐乐的《Unity Shader入门精要》附带项目,其中部分shader已经修改测试
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

77 lines
2.0 KiB

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/TestReflectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_CubeMapRef("Reflect Cube",Cube) = "_Skybox"{}
}
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;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD1;
fixed3 worldNormal : TEXCOORD2;
fixed3 worldViewDir : TEXCOORD3;
fixed3 worldRefl : TEXCOORD4;
};
sampler2D _MainTex;
float4 _MainTex_ST;
samplerCUBE _CubeMapRef;
v2f vert (appdata v)
{
v2f o;
//从模型空间转换到裁剪空间
o.vertex = UnityObjectToClipPos(v.vertex);
//转换UV坐标
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//把法线方向从模型空间转换到世界空间中
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//o.worldNormal = UnityObjectToWorldDir(v.normal);
//o.worldNormal = normalize(mul(unity_ObjectToWorld,v.normal));
//顶点/方向矢量从模型空间变换到世界空间
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
//世界空间中的顶点位置->世界空间中从该点到摄像机的观察方向。
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
//反射运算
o.worldRefl = reflect(-o.worldViewDir,o.worldNormal);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 reflecion = texCUBE(_CubeMapRef,i.worldRefl).rgb;
half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.worldRefl);
// decode cubemap data into actual color
half3 skyColor = DecodeHDR (skyData, unity_SpecCube0_HDR);
return fixed4(skyColor,1);
}
ENDCG
}
}
}