首先在魅族或者其他手机或者均为正常渐变,而偏偏在魅族3(MX3)手机上渐变有问题。
核心代码如下:
float scale = getScaleByPoints(m_nForeGradientBeginPos, m_nForeGradientEndPos, vec2(v_texCoord[0], v_texCoord[1]));
int index = 0;
index += getIndex(0, m_nForeColorNums, scale, m_nForeScales[0]);
index += getIndex(1, m_nForeColorNums, scale, m_nForeScales[1]);
index += getIndex(2, m_nForeColorNums, scale, m_nForeScales[2]);
index += getIndex(3, m_nForeColorNums, scale, m_nForeScales[3]);
index += getIndex(4, m_nForeColorNums, scale, m_nForeScales[4]);
index += getIndex(5, m_nForeColorNums, scale, m_nForeScales[5]);
index += getIndex(6, m_nForeColorNums, scale, m_nForeScales[6]);
index += getIndex(7, m_nForeColorNums, scale, m_nForeScales[7]);
index += getIndex(8, m_nForeColorNums, scale, m_nForeScales[8]);
index += getIndex(9, m_nForeColorNums, scale, m_nForeScales[9]);
if(index == 0)
{
outColor = m_ForeColors[0];
return outColor;
}
float inScale = (scale - m_nForeScales[index - 1]) / (m_nForeScales[index] - m_nForeScales[index - 1]);
outColor = mix(m_ForeColors[index - 1], m_ForeColors[index], inScale);
焦头烂额的找问题,从代码上看没问题,在其他甚至低端手机上代码也没有问题,就在mx3上有问题。
所有变量都单独测试过没有问题,哪问题究竟出在哪里?
此时需要程序员的奇思幻想了,不要太相信编译器的处理逻辑。
那么问题出在哪里了,于是经过效果多次查看,经对比在mx3手机上如果(index-1)反过来等于(index+1)效果就解释通了,那么问题来了,如果我把代码改成如下:
float scale = getScaleByPoints(m_nForeGradientBeginPos, m_nForeGradientEndPos, vec2(v_texCoord[0], v_texCoord[1]));
int index = -1;
index += getIndex(0, m_nForeColorNums, scale, m_nForeScales[0]);
index += getIndex(1, m_nForeColorNums, scale, m_nForeScales[1]);
index += getIndex(2, m_nForeColorNums, scale, m_nForeScales[2]);
index += getIndex(3, m_nForeColorNums, scale, m_nForeScales[3]);
index += getIndex(4, m_nForeColorNums, scale, m_nForeScales[4]);
index += getIndex(5, m_nForeColorNums, scale, m_nForeScales[5]);
index += getIndex(6, m_nForeColorNums, scale, m_nForeScales[6]);
index += getIndex(7, m_nForeColorNums, scale, m_nForeScales[7]);
index += getIndex(8, m_nForeColorNums, scale, m_nForeScales[8]);
index += getIndex(9, m_nForeColorNums, scale, m_nForeScales[9]);
if(index < 0)
{
outColor = m_ForeColors[0];
return outColor;
}
float inScale = (scale - m_nForeScales[index]) / (m_nForeScales[index + 1] - m_nForeScales[index]);
outColor = mix(m_ForeColors[index], m_ForeColors[index + 1], inScale);
测试结果正常了,mx3上也没问题了,md真坑啊!
结果可显而知,在mx3手机上写shader会有以下问题。
int index = 1;
index = index - 1;
结果index的值为2。
index = index + 1;
结果index的值为2。
简不简单,惊不惊喜,意不意外,就这么一个问题谁能想到。