gpt4 book ai didi

libgdx - Shadertoy 着色器导入到 libgdx

转载 作者:行者123 更新时间:2023-12-03 20:50:44 30 4
gpt4 key购买 nike

我到处搜索,但没有引用。
我想用this着色器从 shadertoy 到我的 libgdx 项目,所以我尝试首先从以下位置导入简单着色器:https://www.shadertoy.com/view/XsffRs
我像这样修改了它,但没有成功:

/*
* Original shader from: https://www.shadertoy.com/view/XsffRs
*/

#ifdef GL_ES
precision mediump float;
#endif

uniform float time;
uniform vec2 resolution;

// shadertoy emulation
#define iTime time
#define iResolution resolution

// --------[ Original ShaderToy begins here ]---------- //
#define TAU 6.28318531
float C,S;

mat2 rot(float a){
return mat2(C=cos(a),S=sin(a),-S,C);
}

float map(vec3 p) {
p.yz*=rot(p.z*(.03*sin(iTime*3.)));
p.xz*=rot(p.z*(.03*cos(iTime*3.)));
float m=TAU/6.,
l=length(p.xy),
a=mod(atan(p.y,p.x)-p.z*.5+iTime*5.,m)-.5*m;
return length(vec2(a*l,l-2.))-.8;
}


void main(void)
{
vec2 uv = gl_FragCoord.xy / iResolution.xy;
uv-=.5;
uv.x*=iResolution.x/iResolution.y;
vec3 ro=vec3(uv,-3.),rd=normalize(vec3(uv,1.)),mp=ro;
float i=0.;
for (int ii=0;ii<30;++ii) {
i++;
float md=map(mp);
if (abs(md)<.001)break;
mp+=rd*md;
}
float r=i/30.;
float d=length(mp-ro)*.1;
vec3 c=mix(vec3(.2,.5,.7)*d*d,vec3(.2,.4,.8)*r/d,r*r);
c=sqrt(c);
gl_FragColor = vec4(c,1.);

}

着色器程序代码
    public void create () {

width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();

batch = new SpriteBatch();
ShaderProgram.pedantic = false;
shader = new ShaderProgram(Gdx.files.internal("vert.vert"), Gdx.files.internal("frag.frag"));
if(!shader.isCompiled())
shader.getLog();
}

public void render () {
time+=Gdx.graphics.getDeltaTime();

shader.begin();
shader.setUniformf("resolution", new Vector2(width, height));
shader.setUniformf("time", time);
shader.end();

batch.begin();
batch.setShader(shader);
batch.end();
}

着色器运行没有错误,但黑屏。
编辑:它通过绘制虚拟纹理来工作 Texture t = new Texture(new Pixmap(width,height, Pixmap.Format.RGB565));使用 spritebatch,但不知道为什么需要虚拟纹理?

最佳答案

为了查看着色器的运行情况,您需要绘制一些东西,代码仅指定将使用着色器,但没有使用它绘制任何内容

batch.begin();
batch.setShader(shader);
batch.draw(new Texture(new Pixmap(width,height, Pixmap.Format.RGB565),0,0);
batch.end();

关于libgdx - Shadertoy 着色器导入到 libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63037208/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com