1
\$\begingroup\$

This question is asked in the context of image effect shader implementations in Unity.

Looking through the scripts for image effects in the standard assets package, I notice that initialization is generally done in the CheckResource() function.

The following is a snippet of code from the BloomAndFlares image effect.

public override bool CheckResources()
{
    CheckSupport(false);

    screenBlend = CheckShaderAndCreateMaterial(screenBlendShader, screenBlend);
    lensFlareMaterial = CheckShaderAndCreateMaterial(lensFlareShader, lensFlareMaterial);
    vignetteMaterial = CheckShaderAndCreateMaterial(vignetteShader, vignetteMaterial);
    separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader, separableBlurMaterial);
    addBrightStuffBlendOneOneMaterial = CheckShaderAndCreateMaterial(addBrightStuffOneOneShader, addBrightStuffBlendOneOneMaterial);
    hollywoodFlaresMaterial = CheckShaderAndCreateMaterial(hollywoodFlaresShader, hollywoodFlaresMaterial);
    brightPassFilterMaterial = CheckShaderAndCreateMaterial(brightPassFilterShader, brightPassFilterMaterial);

    if (!isSupported)
        ReportAutoDisable();
    return isSupported;
}

I am probably wrong in assuming that this is essentially the Start() or Awake() functions for PoseEffectsBase, but I decided to try implementing something simillar.

public override bool CheckResources()
{
    saturationPassMat = CheckShaderAndCreateMaterial(saturationPassFilter, saturationPassMat);

    if (!isSupported)
        ReportAutoDisable();
    return isSupported;
}

Unlike the standard asset scripts, CheckResources() does not seem to be called consistently for me. Unity console throws UnassignedReferenceExceptions every frame, for the object saturationPassMat.

When is the CheckResources() function called? Should it be used for initialization just like Start()?

\$\endgroup\$
0
\$\begingroup\$

I looked through the PostEffectsBase class, and saw this.

protected void Start()
{
    CheckResources();
}

So... Yeah.

Kinda feel like an idiot now

| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.