- Home /
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.
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.
@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!
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.
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?
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.
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"
}
Your answer
Follow this Question
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