UnityShader学习13 关于cubemap (类似镜子一样的反射)

做法:利用摄像机指向顶点方向的视向量的反射向量与围在立方次周围的cubemap相交的点就是我们要取的颜色值。在shader里,我们只需要使用texCUBE()函数即可完成。

Shader "ShaderLearn/my_test_08" {
    Properties{
        _Cube("Cubemap", cube) = ""{}
    }

    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_full v){
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                float3 V = WorldSpaceViewDir(v.vertex);
                float3 N = mul(v.normal, unity_WorldToObject);
                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" }

 

posted on 2019-06-21 12:50  炼金师  阅读(271)  评论(0)    收藏  举报

导航