Libgdx 3D-点光源显示黑匣子& rect(PointLight无法正常工作) [英] Libgdx 3D - Point Light shows black box & rect (PointLight not working)

查看:173
本文介绍了Libgdx 3D-点光源显示黑匣子& rect(PointLight无法正常工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个3d场景,当前是一个盒子和矩形,并尝试启用照明.

I am creating a 3d scene currently a box and rect, and trying to enable lighting.

当我创建PointLight并将其添加到Environment时,一切都变成黑色了吗?

When i create a PointLight and add it to Environment everything turns to black color?

我要做的就是创建3d场景并启用点光源,例如太阳或来自点的光线并阴影化对象.

all i want to do is create a 3d scene and enable point light, like a sun or rays coming from a point and shading the objects.

代码:

environment = new Environment();
environment.add(new PointLight().set(1f, 1f, 1f, 0, 0, 20f, 100f));

modelBatch=new ModelBatch();

..

square=new ModelBuilder().createBox(300,300,300,new Material(ColorAttribute.createDiffuse(Color.GREEN)),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
squareinst=new ModelInstance(square);
squareinst.transform.setTranslation(-500,0,0);

-

sprites.get(0).setRotationY(sprites.get(0).getRotationY() + 1f);
sprites.get(1).setRotationY(sprites.get(1).getRotationY() - 1f);
squareinst.transform.rotate(1,0,0,1);

modelBatch.begin(camera);
for(Sprite3D sp:sprites)// has 3d rect models
     sp.draw(modelBatch,environment);
modelBatch.render(squareinst,environment);
modelBatch.end();

PointLight使一切变黑

PointLight turning everything black

不使用环境或灯光

根据我的调查,这里如果点光源不起作用,那么一切都应该像现在一样是黑色的,因为环境需要光,所以在定向光下也可以正常工作(即使旋转后,rect的背面也是黑色的,我不知道为什么)

as per my investigation, here if pointlight is not working then everything should be black as currently, because the environment needs light, it works fine with Directional light (only the backface of rect is black even after rotations, i don't know why)

libgdx版本1.6.1-Android Studio 我在Android设备和台式机上都对其进行了检查

libgdx version 1.6.1 - android studio i checked it on both android device and desktop

请确保我真的需要这个PointLight正常工作,我不知道是否需要自定义着色器,如果可以,请引导我进入一些链接,因为我对着色器没有经验.我还读到了有关PointLight不能在某些设备上运行或无法在启用了opengl 2.0的情况下运行的信息,但我不确定.

please i really need to get this PointLight working, i don't know if it will take a custom shader, if so please guide me to some links because i am not experienced in shaders. I also read about PointLight not working on some device or not working in opengl 2.0 enabled, but i am not sure.

我尝试了很多东西和价值观.我了解环境光,但这对我的情况毫无用处.定向光的用法也很有限(如果不起作用,可以用作备用).

I tried a lot of thing and values. I know about Ambient Light but that has no use to my case. Directional light also has limited usage (can be used as a fallback if this doesn't work).

现在可以使用,请检查以下答案:

Its working now, check the answer below:

  • 如果您使用较大的相机尺寸或较大的型号,请尝试在点光源强度上添加更多的零,直到可见光为止.
  • if you are using big camera size or big model size, please try adding more zeros to the pointlight intensity until the light is visible.

推荐答案

下面是一个非常简单的示例,该示例显示了围绕球体旋转的点光源:

Here is a very simple example that shows a point light being rotated around a sphere:

public class PointLightTest extends ApplicationAdapter {
    ModelBatch modelBatch;
    Environment environment;
    PerspectiveCamera camera;
    CameraInputController camController;
    PointLight pointLight;
    Model model;
    ModelInstance instance;

    @Override
    public void create () {
        modelBatch = new ModelBatch();
        camera = new PerspectiveCamera();
        camera.position.set(5f, 5f, 5f);
        camera.lookAt(0f, 0f, 0f);
        camController = new CameraInputController(camera);
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.0f));
        environment.add(pointLight = new PointLight().set(0.8f, 0.8f, 0.8f, 2f, 0f, 0f, 10f));

        ModelBuilder mb = new ModelBuilder();
        model = mb.createSphere(1f, 1f, 1f, 20, 10, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position | Usage.Normal);
        instance = new ModelInstance(model);

        Gdx.input.setInputProcessor(camController);
    }

    @Override
    public void resize (int width, int height) {
        camera.viewportWidth = width;
        camera.viewportHeight = height;
        camera.update();
    }

    @Override
    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        camController.update();
        pointLight.position.rotate(Vector3.Z, Gdx.graphics.getDeltaTime() * 90f);
        modelBatch.begin(camera);
        modelBatch.render(instance, environment);
        modelBatch.end();
    }

    @Override
    public void dispose () {
        model.dispose();
        modelBatch.dispose();
    }
}

请注意,灯光必须在模型之外且在其范围内才能点亮模型.尝试逐渐将光线从模型移开或移向模型时会发生什么.在另一个示例中,Renderable用来可视化该光源的位置.

Note that the light needs to be outside the model and within the range for it to light the model. Try what happens when you gradually move the light away from the model or towards the model. The Renderable in that other example was used to visualize this location of the light.

这篇关于Libgdx 3D-点光源显示黑匣子& rect(PointLight无法正常工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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