UnityShader 学习笔记 12 高级纹理之折射

本文介绍了一种使用Unity Shader实现高级折射效果的方法。通过定义折射颜色、折射量和折射比例等属性,结合立方体贴图,实现了物体表面的折射效果。在Shader中,通过计算世界坐标下的光线方向和法线方向,应用折射公式,获取折射后的光线方向,并从立方体贴图中采样得到折射纹理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Shader "_MyShader/7_Advanced/1_Refraction"

{

    Properties

    {

        _Color("Color", COLOR) = (1,1,1,1)

            _CubeMap("CubeMap", Cube) = "SkyBox" {}

            _RefractionColor("RefractionColor", COLOR) = (1,1,1,1)

            _RefractionAmount("RefractionAmount", Range(0,1.5)) = 1

            _RefractionRatio("RefractionRatio", Range(0,1.5)) = 1

    }

    SubShader

    {

        Tags { "RenderType"="ForwardBase" }



        Pass

        {

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "UnityCG.cginc"

            #include "Lighting.cginc"

            #include "AutoLight.cginc"



            struct a2v

            {

                float4 vertex : POSITION;

                float3 normal : NORMAL;

            };



            struct v2f

            {

                float4 pos : SV_POSITION;

                float4 worldPos : TEXCOORD1;

                float3 worldNormal : TEXCOORD2;

                float3 worldLight : TEXCOORD3;

                float3 worldView : TEXCOORD4;

                float3 worldRefraction : TEXCOORD5;



                SHADOW_COORDS(5)

            };



            fixed4 _Color;

            samplerCUBE _CubeMap;

            fixed4 _RefractionColor;

            float _RefractionAmount;

            float _RefractionRatio;



            v2f vert (a2v v)

            {

                v2f o;

                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);



                o.worldPos = mul(_Object2World,v.vertex);



                o.worldNormal = UnityObjectToWorldNormal(v.normal);

                o.worldLight = UnityWorldSpaceLightDir(o.worldPos);

                o.worldView = UnityWorldSpaceViewDir(o.worldPos);



                o.worldRefraction = refract(normalize(-o.worldView), normalize(o.worldNormal), _RefractionRatio);



                TRANSFER_SHADOW(o);



                return o;

            }



            fixed4 frag (v2f i) : SV_Target

            {

                fixed3 worldNormalDir = normalize(i.worldNormal);

                fixed3 worldLightDir = normalize(i.worldLight);

                fixed3 worldViewDir = normalize(i.worldView);



                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;



                fixed3 diffuseColor = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormalDir, worldLightDir));



                fixed3 refraction = texCUBE(_CubeMap, i.worldRefraction).rgb * _RefractionColor.rgb;



                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);



                fixed3 color = ambient + lerp(diffuseColor, refraction, _RefractionAmount) * atten;



                return fixed4(color, 1);

            }

            ENDCG

        }

    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值