0

Suppose I have a texture.

I've written a surface shader that takes this texture and divide the RGBA values by 2.

How can I apply this shader in the code to modify my texture? I imagine something like this:

public Texture t;
public Material m;     //material that contains the shader

public void ApplyShader(){
    Texture t2 = Apply(t, m);
}        
Daniel
  • 7,357
  • 7
  • 32
  • 84
  • Does this answer your question? [How to access raw data from RenderTexture in Unity](https://stackoverflow.com/questions/41730124/how-to-access-raw-data-from-rendertexture-in-unity) – Ruzihm Oct 06 '20 at 20:46

3 Answers3

0

You can't apply a shader to a texture, as far as I know or as far as I can find. You can only put the shader on a material which is used on the same renderer as the texture so that the effects are visible.

If you really want to apply those effects without using the shader on the material for that renderer, then you'd want to apply those effects differently. You can use Texture2D.SetPixels() and Texture2D.GetPixels() to change the colors of all the pixels pretty easy. I wouldn't recommend doing that every frame though, FYI. Make sure you called Texture2D.Apply() after you're done.

Otherwise, put the texture on a renderer and give that renderer the material that uses the shader.

Thomas Finch
  • 482
  • 2
  • 6
0

this is old, but since it came up first on my search for the same problem and i got it to work later:

You can use Graphics.Blit() to copy a texture to another while running it through a shader.

//a material containing the shader you want to use
//(i.e. create a new material and assign an 'Image Effect Shader' unity asset)
Material imageFilterMat;

//Then use 
Graphics.Blit(sourceTexture, targetTexture, imageFilterMat);
-1

Actually you can apply compute shader on texture.

Cort
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33124156) – Druid Nov 13 '22 at 21:32