Search Unity

Extract Texture From Shader After Pass

Discussion in 'Scripting' started by BobBobson108, Jul 24, 2015.

  1. BobBobson108

    BobBobson108

    Joined:
    Mar 13, 2008
    Posts:
    57
    Hey folks,

    I'm trying to extract a texture from a shader after the final pass of that shader, so I can get a texture with lighting applied to it.

    Something like this:

    Textured Object => Shader applies lighting => Shader outputs final texture with lighting

    I've tried doing this in the Shader with
    Code (csharp):
    1. SetTexture[_OutputTex] { combine texture }
    I've also tried { combine primary } and using GrabPass { } to get the last pass.

    Then in a script attached to the object I have this, which waits until the rendering has finished before copying the pixel data over to a texture I make at runtime.

    Code (csharp):
    1. IEnumerator AfterRender ()
    2.     {
    3.         while (true)
    4.         {
    5.             yield return new WaitForEndOfFrame();
    6.             Color32[] pixels = (rend.material.GetTexture("_OutputTex") as Texture2D).GetPixels32();
    7.             outputTex.SetPixels32(pixels);
    8.             outputTex.Apply();
    9.         }
    10.     }
    The outputTex does not change though.

    Ultimately I want to be able to take the texture of from a character with lighting applied to it, including any shadows being cast.

    What is the best way to do this?

    Thanks!!
    ~Jake
     
  2. ToastehBro

    ToastehBro

    Joined:
    Jul 5, 2013
    Posts:
    7
    Hey Bob did you ever figure this out?
     
  3. BobBobson108

    BobBobson108

    Joined:
    Mar 13, 2008
    Posts:
    57
    No go sir. I think it could be done with a custom rendering solution.
     
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560