0
\$\begingroup\$

I want to tweak the alpha value based on the distance to the camera. But I see no way of passing the vertex position on to the surface function using surface shaders. Not a problem with frag shader.

It's for fading out vegetation and creating LOD systems.

Shader "asdf" {
SubShader {
    CGPROGRAM
    #pragma surface surf StandardSpecular alphatest:_Cutoff addshadow vertex:vert
     
    struct v2f {
        float4 pos : TEXCOORD1;
    };

    struct Input {
        float2 uv_Maintex;
    };
        
    v2f vert (inout appdata_base v) {
        v2f o;
        o.pos = mul(unity_ObjectToWorld, v.vertex);
        return o;
    }

    void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
        //Need vertex or pixel distance here:
        //float dist = length(v2f.pos.xyz - _WorldSpaceCameraPos.xyz);
        //o.Alpha = saturate(50/dist);
    }
    ENDCG
}
}
\$\endgroup\$
1
\$\begingroup\$

Just ask for worldPos in your Input structure (as documented here):

struct Input {
    float2 uv_Maintex;
    float3 worldPos;
};

void surf (Input IN, inout SurfaceOutputStandardSpecular o) {        
    float dist = length(IN.worldPos - _WorldSpaceCameraPos.xyz);
    o.Alpha = saturate(50/dist);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.