- Home /
How to save result of GPU Perlin Noise (from shader) to a regular Texture2D?
Not sure I am asking the question correctly.
Here is the asset, Turbulence Library. It generates various noises using a shader, but how can I convert the result into regular Texture2D?
So I be able to use it in other materials or save to file.
Answer by tanoshimi · Nov 26, 2016 at 02:05 PM
You'd first use Graphics.Blit to draw the material using the Turbulence Library onto a rendertexture, and then use ReadPixels to copy from there to a regular Texture2D. Something like this:
// Create a temporary render texture
RenderTexture temp = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32);
// Draw to the RT (Texture2D.whiteTexture is just a dummy src)
// mat is a material with the Turbulence Shader applied to it
Graphics.Blit(Texture2D.whiteTexture, temp, mat);
// Copy the rendertexture to a regular texture
Texture2D tex = new Texture2D(width, height);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
tex.Apply();
Thanks for the reply! I copied the code, but for some reason, result texture is completely black. However if I select material in inspector - it displayed with turbulence.
Is this a surface shader or a regular vert/frag shader? From memory surface shaders don't work well with Blit: https://forum.unity3d.com/threads/shaders-not-working-in-graphics-blit-when-using-my-own-rendertexture.98161/
It took some time, but I figured out! Yes, it is surface shader, so it does not work, but it can be quite easily converted in fragment shader and it starts to work perfect!
Thanks you! :)
If someone will encounter the same problem - let me know, I will describe solution in more details.
Your answer
Follow this Question
Related Questions
Transparency and texture atlases, shader calls affecting performance? 2 Answers
Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers
Change particle tint color via script 1 Answer
[Closed] Extracting current state of texture as new texture 0 Answers
My mesh didn’t appear in Unity 0 Answers