• Unity中Shader反射环境



    前言

    Unity中Shader的反射环境,本质就是把环境烘焙成一张Cubemap。

    继续使用上一篇文章的代码测试:

    我们修改一下,把Cubemap采样时,使用的 世界空间下的模型法线替换成法线贴图转化后的世界空间下的法线

    在这里插入图片描述

    可以看到物体反射的基础效果:

    请添加图片描述


    一、制作反射环境的两种办法

    法1:属性面板接收一个 Cubemap 作为反射环境

    法2:把环境烘焙成一张Cubemap


    二、在Unity中实现把环境烘焙成一张Cubemap

    1、先创建一个反射探针(可以直接创建,也可以空物体增加组件)

    在这里插入图片描述

    在这里插入图片描述

    反射探针中当前激活的CubeMap存储在unity_SpecCube0当中,必须要用UNITY_SAMPLE_TEXCUBE进行采样,然后需要对其进行解码

    2、计算得到模型顶点指向摄像头的 视线向量

    half3 worldView = normalize (UnityWorldSpaceViewDir (i.worldPos));

    计算需要提前准备 世界空间下的模型顶点

    3、计算视线向量的反射向量

    half3 R = reflect (-worldView, N);
    计算需要提前准备 世界空间下的模型法线

    4、对反射探针的Cubemap进行纹理采样

    half4 cubemap = UNITY_SAMPLE_TEXCUBE (unity_SpecCube0, R);

    5、对采样后的结果进行解码处理

    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
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
  • 相关阅读:
    SQL Server教程 - T-SQL-编程基础
    路漫漫远修兮-GeoServer2.16.0版本跨域解决
    【JAVA】抽象类和接口类
    基于Springboot的特产销售平台设计与实现毕业设计源码091036
    【Python-闭包】
    openwrt上/etc/localtime报错问题解决
    访问nginx报错502日志:failed (13: Permission denied) while connecting to upstream
    OpenXR Reference Space浅析
    二手车之家业务缓存应用实战
    传统 Web 框架部署与迁移
  • 原文地址:https://blog.csdn.net/qq_51603875/article/details/134527308