简体   繁体   English

某些手机​​上的OpenGL ES 2.0灯光非常暗

[英]OpenGL ES 2.0 light is very darker on some phones

I write game with android opengl es 2.0 and I have a problem with light brightness on some diffrent devices. 我用android opengl es 2.0编写游戏,但是在某些不同的设备上我的灯光亮度有问题。 I use this formula to calculate light in my fragment shader: 我使用以下公式来计算片段着色器中的光:

float shineDamper = 0.0;
float reflectivity = 0.001;


float ambient = 0.5;
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 lightColor = vec3(0.5,0.5,0.5);

vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal,unitLightVector);
float brightness = max(nDot1,0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);
float specularFactor = dot(reflectedLightDirection,unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
vec3 diffuse = brightness * lightColor;
diffuse = max(diffuse,ambient);
vec3 finalSpecular = dampedFactor * reflectivity  * lightColor;
 gl_FragColor =vec4(diffuse,1.0)  * text + vec4(finalSpecular,1.0);

The problem is the light is very dark on some old device but if I change light color to 10 then on old devices is ok but on another phone light is very very bright. 问题是某些旧设备上的灯非常暗,但是如果我将光色更改为10,则在旧设备上可以,但是在另一部手机上,灯非常非常亮。 Light pos is very far from models because i want get the sun effect. 光线pos与模型相距甚远,因为我想获得阳光效果。

Solved!!. 解决了!!。 I just put light calculation to vertex shader. 我只是将光计算应用于顶点着色器。

Most likely it's caused by the fact that some older Android devices only support mediump in the fragment shader which approximates to a half precision float. 这很可能是由于某些较旧的Android设备仅在片段着色器中支持mediump(大约为半精度浮点数)而造成的。 Probably it was struggling to perform some of the calculations or hold some of the values at that limited precision. 可能难以执行某些计算或以有限的精度保留某些值。

The non-normalized vectors toCameraVector and toLightVector are the most likely culprits. 非标准化向量toCameraVectortoLightVector是最可能的罪魁祸首。

A decent fix would be to normalize them on the vertex shader (and still normalize in the fragment shader to minimize artifacts from denormalization during interpolation). 一个不错的解决方法是在顶点着色器上对其进行归一化(并且仍在片段着色器中进行归一化,以最小化插值期间因非归一化而产生的伪影)。

If you're getting good visual results by doing the calc in the vertex shader, then that's ideal, because you've probably massively improved performance too. 如果通过在顶点着色器中进行计算而获得良好的视觉效果,那将是理想的,因为您也可能已经大大提高了性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM