0
\$\begingroup\$

It seems like the shader I'm using make it only outline but not completed lines I mean the outline seems a bit broken in some places and not like in the aircraft one complete outline.

For only Outline I'm using this shader:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "OutlinedOnly/Silhouette Only" {
    Properties {
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Outline ("Outline width", Range (0.0, 0.1)) = .005
    }

CGINCLUDE
#include "UnityCG.cginc"

struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
};

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

uniform float _Outline;
uniform float4 _OutlineColor;

v2f vert(appdata v) {
    // just make a copy of incoming vertex data but scaled according to normal direction
    v2f o;
    o.pos = UnityObjectToClipPos(v.vertex);

    float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    float2 offset = TransformViewToProjection(norm.xy);

    o.pos.xy += offset * o.pos.z * _Outline;
    o.color = _OutlineColor;
    return o;
}
ENDCG

    SubShader {
        Tags { "Queue" = "Transparent" }

        Pass {
            Name "BASE"
            Cull Back
            Blend Zero One

            // uncomment this to hide inner details:
            //Offset -8, -8

            SetTexture [_OutlineColor] {
                ConstantColor (0,0,0,0)
                Combine constant
            }
        }

        // note that a vertex shader is specified here but its using the one above
        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" }
            Cull Front

            // you can choose what kind of blending mode you want for the outline
            //Blend SrcAlpha OneMinusSrcAlpha // Normal
            //Blend One One // Additive
            Blend One OneMinusDstColor // Soft Additive
            //Blend DstColor Zero // Multiplicative
            //Blend DstColor SrcColor // 2x Multiplicative

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

half4 frag(v2f i) :COLOR {
    return i.color;
}
ENDCG
        }


    }

    Fallback "Diffuse"
}

The result is the left robot with the black outline and the cyliner a bit in the front far also in black. You can see the outline is not so completed in some places.

On the aircraft in red the outline is all around the aircraft.

Outline not complete

For the aircraft I'm using some other shader:

Shader "MyOutline/OutlineRender" {
    Properties{
        _MainTex("Base (RGB)", 2D) = "white" {}
    }

        CGINCLUDE
        uniform sampler2D _MainTex;
    uniform float4 _MainTex_TexelSize;
    uniform int _Thickness;
    uniform float _Blending;
    uniform sampler2D _OutlineTextureMask;

    struct v2f {
        float4 pos : POSITION;
        float2 uv : TEXCOORD0;
    };

    fixed IsMask(float2 coord) {
        return tex2D(_OutlineTextureMask, coord).a;
    }
    ENDCG

        SubShader{
        Pass{
        Name "BASE"
        Tags{ "LightMode" = "Always" }

        ZTest Always Cull Off ZWrite Off Fog{ Mode off }

        CGPROGRAM
#pragma target 2.5
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag

        v2f vert(appdata_img v) {
        v2f o;
        o.pos = UnityObjectToClipPos(v.vertex);
        o.uv = v.texcoord.xy;
        return o;
    }

    fixed4 frag(v2f i) : SV_Target{

        v2f maskcoord = i;
    // flip the y if a render texture is being used
    if (_MainTex_TexelSize.y < 0)
    {
        maskcoord.uv.y = 1 - maskcoord.uv.y;
    }

    fixed4 main = tex2D(_MainTex, i.uv);


    //Debug... see mask
    //return tex2D(_OutlineTextureMask, i.uv);

    //Debug... see mask alpha
    //fixed4 mask = tex2D(_OutlineTextureMask, i.uv);
    //return fixed4(mask.a, mask.a, mask.a, 1.0);

    fixed4 mask = main;// tex2D(_OutlineTextureMask, i.uv);

    float dist = 5000.0;

    float2 xx = float2(_MainTex_TexelSize.x, 0);
    float2 yy = float2(0, _MainTex_TexelSize.y);

    int byass = _Thickness + 1.0;

    float maxd = 2 * byass*byass;
    float alpha = 1.0;

    //[unroll(120)]

    for (uint p = 0; p < 144; p++) {
        int k = (p / 12) - 6;
        int j = (p % 12) - 6;
        float val = IsMask(maskcoord.uv + (xx * k) + (yy*j));

        if (IsMask(maskcoord.uv + (xx * k) + (yy*j)) == 1.0) {
            float dd = (k*k) + (j*j);
            if (dd < dist) {
                alpha = max(0, ((maxd - dd) / maxd));
                alpha = alpha*alpha*_Blending;
                mask = tex2D(_OutlineTextureMask, maskcoord.uv + (xx * k) + (yy*j));
                mask = main*(1 - (alpha*0.7)) + mask * alpha;
            }
            dist = min(dd, dist);
        }
    }


    if (IsMask(maskcoord.uv) == 1.0) {
        return main;
    }

    return mask;
    return fixed4(main + mask);

    }
        ENDCG
    }
    }

        Fallback off
}

The shader in black for the robot and cylinder are working but not so good since the outline is not 100% complete around the objects.

And the shader I'm using for the aircraft is 100% complete line around the object but I'm not sure how to use or make with a check box or something that it will be only the outline like the shader in black but with the red shader.

The idea is to make only outline effect like the first shader but with the second shader.

Maybe somehow to implement part of the first shader with the second shader to make it only outline but like the red one ?

\$\endgroup\$
  • \$\begingroup\$ Found how to do it :) \$\endgroup\$ – jdoedoe doedoe Feb 1 '18 at 1:27
  • 2
    \$\begingroup\$ Then you should post (and accept) the answer to help others who may have the same or similar questions. \$\endgroup\$ – Pikalek Feb 1 '18 at 3:43
  • \$\begingroup\$ I will post the answer soon. \$\endgroup\$ – jdoedoe doedoe Feb 1 '18 at 8:33

Your Answer

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

Browse other questions tagged or ask your own question.