- Home /
Output shader to image
Hi all,
Being ignorant on the matter of shaders, I wanted to know if it's possible to specify a texture, or any other texture-like format as the output for a shader.
I'm using this for a postcard/photo decal system, and CaptureScreenshot and GetPixels aren't enough for the job.
So I basically wanted to know if there's any way to ask a shader to output to an image rather than to whatever it usually outputs to
Thanks in advance
Answer by CHPedersen · Jan 04, 2012 at 07:50 AM
You can have a camera render itself to a texture instead of the screen - this is what is known as RenderTextures. Everything passes through the rendering pipeline as normal, and all shaders are executed like you're used to, the system just writes all the rendered pixels in a texture instead of displaying them on the screen. You can read about it if you follow the link I provided.
Keep in mind that this is unfortunately a Unity Pro feature.
Thanks for the explanation. Didn't know that rendertextures were just camera=>texture. Do you happen to know where I can find what shaders can accept as input, and give as output? $$anonymous$$ight be able to find a workaround, since I don't have Unity Pro.
Delving into what, specifically, shaders can input or output is a technical topic worthy of more studying than a comment in a forum offers. ;) Shaders are written in Cg which was made to look syntactically similar to C, in order to make it easier for new developers to get started. But because GPUs function fundamentally differently from a CPU, the programs one writes must follow some very specific rules which often present a limit to what you can do, such as reading and writing data whenever you want. If you're interested in some in-depth knowledge about it, I recommend you read a book on the topic, for example the Cg tutorial available here:
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html
Or, at the very least, study Unity3D's Shaderlab pages which start here:
http://unity3d.com/support/documentation/$$anonymous$$anual/Shaders.html
As a rule of thumb, however, and to briefly answer your question:
Vertex shaders are an intermediate stage and can output most anything you want them to, but a final position (for the vertex it processes) is mandatory output.
Pixel shaders are closer to the final steps (just before scissoring and depth testing), and therefore, their output options are a lot more limited - usually, they have to reduce all calculations to a single pixel value to be output to the framebuffer. To my knowledge, you cannot write values to a texture in its memory, that's one of the limitations; Cg has no concept of I/O. You can only output pixels, and if you want the target framebuffer, where the output ends up, to be a texture and not the screen, this must be specified at the application level (RenderTexture), before things enter the rendering pipeline.
Your answer
Follow this Question
Related Questions
Colors are wrong in in-game screenshot 1 Answer
Stippled Shader? 2 Answers
Overlapping Transparent Objects to Same Colour 0 Answers
Cant get the World Pos from Depth! 0 Answers
Why is my texture2D screenshot coming out as a grey box? 1 Answer