• 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
1
Question by numberkruncher · Feb 16, 2012 at 03:48 PM · animationblendervertexcolor

How to import vertex colour animation?

I have animated vertex colours using Blender which give the effect of dimming lights. How can I import this animation into Unity?

As an alternative approach, is it possible to create an iOS friendly shader that would achieve the following:

  • Red channel of vertex colour is used for one set of lighting

  • Blue channel of vertex colour is used for second set of lighting

  • Shader has 2 properties (lightAmount1 and lightAmount2) floating point 0 to 1

  • Calculate emission colour (RGB) from:

    min(1.0, lightAmount1 * vertexcolor.red + lightAmount2 * vertexcolor.blue)
    

If this is possible, then I could animate those two properties.

Comment
Add comment · Show 18
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 Jessy · Feb 16, 2012 at 06:12 PM 0
Share

The shader is totally "possible". From looking at that code, I'd say you ought to use the mix instruction ins$$anonymous$$d of two multiplies, and find a way to eli$$anonymous$$ate the $$anonymous$$, because it doesn't seem like anything more than unnecessary complexity, but that's about all I can say, without knowing what else you need in the shader.

avatar image numberkruncher · Feb 16, 2012 at 06:32 PM 0
Share

@Jessy The shader literally needs to be vertex lit with a texture and two simulated light sources. What sort of shader would I need to write? I have only written basic shaders before. Are you by any chance JessyUV from YouTube? If so, those are some great tutorials!

avatar image Jessy · Feb 16, 2012 at 07:19 PM 1
Share

I am; thanks! Unfortunately, those are all on fixed function shaders that can't do what you want very efficiently. (You'd probably make use of the Dot3 combiner to pull out your greyscale lighting.) You can write a surface shader, in Cg, or in what I use, GLSL. The latter has the best potential for performance but it's also really poorly-documented. Here's the best resource I know of; within #ifdef VERTEXLIGHT_ON is where you'd want to put lighting.

I'd really like to know the answer to this question though. I haven't seen it done and doubt that it's possible (without remapping the animation to other parameters), but I hope I'm wrong.

avatar image numberkruncher · Feb 17, 2012 at 01:43 AM 0
Share

@Jessy without your video tutorials I wouldn't know a thing about shaders, I always found it very difficult to get into. For some reason I cannot use GLSL on my Windows PC, it says graphics card not supported...so I've used Cg for now. I have got the main part working, I am not sure how to add vertex lighting though at the moment:

Shader "Custom/Animated Vertex Emission" {

Properties { _$$anonymous$$ainTex ("Texture", 2D) = "white" {} _EmissionA ("Emission A", Range(0, 1)) = 1 _EmissionB ("Emission B", Range(0, 1)) = 0 }

SubShader { Pass { CGPROGRA$$anonymous$$ #pragma vertex vert #pragma fragment frag

     #include "UnityCG.cginc"

     struct appdata {
         float4 vertex    : POSITION;
         float4 texcoord    : TEXCOORD0;
         float4 color    : COLOR;
     };

     struct v2f {
         float4 pos        : SV_POSITION;
         float2 uv        : TEXCOORD0;
         fixed4 color    : COLOR;
     };

     sampler2D _$$anonymous$$ainTex;
     float _EmissionA;
     float _EmissionB;
     
     float4 _$$anonymous$$ainTex_ST;

     v2f vert(appdata v) {
         v2f o;
         
         o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);

         float c = $$anonymous$$(1, _EmissionA * v.color.r + _EmissionB * v.color.b);
         o.color = float4(c, c, c, 1);
         o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);

         return o;
     }

     half4 frag(v2f i) : COLOR {
         half4 outColor = tex2D(_$$anonymous$$ainTex, i.uv);
         return outColor * i.color;
     }
 ENDCG
 }

} }

avatar image numberkruncher · Feb 17, 2012 at 01:44 AM 0
Share

btw, this is my very first vertex/fragment shader :-)

avatar image Jessy · Feb 17, 2012 at 02:57 AM 1
Share

Nice work! I would think it wouldn't be hard to create a surface shader based on that; have you tried? The first page of that wikibook has instructions for running the Editor in OpenGL; did you have the error even in that mode?

avatar image numberkruncher · Feb 17, 2012 at 03:22 AM 0
Share

@Jessy cheers, the GLSL shaders work when I add the command line switch. I was under the impression that surface shaders performed slow on iOS because they operate on a per-pixel basis, so I didn't give it much thought. I wanted to use a basic surface shader with a light cookie (it looked really nice and added a lot of atmosphere) but I was warned that it would perform poorly on iOS.

avatar image numberkruncher · Feb 17, 2012 at 03:50 AM 0
Share

@Jessy Here is my attempt at rewriting this as a surface shader. Tbh I am a little confused. I haven't tried this on iOS yet, but it seems to be just what I want but I am a little concerned about performance

SubShader { Tags { "RenderType"="Opaque" } Fog { Color[_AddFog] }

 CGPROGRA$$anonymous$$
     #pragma surface surf Lambert

     struct Input {
         float2 uv_$$anonymous$$ainTex;
         float4 color        : COLOR;
     };

     sampler2D _$$anonymous$$ainTex;

     float _EmissionA;
     float _EmissionB;

     void surf(Input IN, inout SurfaceOutput o) {
         float4 temp = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);

         float c = $$anonymous$$(1, _EmissionA * IN.color.r + _EmissionB * IN.color.b);

         o.Albedo = half3(c * temp.r, c * temp.g, c * temp.b);
     }
 ENDCG

}

avatar image Jessy · Feb 17, 2012 at 03:55 AM 0
Share

You should check the portion of the compiled+translated GLSL with PVRUniSCo that corresponds to the condition(s) under which you'll use it. If it's poor, then you can base a GLSL version off of that. (Thats how I learned a lot of techniques!)

avatar image numberkruncher · Feb 17, 2012 at 04:06 AM 0
Share

@Jessy I have opened the compiled shader (quite a long file with opengl, d3d9, gles, etc.) but I cannot find the text "PVRUniSCo" in there and there do not appear to be any GLSLPROGRA$$anonymous$$ statements either. Thanks for your help, it is greatly appreciated

avatar image Jessy · Feb 17, 2012 at 04:30 AM 0
Share

PVRUniSCo is a code profiling tool for PowerVR GPUs, so you'll find it on Imagination Tech's site, not in a shader. The "gles" subshaders are what to check out in the ugly auto-translated code. (They're the ones with precision modifiers.)

avatar image numberkruncher · Feb 17, 2012 at 04:44 PM 0
Share

@Jessy I have extracted the "gles " fragment and vertex shaders and put them into separate files for compiling within PVRUniSCo. It has given the following profile output:

Vertex Shader:

Emulated cycles best         56
Emulated cycles worst        56
Ratio cycles/USSE         1.4
Primary attributes         16
Temporary registers         7

Fragment Shader:

Emulated cycles best         19
Emulated cycles worst        19
Ratio cycles/USSE         1.1
Primary attributes         4
Temporary registers         5

What suggests a poorly perfor$$anonymous$$g shader?

avatar image numberkruncher · Feb 17, 2012 at 05:01 PM 0
Share

I am not sure why the source code has vanished from the above comments, in fact it appears that the comments have gone altogether. So I have posted them in answer format so that others can benefit. I suppose a moderator must have removed them for some reason...

avatar image Jessy · Feb 17, 2012 at 05:23 PM 1
Share

Those are pretty heavy for use on current devices, and surely could be optimized, but that level of instruction suggests that you'd need to be clear about what you could leave out. It's too complex for me to recommend anything better without knowing exactly what you need, but using "half" and "fixed" more, which translate to "mediump" and "lowp", respectively, should probably be the first place for you to go.

avatar image numberkruncher · Feb 18, 2012 at 01:00 AM 0
Share

@Jessy All of my attempts at using surface shaders (with lighting) run very poorly on the iPhone. The Cg variation seems to work very smoothly.

I applied it to 500 moving objects (with animation) just to see how well it would work and I am getting 28-30fps on iPhone. Of course that is without vertex lighting.

I will update this question if I manage to work out how to add simple vertex lighting. I will also have a go at writing a GLSL version from the ground up. I cannot get my head around the automatically generated shader.

Thanks for all of the advice

avatar image Jessy · Feb 18, 2012 at 03:32 AM 0
Share

Number of objects does't matter to the GPU. If vert count is high, then that might affect performance, because your reported vert shader instructions are high, but there is zero overdraw on iOS for opaque geometry due to PowerVR. I'm sorry that UT makes it so hard on you to get good performance; I've been dealing with that problem for years.

avatar image numberkruncher · Feb 20, 2012 at 03:17 AM 0
Share

@Jessy That is good to know. What do you mean by UT? Can you point me to some games that you have worked on, I am very interested to see what you have produced.

avatar image Jessy · Feb 20, 2012 at 03:25 AM 0
Share

They went by Over The Edge Entertainment (OTEE) several years ago. Go ahead and send me a P$$anonymous$$ on the forum; I go by "Jessy" there. ;-D

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by numberkruncher · Feb 17, 2012 at 04:59 PM

Many thanks to Jessy for helping with this question.

Vertex colours can be animated using a shader by introducing two properties. One for each simulated light source. The red colour component of a vertex colour can be used to illuminate one light source whilst the blue for another. It is easily possible to have 4 simulated light sources (R, G, B, A).

I have called these properties Emission A and Emission B and a value of 0 indicates that the vertex colour component should not contribute to lighting whereas 1 indicates full contribution.

These properties can be easily animated using the animation editor in Unity. Alternatively it is also possible to script the animation if need be.

I want to use these values to simulate two light sources smoothly dimming, but I suspect that there are many other applications of this technique.

I am aiming for iOS platforms and so have created two alternative implementations to compare performance. I do not at this time know which of the two variants performs more efficiently.

EDIT: The first approach seems to perform more efficiently than the second. The first approach does not include vertex lighting. I will add that to the answer if I can figure it out.

Cg Vertex and Fragment shader as follows:

 Shader "Custom/Animated Vertex Emission" {
 Properties {
     _MainTex ("Texture", 2D) = "white" {}
     _EmissionA ("Emission A", Range(0, 1)) = 1
     _EmissionB ("Emission B", Range(0, 1)) = 0
 }
 
     Pass {
     CGPROGRAM
         #pragma vertex vert
         
         #pragma fragment frag
         #pragma fragmentoption ARB_fog_exp2
         #pragma fragmentoption ARB_precision_hint_fastest
         
         #include "UnityCG.cginc"
         
         struct appdata {
             float4 vertex    : POSITION;
             float4 texcoord    : TEXCOORD0;
             float4 color    : COLOR;
         };
         
         struct v2f {
             float4 pos        : SV_POSITION;
             float2 uv        : TEXCOORD0;
             fixed4 color    : COLOR;
         };
         
         sampler2D _MainTex;
         float _EmissionA;
         float _EmissionB;
         
         float4 _MainTex_ST;
         
         v2f vert(appdata v) {
             v2f o;
             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
             
             float c = min(1, _EmissionA * v.color.r + _EmissionB * v.color.b);
             o.color = float4(c, c, c, 1);
             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
             
             return o;
         }
         
         half4 frag(v2f i) : COLOR {
             half4 outColor = tex2D(_MainTex, i.uv);
             return outColor * i.color;
         }
     ENDCG
     }
 
 }
 
 Fallback "Custom/Unlit"
 
 }


Unity Surface Shader:

 Shader "Custom/Animated Vertex Emission" {
 Properties {
     _MainTex ("Texture", 2D) = "white" {}
     _EmissionA ("Emission A", Range(0, 1)) = 1
     _EmissionB ("Emission B", Range(0, 1)) = 0
 }
 
 SubShader {
     Tags { "RenderType"="Opaque" }
 
     CGPROGRAM
         #pragma surface surf Lambert
 
         struct Input {
             float2 uv_MainTex;
             float3 color        : COLOR;
         };
 
         sampler2D _MainTex;
 
         float _EmissionA;
         float _EmissionB;
 
         void surf(Input IN, inout SurfaceOutput o) {
             float3 temp = tex2D(_MainTex, IN.uv_MainTex).rgb;
 
             float c = min(1, _EmissionA * IN.color.r + _EmissionB * IN.color.b);
             o.Albedo = half3(c * temp.r, c * temp.g, c * temp.b);
         }
     ENDCG
 }
 
 Fallback "Custom/Unlit"
 
 }
Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Conversion warning: has translation animation. It is not supported 2 Answers

Animation From blender to Unity Animator - Import Error 1 Answer

Blender model animations not found after import 3 Answers

Blender Path animation to Unity? 2 Answers

Mechanim Won't Move my Avatar Forward 1 Answer


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