当前位置: 首页 > 工具软件 > G3D > 使用案例 >

libgdx g3d模型漫反射+镜面在vivo xplay等机子上glsl编译不过解决办法

车峻熙
2023-12-01

Android:
`Vertex shader
ERROR: 0:32: 'a_position' : Only consts can be used in a global initializer
ERROR: 0:70: 'a_normal' : Only consts can be used in a global initializer
ERROR: 0:89: 'a_binormal' : Only consts can be used in a global initializer
ERROR: 0:108: 'a_tangent' : Only consts can be used in a global initializer
ERROR: 0:130: 'a_texCoord0' : Only consts can be used in a global initializer
ERROR: 0:447: ')' : Syntax error: syntax error
ERROR: 6 compilation errors. No code generated.

Fragment shader:
ERROR: 0:206: '(' : Syntax error: syntax error
ERROR: 1 compilation errors. No code generated.

`
我使用的是gdx官方的sample test.glsl,发现在部分手机上,模型的漫反射和镜面无效,查看了一天,并且解决了,有必要分享出来哈,如果用gdx来加载3D模型,需要一些反射镜面光环境映射,模型看起来也会比较生动比较真实一些。

有出现上面的错误日志的,请往下看把哈。
官方g3d glsl地址
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/g3d/shaders/test.glsl


首先解决Only consts can be used in a global initializer:

把 a_position、a_position、a_position、a_tangent、a_texCoord0变量的初始化放到main函数中初始化即可。轻松解决掉5个错误。还剩2个比较棘手。


VS下的'(' : Syntax error:  syntax error导致错误原因是 传递参数的时候调用的方法无效,其实我也不知道为啥,调用方法无效,方法已经定义了,为什么无效,无果,只能手动去把这些方法重新在main中实现一下如下代码:

[VS]
#include "g_attributes.glsl:VS"
#include "u_uniforms.glsl"
#include "skinning.glsl"
#include "common.glsl:VS"
#include "common.glsl:tangentVectorsVS"
#include "lights.glsl:lights"


varying vec3 v_lightDir;
varying vec3 v_lightCol;
varying vec3 v_viewDir;
varying vec3 v_ambientLight;
#ifdef environmentCubemapFlag
varying vec3 v_reflect;
#endif
 
void main() {

//初始化
    #if defined(positionFlag)
    g_position = vec4(a_position, 1.0);
    #else
	g_position = vec4(0.0, 0.0, 0.0, 1.0);
    #endif


    #if defined(normalFlag)
	g_normal = a_normal;
    #else
	g_normal = vec3(0.0, 0.0, 1.0);
    #endif

    #if defined(binormalFlag)
    	 g_binormal = a_binormal;
    #else
    	 g_binormal = vec3(0.0, 1.0, 0.0);
    #endif // binormalFlag


#if defined(tangentFlag)
	 g_tangent = a_tangent;
#else
	 g_tangent = vec3(1.0, 0.0, 0.0);
#endif // tangentFlag

#if defined(texCoord0Flag)
	 g_texCoord0 = a_texCoord0;
#else
	 g_texCoord0 = vec2(0.0, 0.0);
#endif // texCoord0Flag

	calculateTangentVectors();


	g_position = applySkinning(g_position);
	g_normal = normalize(u_normalMatrix * applySkinning(g_normal));
	g_binormal = normalize(u_normalMatrix * applySkinning(g_binormal));
	g_tangent = normalize(u_normalMatrix * applySkinning(g_tangent));
	
	g_position = u_worldTrans * g_position;
	gl_Position = u_projViewTrans * g_position;
	
	mat3 worldToTangent;
	worldToTangent[0] = g_tangent;
	worldToTangent[1] = g_binormal;
	worldToTangent[2] = g_normal;

	v_ambientLight = getAmbient(g_normal);
	
	v_lightDir = normalize(-u_dirLights[0].direction) * worldToTangent;
	v_lightCol = u_dirLights[0].color;
	vec3 viewDir = normalize(u_cameraPosition.xyz - g_position.xyz);
	v_viewDir = viewDir * worldToTangent;
#ifdef environmentCubemapFlag
	v_reflect = reflect(-viewDir, g_normal);
#endif
//	pushColor();
//	pushTexCoord0();//fix
    v_color=g_color;
    v_texCoord0=g_texCoord0;
}

FS中错误点,相对多一点,

//  pullColor();
// pullTexCoord0();
//  diffuse = applyColorDiffuse(g_color);
//  vec3 specular = fetchColorSpecular();
同理,需要重新实现这几个方法如下代码:

   g_color = v_color;
    g_texCoord0 = v_texCoord0;
    vec4 diffuse;
     //	diffuse = applyColorDiffuse(g_color);
     #if defined(diffuseTextureFlag) || defined(diffuseColorFlag)
     vec4 fetchDiffuse;
     #if defined(diffuseTextureFlag) && defined(diffuseColorFlag)
     fetchDiffuse=(texture2D(u_diffuseTexture, g_texCoord0) * u_diffuseColor);
     #elif defined(diffuseTextureFlag)
     fetchDiffuse= (texture2D(u_diffuseTexture, g_texCoord0));
     #elif defined(diffuseColorFlag)
     fetchDiffuse=u_diffuseColor;
     #else
     fetchDiffuse= vec4(1.0);
     #endif
     diffuse= g_color * fetchDiffuse;
     #else
     diffuse=g_color;
     #endif

    //fix vivo xplay6 bug
    //	vec3 specular = fetchColorSpecular();
    vec3 specular;
    //g_texCoord0, vec3(0.0)

    #if defined(specularTextureFlag) && defined(specularColorFlag)
    specular= (texture2D(u_specularTexture, g_texCoord0).rgb * u_specularColor.rgb);
    #elif defined(specularTextureFlag)
    specular=texture2D(u_specularTexture, g_texCoord0).rgb;
    #elif defined(specularColorFlag)
    specular=u_specularColor.rgb;
    #else
    specular= vec3(0.0);
    #endif

到此BUG就修复完成,gdx 3d模型加载,不知道还会不会碰到更多的BUG,真心痛苦,后面碰到再说了。


 类似资料: