x


shader access channels textures (photoshop R,G,B)

Hi

Can one access channels from a texture (photoshop R,G,B channels) within a shader using Unity Shader language or would this need to be done in GC/GLSL?

more ▼

asked Nov 23 '09 at 04:06 PM

maxfax2009 gravatar image

maxfax2009
82 8 8 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Yes, you can.

If 'theTexture' is defined as a Sampler2D, you can do this:

half4 pixelColor = tex2D (theTexture, uv); // sample the texture at position 'uv'

and then read the r, g, b or a values using:

pixelColor.r
pixelColor.g
pixelColor.b
pixelColor.a

Read more about "Texture Samplers" on the nvidia cg tutorial, here: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.html

You can also use 'swizzles' to pull out custom combinations of components of the color array (or other 3 or 4 item vector), eg:

pixelColor.rgb  // would create a new half3
pixelColor.aaaa // same as half4(pixelColor.a,pixelColor.a,pixelColor.a,pixelColor.a)
uv.yx           // same as half2(u.x,v.x)

...etc

For more information on swizzling, read here (about one third the way down the page): http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter05.html

more ▼

answered Nov 23 '09 at 04:30 PM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

For more on Unity, ShaderLab, Cg, GLSL and HLSL - please read:

Unity Answers - Can I use HLSL or GLSL shaders in Unity?

And, of course, the docs.

more ▼

answered Nov 23 '09 at 07:42 PM

robert gravatar image

robert ♦
1.1k 3 7 27

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1664

asked: Nov 23 '09 at 04:06 PM

Seen: 1453 times

Last Updated: Nov 23 '09 at 04:06 PM