I will give a shader slice:

float4 frag(VertexOutput i) : COLOR { // Read inputs float4 _a = tex2D( _A, i.uv ); float4 _b = tex2D( _B, i.uv ); float4 _t = tex2D( _T, i.uv ); // Operator float4 outputColor = lerp(_a, _b, _t); // Return return outputColor * _OutputMask; } 

How to get from a script in a unit what it returns: return ...? I just need to save the result to a variable and continue to work with it.

    1 answer 1

    Recently experimented with this. In short, you need to force the video card to render the shader to RenderTexture using Blit() .

    1. In the editor, create material with your shader.
    2. In the script we take this material.
    3. In the script, create or take the original texture if needed (in the shader we see tex2D() , which means we need it, obviously).
    4. In the script, create an instance of RenderTexture desired size and format.
    5. In Update() (or when you need it there) we call:

      Graphics.Blit (dataTexture, renderTexture, computeMaterial);

    where dataTexture is the source texture from clause 3, renderTexture is the target texture from clause 4, computeMaterial is the material from clauses 1-2.

    Next you need to read the result from the renderTexture with the right pixels.


    The choice of format for the source and target textures is a somewhat tricky question in itself due to the different support of different formats by different video cards. I used Alpha8 for the original, as I needed only one channel with the smallest amount of information transmitted; and ARGB32 for the target.


    If you need to get data from a shader and at the same time use it to display a picture on a monitor, then, as I understand it, you’ll have to render it two times: one normal and one via Blit() . Or you can RenderTexture on the model, especially if it's just a plane.


    If only computation is needed, and there is no need for support for devices that cannot DX11 (there are still a lot of such, as I understand it), then it is simpler and wiser to use computational shaders .

    • Thanks for the information. Now I am studying computational shaders. When I get a more or less acceptable result, I will write here. - Dmitry Sidorov