@@ -43,33 +43,97 @@ array must be in the form of uvec4 array[] */
4343#define UINT_FROM_UVEC4_ARRAY( array, id ) ( ( array )[( id ) / 4 ][( id ) % 4 ] )
4444#define UVEC2_FROM_UVEC4_ARRAY( array, id ) ( ( id ) % 2 == 0 ? ( array )[( id ) / 2 ].xy : ( array )[( id ) / 2 ].zw )
4545
46+ // Common functions
47+
48+ vec4 UnpackColor( const in int color )
49+ {
50+ #if __VERSION__ > 120
51+ uint uColor = color;
52+ return unpackUnorm4x8( uColor );
53+ #else
54+ int v = color, m = 0 ;
55+
56+ if ( v < 0 )
57+ {
58+ v = 2147483647 - abs ( v ) + 1 ;
59+ m = 128 ;
60+ }
61+
62+ int a = v / 16777216 ;
63+ v -= a * 16777216 ;
64+ int b = v / 65536 ;
65+ v -= b * 65536 ;
66+ int g = v / 256 ;
67+ v -= g * 256 ;
68+ int r = v;
69+ a = a + m;
70+
71+ return vec4 ( r, g, b, a ) / 255 ;
72+ #endif
73+ }
74+
4675/* Bit 0: color * 1
4776Bit 1: color * ( -1 )
4877Bit 2: color += lightFactor
4978Bit 3: alpha * 1
5079Bit 4: alpha * ( -1 )
5180Bit 5: alpha = 1
81+ // There should be no bit above.
5282Bit 6-9: lightFactor */
5383
5484float colorModArray[3 ] = float [3 ] ( 0 .0f, 1 .0f, - 1 .0f );
5585
56- vec4 ColorModulateToColor( const in uint colorMod ) {
57- vec4 colorModulate = vec4 ( colorModArray[colorMod & 3 ] );
58- colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3 ] );
86+ vec4 ColorModulateToColor( const in int colorMod ) {
87+ #if __VERSION__ > 120
88+ int rgbIndex = colorMod & 3 ;
89+ int alphaIndex = ( colorMod & 24 ) >> 3 ;
90+ #else
91+ int rgbBit0 = colorMod % 2 ;
92+ int rgbBit1 = ( colorMod / 2 ) % 2 ;
93+ int alphaBit0 = ( colorMod / 8 ) % 2 ;
94+ int alphaBit1 = ( colorMod / 16 ) % 2 ;
95+ int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
96+ int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
97+ #endif
98+
99+ vec4 colorModulate = vec4 ( colorModArray[ rgbIndex ] );
100+ colorModulate.a = colorModArray[ alphaIndex ];
59101 return colorModulate;
60102}
61103
62- vec4 ColorModulateToColor( const in uint colorMod, const in float lightFactor ) {
63- vec4 colorModulate = vec4 ( colorModArray[colorMod & 3 ] + ( ( colorMod & 4 ) >> 2 ) * lightFactor );
64- colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3 ] );
104+ vec4 ColorModulateToColor( const in int colorMod, const in float lightFactor ) {
105+ #if __VERSION__ > 120
106+ int rgbIndex = colorMod & 3 ;
107+ int alphaIndex = ( colorMod & 24 ) >> 3 ;
108+ int hasLight = ( colorMod & 4 ) >> 2 ;
109+ #else
110+ int rgbBit0 = colorMod % 2 ;
111+ int rgbBit1 = ( colorMod / 2 ) % 2 ;
112+ int hasLight = ( colorMod / 4 ) % 2 ;
113+ int alphaBit0 = ( colorMod / 8 ) % 2 ;
114+ int alphaBit1 = ( colorMod / 16 ) % 2 ;
115+ int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
116+ int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
117+ #endif
118+
119+ vec4 colorModulate = vec4 ( colorModArray[ rgbIndex ] + ( hasLight * lightFactor ) );
120+ colorModulate.a = colorModArray[ alphaIndex ];
65121 return colorModulate;
66122}
67123
68- float ColorModulateToLightFactor( const in uint colorMod ) {
124+ float ColorModulateToLightFactor( const in int colorMod ) {
125+ #if __VERSION__ > 120
69126 return ( colorMod >> 6 ) & 0xF;
127+ #else
128+ return float ( colorMod / 64 );
129+ #endif
70130}
71131
72132// This is used to skip vertex colours if the colorMod doesn't need them
73- bool ColorModulateToVertexColor( const in uint colorMod ) {
133+ bool ColorModulateToVertexColor( const in int colorMod ) {
134+ #if __VERSION__ > 120
74135 return ( colorMod & 32 ) == 32 ;
136+ #else
137+ return ( colorMod / 32 ) % 2 == 1 ;
138+ #endif
75139}
0 commit comments