0

I currently work on a project in Unity 5. I am trying to apply a shader to one of my cameras using Camera.RenderWithShader, and after that read and save the image. Here is the code:

Texture2D screenshot = new Texture2D(this.screenWidth, this.screenHeight, TextureFormat.RGB24, false);
this.mainCamera.RenderWithShader(this.myShader,"RenderType");
screenshot.ReadPixels(new Rect(0, 0, this.cameraWidth, this.cameraHeight), 0, 0);

The problem is that, after I save the screenshot texture as a Bitmap, the shader is not applied on the entire image.

But if I use Camera.Render() and apply the shader using OnRenderImage(RenderTexture,RenderTexture), it works.

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
     Graphics.Blit(source, destination, this.disparityMaterial);
}

So, my question is: What is the difference between these two approaches and how can I make the Camera.RenderWithShader function work properly?

1 Answer 1

1

RenderWithShader and OnRenderImage are two completely different things and have nothing to do with each other.
Read the linked manual pages for details and a better understand, but long story short:
Prior is applying a shader to all (game)objects the camera can see without any image filters applied so basically it's about using a different shader for the same objects/prefabs/materials to alter something the way you want for the viewer (in your case, GOs should also have their tag set to "RenderType", otherwise the shader will not be applied on them),
Latter one however is a "post processing" feature, applying filters only on images already rendered. I.e. an image effect feature.

So a good use to prior one is e.g. nightvision on/off, or remove cloth from chicks with that special glasses the player can put hands on (mmmmm), etc while the latter one is clearly just image effects, e.g. a secret agent takes photos while one-finger-kills enemies, but when he gets hit, his equipment is more and more damaged, so photos taken as intel are getting more and more blurry, broken up and such - if that makes sense.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.