본문 바로가기

유니티/쉐이더

[Unity Shader] 가까워질수록 보이는 쉐이더, 카메라 위치 받아오기

Properties
{
    _DisPower("Distance Power", float) = 0
}

SubShader
{
    Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert alpha:fade
    #pragma target 3.0

    float _DisPower;

    struct Input
    {
        float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
        fixed4 c = fixed4(1, 1, 1, 1);
        float d = distance(IN.worldPos, _WorldSpaceCameraPos) / _DisPower;
        d = 1 - saturate(d);

        o.Albedo = c.rgb;
        o.Alpha = d;
    }
    ENDCG
}
FallBack "Diffuse"

 

쉐이더에 내장된 변수(_WorldSpaceCameraPos)를 이용해 카메라 위치를 받아올 수 있다.

 

더 많은 빌트인 셰이더 변수

https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html