To write a Unity shader that samples _MainTexonly if_Val (a float property) is less than 0.5, and otherwise perhaps does something else (like sample _MaskTex, or discard, or blend), here's how you can structure the fragment shader in HLSL.
✅ Basic Shader Logic:
Use a simple if statement in the fragment function.
Sample _MainTex only if _Val < 0.5.
You can define what happens when _Val >= 0.5. For example, sample _MaskTex.
🧾 Example Shader Code (Surface Shader Style - Simple Fragment Logic)
Here’s an example using a Surface Shader, which is typical in Unity:
This uses Unity's Surface Shader system for simplicity and compatibility with lighting.
The condition if (_Val < 0.5) controls which texture to use.
You can adjust the logic inside the branches (e.g., discard pixels, mix colors, etc.).
🔁 If You're Using a Fragment Shader (Unlit / Custom Pass)
If you're writing a more modern or custom fragment shader (e.g., unlit or HDRP-compatible), the structure will be different — let me know if you need that version too!
Let me know:
Are you using URP / HDRP?
Should it be lit or unlit?
Do you want to discard pixels instead of sampling another texture?
I can tailor the shader accordingly!
User
in cpu programming language, we can ref _MainTex _MaskTex by variable : var _tex = _Val < 0.5f ? _MainTex : _MaskTex; then tex2D(_tex, ...); why shader programming can't
Assistant
Great question!
You're asking why in shader programming (like HLSL used in Unity), you can't do something like this:
cpp
// This works in CPU code, but not directly in HLSL shader code:
var _tex = _Val < 0.5f ? _MainTex : _MaskTex;
fixed4 color = tex2D(_tex, uv);
Whereas you can do it without issue in general-purpose CPU languages like C# or JavaScript.
🔍 Why This Doesn’t Work in Shaders
🧠 Short Answer:
In HLSL, especially on many platforms and hardware (mobile GPUs, older desktop GPUs), dynamic texture sampling via variables is either restricted or discouraged due to:
GPU architecture limitations
Shader compiler optimizations
Texture register binding model
📌 Long Answer: Technical Details
✅ In CPU Code:
You're working with pointers/references.
The CPU can dynamically choose which texture to sample at runtime easily.
Branching and indirection are cheap.
❌ In GPU Shaders:
Textures are bound to fixed registers during compilation (e.g., t0, t1, etc.).
Sampling from a variable texture (Texture2D myTex = condition ? t0 : t1;) introduces indirect texture access, which may:
Not be supported on all hardware.
Cause performance issues (cache misses, divergent execution).
Be disallowed by the shader compiler for optimization reasons.
So even though some modern GPUs support dynamic indexing/sampling, shading languages like HLSL/Cg/GLSL restrict this to ensure portability and performance.
✅ So How Do You Handle It?
You need to write it like this:
hlsl
fixed4 color;
if (_Val < 0.5)
{
color = tex2D(_MainTex, uv);
}
else
{
color = tex2D(_MaskTex, uv);
}
This way:
Each tex2D call uses a statically known texture.
The shader compiler knows exactly which register each texture uses.
Optimization is possible (and required for some platforms).
🛠 Optional: Use UNITY_BRANCH Macro
To help optimize branching on GPU (especially mobile), you can use:
hlsl
UNITY_BRANCH
if (_Val < 0.5)
{
color = tex2D(_MainTex, uv);
}
else
{
color = tex2D(_MaskTex, uv);
}
This tells Unity to compile the branch so that only one path runs per pixel (instead of evaluating both), improving performance.
📚 Summary
Feature
CPU Code
Shader Code (HLSL)
Dynamic Texture Assignment
✅ Yes
❌ No / Limited
Static Texture Sampling
✅ Yes
✅ Yes
Branching with if
✅ Yes
✅ Yes (with care)
Performance-aware branching
N/A
✅ Use UNITY_BRANCH
💡 Pro Tip:
If you have many textures and want to index into them dynamically (e.g., array of textures), look into Texture Arrays or Compute Shaders, which allow more flexible usage — but again, they require special handling and aren't universally supported across all platforms.
Let me know if you'd like an example using texture arrays or compute shaders!