• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Jaywalker · Jul 20, 2011 at 08:02 PM · texturecolorvertex-lit

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"
   }
Comment

People who like this

0 Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jaywalker · Jul 21, 2011 at 04:22 AM 0
Share

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?

avatar image Jessy · Jul 21, 2011 at 06:51 AM 1
Share

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.

avatar image Jaywalker · Jul 21, 2011 at 08:11 AM 0
Share

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 @

http://forum.unity3d.com/threads/88704-iOS-How-does-one-write-shaders-for-OpenGL-ES-2.0?highlight=Glsl+examples

or would you say, just use the mobile/vertexlit shader with multiple textures because I use the beast lightmapping? and cannot optimize properly?

avatar image Jessy · Jul 21, 2011 at 08:17 AM 1
Share

You should use vertex colors to get batching, instead. 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 instead: http://en.wikibooks.org/wiki/GLSL_Programming/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.

avatar image Jessy · Jul 21, 2011 at 02:47 PM 1
Share

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=Masked_Tint

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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
         }
     }
 }
  
 }
Comment
Jaywalker

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jaywalker · Jul 21, 2011 at 04:29 PM 0
Share

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!

avatar image Recluse · Aug 11, 2011 at 04:38 PM 0
Share

Thanks Jessy, is there a shader that will combine a Beast lightmap and a realtime vert point light?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't find a way to simplify this code... any help appreciated... 0 Answers

is it possible to make 2d real time coloring with masks (paint app) 1 Answer

Shader Texture Change 3 Answers

How do I fill a texture with one color? 2 Answers

All my stuff become pink like if a pink light could be there. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges