聚光灯在libGDX中不起作用 [英] spotlight not working in libGDX

查看:124
本文介绍了聚光灯在libGDX中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在libGDX中玩Spotlight.我使用此代码将光源投射到-y方向.令我惊讶的是,我的floorModel是完全黑色的.我去看文档,发现了一个有趣的事实:

I was playing around with spotLight in libGDX. I used this code to project the light source in -y direction. To my surpise, my floorModel was completely black. I went to docs and found this interesting fact:

请注意,默认着色器不支持聚光灯,您必须提供自己的着色器才能使用此类.

我认为这可能是聚光灯无法正常工作的原因.但是,我发现了0个正确设置聚光灯的示例.您能否提供一个有关正确设置聚光灯的简单示例?

I think this may be the reason the what spotlight isn't working. I have however found 0 examples of setting up spot light properly. Could you provide a simple example on setting up spot light properly?

针对那些阅读第一个问题的人进行第二次导出自定义对象之后,灯光交互正按预期进行,这意味着仅使用聚光灯时该对象不可见.不知道为什么.

EDIT for those who read the first question: after exporting custom object for a second time, the light interaction was working as exptected-meaning the object was not visible when only spotlight was used. Not sure why.

public void show() {
       float factor=3f;
    camera = new PerspectiveCamera(45, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    camera.position.set(1.4f * factor, 6f, -1f * factor);


    camera.lookAt(0f,0f,0f);
    camera.near =0.1f;
    camera.far = 300f;

    modelBatch = new ModelBatch();
    modelBuilder=new ModelBuilder();

    UBJsonReader jsonReader = new UBJsonReader();

    G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);
    model = modelLoader.loadModel(Gdx.files.getFileHandle("convertedModel3.g3db", Files.FileType.Internal));
    floorModel = modelBuilder.createBox(30,1,30,new Material(ColorAttribute.createDiffuse(Color.BLUE)), VertexAttributes.Usage.Position|VertexAttributes.Usage.Normal);
    modelInstance = new ModelInstance(model);
    floorInstance=new ModelInstance(floorModel);

    Array<Material> materials=modelInstance.materials;

    materials.get(0).set(ColorAttribute.createDiffuse(Color.WHITE));
    materials.get(0).set(ColorAttribute.createReflection(Color.WHITE));


    float intensity = 0.1f;
    environment = new Environment();

    Vector3 pos = new Vector3(0,10,0);
    Vector3 dir = new Vector3(0,-10,0);

    environment.add(new SpotLight().set(Color.WHITE,pos,dir,1f,100,1));
}



@Override
    public void render(float delta) {
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        camera.update();


        modelBatch.begin(camera);
        modelBatch.render(modelInstance, environment);
        modelBatch.render(floorInstance,environment);
        modelBatch.end();}

推荐答案

如文档所述,当前默认着色器不支持Spotlight.这是因为良好的通用聚光灯需要每个片段照明,而默认着色器使用每个顶点照明.因此,您将不会找到一个简单的例子.您可以等待此拉取请求被包括在内.您可以使用它作为示例(尽管这可能对您的用法有些大材小用,而且肯定不简单).或者,您可以提供自己的包含聚光灯的着色器.

As the documentation says, currently spotlight is not supported by the default shader. This is because good generic spotlight requires per fragment lighting and the default shader uses per vertex lighting. Therefor you wont find a simple example on that. You could wait for this pull request to be included. You could use that as an example (although it's probably an overkill for your usage and surely not simple). Or you could provide your own shader that includes spot lighting.

这是有关如何创建广告的基本教程着色器. 此Wiki页面包含有关如何自定义着色器的更多信息.实际的着色器实现不是真正针对libGDX的(统一名称除外),您可以使用与目标设备兼容的任何喜欢的算法.

Here's a basic tutorial on how to create a shader. This wiki page contains more information about how to customize the shader. The actual shader implementation is not really libGDX specific (except for the uniform names), you can use any algorithm you like that is compatible with your target device.

此处,您可以找到有关如何调整默认设置(每个顶点照明)的示例)着色器,以在某种程度上支持聚光灯(质量取决于您的型号).请注意,已经包含了对DefaultShader类的修改,在这种情况下,您只需要关注实际的着色器(GLSL)程序即可.

Here you can find an example on how to adjust the default (per vertex lighting) shader to somewhat support spotlight (the quality depends on your models). Note that the modification to the DefaultShader class are already included, you'd only have to focus on the actual shader (GLSL) programs in that case.

这篇关于聚光灯在libGDX中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆