Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline width is dependent on camera distance from object #33

Open
SpaceDogLaika opened this issue Nov 23, 2022 · 1 comment
Open

Outline width is dependent on camera distance from object #33

SpaceDogLaika opened this issue Nov 23, 2022 · 1 comment

Comments

@SpaceDogLaika
Copy link

The width/thickness of the outline changes as the camera gets closer/further away from the game object. How do you set a constant width?

@Kotevich
Copy link

Kotevich commented Sep 25, 2024

The width/thickness of the outline changes as the camera gets closer/further away from the game object. How do you set a constant width?

The with is constant, but it's appears larger than the object because no outline scaling occurs when you move your camera far from object. You can simply improve it

Add this to OutlineFill.shader code

Add new property Scale factor

  Properties {
    [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0

    _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
    _OutlineWidth("Outline Width", Range(0, 20)) = 5
    _ScaleFactor("Outline Scale Factor", Range(0, 1)) = 1
  }

Modify vert function

      uniform fixed4 _OutlineColor;
      uniform float _OutlineWidth;
      uniform float _ScaleFactor; 

      v2f vert(appdata input) {
        v2f output;

        UNITY_SETUP_INSTANCE_ID(input);
        UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

        float3 normal = any(input.smoothNormal) ? input.smoothNormal : input.normal;
        float3 viewPosition = UnityObjectToViewPos(input.vertex);
        float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));

        // Get distance to camera
        float distanceToCamera = length(viewPosition);

        // Calculate scale factor using camera distance
        float scaleFactor = 1.0 / distanceToCamera; 

        // 0 - standard behavior, 1 - full scaling
        float dynamicOutlineWidth = lerp(_OutlineWidth, _OutlineWidth * scaleFactor, _ScaleFactor);

       // Here we use our dynamic outline width
        output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * dynamicOutlineWidth / 1000.0);

        output.color = _OutlineColor;

        return output;
      }

It needs more calculation of course

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants