- Home /
Is it possible to re-use a texture within a shader on separate uv channels?
Hi, Im trying to optimise a my shader as far as possible, I have a single texture that i want to split into 2 essentially, here's what im trying to do: 1 texture - rgd = uv1 a = uv2
I have an asset that has a diffuse texture layout in uv1 and an AO Bake hidden in the alpha channel that requires uv2.
Currently I am having to pull in the texture with a sampler2D - attach that to a fixed4 and apply the uv1 to it but then to achieve the second uv2 I need to repeat the above? im also having to use two separate texture slots for the same texture? is there a more efficient way of doing this?
my current shader below:
Shader "Custom/OptimalDiffuse_uv2-AOBake" {
Properties
{
_MainTex ("Main Texture (RGB)", 2D) = "white" {}
_AOBake ("Main Texture (A) - AO Bake", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="opaque"}
LOD 250
CGPROGRAM
#pragma surface surf Lambert noforwardadd
sampler2D _MainTex;
sampler2D _AOBake;
struct Input
{
fixed2 uv_MainTex;
fixed2 uv2_AOBake;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
fixed4 c2 = tex2D (_AOBake, IN.uv2_AOBake);
o.Albedo = (c.rgb * c2.a);
}
ENDCG
}
Fallback "Mobile/Diffuse"
}
Any Advice would really be welcome, Thanks.
Answer by Owen-Reynolds · Jan 19, 2014 at 05:40 PM
You can definitely have a single texture, which is looked-up (`tex2D`'d) multiple times, with different UVs.
Maybe you can compute uv2 from uv1. Otherwise you'll have to figure out how to tell Unity to send just a UV2 array.
But I'm not sure that sending the same texture twice is even a problem. The texture manager usually handles all textrues for all draws. So should notice AOBake uses an already loaded repeat.
Thanks for the reply, as you can see im pretty new to shaders. would you be able to shed some light in how i might be able to achieve this?
from a users point of view I'd like to be able to pull everything from the single texture?
This is my attempt but it is now broken:
Shader "Custom/OptimalDiffuse_uv2-AOBake - WIP" {
Properties
{
_$$anonymous$$ainTex ("$$anonymous$$ain Texture (RGB)", 2D) = "white" {}
//_AOBake ("$$anonymous$$ain Texture (A) - AO Bake", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="opaque"}
LOD 250
CGPROGRA$$anonymous$$
#pragma surface surf Lambert noforwardadd
sampler2D _$$anonymous$$ainTex;
//sampler2D _AOBake;
struct Input
{
fixed2 uv_$$anonymous$$ainTex;
fixed2 uv2_$$anonymous$$ainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
fixed4 c2 = tex2D (_$$anonymous$$ainTex, IN.uv2_$$anonymous$$ainTex);
o.Albedo = (c.rgb * c2.a);
}
ENDCG
}
Fallback "$$anonymous$$obile/Diffuse"
}
Using mainTex twice like that (in surf) is absolutely fine. But I think, since you don't have a 2nd texture, you don't have a uv2. Now, something like tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex*2)
would be fine. Any legal float2 can be in that 2nd slot.
Non-Unity, you manually send all vertex data, so can just send more to have uv2 in the shader. But Unity does so much for you, I'm not sure how to tell it to send more arbitrary vertex data. Using a 2nd dummy texture (even an empty one?) might be the simplest way to get uv2.
Hi Owen, thanks again. Can I just confirm by "surf" are you refering to it being a surface shader? Also what is the "*" doing in tex2D($$anonymous$$ainTex, IN.uv$$anonymous$$ainTex*2)?
I haven't played with this yet but will do hopefully later on :)
Shaders have some magic, but the guts are just program$$anonymous$$g. The star is the same as in A*2
Your answer
Follow this Question
Related Questions
Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers
Combining a custom shader with a normal one 0 Answers
How to add Emission to my custom shader? 2 Answers
Adding a clip() to the default shader? 0 Answers
Shader - What is float3.xy? 1 Answer