spotlight not working in libGDX

烂漫一生 提交于 2019-12-08 03:52:52

问题


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:

Note that the default shader doesn't support spot lights, you'll have to supply your own shader to use this class.

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();}

回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/34383397/spotlight-not-working-in-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!