0

I have two lines created using the line renderer. They are the same length and position except for the Y values.

Line 1 positions: start: (constStartX, 0.005f, constStartZ) end: (constEndX, 0.001f, constEndZ)

Line 2 positions: start: (constStartX, 0.001f, constStartZ) end: (constEndX, 0.005f, constEndZ)

I want the result to be that when looking at the lines from a camera at the top we only see half of each line (the half that is higher than the other line.)

This works great with a perspective camera but not with an orthographic camera. It renderers one line completely over the other, even in the half where that line is lower in the 3D space.

Is there a way to get the desired behavior (the line with the greatest y value is rendered at each point) with an orthographic camera? Thanks!

1 Answer 1

0

I needed to add a custom material with a custom shader. The shader is the following:

Shader "Custom/LineDepth"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "Queue"="Geometry" "RenderType"="Opaque" }
        LOD 200

        ZWrite On
        ZTest LEqual

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 color : COLOR;
            };

            fixed4 _Color;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = v.color * _Color;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return i.color;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.