- Home /
lightmap plus per material color
Hey,
I'm trying to make the fastest shader for mobile (iOS) with support for: texture, lightmap and per material color. I've got it working by just stripping the Mobile/diffuseDetail shader of its detail texture.
Shader "Mobile/VertexLitColor" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass{
Material{
Diffuse [_Color]
}
Lighting On
SetTexture [_MainTex] { combine texture * primary Double, texture * primary}
}
}
FallBack "VertexLit", 2
}
Can anyone tell me if this is the faster thing to do or if it's just the same as using a diffuse texture (I only use lights for baking, no realtime lights)
Thanks a lot!
Ok after some great tips/link from Jessy I'm trying to build a GLSL shader but its seaming way over my heat atm. Here is my current attempt, it has the color, texture and the lightmap kind of works but then if I rotate the camera the object goes black at certain angles.
I'm starting to get bits and pieces of how this works but its still way over my head :)
Shader "Mobile/VertexLitColor" {
Properties {
_MainTex ("Texture Image", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Pass {
GLSLPROGRAM
uniform sampler2D _MainTex, unity_Lightmap;
uniform mat4 unity_LightmapMatrix;
uniform vec4 _Color ;
varying vec4 textureCoordinates;
varying vec2 textureCoordinatesLM;
#ifdef VERTEX
void main()
{
textureCoordinates = gl_MultiTexCoord0;
textureCoordinatesLM = vec2(unity_LightmapMatrix[0].x, unity_LightmapMatrix[1].y) * gl_MultiTexCoord1.xy + unity_LightmapMatrix[3].xy;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
void main()
{
gl_FragColor = texture2D(_MainTex, vec2(textureCoordinates)) * _Color * texture2D(unity_Lightmap, textureCoordinatesLM);
}
#endif
ENDGLSL
}
}
// The definition of a fallback shader should be commented out during development:
// Fallback "Unlit/Texture"
}
If i remove the fallback to vertexlit the shader breaks, does that mean its just always using the vertexlit shader even thoigh the color override still works?
That doesn't happen on my end, and I unfortunately don't have time to give you a good answer at the moment, but the shader doesn't do what you're saying it does. There is no lightmap in there. And if you're baking light, then you don't need per-material color, unless you're using built-in Beast lightmapping, in which case you can't optimize properly. Additionally, GLSL is going to make the shader noticeably faster.
Thanks! (and just ignore this comment if you don't have the time! I just have a little follow up question if you do :) )
I'm using the beast lightmapping, and I would like to use the: per material color, to color certain objects without adding the same texture with just a different color. Ill be trying my hands on GLSL shader from what I can learn from the forum. I found a thread where you also posted which seems promising @
or would you say, just use the mobile/vertexlit shader with multiple textures because I use the beast lightmapping? and cannot optimize properly?
You should use vertex colors to get batching, ins$$anonymous$$d. It's not as performant as baking it into the lightmap, but it's better than per-material colors. I learned that the realtime lighting in there doesn't always work, and you should check this out ins$$anonymous$$d: http://en.wikibooks.org/wiki/GLSL_Program$$anonymous$$g/Unity . The lightmapped passes are okay but I've gotten better at them and can maybe help you out later, but you should give it a try first.
Right, you've got the idea about the Beast implementation. When you set a color in a shader/material, that means that meshes with different colors can't batch. You need a way to get colors into the shader without relying on properties that require multiple materials, and vertex colors are perfect for that. http://www.unifycommunity.com/wiki/index.php?title=$$anonymous$$asked_Tint
Answer by Jessy · Jul 21, 2011 at 04:14 PM
Here's a shader that uses vert colors and Unity's lightmapping. I don't know what you want to do about the realtime preview; Unity doesn't provide a good means of dealing with that, that I know of. Maybe use Diffuse, and then switch shaders after you've baked?
Shader "Vert Color Diffuse" {
Properties {
_MainTex ("Base", 2D) = "white"
}
SubShader {
Pass {
Tags {"LightMode"="VertexLM" }
GLSLPROGRAM
uniform mediump mat4 unity_LightmapMatrix;
varying lowp vec4 color;
varying mediump vec2 uv;
varying lowp vec2 uv2;
#ifdef VERTEX
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
uv = gl_MultiTexCoord0.xy;
uv2 = vec2(unity_LightmapMatrix[0].x, unity_LightmapMatrix[1].y)
* gl_MultiTexCoord1.xy + unity_LightmapMatrix[3].xy;
color = gl_Color * 2.;
}
#endif
#ifdef FRAGMENT
uniform lowp sampler2D _MainTex, unity_Lightmap;
void main() {
gl_FragColor = texture2D(_MainTex, uv) * texture2D(unity_Lightmap, uv2) * color;
}
#endif
ENDGLSL
}
Pass { // Editor only - Graphics Emulation uses the wrong lightmap type
Tags {"LightMode"="VertexLMRGBM" }
GLSLPROGRAM
uniform mat4 unity_LightmapMatrix;
varying vec4 color;
varying vec2 uv, uv2;
#ifdef VERTEX
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
uv = gl_MultiTexCoord0.xy;
uv2 = vec2(unity_LightmapMatrix[0].x, unity_LightmapMatrix[1].y)
* gl_MultiTexCoord1.xy + unity_LightmapMatrix[3].xy;
color = gl_Color * 2.;
}
#endif
#ifdef FRAGMENT
uniform sampler2D _MainTex, unity_Lightmap;
void main() {
gl_FragColor = texture2D(_MainTex, uv) * texture2D(unity_Lightmap, uv2) * color;
}
#endif
ENDGLSL
}
}
SubShader {
Pass {
BindChannels {
Bind "vertex", vertex
Bind "color", color
Bind "texcoord", texcoord0
Bind "texcoord1", texcoord1
}
SetTexture[_MainTex] {Combine primary * texture}
SetTexture[unity_Lightmap] {
Matrix[unity_LightmapMatrix]
Combine previous * texture Double
}
}
}
}
Wow awesome! thanks a lot! switching back and forth between diffuse and this is not a problem. Ill just keep it on diffuse and switch the shaders pre-building. thanks again!
Thanks Jessy, is there a shader that will combine a Beast lightmap and a realtime vert point light?
Your answer
Follow this Question
Related Questions
Get color coordinate from the texture and convert to world space 1 Answer
Assigning UV Map to model at runtime 0 Answers
Vehicle color select? 2 Answers
How to Make a Character Flicker? 1 Answer
GetPixel Returning Inaccurate Color 1 Answer