- Home /
Problem with texture blending and vertex color shader for iOS
I'm trying to create a shader that uses the vertex colors of the model for lightning, and blends two texture together ( I'm looking for an additive result )
What I got so far is:
Shader "WarpDash/UnlitVertexColorsAdditiveBlend" {
Properties {
_Blend ("Blend", Range (0, 1) ) = 0.5
_MainTex ("Texture", 2D) = "white" {}
_BlendTexture ("Blend Texture", 2D) = "black"
}
Category {
Tags { "Queue"="Geometry" }
Lighting Off
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
SubShader {
Pass {
SetTexture[_MainTex] {
Combine texture * primary DOUBLE
}
SetTexture [_BlendTexture] {
ConstantColor ([_Blend],[_Blend],[_Blend],[_Blend])
Combine texture * constant + previous
}
}
}
}
}
The shader works as I want when I'm not emulating OpenGL ES2.0. As far as I can figure out "Combine texture * constant + previous" is the problem, but I can't seem to find a way around it. I was hoping I could do this in one pass...
Thanks a bunch!
Answer by Jessy · Jun 14, 2011 at 07:01 PM
The only three argument combiner function that works on iOS is Combine src1 Lerp(src2) src3
, so your second texture stage won't work.
If you need help writing the shader; please tell me exactly what you want to do; I can't tell from your code. (I understand what your code does, but not why. One texture can be overbrightened by vert colors, but the other can't? Is Blend Texture a firey effect?) It's possible it can't be done in one pass with the MBXLite or even GLES 1.1 altogether; if so, do you want a single pass that looks wrong, or two passes that look right?
Properties {
_Blend ("Blend", Range (0, 1)) = 0.5
_MainTex ("Texture (A = Aditive Blend)", 2D) = ""
}
SubShader {Pass {
GLSLPROGRAM
varying lowp vec2 uv;
varying lowp vec3 color;
#ifdef VERTEX
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
uv = gl_MultiTexCoord0.xy;
color = gl_Color.rgb * 2.;
}
#endif
#ifdef FRAGMENT
uniform sampler2D _MainTex;
uniform lowp float _Blend;
void main() {
lowp vec4 texture = texture2D(_MainTex, uv);
gl_FragColor = vec4(texture.rgb * color + texture.a * _Blend, 1);
}
#endif
ENDGLSL
}}
SubShader {
BindChannels {
Bind "vertex", vertex
Bind "color", color
Bind "texCoord", texCoord
}
Pass {
SetTexture[_MainTex] {
ConstantColor (0,0,0, [_Blend])
Combine texture * primary Double, texture * constant
}
SetTexture[_] {Combine previous + previous alpha}
}
}
Hi Jessy,
What I want is a shader that takes the first texture "_$$anonymous$$ainTex" and applies it on the model taking vertex colors into account. I Already have a working version of that kind of shader.
But now I would like to use a seconds texture to add light to the result using a blending slider Think of a texture that has some light sources (leds) textured on it. I want to use a black and white texture to add glow around the lights in the texture ( I need the result to be Additive ).
The above shader does exactly what I want, but since the three argument combiner function I use does not work on the target platform I'm stuck.
I realize that it might not be possible to do in just one pass, but I would like to have this shader anyways. And if its possible to get a similar result using one pass that would be great too!
Thanks alot!
If you "want to use a black and white texture", how about storing it in the alpha channel (code added above)?
Hey it works! You just made my day Jessy! Thanks a lot!
Your answer
Follow this Question
Related Questions
What shader blending modes don't work on iOS? 1 Answer
iOS Shader - Emulator vs Device Inconsistency 0 Answers
how to build impressive shaders for iOS? 1 Answer
Blending 2 textures using vertex COLOR 0 Answers
Vertex color blending shader 1 Answer