- Home /
How to create texture in shader
Hello!
I have searched a lot and I have found no answer. If I make a shader, and make a texture2d variable in it, it is supposed that I then drag-n-drop some png/psd/whatever file there.
But how can I make this texture inside shader proceduraly? For example, I want it to be 256x256 and I want every pixel just to be random color. And the random color of each pixel should be calculated only once when the object is instantiated. So it's like a pregeneration of a texture on program start or object instantiation.
Thank you!
Answer by Steven-1 · Mar 11, 2013 at 03:17 PM
don't do this in a shader (not even possible as far as I know), do this in a script at startup. Use the Texture2D class: http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.html
Use SetPixels()
It's all fairly straightforward
If you even feel like going lower in program$$anonymous$$g you can use the GL class: http://docs.unity3d.com/Documentation/ScriptReference/GL.html
but the answer above is dealing with Texture which is what you ask for.
Generating a texture in a shader is actually very possible, although I don't know specifically how to do it in Unity.
When generating data for huge terrains procedurally in realtime, you will often want to perform these computations on the GPU.
As an example, here I am generating a normal map procedurally in realtime in a GLSL shader.
This allows for much greater detail locally than would be achievable by pregenerating data (unless you have a ton of memory to store the amount of data required for this detail level).
Sadly, as said, I don't know how to do this in Unity yet. However, I have made the source code for the application shown in the video above available online: http://github.com/zachanima/shader
Your answer
Follow this Question
Related Questions
How to create an procedural texture in shader graph for many objects, and save it? 0 Answers
is there a way to make a gradient material, white to black, in Unity3D? 2 Answers
Mapping a texture to a meshes normals? 0 Answers
Merging, Transitioning, or Blending from one texture to the other based upon Height Maps 0 Answers
How to avoid Texture.Apply() 1 Answer