Texture Creation

Hey there!Can someone please explain me how to create textures using Shaders or direct me to an article,please?I have searched for quite a while now,but nothing about this…
In Scripting is pretty easy using for loops.But eats up CPU :[ …How would you do it in CG?

Thanks!

You would use a 2D art application such as gimp, photoshop, or genetica. (http://www.spiralgraphics.biz/genetica.htm) Then import it into Unity and create a material. After that you would drag your texture to the “texture” slot in the material editor and choose an effect for the material. I can create a youtube tutorial if you need further assistance. :slight_smile:

Hope that’s what you meant.

Hey there!Thanks for the reply and implication but i was intrested in creating it procedurally in Unity :frowning:

So that we calculate and set each pixel inside the shader.I know how it’s done by scripting,but i am curious how to do it using shaders

Thank you!

Anyone,please?

To do this with shaders, you would set up whatever geometry you want rendered in front of a camera, make a RenderTexture the active render target using RenderTexture.active, and use Camera.Render() to manually render once to that texture. Then all your geometry will be rendered to the RenderTexture in the same way as things are usually rendered to the screen.

I see…But i was wondering about the coding part in shaders…How would i set each pixel of the texture?

In scripting you would just do this:

for(var x : int;x < width;x++){
for(var y : int;y < height;y++){
texture.SetPixel (x, y, color);
}
}

I am curious how would you do this in Shaders…

for example, try to play with these examples,

the color part is happening at,

half4 frag( v2f i ) : COLOR {
    half4 col = float4(1,0,0,1); // (r,g,b,alpha)
    return col;
}

take the 2nd example from that page,
replace “half4 frag” part with this ^
it one makes an all red object…

or try with this red gradient,

half4 frag( v2f i ) : COLOR {
    half4 col = float4(i.uv.y,0,0,1); // (r,g,b,alpha)
    return col;
}

Yay!Thanks a ton!Exacly what i was looking for!

Just one more question : How would i get the coords of an actual texture that i assign (from the Properties{} ) , so it can be later read by a script using GetPixel()?

Sorry if the questions sound silly,but i am very new to CG :slight_smile:

mgear’s examples are just regular shaders that don’t happen to use textures as inputs. They do not write to a texture any more than any other shader. If you actually want to create a texture with a single shader, your best bet is to use Graphics.Blit(), which is a shortcut for what I outlined above, but using a single quad with a single Material instead of an arbitrary Camera view.

If you want to read pixel values in a script, you will have to make a copy using Texture2D.ReadPixels().

Unfortunately there is no simpler way, like a “run this shader once on a texture of this size” function. This is because shaders run on the GPU, which is separate hardware and memory from the CPU, where scripts run. Graphics.Blit() is about as easy as you’re going to get.

Alright,thank you :slight_smile: