ToonShader offset and Camera fov

Hey guys,
I’m aplying a camera zoom (making fov smaller) to my game, but the toonshader offset, responsible for drawing the outline, gets very thick related to the the rest of objects in scene.

The offset isn’t related with the fov, it’s always the same. So when it gets a closer look, obviously it seems thicker.

So, I was wondering if someone had a problem like that and/or have a tip/idea or workaround to solve this problem.

I don’t know the toon shader very well, if you can access the outline variable in a script you can tie it to the distance from the camera object.

If the camera only follows one object, give it its own material or this will affect all objects with the same material.

the script could look something like

var camera : Camera;
var toonShader : ToonShader;

function Update(){
    toonShader.lineWidthOrWhateverTheVariableNameIs = 
        (camera.transform.position - toonShader.transform.position).magnitude * scaler;

Hey Matthew,

I solved the problem using Shader.SetGlobalFloat("_CAMERA_FOV", Camera.fov);
with that I could access the fov in shader and fix the offset value.
basically I’m using this:

uniform float _CAMERA_FOV = 60.0f;
float2 offset = TransformViewToProjection(norm.xy) * _CAMERA_FOV/60.0f;

but it’s probably too primitive, I’ll sophisticate it a little bit later.

Hey Matthew, thanks for the reply. That’s a good idea, it has a property called outLineWidth. I’ll try that, but first I’m thinking about a solution within the shader code. I have many cameras, and I think that getting the fov from camera in shader would be more perfomatic

Heres an example of the problem[1947-toonshader.png|1947]

Hey wr_uk,

you have to paste that code in the shader code.

the Shader.SetGlobalFloat(“_CAMERA_FOV”, Camera.fov); code you put where you’re changing the camera.fov

Can anybody help?

by “where you’re changing the camera.fov” I meant in the camera class, probably on update or LastUpdate method.

and since you’re using the outline pass from “Toon/Basic Outline”, you have to edit the “Toon/Basic Outline”, not the “Toon/Lighted Outline”.

got it?

No I’m completely lost.

Are you saying I need to pasted the script in the ‘Toon/Basic Outline’? If So where abouts?

Try this code to adjust the line width by camera distance.
Simply add this script to the object with the Toon Shader (even if the render is multimaterial).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OutLineAdjust : MonoBehaviour {

    public Material[] thisMats;
    public float scale = 0.0002f;
    public float Minumum = 0.0025f;
    List<float> OrValues = new List<float>();

    void Awake () {
        thisMats = GetComponentInChildren<Renderer>().materials;
        for (int i = 0; i < thisMats.Length; i++)
            OrValues.Add( thisMats*.GetFloat("_Outline"));*


  • void Update () {*
    float Value = (Camera.main.transform.position - transform.position).magnitude * scale;

for (int i = 0; i < thisMats.Length; i++)
Value = Mathf.Clamp(Value, Minumum, OrValues*);*
thisMats*.SetFloat("Outline", Value);*