• Unity shader学习之折射


    shader如下:

    Shader "Custom/Refraction"
    {
        Properties
        {
            _Cubemap("Cubemap", Cube) = "" {}
            _RefractRatio("Refract Ratio", Range(0,1)) = 0.9
            _RefractionAmount("Reflection Amount", Range(0, 1)) = 0.5
        }
    
        SubShader
        {
            Pass
            {
                Tags
                {
                    "LightMode" = "ForwardBase"
                }
                Cull Off
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
    
                #include "UnityCG.cginc"
                #include "Lighting.cginc"
                #include "AutoLight.cginc"
    
                samplerCUBE _Cubemap;
                float _RefractRatio;
                float _RefractionAmount;
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    fixed4 color : COLOR;
                    float3 normal : NORMAl;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    fixed4 color : COLOR;
                    float3 worldNormal : TEXCOORD0;
                    float3 worldPos : TEXCOORD1;
                    SHADOW_COORDS(2)
                };
    
                v2f vert(appdata v)
                {
                    v2f o;
                    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.color = v.color;
                    o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal));
                    o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                    TRANSFER_SHADOW(o);
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_TARGET
                {
                    fixed3 albedo = i.color.rgb;
    
                    fixed3 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT.rgb;
    
                    float3 worldLight = UnityWorldSpaceLightDir(i.worldPos);
                    fixed3 diffuse = albedo * _LightColor0.rgb * max(0, dot(worldLight, i.worldNormal));
    
                    float3 worldView = UnityWorldSpaceViewDir(i.worldPos);
                    float3 refDir = refract(-normalize(worldView), normalize(i.worldNormal), _RefractRatio);
                    fixed3 refCol = texCUBE(_Cubemap, refDir);
    
                    UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
    
                    fixed3 col = ambient + lerp(diffuse, refCol, _RefractionAmount) * atten;
    
                    return fixed4(col, 1);
                }
    
                ENDCG
            }
        }
    
        Fallback "VertexLit"
    }

    效果如下:

  • 相关阅读:
    ActiveReports中如何在后台导出运行时绑定数据源报表
    ActiveReports中如何控制页面的记录数
    WordPress建站固定链接问题
    Linux环境下使用g++编译C++
    Git diff结果显示分析
    VTK使用过程中遇到的问题
    右值引用、移动语义和完美转发(下)
    右值引用、移动语义和完美转发(中)
    右值引用、移动语义和完美转发(上)
    new和malloc的联系与区别(下)
  • 原文地址:https://www.cnblogs.com/jietian331/p/7200277.html
Copyright © 2020-2023  润新知