2017-05-29 43 views
0

我爲基於體素的世界開發了一個渲染引擎,它使用了非常高效的渲染方案。我已經能夠通過加載新材料來爲渲染使用特定的顏色。我遇到的問題是加載着色器,只是使顏色看起來不錯。我之前在LWJGL中做過這個,但我似乎無法使用LibGdx。我已經閱讀了blog.xoppa的所有內容,顯然,着色器就是我現在正在努力處理最多的內容。也許我需要某種SSAO着色器或推遲的AO着色器?在LibGDX中使用簡單的顏色着色器

Shader.frag

varying vec3 position; 
varying vec3 normal; 
varying vec4 color; 

void main(){ 
vec4 ambient = vec4(vec3(abs(normal.x)*.8 + abs(normal.z)*.9 + abs(normal.y)*1), 1); 

gl_FragColor = vec4(color) * ambient; 

}

Shader.vert

varying vec3 position; 
varying vec3 normal; 
varying vec4 color; 

void main(){ 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 
    gl_FrontColor = gl_Color; 
    position = vec3(gl_Vertex); 
    normal = vec3(gl_Normal); 
    color = vec4(gl_Color); 

}

我TestShader延伸陰影r,我也有其他所有實現的方法。這些是相關的。

... 
@Override 
public void init() { 
    program = new ShaderProgram(vert, frag); 
    if (!program.isCompiled()) 
     throw new GdxRuntimeException(program.getLog()); 

} ... 

@Override 
public void begin(Camera camera, RenderContext context) { 
    this.camera = camera; 
    this.context = context; 
    program.begin(); 
    context.setDepthTest(GL_LEQUAL); 
    context.setCullFace(GL_BACK); 
} ... 

@Override 
public void render(Renderable renderable) { 
    renderable.meshPart.render(program); 
} ... 

什麼它看起來像:(從之前)goingFor 它看起來像一個默認Libgdx着色器,顯然我沒有多種類型的每塊體素的,但還即未來:current

+0

這是OpenGL ES的或臺式機的OpenGL? –

+0

@NicolBolas將libgdx用於桌面,使其OpenGl ES –

回答

0

好吧,我確實需要使用這使得它看起來不錯,簡單的SSAO着色器:

片段着色器:

uniform sampler2D texture0; 
uniform sampler2D texture1; 

uniform vec2 camerarange; 
uniform vec2 screensize; 

float readDepth(in vec2 coord) { 
    return (2.0 * camerarange.x)/(camerarange.y + camerarange.x - texture2D(texture0, coord).x * (camerarange.y - camerarange.x)); 
} 


void main(void) 
{ 
    vec2 texCoord = gl_TexCoord[0].st; 
    //vec2 texCoord = texture2D(texture0, gl_TexCoord[0].st).xy; 
    //vec3 texColor = texture2D(texture1, gl_TexCoord[0].st).rgb; 

    float depth = readDepth(texCoord); 
    float d; 

    float pw = 1.0/screensize.x; 
    float ph = 1.0/screensize.y; 

    float aoCap = 0.45; 

    float ao = 0.0; 

    float aoMultiplier=1000.0; 

    float depthTolerance = 0.00001; 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    pw*=2.0; 
    ph*=2.0; 
    aoMultiplier/=2.0; 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    pw*=2.0; 
    ph*=2.0; 
    aoMultiplier/=2.0; 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    pw*=2.0; 
    ph*=2.0; 
    aoMultiplier/=2.0; 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y+ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x+pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    d=readDepth(vec2(texCoord.x-pw,texCoord.y-ph)); 
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier); 

    ao/=16.0; 

    gl_FragColor = vec4(1.05 - ao) * texture2D(texture1, texCoord); 
} 

頂點着色器

#version 110 

void main(){ 
    gl_Position = ftransform(); 
    gl_TexCoord[0] = gl_MultiTexCoord0; 
} 

change