Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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 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.

Show more comments

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.

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

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

Animation is messed up when imported to unity? 0 Answers

Animated FBX: Model Rest Position 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