I’m probably asking this question wrong. I’m using Turbulence Library to generate noise as shaders. What I want to do is take the noise generated by these shaders and apply them to Texture2Ds that I’m procedurally generating.
What I want to do is take the value of each pixel, then while procedurally generating a texture, use to determine how much I put another color “on top of” another color in a procedurally generated mesh. E.G. This
becomes this
I strongly suspect that I will just have to write my own shader from scratch, but is there no way to just take noise from a shader and apply it to a Texture2D so I can continue to use the Standard shader?
If you want to turn the shader into a texture, create a material from the noise shader. Next, create a custom texture by “Create > Custom Texture”. Set the Intialization Mode to “OnLoad”, set the Source to “Material” and set the Material to the noise shader material you just created. You might need to switch the UpdateMode off and on of “OnLoad” to get this to work. This will get you a texture from your shader, but it is a little unstable. Unfortunately, you can’t use Graphics.Blit to get your texture because Turbulence uses surface shaders rather than fragment shaders.
I actually suggest modifying their shader or writing your own though. Things to keep in mind when you are modifying their shaders:
-
Add a property for Base Texture and Color Overlay in the shader properties. This will allow you to set these variables in the inspector or through code:
Properties
{
_Base (“Base Texture”, 2D) = “” {}
_Overlay (“Overlay Color”, Color) = (1, 1, 1, 1)
_Octaves ("Octaves", Float) = 8.0
_Frequency ("Frequency", Float) = 1.0
_Amplitude ("Amplitude", Float) = 1.0
_Lacunarity ("Lacunarity", Float) = 1.92
_Persistence ("Persistence", Float) = 0.8
_Offset ("Offset", Vector) = (0.0, 0.0, 0.0, 0.0)
}
-
Further in the shader, you will find a SubShader section where the variable names are repeated. Add your variables to this. This is so that Unity knows to send the inspector variables to the CG program:
sampler2D _Base;
fixed4 _Overlay;
fixed _Octaves;
float _Frequency;
float _Amplitude;
float2 _Offset;
float _Lacunarity;
float _Persistence;
Lastly you’ll need to modify the surf function. This function takes any UVs or data you need as input, and fills in output structure SurfaceOutput. It is called for every pixel that you see. Here is how an example on their Perlin shader:
void surf (Input IN, inout SurfaceOutput o)
{
// h is a randomly generated noise value for this pixel.
// Normally, turbulance uses h as the color value of the pixel.
float h = PerlinNormal(IN.pos.xy, _Octaves, _Offset, _Frequency, _Amplitude, _Lacunarity, _Persistence);
// We will use h as the blend value between our base texture and our overlay color
// tex2D (_Base, IN.pos) gets the color of our texture at this pixel
// lerp interpolates for us
float4 c = lerp( tex2D (_Base, IN.pos), _Overlay, h);
o.Albedo = c.rgb;
o.Alpha = 1.0;
}
This got me something like this: