- Home /
ShaderLab: can I specify a portion of a texture?
Greetings,
I'm writing a custom shader. Currently, my shader accepts several materials, layered on top of one another to create an interesting 2D play on light and shadow.
_MainTex ("Main Diffuse", 2D) = "white" {}
_SubTex ("Secondary Diffuse", 2D) = "white" {}
_ShadowTex ("Shadow Texture", 2D) = "white" {}
However, this requires 3 separate images.
What I'd like to do, is place all three textures on one bigger texture, with one sub-texture in each quadrant. The shader would then use three quadrants of the bigger texture, instead of three separate, smaller textures.
This would theoretically be accomplished with three texture fields (extreme pseudo code, dunno if this is possible):
// Quadrant samples
_MainTex ("Main Diffuse", 2D) = "white" { 0,0,0.5,0.5 }
_SubTex ("Secondary Diffuse", 2D) = "white" { 0,.5,.5,1 }
_ShadowTex ("Shadow Texture", 2D) = "white" { .5,0,1,.5 }
Or, it could be accomplished through sub-shader code:
SetTexture [_MainTex]{
quadrant (0,0,.5,.5) // Doesn't exist
combine constant lerp (constant) texture
}
This may require a fragment shader, which I'm trying to avoid for compatability reasons.
I'm continuing to research it myself, but any advice would be appreciated. Thanks!
You should not put comments as answers...and you should mark the answer that solved your problem as correct ;) Welcome to Unity Answers!
Answer by Paulius-Liekis · Nov 05, 2012 at 02:39 PM
You can do it as long as your UVs are in 0-1 range.
If you want to do it in fixed pipeline shaders you ahve to use texture UV matrix (http://docs.unity3d.com/Documentation/Components/SL-SetTexture.html) to scale and offset texture UVs.
This was the exact article I needed. I cobbled together a decent matrix generator, and it's now working great. Thanks!
Bad conduct to mark your own answer as the solution. Honor those who have helped you.
I'm sorry, it wasn't my intention to do that. It actually was Paulius' answer that fixed my situation, but I marked my own as the answer by accident. Thank you for pointing that out, I'll try to be more careful in the future.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to create a shader that makes the texture look far away? 0 Answers
Help with GUI, Textures and Materials (Health Circle) 1 Answer
How to quickly change the texture on a material? 1 Answer
How do I change the default material size in ProBuilder? 0 Answers
Applying materials/textures on imported fbx, dae, etc 2 Answers