LibGDX Modified DefaultShader无法正确呈现

时间:2015-11-13 01:46:23

标签: java opengl libgdx glsl

我最近钻研了GLSL着色器(在LibGDX中)并且遇到了一些问题。

最初我编写了自己的着色器,实现了Shader接口,该接口在绘制方面部分工作,并根据顶点位移做了我想要的。

我决定只扩展DefaultShader,因为我不想改变绘图,并且位移的东西非常小。

当我尝试将此着色器与我的ModelBatch一起使用时,#canRender(Renderable r)返回false,当我强制它渲染时,它会将所有内容都绘制为黑色。

我使用的顶点和片段着色器几乎与默认着色器相同 - 我在顶点着色器中添加了几行顶点位移。片段着色器与默认值相同。

我认为这些着色器是ModelBatch在渲染我的模型时默认的,但我不确定它是否默认为不同的东西,或者我添加的内容是否以某种方式打破了它。

我想到的一件事可能是在着色器的创建中:我无法弄清楚Renderable传递给DefaultShader构造函数的东西 - 我试图在其中的某个地方找到对此的调用来源但没有运气。

这是实际的渲染调用(如果我没有将着色器传递给渲染调用,这是正确的,但显然没有给我我想要的功能):

if(assets.getPlayerInstance() != null){

        if(shader == null){
            shader = new WarpShader(assets.getPlayerInstance().getRenderable(new Renderable()));
            shader.init();
        }

        shader.updatePlayerPosition(assets.getPlayerInstance().transform.getTranslation(vec3Buff));

        modelBatch.begin(cam);

        modelBatch.render(assets.getInstances(), environment, shader);

        modelBatch.render(assets.getPlayerInstance(), environment);

        modelBatch.end();

    }

这是我的着色器类,因为你可以看到它几乎没有任何额外的东西:

public class WarpShader extends DefaultShader {

private Vector3 playerPosition = new Vector3();

public WarpShader(Renderable renderable) {
    super(renderable, new Config(Gdx.files.internal("data/shaders/test.vertex.glsl").readString()
            , Gdx.files.internal("data/shaders/test.frag.glsl").readString()));
}

@Override
public void init() {
    super.init();
}

@Override
public int compareTo(Shader other) {
    return super.compareTo(other);
}

@Override
public boolean canRender(Renderable instance) {

    return super.canRender(instance);
}

@Override
public void begin(Camera camera, RenderContext context) {   
    super.begin(camera, context);
}

@Override
public void render(Renderable renderable) {
    program.setUniform3fv("u_playerPos", new float[]{playerPosition.x,  playerPosition.y,  playerPosition.z}, 0, 3);
    super.render(renderable);
}

public void updatePlayerPosition(Vector3 pos){
    playerPosition=pos;
}

@Override
public void end() {
    super.end();
}

@Override
public void dispose() {
    super.dispose();
}

}

这是我的顶点着色器(可能不需要阅读它,真的没有添加太多):

#if defined(diffuseTextureFlag) || defined(specularTextureFlag)
#define textureFlag
#endif

#if defined(specularTextureFlag) || defined(specularColorFlag)
#define specularFlag
#endif

#if defined(specularFlag) || defined(fogFlag)
#define cameraPositionFlag
#endif

attribute vec3 a_position;
uniform mat4 u_projViewTrans;

uniform vec3 u_playerPos;  // ADDED THIS

#if defined(colorFlag)
varying vec4 v_color;
attribute vec4 a_color;
#endif // colorFlag

#ifdef normalFlag
attribute vec3 a_normal;
uniform mat3 u_normalMatrix;
varying vec3 v_normal;
#endif // normalFlag

#ifdef textureFlag
attribute vec2 a_texCoord0;
#endif // textureFlag

#ifdef diffuseTextureFlag
uniform vec4 u_diffuseUVTransform;
varying vec2 v_diffuseUV;
#endif

#ifdef specularTextureFlag
uniform vec4 u_specularUVTransform;
varying vec2 v_specularUV;
#endif

#ifdef boneWeight0Flag
#define boneWeightsFlag
attribute vec2 a_boneWeight0;
#endif //boneWeight0Flag

#ifdef boneWeight1Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight1;
#endif //boneWeight1Flag

#ifdef boneWeight2Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight2;
#endif //boneWeight2Flag

#ifdef boneWeight3Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight3;
#endif //boneWeight3Flag

#ifdef boneWeight4Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight4;
#endif //boneWeight4Flag

#ifdef boneWeight5Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight5;
#endif //boneWeight5Flag

#ifdef boneWeight6Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight6;
#endif //boneWeight6Flag

#ifdef boneWeight7Flag
#ifndef boneWeightsFlag
#define boneWeightsFlag
#endif
attribute vec2 a_boneWeight7;
#endif //boneWeight7Flag

#if defined(numBones) && defined(boneWeightsFlag)
#if (numBones > 0) 
#define skinningFlag
#endif
#endif

uniform mat4 u_worldTrans;

#if defined(numBones)
#if numBones > 0
uniform mat4 u_bones[numBones];
#endif //numBones
#endif

#ifdef shininessFlag
uniform float u_shininess;
#else
const float u_shininess = 20.0;
#endif // shininessFlag

#ifdef blendedFlag
uniform float u_opacity;
varying float v_opacity;

#ifdef alphaTestFlag
uniform float u_alphaTest;
varying float v_alphaTest;
#endif //alphaTestFlag
#endif // blendedFlag

#ifdef lightingFlag
varying vec3 v_lightDiffuse;

#ifdef ambientLightFlag
uniform vec3 u_ambientLight;
#endif // ambientLightFlag

#ifdef ambientCubemapFlag
uniform vec3 u_ambientCubemap[6];
#endif // ambientCubemapFlag 

#ifdef sphericalHarmonicsFlag
uniform vec3 u_sphericalHarmonics[9];
#endif //sphericalHarmonicsFlag

#ifdef specularFlag
varying vec3 v_lightSpecular;
#endif // specularFlag

#ifdef cameraPositionFlag
uniform vec4 u_cameraPosition;
#endif // cameraPositionFlag

#ifdef fogFlag
varying float v_fog;
#endif // fogFlag


#if defined(numDirectionalLights) && (numDirectionalLights > 0)
struct DirectionalLight
{
    vec3 color;
    vec3 direction;
};
uniform DirectionalLight u_dirLights[numDirectionalLights];
#endif // numDirectionalLights

#if defined(numPointLights) && (numPointLights > 0)
struct PointLight
{
    vec3 color;
    vec3 position;
};
uniform PointLight u_pointLights[numPointLights];
#endif // numPointLights

#if defined(ambientLightFlag) || defined(ambientCubemapFlag) || defined(sphericalHarmonicsFlag)
#define ambientFlag
#endif //ambientFlag

#ifdef shadowMapFlag
uniform mat4 u_shadowMapProjViewTrans;
varying vec3 v_shadowMapUv;
#define separateAmbientFlag
#endif //shadowMapFlag

#if defined(ambientFlag) && defined(separateAmbientFlag)
varying vec3 v_ambientLight;
#endif //separateAmbientFlag

#endif // lightingFlag

void main() {
    #ifdef diffuseTextureFlag
        v_diffuseUV = u_diffuseUVTransform.xy + a_texCoord0 * u_diffuseUVTransform.zw;
    #endif //diffuseTextureFlag

    #ifdef specularTextureFlag
        v_specularUV = u_specularUVTransform.xy + a_texCoord0 * u_specularUVTransform.zw;
    #endif //specularTextureFlag

    #if defined(colorFlag)
        v_color = a_color;
    #endif // colorFlag

    #ifdef blendedFlag
        v_opacity = u_opacity;
        #ifdef alphaTestFlag
            v_alphaTest = u_alphaTest;
        #endif //alphaTestFlag
    #endif // blendedFlag

    #ifdef skinningFlag
        mat4 skinning = mat4(0.0);
        #ifdef boneWeight0Flag
            skinning += (a_boneWeight0.y) * u_bones[int(a_boneWeight0.x)];
        #endif //boneWeight0Flag
        #ifdef boneWeight1Flag              
            skinning += (a_boneWeight1.y) * u_bones[int(a_boneWeight1.x)];
        #endif //boneWeight1Flag
        #ifdef boneWeight2Flag      
            skinning += (a_boneWeight2.y) * u_bones[int(a_boneWeight2.x)];
        #endif //boneWeight2Flag
        #ifdef boneWeight3Flag
            skinning += (a_boneWeight3.y) * u_bones[int(a_boneWeight3.x)];
        #endif //boneWeight3Flag
        #ifdef boneWeight4Flag
            skinning += (a_boneWeight4.y) * u_bones[int(a_boneWeight4.x)];
        #endif //boneWeight4Flag
        #ifdef boneWeight5Flag
            skinning += (a_boneWeight5.y) * u_bones[int(a_boneWeight5.x)];
        #endif //boneWeight5Flag
        #ifdef boneWeight6Flag
            skinning += (a_boneWeight6.y) * u_bones[int(a_boneWeight6.x)];
        #endif //boneWeight6Flag
        #ifdef boneWeight7Flag
            skinning += (a_boneWeight7.y) * u_bones[int(a_boneWeight7.x)];
        #endif //boneWeight7Flag
    #endif //skinningFlag

    #ifdef skinningFlag
        vec4 pos = u_worldTrans * skinning * vec4(a_position, 1.0);
        vec4 playerPos = u_worldTrans * skinning * vec4(u_playerPos, 1.0); // ADDED THIS
    #else
        vec4 pos = u_worldTrans * vec4(a_position, 1.0);
        vec4 playerPos = u_worldTrans * vec4(u_playerPos, 1.0); // ADDED THIS
    #endif

    gl_Position = u_projViewTrans * pos;

    //Apply horizon                             // ADDED THESE 3 LINES
    vec4 playerWorld = u_projViewTrans * playerPos;
    float xDelta = (gl_Position[1] - playerWorld[0]);
    gl_Position[1] += xDelta*xDelta*-0.002;

    #ifdef shadowMapFlag
        vec4 spos = u_shadowMapProjViewTrans * pos;
        v_shadowMapUv.xy = (spos.xy / spos.w) * 0.5 + 0.5;
        v_shadowMapUv.z = min(spos.z * 0.5 + 0.5, 0.998);
    #endif //shadowMapFlag

    #if defined(normalFlag)
        #if defined(skinningFlag)
            vec3 normal = normalize((u_worldTrans * skinning * vec4(a_normal, 0.0)).xyz);
        #else
            vec3 normal = normalize(u_normalMatrix * a_normal);
        #endif
        v_normal = normal;
    #endif // normalFlag

    #ifdef fogFlag
        vec3 flen = u_cameraPosition.xyz - pos.xyz;
        float fog = dot(flen, flen) * u_cameraPosition.w;
        v_fog = min(fog, 1.0);
    #endif

    #ifdef lightingFlag
        #if defined(ambientLightFlag)
            vec3 ambientLight = u_ambientLight;
        #elif defined(ambientFlag)
            vec3 ambientLight = vec3(0.0);
        #endif

        #ifdef ambientCubemapFlag       
            vec3 squaredNormal = normal * normal;
            vec3 isPositive  = step(0.0, normal);
            ambientLight += squaredNormal.x * mix(u_ambientCubemap[0], u_ambientCubemap[1], isPositive.x) +
                    squaredNormal.y * mix(u_ambientCubemap[2], u_ambientCubemap[3], isPositive.y) +
                    squaredNormal.z * mix(u_ambientCubemap[4], u_ambientCubemap[5], isPositive.z);
        #endif // ambientCubemapFlag

        #ifdef sphericalHarmonicsFlag
            ambientLight += u_sphericalHarmonics[0];
            ambientLight += u_sphericalHarmonics[1] * normal.x;
            ambientLight += u_sphericalHarmonics[2] * normal.y;
            ambientLight += u_sphericalHarmonics[3] * normal.z;
            ambientLight += u_sphericalHarmonics[4] * (normal.x * normal.z);
            ambientLight += u_sphericalHarmonics[5] * (normal.z * normal.y);
            ambientLight += u_sphericalHarmonics[6] * (normal.y * normal.x);
            ambientLight += u_sphericalHarmonics[7] * (3.0 * normal.z * normal.z - 1.0);
            ambientLight += u_sphericalHarmonics[8] * (normal.x * normal.x - normal.y * normal.y);          
        #endif // sphericalHarmonicsFlag

        #ifdef ambientFlag
            #ifdef separateAmbientFlag
                v_ambientLight = ambientLight;
                v_lightDiffuse = vec3(0.0);
            #else
                v_lightDiffuse = ambientLight;
            #endif //separateAmbientFlag
        #else
            v_lightDiffuse = vec3(0.0);
        #endif //ambientFlag


        #ifdef specularFlag
            v_lightSpecular = vec3(0.0);
            vec3 viewVec = normalize(u_cameraPosition.xyz - pos.xyz);
        #endif // specularFlag

        #if defined(numDirectionalLights) && (numDirectionalLights > 0) && defined(normalFlag)
            for (int i = 0; i < numDirectionalLights; i++) {
                vec3 lightDir = -u_dirLights[i].direction;
                float NdotL = clamp(dot(normal, lightDir), 0.0, 1.0);
                vec3 value = u_dirLights[i].color * NdotL;
                v_lightDiffuse += value;
                #ifdef specularFlag
                    float halfDotView = max(0.0, dot(normal, normalize(lightDir + viewVec)));
                    v_lightSpecular += value * pow(halfDotView, u_shininess);
                #endif // specularFlag
            }
        #endif // numDirectionalLights

        #if defined(numPointLights) && (numPointLights > 0) && defined(normalFlag)
            for (int i = 0; i < numPointLights; i++) {
                vec3 lightDir = u_pointLights[i].position - pos.xyz;
                float dist2 = dot(lightDir, lightDir);
                lightDir *= inversesqrt(dist2);
                float NdotL = clamp(dot(normal, lightDir), 0.0, 1.0);
                vec3 value = u_pointLights[i].color * (NdotL / (1.0 + dist2));
                v_lightDiffuse += value;
                #ifdef specularFlag
                    float halfDotView = max(0.0, dot(normal, normalize(lightDir + viewVec)));
                    v_lightSpecular += value * pow(halfDotView, u_shininess);
                #endif // specularFlag
            }
        #endif // numPointLights
    #endif // lightingFlag
}

片段着色器就是这个:

LibGDX Default Fragment Shader

谢谢!

1 个答案:

答案 0 :(得分:0)

DefaultShader是一个所谓的超级着色器。您不能简单地创建它的一个实例并将其用于所有渲染。相反,它需要针对特定​​用例进行编译。这就是Renderable构造函数参数的用途。

您应该提供自己的ShaderProvider,而不是在绘制调用中指定着色器。幸运的是,这只是几行代码非常简单:

modelBatch = new ModelBatch(new DefaultShaderProvider() {
    @Override
    protected Shader createShader (Renderable renderable) {
        return new WarpShader(renderable);
    }
});

请务必阅读this wiki page,它会提供更深入的信息以及如何进一步定制,例如当你想要回到默认着色器时。

另请参阅this answerthis answer