WebCam effects with ShaderGraph in Unity

ShaderGraph is the official Unity node based shader tool. This tool simplify shader authoring workflows and let you create them without writting any piece of code.

We can use it to create all sort of shaders, for example we can interface it with a WebCamTexture and apply effects to it.

We will make this in two parts, the first part is a script that capture webcam information and send it to the shader. The second part use the texture and apply any type of effect, we will use a ShaderGraph for that part.


private void Start()
{
    if (_webCamTexture == null)
    {
        _webCamTexture = new WebCamTexture();
    }

    _webCamTexture.requestedHeight = 512;
    _webCamTexture.requestedWidth = 512;
    _webCamTexture.Play();
}

private void Update()
{
    if (_material == null)
        return;

    _material.SetTexture("_WebcamTex", _webCamTexture);
}

This script will create a WebCamTexture and send it to the ShaderGraph.

On the shader side, we expose a texture property named "_WebcamTex" and we apply a custom pixelate effect to it.

SG2

Result

ShaderGraph

For reference, the code is available on GitHub