【问题标题】:How to control the light position from the shader and in libgdx?如何控制着色器和 libgdx 中的灯光位置?
【发布时间】:2023-03-26 00:49:02
【问题描述】:

所以我一直在从事一个涉及自定义着色器的项目,并且在这里的人们的帮助下,我已经能够在 Rabbid76 的帮助下实现一个带有 Lambert 和 phong glsl 代码的着色器。

现在我想在 glsl 代码和 libgdx 中利用灯光的位置。

Rabbid76 将这段代码借给了我一个带有结构 Lightpoint 的 phong 阴影。我试了一下,除了黑屏什么都没有。我假设有一个统一的 PointLight num_pointLights 指向该结构。我不确定如何在 libgdx 代码中将其合并为统一,或作为 libgdx 中的变量。

顶点:

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;

varying vec2 v_texCoords;
varying vec3 v_vertPosWorld;
varying vec3 v_vertNVWorld;

void main()
{
    vec4 vertPos   = u_worldTrans * vec4(a_position, 1.0);
    v_vertPosWorld = vertPos.xyz;
    v_vertNVWorld  = normalize(mat3(u_worldTrans) * a_normal);
    v_texCoords    = a_texCoord0;
    gl_Position    = u_projViewTrans * vertPos;
}

片段:

varying vec2 v_texCoords;
varying vec3 v_vertPosWorld;
varying vec3 v_vertNVWorld;

uniform sampler2D u_texture;

struct PointLight
{
    vec3 color;
    vec3 position;
    float intensity;
};

uniform PointLight u_pointLights[1];

void main()
{
    vec3  toLightVector  = normalize(u_pointLights[0].position - v_vertPosWorld.xyz);
    float lightIntensity = max( 1.0, dot(v_vertNVWorld, toLightVector));
    vec4  texCol         = texture( u_texture, v_texCoords.st );
    vec3  finalCol       = texCol.rgb * lightIntensity * u_pointLights[0].color;
    gl_FragColor         = vec4( finalCol.rgb * lightIntensity, 10.0 );
}

有人建议我可以像这样初始化 PointLight 类:environment.add(pointLight = new PointLight().set(.....));,但我不确定这将如何工作,因为我会使用环境类变量而不是我的着色器。

我感觉离这个很近了。但我什至不确定是否可以使用这种方法。如果有人能够分享一些关于如何以有意义的方式使用灯光位置或结构的经验和知识,我真的很感激。 :)

这是我当前项目的链接: here

【问题讨论】:

  • 在您提供的 Spotlight 链接中,Xoppa 引用了一个链接,它建议使用 defaultShader 和配置,如 'DefaultShader.Config config = new DefaultShader.Config();' 'config.numPointLights = 0;' 'modelBatch = new ModelBatch(new DefaultShaderProvider(config));'我可以使用我的测试着色器(自定义着色器)代替默认着色器吗?我现在时间不多,所以我回家后尝试实施。此外,该链接现在正在工作。再次感谢。
  • ..回答你的问题..ummm...我使用绑定命令绑定纹理。老实说,我什至不知道在着色器中使用 u_texture 并作为 setUniformi 命令有什么作用?但是我会测试它是否真的没有它。谢谢。

标签: 3d libgdx shader


【解决方案1】:

经过一番修补并想知道该怎么做后,我决定查看this。我不知何故遇到了:

Vector3 LIGHT_POS = new Vector3(0, 0, 0);

..然后看到:

program.setUniformf("lightPosition", LIGHT_POS);

..它引用了着色器中的内容。我不确定我是否能解决这个问题。

o_O

我在着色器的渲染函数中增加了 Vector3 LIGHT_POS 中的 x 变量。它似乎运作良好。我想当我开始使用 DirectionalLight 时,我应该尝试一下。

代码:here

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-02-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多