Unity中Shader的反射环境,本质就是把环境烘焙成一张Cubemap。
继续使用上一篇文章的代码测试:
我们修改一下,把Cubemap采样时,使用的 世界空间下的模型法线替换成法线贴图转化后的世界空间下的法线
可以看到物体反射的基础效果:
反射探针中当前激活的CubeMap存储在unity_SpecCube0当中,必须要用UNITY_SAMPLE_TEXCUBE进行采样,然后需要对其进行解码
half3 worldView = normalize (UnityWorldSpaceViewDir (i.worldPos));
计算需要提前准备 世界空间下的模型顶点
half3 R = reflect (-worldView, N);
计算需要提前准备 世界空间下的模型法线
half4 cubemap = UNITY_SAMPLE_TEXCUBE (unity_SpecCube0, R);
half3 skyColor = DecodeHDR (cubemap, unity_SpecCube0_HDR);
最终代码:
//纹理的多级渐远 Mipmap
//纹理的环绕方式
//法线贴图
//反射环境
Shader "MyShader/P2_1_9"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[KeywordEnum (Repeat,Clamp)]_WrapMode("WrapMode",int) = 0
[IntRange]_Mipmap ("Mipmap",Range(0,10)) = 0
//法线贴图
[Normal]_NormalTex("NormalTex",2D) = "bump" {}
//在属性面板定义立方体纹理
_CubeMap("CubeMap",Cube) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _WRAPMODE_REPEAT _WRAPMODE_CLAMP
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 localPos : TEXCOORD1;
float3 worldPos : TEXCOORD2;
half3 worldNormal : TEXCOORD3;
float3 tSpace0:TEXCOORD4;
float3 tSpace1:TEXCOORD5;
float3 tSpace2:TEXCOORD6;
};
sampler2D _MainTex;
float4 _MainTex_ST;
half _Mipmap;
samplerCUBE _CubeMap;
sampler2D _NormalTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.localPos = v.vertex.xyz;
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
half3 worldTangent = UnityObjectToWorldDir(v.tangent);
//v.tangent.w:DCC软件中顶点UV值中的V值翻转情况.
//unity_WorldTransformParams.w:模型缩放是否有奇数负值.
half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
half3 worldBinormal = cross(o.worldNormal, worldTangent) * tangentSign;
o.tSpace0 = float3(worldTangent.x,worldBinormal.x,o.worldNormal.x);
o.tSpace1 = float3(worldTangent.y,worldBinormal.y,o.worldNormal.y);
o.tSpace2 = float3(worldTangent.z,worldBinormal.z,o.worldNormal.z);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//WrapMode
#if _WRAPMODE_REPEAT
i.uv = frac(i.uv);
#elif _WRAPMODE_CLAMP
//法一:
//i.uv = clamp(i.uv,0,1);
//法二:
i.uv = saturate(i.uv);
#endif
float4 uvMipmap = fixed4(i.uv,0,_Mipmap);
fixed4 col = tex2Dlod(_MainTex, uvMipmap);
//法线纹理
fixed3 normalTex = UnpackNormal(tex2D(_NormalTex,i.uv));
//max(0,dot(N,L))
fixed3 N1 = normalize(normalTex);
fixed3 L = _WorldSpaceLightPos0.xyz;
//return fixed4(normalTex,1);
//计算出世界空间下的法线
half3 worldNormal = half3(dot(i.tSpace0,normalTex),dot(i.tSpace1,normalTex),dot(i.tSpace2,normalTex));
//return max(0,dot(worldNormal,L));
//CubeMap
//fixed4 cubemap = texCUBE(_CubeMap,i.localPos);
//V,N,R
fixed3 V = normalize(i.worldPos - _WorldSpaceCameraPos);
fixed3 N = normalize(worldNormal);
fixed3 R = reflect(V,N);
fixed4 cubemap = texCUBE(_CubeMap,R);
//return cubemap;
//反射探针中当前激活的CubeMap存储在unity_SpecCube0当中,必须要用UNITY_SAMPLE_TEXCUBE进行采样,然后需要对其进行解码
//half3 worldView = normalize (UnityWorldSpaceViewDir (i.worldPos));
//half3 R = reflect (-worldView, N);
cubemap = UNITY_SAMPLE_TEXCUBE (unity_SpecCube0, R);
half3 skyColor = DecodeHDR (cubemap, unity_SpecCube0_HDR);
return fixed4(skyColor,1);
return col;
}
ENDCG
}
}
}