@@ -43,33 +43,96 @@ 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+ return unpackUnorm4x8( color );
52+ #else
53+ int v = color, m = 0 ;
54+
55+ if ( v < 0 )
56+ {
57+ v = 2147483647 - abs ( v ) + 1 ;
58+ m = 128 ;
59+ }
60+
61+ int a = v / 16777216 ;
62+ v -= a * 16777216 ;
63+ int b = v / 65536 ;
64+ v -= b * 65536 ;
65+ int g = v / 256 ;
66+ v -= g * 256 ;
67+ int r = v;
68+ a = a + m;
69+
70+ return vec4 ( r, g, b, a ) / 255 ;
71+ #endif
72+ }
73+
4674/* Bit 0: color * 1
4775Bit 1: color * ( -1 )
4876Bit 2: color += lightFactor
4977Bit 3: alpha * 1
5078Bit 4: alpha * ( -1 )
5179Bit 5: alpha = 1
52- Bit 6-9: lightFactor */
80+ Bit 6-9: lightFactor
81+ All bits after lightFactor should be left zeroed. */
5382
5483float colorModArray[3 ] = float [3 ] ( 0 .0f, 1 .0f, - 1 .0f );
5584
56- vec4 ColorModulateToColor( const in uint colorMod ) {
57- vec4 colorModulate = vec4 ( colorModArray[colorMod & 3 ] );
58- colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3 ] );
85+ vec4 ColorModulateToColor( const in int colorMod ) {
86+ #if __VERSION__ > 120
87+ int rgbIndex = colorMod & 3 ;
88+ int alphaIndex = ( colorMod & 24 ) >> 3 ;
89+ #else
90+ int rgbBit0 = colorMod % 2 ;
91+ int rgbBit1 = ( colorMod / 2 ) % 2 ;
92+ int alphaBit0 = ( colorMod / 8 ) % 2 ;
93+ int alphaBit1 = ( colorMod / 16 ) % 2 ;
94+ int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
95+ int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
96+ #endif
97+
98+ vec4 colorModulate = vec4 ( colorModArray[ rgbIndex ] );
99+ colorModulate.a = colorModArray[ alphaIndex ];
59100 return colorModulate;
60101}
61102
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 ] );
103+ vec4 ColorModulateToColor( const in int colorMod, const in float lightFactor ) {
104+ #if __VERSION__ > 120
105+ int rgbIndex = colorMod & 3 ;
106+ int alphaIndex = ( colorMod & 24 ) >> 3 ;
107+ int hasLight = ( colorMod & 4 ) >> 2 ;
108+ #else
109+ int rgbBit0 = colorMod % 2 ;
110+ int rgbBit1 = ( colorMod / 2 ) % 2 ;
111+ int hasLight = ( colorMod / 4 ) % 2 ;
112+ int alphaBit0 = ( colorMod / 8 ) % 2 ;
113+ int alphaBit1 = ( colorMod / 16 ) % 2 ;
114+ int rgbIndex = rgbBit0 + ( rgbBit1 * 2 );
115+ int alphaIndex = alphaBit0 + ( alphaBit1 * 2 );
116+ #endif
117+
118+ vec4 colorModulate = vec4 ( colorModArray[ rgbIndex ] + ( hasLight * lightFactor ) );
119+ colorModulate.a = colorModArray[ alphaIndex ];
65120 return colorModulate;
66121}
67122
68- float ColorModulateToLightFactor( const in uint colorMod ) {
123+ float ColorModulateToLightFactor( const in int colorMod ) {
124+ #if __VERSION__ > 120
69125 return ( colorMod >> 6 ) & 0xF;
126+ #else
127+ return float ( colorMod / 64 );
128+ #endif
70129}
71130
72131// This is used to skip vertex colours if the colorMod doesn't need them
73- bool ColorModulateToVertexColor( const in uint colorMod ) {
132+ bool ColorModulateToVertexColor( const in int colorMod ) {
133+ #if __VERSION__ > 120
74134 return ( colorMod & 32 ) == 32 ;
135+ #else
136+ return ( colorMod / 32 ) % 2 == 1 ;
137+ #endif
75138}
0 commit comments