- Home /
How to mix textures with multipass shader on Unity ?
How to mix textures with multipass shader on Unity ?
I want to mix textures with multipass shader on Unity as alpha-blending. But I don't know how to get the first pass's result with shader on Unity.
I've tried the variable "previous" and "primary", but they couldn't be used.
This is my shader.
Shader "Unlit/MixTextures" {
Properties {
_MainTex ("Main Base(RGB) Alpha(A)", 2D) = "white" {}
_MainTex2 ("Main2 Base(RGB) Alpha(A)", 2D) = "white" {}
}
SubShader {
Tags{"Queue"="Transparent"}
Pass{
// Base
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float4 _MainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 frag( v2f i ) : COLOR {
return tex2D(_MainTex, i.uv);
}
ENDCG
}
// pass 2
Pass{
// Add parts
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex2;
float4 _TextureOffsetAndSize2;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
float4 _MainTex2_ST;
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex2);
return o;
}
float4 frag( v2f i ) : COLOR {
// * I want to use 1st pass result here * //
float4 tex = tex2D(_MainTex2, i.uv);
tex.x *= tex.w;
tex.y *= tex.w;
tex.z *= tex.w;
return tex;
}
ENDCG
}
}
Fallback "Diffuse"
}
For this particular shader, it seems easier to do it all in one pass. Just move the line from frag#1 into frag#2 (rename a var) and lose pass#1. Even if you have two unwraps (isn't TRANSFOR$$anonymous$$_TEX mostly o.uv=v.uv;?) you can have the vertex shader pass the second along in TEXCOORD1. Your shader, it's creating "alpha blended" colors?
Thank you for your replying! You're all right. I can realize this function with one pass. $$anonymous$$y concrete shader will create "alpha blended" colors. So I want to know how to get the result of previous pass.
Answer by Peter Bruun-Rasmussen · May 30, 2011 at 12:26 PM
You should use the Blend-tag to blend the result of two passes. Something like this:
Shader "Unlit/MixTextures" {
Properties {
_MainTex ("Main Base(RGB) Alpha(A)", 2D) = "white" {}
_MainTex2 ("Main2 Base(RGB) Alpha(A)", 2D) = "white" {}
}
SubShader {
Tags{"Queue"="Transparent"}
Pass{
Lighting Off
SetTexture [_MainTex] {
combine texture, texture
}
}
Pass{
Blend DstColor Zero
Lighting Off
SetTexture [_MainTex2] {
combine texture, texture
}
}
}
Fallback "Diffuse"
}
Thank you for your replying. I tried your code. But there is a problem. On my scene, the color of 1st pass turned to be darker than that I don't apply multi-passes ...
Your answer
Follow this Question
Related Questions
Why is Unity generating grayscale normal maps? 0 Answers
Effects of Graphics.Blit to rendertexture are temporary 2 Answers
Assigning a texture for object in inspector. Is it loaded right away? 0 Answers
Why is my terrain so dark? 1 Answer
Converting a RenderTexture to a Texture2D for use in a shader 2 Answers