3
\$\begingroup\$

I am trying to write a composite Image Effect shader with a script that works as such:

  1. The camera texture is taken, and stored into two different variables. For examples sake they will be tex1 and tex2.

  2. tex1 is modified by a shader.

  3. Both tex1 and tex2 are taken into another shader, which composes them together. The result is stored as tex.

  4. tex is printed to the screen.

I am not having an issue with the actual content of the shader manipulating the image-- But I do not know how to pass in both textures to one shader as needs to be done in step three. Thanks in advance if anyone can help.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Here's how I might approach it, using Graphics.Blit and two materials/shaders for the separate modification steps. You can call this from OnRenderImage to have it applied as a post effect once the camera's rendering is finished.

void CompositeTextures(RenderTexture tex1, RenderTexture tex2, RenderTexture output) {

    // Create/reuse a temporary RenderTexture as an intermediate
    // (this can be cheaper than reserving memory for it throughout).
    var temp = RenderTexture.GetTemporary(tex1.width, tex1.height, 0, tex1.format);

    // Perform the first shader operation, modifying tex1
    // and storing the result in a temporary buffer.
    Graphics.Blit(tex1, temp, firstOpMaterial, firstOpPass);

    // Assign tex2 as an auxiliary texture 
    // to be sampled by our second shader operation.
    secondOpMaterial.SetTexture("_Tex2", tex2);

    // Perform the compositing step to populate the output, 
    // with the modified tex1 piped through as _MainTex
    // and tex2 piped to a sampler called _Tex2.
    Graphics.Blit(temp, output, secondOpMaterial, secondOpPass);

    // Recycle the temporary render target we used.
    RenderTexture.ReleaseTemporary(temp);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .