我正在尝试使用着色器脚本渲染随机地形
tex = new Texture(Gdx.files.internal("ground.png"));
top = new Texture(Gdx.files.internal("top.png"));
ShaderProgram.pedantic = false;
shader = new ShaderProgram(VERT, FRAG);
shader.begin();
shader.setUniformi("u_top", 1);
shader.end();
top.bind(1);
Gdx.gl.glActiveTexture(GL10.GL_TEXTURE0);
batch = new SpriteBatch(1000, shader);
batch.setShader(shader);
batch.begin();
for (int i = 0; i < 4; i++)
batch.draw(tex, i * 256 * Initiate.getScale(), 0,
256 * Initiate.getScale(), 256 * Initiate.getScale());
batch.end();
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main()
{
vTexCoord = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture1;
uniform sampler2D u_texture2;
void main()
vec4 texColor ;
//calculate top vertex
float clamp = 0.5 + slope*(sin(vTexCoord0.x*mfrq)/mfrq+sin(vTexCoord0.x*frq)/frq+sin(vTexCoord0.x*nfrq)/nfrq);
//if larger the draw texture
if(vTexCoord0.y > clamp){
texColor = texture2D(u_texture1, vTexCoord1);
}
// else map coordinate for top texture and draw
else{
float tempy = 16.0*(vTexCoord0.y + 0.0625- clamp);
texColor = texture2D(u_texture2, vec2 (vTexCoord1.x,tempy));
}
gl_FragColor = texColor;
答案 0 :(得分:1)
您可以在片段着色器中通过计算两个参数来做到这一点 - 沿地形表面的距离和垂直于表面的深度,这可以通过属性完成或在顶点着色器中计算,具体取决于您如何生成地形,然后只是样本基于这两个坐标的纹理,沿第一个坐标平铺。
沿表面的距离只是形成表面的边长。 深度可以计算为从顶点到表面边缘的距离,插值将为您提供整个多边形的近似深度。四边形应该给你更好的插值然后三角形。